diff src/libgfx.c @ 1288:6c8b19d1d196

More work on libgfx.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Aug 2017 17:32:19 +0300
parents 32051ad352c8
children e7dc9bb9777e
line wrap: on
line diff
--- a/src/libgfx.c	Fri Aug 18 15:27:36 2017 +0300
+++ b/src/libgfx.c	Fri Aug 18 17:32:19 2017 +0300
@@ -137,6 +137,12 @@
 }
 
 
+int dmImageConvertTo(DMImage **dst, const DMImage *src, const DMImageSpec *spec)
+{
+    return DMERR_OK;
+}
+
+
 int dmWriteImageData(DMImage *img, void *cbdata, int (*writeRowCB)(void *, Uint8 *, size_t), const DMImageSpec *spec)
 {
     int x, y, yscale, xscale, res = 0, rowSize, rowWidth;
@@ -254,7 +260,7 @@
 }
 
 
-int dmWriteRAWImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWriteRAWImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
     int xc, yc, plane, res;
     DMBitStreamContext bs;
@@ -299,7 +305,7 @@
 }
 
 
-int dmWriteRAWImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWriteRAWImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     FILE *fp;
     int res;
@@ -327,8 +333,10 @@
 }
 
 
-int dmWritePPMImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWritePPMImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
+    DMImageSpec tmpSpec;
+
     // Write PPM header
     fprintf(fp,
         "P6\n%d %d\n255\n",
@@ -336,12 +344,13 @@
         img->height * spec->scaleY);
 
     // Write image data
-    spec->format = DM_IFMT_RGB;
-    return dmWriteImageData(img, (void *) fp, dmWritePPMRow, spec);
+    memcpy(&tmpSpec, spec, sizeof(DMImageSpec));
+    tmpSpec.format = DM_IFMT_RGB;
+    return dmWriteImageData(img, (void *) fp, dmWritePPMRow, &tmpSpec);
 }
 
 
-int dmWritePPMImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWritePPMImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     FILE *fp;
     int res;
@@ -376,7 +385,7 @@
 }
 
 
-int dmWritePNGImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWritePNGImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
     png_structp png_ptr = NULL;
     png_infop info_ptr = NULL;
@@ -483,7 +492,7 @@
 }
 
 
-int dmWritePNGImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWritePNGImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     int res;
     FILE *fp;
@@ -699,8 +708,12 @@
 typedef struct
 {
     Uint8 manufacturer,     // always 0x0a
-            version,        // Z-Soft PCX Paintbrush version:
-                            // 0 = v2.5, 2 = v2.8 with palette, 3 = v2.8 without palette, 5 = v3.0 or better
+            version,        // Z-Soft PC Paintbrush version:
+                            // 0 = v2.5
+                            // 2 = v2.8 with palette,
+                            // 3 = v2.8 without palette
+                            // 4 = PC Paintbrush for Windows
+                            // 5 = v3.0 or better
             encoding,       // usually 0x01 = RLE, 0x00 = uncompressed
             bitsPerPlane;   // bits per pixel per plane
 
@@ -809,7 +822,7 @@
 }
 
 
-int dmWritePCXImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
     DMPCXData pcx;
     DMPCXHeader hdr;
@@ -823,7 +836,7 @@
 
     // Create PCX header
     dmMemset(&hdr, 0, sizeof(hdr));
-    if (spec->paletted)
+    if (spec->paletted && img->pal != NULL)
     {
         for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++)
         {
@@ -938,7 +951,7 @@
 }
 
 
-int dmWritePCXImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWritePCXImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     FILE *fp;
     int res;
@@ -1826,7 +1839,7 @@
 static int fmtProbePCX(const Uint8 *buf, const size_t len)
 {
     if (len > 128 + 32 &&
-        buf[0] == 10 &&
+
         (buf[1] == 5 || buf[1] == 2 || buf[1] == 3) &&
         buf[2] == 1 &&
         (buf[3] == 8 || buf[3] == 4 || buf[3] == 3 || buf[3] == 1) &&