changeset 409:b529b7e8ff83

Various improvements and cruft cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 03 Nov 2012 02:40:07 +0200
parents 37e65cdcc749
children e4b2f689aff6
files gfxconv.c
diffstat 1 files changed, 115 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/gfxconv.c	Sat Nov 03 02:39:52 2012 +0200
+++ b/gfxconv.c	Sat Nov 03 02:40:07 2012 +0200
@@ -351,7 +351,7 @@
 }
 
 
-void dmDumpCharASCII(FILE *outFile, const uint8_t *buf, int *offs, int format, BOOL multicolor)
+void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor)
 {
     int yc;
 
@@ -365,7 +365,7 @@
 }
 
 
-void dmDumpSpriteASCII(FILE *outFile, const uint8_t *buf, int *offs, int format, BOOL multicolor)
+void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor)
 {
     int bufOffs, xc, yc;
 
@@ -385,10 +385,10 @@
 }
 
 
-int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, uint8_t *, size_t), int scale, int format)
+int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, Uint8 *, size_t), int scale, int format)
 {
     int x, y, yscale, xscale, res = 0, rowSize, rowWidth;
-    uint8_t *row = NULL;
+    Uint8 *row = NULL;
 
     // Allocate memory for row buffer
     rowWidth = img->width * scale;
@@ -396,21 +396,21 @@
 
     if ((row = dmMalloc(rowSize + 16)) == NULL)
     {
-        res = -16;
+        res = DMERR_MALLOC;
         goto done;
     }
 
     // Generate the image
     for (y = 0; y < img->height; y++)
     {
-        uint8_t *ptr = row,
+        Uint8 *ptr = row,
                 *ptr1 = row,
                 *ptr2 = ptr1 + rowWidth,
                 *ptr3 = ptr2 + rowWidth;
 
         for (x = 0; x < img->width; x++)
         {
-            uint8_t c = img->data[(y * img->pitch) + x], qr, qg, qb, qa;
+            Uint8 c = img->data[(y * img->pitch) + x], qr, qg, qb, qa;
             switch (format)
             {
                 case DM_IFMT_PALETTE:
@@ -465,7 +465,7 @@
         {
             if (!writeRowCB(cbdata, row, rowSize))
             {
-                res = -32;
+                res = DMERR_FWRITE;
                 goto done;
             }
         }
@@ -517,7 +517,7 @@
 } DMRawData;
 
 
-static BOOL dmWriteIFFMasterRAWRow(void *cbdata, uint8_t *row, size_t len)
+static BOOL dmWriteIFFMasterRAWRow(void *cbdata, Uint8 *row, size_t len)
 {
     DMRawData *raw = (DMRawData *) cbdata;
     size_t i;
@@ -526,7 +526,7 @@
     {
     }
 
-    return fwrite(row, sizeof(uint8_t), len, raw->fp) == len;
+    return fwrite(row, sizeof(Uint8), len, raw->fp) == len;
 }
 
 
@@ -559,9 +559,9 @@
 }
 
 
-static BOOL dmWritePPMRow(void *cbdata, uint8_t *row, size_t len)
+static BOOL dmWritePPMRow(void *cbdata, Uint8 *row, size_t len)
 {
-    return fwrite(row, sizeof(uint8_t), len, (FILE *) cbdata) == len;
+    return fwrite(row, sizeof(Uint8), len, (FILE *) cbdata) == len;
 }
 
 
@@ -597,7 +597,7 @@
 
 
 #ifdef HAVE_LIBPNG
-static BOOL dmWritePNGRow(void *cbdata, uint8_t *row, size_t len)
+static BOOL dmWritePNGRow(void *cbdata, Uint8 *row, size_t len)
 {
     png_structp png_ptr = cbdata;
     (void) len;
@@ -743,49 +743,49 @@
 
 typedef struct
 {
-    uint8_t r,g,b;
+    Uint8 r,g,b;
 } DMPCXColor;
 
 
 typedef struct
 {
-    uint8_t manufacturer,
+    Uint8 manufacturer,
             version,
             encoding,
             bpp;
-    uint16_t xmin, ymin, xmax, ymax;
-    uint16_t hres, vres;
+    Uint16 xmin, ymin, xmax, ymax;
+    Uint16 hres, vres;
     DMPCXColor colormap[16];
-    uint8_t reserved;
-    uint8_t nplanes;
-    uint16_t bpl;
-    uint16_t palinfo;
-    uint8_t filler[58];
+    Uint8 reserved;
+    Uint8 nplanes;
+    Uint16 bpl;
+    Uint16 palinfo;
+    Uint8 filler[58];
 } DMPCXHeader;
 
 typedef struct
 {
     DMPCXHeader *header;
-    uint8_t *buf;
+    Uint8 *buf;
     size_t bufLen, bufOffs;
     int format;
     FILE *fp;
 } DMPCXData;
 
 
-static inline uint8_t dmPCXGetByte(uint8_t *row, const size_t len, const size_t soffs)
+static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs)
 {
     return (soffs < len) ? row[soffs] : 0;
 }
 
 static BOOL dmPCXFlush(DMPCXData *pcx)
 {
-    BOOL ret = fwrite(pcx->buf, sizeof(uint8_t), pcx->bufOffs, pcx->fp) == pcx->bufOffs;
+    BOOL ret = fwrite(pcx->buf, sizeof(Uint8), pcx->bufOffs, pcx->fp) == pcx->bufOffs;
     pcx->bufOffs = 0;
     return ret;
 }
 
-static inline BOOL dmPCXPutByte(DMPCXData *pcx, const uint8_t val)
+static inline BOOL dmPCXPutByte(DMPCXData *pcx, const Uint8 val)
 {
     if (pcx->bufOffs < pcx->bufLen)
     {
@@ -796,7 +796,7 @@
         return dmPCXFlush(pcx);
 }
 
-BOOL dmWritePCXRow(void *cbdata, uint8_t *row, size_t len)
+BOOL dmWritePCXRow(void *cbdata, Uint8 *row, size_t len)
 {
     DMPCXData *pcx = (DMPCXData *) cbdata;
     int plane;
@@ -804,7 +804,7 @@
     
     for (plane = 0; plane < pcx->header->nplanes; plane++)
     {
-        uint8_t data = dmPCXGetByte(row, len, soffs++),
+        Uint8 data = dmPCXGetByte(row, len, soffs++),
                 count = 1;
 
         pcx->bufOffs = 0;
@@ -863,7 +863,7 @@
     if ((pcx.fp = fopen(filename, "wb")) == NULL)
     {
         dmError("PCX: Could not open file '%s' for writing.\n", filename);
-        res = -15;
+        res = DMERR_FOPEN;
         goto error;
     }
 
@@ -900,7 +900,7 @@
     {
         dmError("PCX: Could not allocate %d bytes for RLE compression buffer.\n",
             pcx.bufLen);
-        res = -11;
+        res = DMERR_MALLOC;
         goto error;
     }
 
@@ -911,7 +911,7 @@
         !dm_fwrite_byte(pcx.fp, hdr.bpp))
     {
         dmError("PCX: Could not write basic header data.\n");
-        res = -10;
+        res = DMERR_FWRITE;
         goto error;
     }
     
@@ -923,14 +923,14 @@
         !dm_fwrite_le16(pcx.fp, hdr.vres))
     {
         dmError("PCX: Could not write image dimensions.\n");
-        res = -9;
+        res = DMERR_FWRITE;
         goto error;
     }
 
-    if (!dm_fwrite_str(pcx.fp, (uint8_t *) &hdr.colormap, sizeof(hdr.colormap)))
+    if (!dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.colormap, sizeof(hdr.colormap)))
     {
         dmError("PCX: Could not write colormap.\n");
-        res = -8;
+        res = DMERR_FWRITE;
         goto error;
     }
     
@@ -938,10 +938,10 @@
         !dm_fwrite_byte(pcx.fp, hdr.nplanes) ||
         !dm_fwrite_le16(pcx.fp, hdr.bpl) ||
         !dm_fwrite_le16(pcx.fp, hdr.palinfo) ||
-        !dm_fwrite_str(pcx.fp, (uint8_t *) &hdr.filler, sizeof(hdr.filler)))
+        !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
     {
         dmError("PCX: Could not write header remainder.\n");
-        res = -7;
+        res = DMERR_FWRITE;
         goto error;
     }
 
@@ -980,13 +980,13 @@
 }
 
 
