Skip to content

Commit

Permalink
Fixed a possible problem when using a colour as a mask.
Browse files Browse the repository at this point in the history
Added "-oapp" option to append output to a file.
Added "-ohex[=<str>]" option to output as Hex text.
  • Loading branch information
robabod committed Aug 3, 2021
1 parent a6d5c44 commit 480ea01
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 19 deletions.
17 changes: 16 additions & 1 deletion CommandLinePngConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ CCommandLinePngConv::CCommandLinePngConv()
, m_bUseRGBMask(false)
, m_bInverseByte(false)
, m_sLeadText("db ")
, m_bOutputAppend(false)
, m_bHexOutput(false)
, m_sLeadTextByte("$")
{
m_pOrigin.x = m_pOrigin.y = 0;
m_pSize.x = m_pSize.y = -1;
Expand Down Expand Up @@ -87,6 +90,15 @@ void CCommandLinePngConv::ParseParam(std::string param, bool bFlag, bool bLast)
m_bInverseByte = true;
else if (!param.compare(0, 4, "odb="))
m_sLeadText = param.substr(4);
else if (!param.compare("oapp"))
m_bOutputAppend = true;
else if (!param.compare("ohex"))
m_bHexOutput = true;
else if (!param.compare(0, 5, "ohex="))
{
m_bHexOutput = true;
m_sLeadTextByte = param.substr(5);
}
else
m_bCommandLineError = true;
}
Expand All @@ -112,7 +124,7 @@ std::string CCommandLinePngConv::Help()
{
std::string help;
// add our params to the list...
help = "Usage:\n " + m_sAppName + " [-h|-help] [-ver] [-ss] [-pos=x,y] [-size=x,y] [-imask] [-mask=<mask>] [-ostd] [-otxt|-obin] [-lrt|rtl] [-zz] [-usd] input <output>";
help = "Usage:\n " + m_sAppName + " [-h|-help] [-ver] [-ss] [-pos=x,y] [-size=x,y] [-imask] [-mask=<mask>] [-ostd] [-otxt|-obin] [-lrt|rtl] [-zz] [-usd] [-ohex[=<$>]] [-oapp] input <output>";

help += "\n\nFlags:";

Expand All @@ -136,7 +148,10 @@ std::string CCommandLinePngConv::Help()
help += "\n -otxt Output as text";
help += "\n -odb=<db> Leading text when -otxt option used. Default is \"db \".";
help += "\n \"-odb=\" will give no leading text in output.";
help += "\n -ohex[=<$>] Set text output to Hex format, with optional leading char.";
help += "\n Default output is decimal. Default leading hex char is \"$\".";
help += "\n -obin Output as binary (default)";
help += "\n -oapp Append output (default erases original output file)";
help += "\n -ltr|rtl Set output left-to-right (default) or right-to-left";
help += "\n -zz ZigZag Output (alternate ltr, rtl)";
help += "\n -usd Upside down (output bottom line first)";
Expand Down
3 changes: 3 additions & 0 deletions CommandLinePngConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CCommandLinePngConv : public CCommandLine
std::string m_sPngFileName;
std::string m_sOutFileName;
std::string m_sLeadText;
std::string m_sLeadTextByte;
POINT m_pOrigin;
POINT m_pSize;
RGBA m_rgbaMaskColour;
Expand All @@ -37,6 +38,8 @@ class CCommandLinePngConv : public CCommandLine
bool m_bVersion;
bool m_bInverseByte;
bool m_bInverseMask;
bool m_bOutputAppend;
bool m_bHexOutput;
ZXIMAGEFORMAT m_nMaskFormat;
protected:
bool DecodeRGB3(std::string param);
Expand Down
50 changes: 44 additions & 6 deletions ZxImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CZxImage::CZxImage(POINT _size)
, m_bZigZag(false)
, m_bUseRGBMask(false)
, m_bByteInvert(false)
, m_bHexOutput(false)
{
// correct the X size so it's a multiple of 8
if (m_ptSize.x % 8)
Expand Down Expand Up @@ -76,7 +77,11 @@ CZxImage::ProcessRGBAImage(CRGBAImage &_rgba)
if (!m_bUseRGBMask)
maskon = _rgba(y_idx, (8*x_idx) + b_idx).PixelIsMasked(m_nMaskThreshold);
else
{
maskon = _rgba(y_idx, (8*x_idx) + b_idx).PixelIsMasked(m_rgbMaskColour);
if (maskon)
byteon = false;
}
}
catch (CRGBAImage::CExBoundsViolation)
{
Expand Down Expand Up @@ -116,6 +121,30 @@ void CZxImage::SetLeadText(std::string _ss)
}
}

