changeset 1298:f0d6aac3adc4

Some cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 19 Aug 2017 03:20:02 +0300
parents 5bd64397453b
children b0c0be4c76f9
files src/libgfx.c
diffstat 1 files changed, 18 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/libgfx.c	Sat Aug 19 02:17:02 2017 +0300
+++ b/src/libgfx.c	Sat Aug 19 03:20:02 2017 +0300
@@ -744,6 +744,8 @@
 } DMPCXData;
 
 
+// Returns one byte from row buffer (of length len) at offset soffs,
+// OR zero if the offset is outside buffer.
 static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs)
 {
     return (soffs < len) ? row[soffs] : 0;
@@ -1062,49 +1064,21 @@
     DMPCXHeader hdr;
     int res = 0;
     BOOL isPaletted;
-
     pcx.buf = NULL;
 
     // Read PCX header
     if (!dm_fread_byte(fp, &hdr.manufacturer) ||
         !dm_fread_byte(fp, &hdr.version) ||
         !dm_fread_byte(fp, &hdr.encoding) ||
-        !dm_fread_byte(fp, &hdr.bitsPerPlane))
-    {
-        res = dmError(DMERR_FREAD,
-            "PCX: Could not read basic header data.\n");
-        goto error;
-    }
-
-    if (hdr.manufacturer != 10 ||
-        hdr.version != 5 ||
-        hdr.encoding != 1)
-    {
-        res = dmError(DMERR_NOT_SUPPORTED,
-            "PCX: Not a PCX file, or unsupported variant.\n");
-        goto error;
-    }
-
-    if (!dm_fread_le16(fp, &hdr.xmin) ||
+        !dm_fread_byte(fp, &hdr.bitsPerPlane) ||
+        !dm_fread_le16(fp, &hdr.xmin) ||
         !dm_fread_le16(fp, &hdr.ymin) ||
         !dm_fread_le16(fp, &hdr.xmax) ||
         !dm_fread_le16(fp, &hdr.ymax) ||
         !dm_fread_le16(fp, &hdr.hres) ||
-        !dm_fread_le16(fp, &hdr.vres))
-    {
-        res = dmError(DMERR_FREAD,
-            "PCX: Could not read image dimensions.\n");
-        goto error;
-    }
-
-    if (!dm_fread_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)))
-    {
-        res = dmError(DMERR_FREAD,
-            "PCX: Could not read colormap.\n");
-        goto error;
-    }
-
-    if (!dm_fread_byte(fp, &hdr.reserved) ||
+        !dm_fread_le16(fp, &hdr.vres) ||
+        !dm_fread_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)) ||
+        !dm_fread_byte(fp, &hdr.reserved) ||
         !dm_fread_byte(fp, &hdr.nplanes) ||
         !dm_fread_le16(fp, &hdr.bpl) ||
         !dm_fread_le16(fp, &hdr.palInfo) ||
@@ -1113,7 +1087,16 @@
         !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
     {
         res = dmError(DMERR_FREAD,
-            "PCX: Could not read header remainder.\n");
+            "PCX: Could not read image header data.\n");
+        goto error;
+    }
+
+    if (hdr.manufacturer != 10 ||
+        hdr.version > 5 ||
+        hdr.encoding != 1)
+    {
+        res = dmError(DMERR_NOT_SUPPORTED,
+            "PCX: Not a PCX file, or unsupported variant.\n");
         goto error;
     }
 
@@ -1154,6 +1137,7 @@
         goto error;
     }
 
+    // Sanity check bytes per line value
     if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8)
     {
         res = dmError(DMERR_MALLOC,