-static BOOL dmPCXDecodeRLERow(FILE *fp, uint8_t *buf, const size_t bufLen)
+static BOOL dmPCXDecodeRLERow(FILE *fp, Uint8 *buf, const size_t bufLen)
 {
     size_t offs = 0;
     do
     {
         int count;
-        uint8_t data;
+        Uint8 data;
 
         if (!dm_fread_byte(fp, &data))
             return FALSE;
@@ -1016,7 +1016,7 @@
     DMPCXHeader hdr;
     BOOL paletted;
     int res = 0, yc, xc;
-    uint8_t *dp;
+    Uint8 *dp;
 
     pcx.buf = NULL;
 
@@ -1027,7 +1027,8 @@
         !dm_fread_byte(fp, &hdr.bpp))
     {
         dmError("PCX: Could not read basic header data.\n");
-        res = -9;
+        res = DMERR_FREAD;
+        goto error;
     }
     
     if (hdr.manufacturer != 10 ||
@@ -1036,7 +1037,7 @@
         hdr.bpp != 8)
     {
         dmError("PCX: Not a PCX file, or unsupported variant.\n");
-        res = -11;
+        res = DMERR_FREAD;
         goto error;
     }
     
@@ -1048,14 +1049,14 @@
         !dm_fread_le16(fp, &hdr.vres))
     {
         dmError("PCX: Could not read image dimensions.\n");
-        res = -8;
+        res = DMERR_FREAD;
         goto error;
     }
 