std::ostream& txtoutput(std::ostream& ost, const CZxImage& zxi)
{
// This method outputs the required manipulators for text output.
if (zxi.m_bHexOutput)
ost << zxi.m_sLeadTextByte << std::ios::hex;
return ost;
}

std::ostream& txtoutput(std::ostream& ost, const CZxImage& zxi, int i)
{
// This method outputs the required manipulators for text output.
if (zxi.m_bHexOutput)
{
ost << zxi.m_sLeadTextByte;
ost.width(2);
ost.fill('0');
ost.setf(std::ios_base::hex, std::ios_base::basefield);
ost << i;
}
else
ost << i;
return ost;
}

std::ostream& operator<< (std::ostream& ost, const CZxImage& zxi)
{
// this is the operator that actually streams the contents out according to various set options.
Expand Down Expand Up @@ -192,29 +221,37 @@ std::ostream& operator<< (std::ostream& ost, const CZxImage& zxi)
case ZXIMAGE_FORMAT_B:
case ZXIMAGE_FORMAT_BBMM:
if (zxi.m_bTextOutput)
ost << static_cast<int>(zxi.m_imgByte[y_idx][x_idx]);
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
else
ost << zxi.m_imgByte[y_idx][x_idx];
break;

case ZXIMAGE_FORMAT_M:
case ZXIMAGE_FORMAT_MMBB:
if (zxi.m_bTextOutput)
ost << static_cast<int>(zxi.m_imgMask[y_idx][x_idx]);
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
else
ost << zxi.m_imgMask[y_idx][x_idx];
break;

case ZXIMAGE_FORMAT_BM:
if (zxi.m_bTextOutput)
ost << static_cast<int>(zxi.m_imgByte[y_idx][x_idx]) << "," << static_cast<int>(zxi.m_imgMask[y_idx][x_idx]);
{
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
ost << ",";
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
}
else
ost << zxi.m_imgByte[y_idx][x_idx] << zxi.m_imgMask[y_idx][x_idx];
break;

case ZXIMAGE_FORMAT_MB:
if (zxi.m_bTextOutput)
ost << static_cast<int>(zxi.m_imgMask[y_idx][x_idx]) << "," << static_cast<int>(zxi.m_imgByte[y_idx][x_idx]);
{
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
ost << ",";
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
}
else
ost << zxi.m_imgMask[y_idx][x_idx] << zxi.m_imgByte[y_idx][x_idx];
break;
Expand Down Expand Up @@ -249,14 +286,14 @@ std::ostream& operator<< (std::ostream& ost, const CZxImage& zxi)
{
case ZXIMAGE_FORMAT_BBMM:
if (zxi.m_bTextOutput)
ost << static_cast<int>(zxi.m_imgMask[y_idx][x_idx]);
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
else
ost << zxi.m_imgMask[y_idx][x_idx];
break;

case ZXIMAGE_FORMAT_MMBB:
if (zxi.m_bTextOutput)
ost << static_cast<int>(zxi.m_imgByte[y_idx][x_idx]);
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
else
ost << zxi.m_imgByte[y_idx][x_idx];
break;
Expand All @@ -276,3 +313,4 @@ std::ostream& operator<< (std::ostream& ost, const CZxImage& zxi)

return ost;
}

5 changes: 5 additions & 0 deletions ZxImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CZxImage
{
public:
friend std::ostream& operator<< (std::ostream& ost, const CZxImage& zxi);
friend std::ostream& txtoutput(std::ostream& ost, const CZxImage& zxi);
friend std::ostream& txtoutput(std::ostream& ost, const CZxImage& zxi, int i);
ProcessRGBAImage(CRGBAImage &_rgba);
void SetRGBMask(RGBA _rgba) {m_rgbMaskColour = _rgba; m_bUseRGBMask = true;};
void InvertMask(bool _invert = true) {m_bMaskInvert = _invert;};
Expand All @@ -26,6 +28,7 @@ class CZxImage
void SetGreyThreshold(int _grey) {if (_grey >= 0 && _grey <= 255) m_nGreyThreshold = _grey;};
void SetMaskThreshold(int _mask) {if (_mask >= 0 && _mask <= 255) m_nMaskThreshold = _mask;};
void SetTextOutput(bool _text = true) {m_bTextOutput = _text;};
void SetHexOutput(bool _hex = true, std::string _ss = "$") {m_bHexOutput = _hex; m_sLeadTextByte = _ss;};
void SetReverseOutput(bool _rev = true) {m_bReverse = _rev;};
void SetUpsideDown(bool _usd = true) {m_bUpsideDown = _usd;};
void SetZigZag(bool _zz = true) {m_bZigZag = _zz;};
Expand All @@ -39,6 +42,8 @@ class CZxImage
ZXIMAGEFORMAT m_nMaskFormat;
RGBA m_rgbMaskColour;
std::string m_sLeadText;
std::string m_sLeadTextByte;
bool m_bHexOutput;
bool m_bUseRGBMask;
bool m_bByteInvert;
bool m_bMaskInvert;
Expand Down
Binary file added brick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions pngconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ int main(int argc, char* argv[])
zximg.SetUpsideDown(cmdLine.m_bUpsideDown);
zximg.SetZigZag(cmdLine.m_bZigZag);
zximg.SetLeadText(cmdLine.m_sLeadText);
zximg.SetHexOutput(cmdLine.m_bHexOutput, cmdLine.m_sLeadTextByte);

// now we'll set up our output stream.
// we'll send to either stdout or a file.
Expand All @@ -106,6 +107,8 @@ int main(int argc, char* argv[])
int open_flags = std::ios::out;
if (!cmdLine.m_bTxtOut)
open_flags |= std::ios::binary;
if (cmdLine.m_bOutputAppend)
open_flags |= std::ios::app;
fileout.open(cmdLine.m_sOutFileName.c_str(), open_flags);
if (!fileout.is_open())
{
Expand Down
66 changes: 66 additions & 0 deletions pngconv.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Microsoft Developer Studio Generated Dependency File, included by pngconv.mak

.\CommandLine.cpp : \
".\CommandLine.h"\
".\pngconv.h"\


.\CommandLinePngConv.cpp : \
".\CommandLine.h"\
".\CommandLinePngConv.h"\
".\pngconv.h"\
".\typedefs.h"\


.\pngconv.cpp : \
".\CommandLine.h"\
".\CommandLinePngConv.h"\
".\exceptions.h"\
".\libpng-1.2.12\png.h"\
".\libpng-1.2.12\pngconf.h"\
".\pngconv.h"\
".\PngImage.h"\
".\RGBAImage.h"\
".\typedefs.h"\
".\zlib\zconf.h"\
".\zlib\zlib.h"\
".\ZxImage.h"\


.\PngImage.cpp : \
".\exceptions.h"\
".\libpng-1.2.12\png.h"\
".\libpng-1.2.12\pngconf.h"\
".\pngconv.h"\
".\PngImage.h"\
".\typedefs.h"\
".\zlib\zconf.h"\
".\zlib\zlib.h"\


.\RGBAImage.cpp : \
".\libpng-1.2.12\png.h"\
".\libpng-1.2.12\pngconf.h"\
".\pngconv.h"\
".\PngImage.h"\
".\RGBAImage.h"\
".\typedefs.h"\
".\zlib\zconf.h"\
".\zlib\zlib.h"\


.\StdAfx.cpp : \
".\StdAfx.h"\


.\ZxImage.cpp : \
".\libpng-1.2.12\png.h"\
".\libpng-1.2.12\pngconf.h"\
".\pngconv.h"\
".\PngImage.h"\
".\RGBAImage.h"\
".\typedefs.h"\
".\zlib\zconf.h"\
".\zlib\zlib.h"\
".\ZxImage.h"\

8 changes: 4 additions & 4 deletions pngconv.rc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,0,2,0
PRODUCTVERSION 0,0,2,0
FILEVERSION 0,0,3,0
PRODUCTVERSION 0,0,3,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -85,14 +85,14 @@ BEGIN
VALUE "Comments", "\0"
VALUE "CompanyName", "raww\0"
VALUE "FileDescription", "PNG to Spectrum Conversion Utility\0"
VALUE "FileVersion", "0, 0, 2, 0\0"
VALUE "FileVersion", "0, 0, 3, 0\0"
VALUE "InternalName", "pngconv.exe\0"
VALUE "LegalCopyright", "� 2006 Robert Cooper\0"
VALUE "LegalTrademarks", "\0"
VALUE "OriginalFilename", "pngconv.exe\0"
VALUE "PrivateBuild", "\0"
VALUE "ProductName", "pngconv\0"
VALUE "ProductVersion", "0, 0, 2, 0\0"
VALUE "ProductVersion", "0, 0, 3, 0\0"
VALUE "SpecialBuild", "\0"
END
END
Expand Down
30 changes: 23 additions & 7 deletions pngconv.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
PngConv v0.0.2
PngConv v0.0.3
=------------=

PNGConv is a PNG to ZX Spectrum Image Convertor. The aim is to allow you to
perform image conversions from the command-line, primarily from with a makefile
or batch file. This is an early test release, so don't rely on it to do what you
would expect.

perform image conversions from the command-line, primarily from within a
makefile or batch file.


History:
========

v0.0.3 - 06/12/2006
- Fixed a possible problem when using a colour as a mask.
It could potentially be recognised as image data.
+ Added "-oapp" option to append output to a file.
+ Added "-ohex[=<str>]" option to output as Hex text.

v0.0.2 - 07/11/2006
------
+ Allow definition of a colour as mask. Overrides use of Alpha channel if
Expand Down Expand Up @@ -75,10 +79,16 @@ output : This is the output file. If this parameter is not given the

-odb=<db> : Set leading text to <db>. Defaults to "db ". Tab characters
can be entered using "\t". -odb= on it's own gives no
leading text.
leading text.

-ohex[=<$>] : Set text output to Hex format, with optional leading char.
Default is to output in decimal.
Defaults leading hex char is "$".

-obin : Output as binary (default).

-oapp : Append output (default erases original output file).

-ltr|rtl : Output left-to-right (default), or right-to-left.

-zz : ZigZag (alternate lines ltr/rtl).
Expand All @@ -91,7 +101,7 @@ Future additions
================
- Output as screen$ format.
- Support colour.

- Allow stdin as input (allows piping from another command).


Examples
Expand All @@ -116,5 +126,11 @@ To output alternate lines of inverted mask then byte, upside-down, zig-zagging
the sprite, as text to stdout:
pngconv -mask=mmbb -imask -usd -zz -otxt -ostd image.png

To output a simple sprite to a hex text file, with leading char of #:
pngconv -otxt -ohex=# -odb=BYTE image.png image.out

To output two sections of an image into a single file:
pngconv -pos=0,0 -size=8,64 image.png image.out
pngconv -pos=8,0 -size=8,64 -oapp image.png image.out

Documentation produced on:
2 changes: 1 addition & 1 deletion typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef __PNGCONV__TYPEDEFS__H__INC
#define __PNGCONV__TYPEDEFS__H__INC

#define PNGCONV_VERSION_STRING "0.0.2"
#define PNGCONV_VERSION_STRING "0.0.3"
#define PNGCONV_BUILD_DATE __DATE__
#define PNGCONV_BUILD_TIME __TIME__
#define PNGCONV_SHORT_TITLE "PngConv"
Expand Down

0 comments on commit 480ea01

Please sign in to comment.