-    if (!dm_fread_str(fp, (uint8_t *) &hdr.colormap, sizeof(hdr.colormap)))
+    if (!dm_fread_str(fp, (Uint8 *) &hdr.colormap, sizeof(hdr.colormap)))
     {
         dmError("PCX: Could not read colormap.\n");
-        res = -7;
+        res = DMERR_FREAD;
         goto error;
     }
     
@@ -1063,17 +1064,17 @@
         !dm_fread_byte(fp, &hdr.nplanes) ||
         !dm_fread_le16(fp, &hdr.bpl) ||
         !dm_fread_le16(fp, &hdr.palinfo) ||
-        !dm_fread_str(fp, (uint8_t *) &hdr.filler, sizeof(hdr.filler)))
+        !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
     {
         dmError("PCX: Could not read header remainder.\n");
-        res = -6;
+        res = DMERR_FREAD;
         goto error;
     }
     
     if (hdr.nplanes != 3 && hdr.nplanes != 1)
     {
         dmError("PCX: Unsupported number of bitplanes %d.\n", hdr.nplanes);
-        res = -4;
+        res = DMERR_FREAD;
         goto error;
     }
 
@@ -1081,16 +1082,16 @@
     if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1)) == NULL)
     {
         dmError("PCX: Could not allocate image structure.\n");
-        res = -5;
+        res = DMERR_MALLOC;
         goto error;
     }
-    
+
     paletted = hdr.nplanes == 1;
     pcx.bufLen = hdr.nplanes * hdr.bpl;
     if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL)
     {
         dmError("PCX: Could not allocate RLE buffer.\n");
-        res = -3;
+        res = DMERR_MALLOC;
         goto error;
     }
 
@@ -1102,7 +1103,7 @@
         if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen))
         {
             dmError("PCX: Error decoding RLE data.\n");
-            res = -100;
+            res = DMERR_INVALID_DATA;
             goto error;
         }
         
@@ -1115,7 +1116,7 @@
             
             case 3:
                 {
-                    uint8_t *dptr = dp,
+                    Uint8 *dptr = dp,
                             *sptr1 = pcx.buf,
                             *sptr2 = sptr1 + hdr.bpl,
                             *sptr3 = sptr2 + hdr.bpl;
@@ -1137,25 +1138,56 @@
     if (paletted)
     {
         int i;
-        uint8_t tmpb;
+        Uint8 tmpb;
+        BOOL read;
 
         if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C)
-            goto error;
-
-        for (i = 0; i < img->ncolors; i++)
         {
-            if (!dm_fread_byte(fp, &tmpb))
-                goto error;
-            img->pal[i].r = tmpb;
+            read = FALSE;
+            img->ncolors = 16;
+        }
+        else
+        {
+            read = TRUE;
+            img->ncolors = 256;
+        }
 
-            if (!dm_fread_byte(fp, &tmpb))
-                goto error;
-            img->pal[i].g = tmpb;
+        if ((img->pal = dmCalloc(img->ncolors, sizeof(DMColor))) == NULL)
+        {
+            dmError("PCX: Could not allocate palette data!\n");
+            res = DMERR_MALLOC;
+            goto error;
+        }
+        
+        if (read)
+        {
+            for (i = 0; i < img->ncolors; i++)
+            {
+                Uint8 tmpR, tmpG, tmpB;
+                if (!dm_fread_byte(fp, &tmpR) ||
+                    !dm_fread_byte(fp, &tmpG) ||
+                    !dm_fread_byte(fp, &tmpB))
+                    goto error;
 
-            if (!dm_fread_byte(fp, &tmpb))
-                goto error;
-            img->pal[i].b = tmpb;
+                img->pal[i].r = tmpR;
+                img->pal[i].g = tmpG;
+                img->pal[i].b = tmpB;
+            }
         }
+        else
+        {
+            for (i = 0; i < img->ncolors; i++)
+            {
+                if (i < 16)
+                {
+                    img->pal[i].r = hdr.colormap[i].r;
+                    img->pal[i].g = hdr.colormap[i].g;
+                    img->pal[i].b = hdr.colormap[i].b;
+                }
+            }
+        }
+        
+
     }
 
 error:
@@ -1184,7 +1216,7 @@
 
 int fmtProbePNGImageFILE(FILE *fp)
 {
-    uint8_t buf[6];
+    Uint8 buf[6];
 //    if (!dm_fread_str(fp, 
     return DM_PROBE_SCORE_FALSE;
 }
@@ -1214,11 +1246,11 @@
 int dmConvertBMP2(DMImage *screen, const DM64Image *img)
 {
     int yc;
-    uint8_t *dp = screen->data;
+    Uint8 *dp = screen->data;
     
     for (yc = 0; yc < screen->height; yc++)
     {
-        uint8_t *d = dp;
+        Uint8 *d = dp;
         const int y = yc / 8, yb = yc & 7;
         const int scroffsy = y * C64_SCR_CH_WIDTH;
         const int bmoffsy = y * C64_SCR_WIDTH;
@@ -1230,7 +1262,7 @@
             const int scroffs = scroffsy + x;
             const int b = img->bitmap[0][bmoffsy + (x * 8) + yb];
             const int v = 6 - ((xc * 2) & 6);
-            uint8_t c;
+            Uint8 c;
 
             switch ((b >> v) & 3)
             {
@@ -1273,7 +1305,7 @@
                 char *palFilename = dm_strdup_printf("%s.pal", filename);
                 res = dmWriteIFFMasterRAWPalette(palFilename, image, 1 << bpp);
                 dmFree(palFilename);
-                if (res != 0)
+                if (res != DMERR_OK)
                     return res;
 
                 return dmWriteIFFMasterRAWImage(filename, image, scale, bpp);
@@ -1289,7 +1321,7 @@
 {
     int dataOffs, itemCount, outWidth, outWidthPX, outHeight;
     size_t bufSize;
-    uint8_t *bufData;
+    Uint8 *bufData;
 
     switch (optInFormat)
     {
@@ -1370,14 +1402,6 @@
         char *outFilename = NULL;
         int outX = 0, outY = 0, err;
 
-#ifndef HAVE_LIBPNG
-        if (optOutFormat == OUTFMT_PNG)
-        {
-            dmError("PNG output format support not compiled in, sorry.\n");
-            goto error;
-        }
-#endif
-
         if (optSequential)
         {
             if (optOutFilename == NULL)
@@ -1429,7 +1453,7 @@
             }
 
             if ((err = dmC64ConvertCSData(outImage, outX * outWidthPX, outY * outHeight,
-                bufData, outWidth, outHeight, optInMulticolor, optColors)) != 0)
+                bufData, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK)
             {
                 dmError("Internal error in conversion of raw data to bitmap: %d.\n", err);
                 break;
@@ -1492,6 +1516,14 @@
         argHandleOpt, argHandleFile, TRUE))
         exit(1);
 
+#ifndef HAVE_LIBPNG
+    if (optOutFormat == OUTFMT_PNG)
+    {
+        dmError("PNG output format support not compiled in, sorry.\n");
+        exit(3);
+    }
+#endif
+
     // Determine input format, if not specified'
     if (optInFormat == INFMT_AUTO && optInFilename != NULL)
     {
@@ -1609,6 +1641,9 @@
 //                        res = dmReadPNGImageFILE(inFile, &img);
                         break;
                 }
+
+                if (res != DMERR_OK)
+                    break;
                 
                 switch (optOutFormat)
                 {