changeset 511:4cdcaeb68b54

Collapse most of the probing functions into one generic probe, as they only check loading address and file size.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Nov 2012 15:52:44 +0200
parents 43ea59887c69
children 95a6b6af53f1
files lib64gfx.c lib64gfx.h
diffstat 2 files changed, 41 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/lib64gfx.c	Mon Nov 19 15:06:01 2012 +0200
+++ b/lib64gfx.c	Mon Nov 19 15:52:44 2012 +0200
@@ -116,24 +116,23 @@
 }
 
 
-static int fmtProbeDrazPaint(const Uint8 *buf, const size_t len)
+static BOOL dmCompareAddr16(const Uint8 *buf, const size_t offs, const Uint16 addr)
 {
-    if (len == 10051 && buf[0] == 0x00 && buf[1] == 0x58)
-        return DM_PROBE_SCORE_GOOD;
-
-    return DM_PROBE_SCORE_FALSE;
+    return (buf[offs] == (addr & 0xff)) && (buf[offs + 1] == ((addr >> 8) & 0xff));
 }
 
 
-static int fmtProbeDrazPaint20Packed(const Uint8 *buf, const size_t len)
+static int fmtProbeDrazPaint20Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
 {
     const char *ident = (const char *) buf + 2;
-    if (len > 22 && buf[0] == 0x00 && buf[1] == 0x58 &&
-            strncmp(ident, "DRAZPAINT ", 10) == 0 &&
-            ident[11] == '.' && (
-            (ident[10] == '1' && ident[12] == '4') ||
-            (ident[10] == '2' && ident[12] == '0')
-            ))
+
+    if (len > 22 &&
+        dmCompareAddr16(buf, 0, fmt->addr) &&
+        strncmp(ident, "DRAZPAINT ", 10) == 0 &&
+        ident[11] == '.' && (
+        (ident[10] == '1' && ident[12] == '4') ||
+        (ident[10] == '2' && ident[12] == '0')
+        ))
         return DM_PROBE_SCORE_MAX;
 
     return DM_PROBE_SCORE_FALSE;
@@ -178,19 +177,12 @@
 }
 
 
-static int fmtProbeDrazLace10(const Uint8 *buf, const size_t len)
-{
-    if (len == 18242 && buf[0] == 0x00 && buf[1] == 0x58)
-        return DM_PROBE_SCORE_GOOD;
-    return DM_PROBE_SCORE_FALSE;
-}
-
-
-static int fmtProbeDrazLace10Packed(const Uint8 *buf, const size_t len)
+static int fmtProbeDrazLace10Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
 {
     const char *ident = (const char *) buf + 2;
-    if (len > 22 && buf[0] == 0x00 && buf[1] == 0x58 &&
-            strncmp(ident, "DRAZLACE! 1.0", 13) == 0)
+    if (len > 22 &&
+        dmCompareAddr16(buf, 0, fmt->addr) &&
+        strncmp(ident, "DRAZLACE! 1.0", 13) == 0)
         return DM_PROBE_SCORE_MAX;
     
     return DM_PROBE_SCORE_FALSE;
@@ -208,10 +200,10 @@
 
 
 #define AMICA_DM_PROBE_SIZE 1024
-static int fmtProbeAmicaPaintPacked(const Uint8 *buf, const size_t len)
+static int fmtProbeAmicaPaintPacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
 {
     int i, n;
-    if (len < AMICA_DM_PROBE_SIZE || buf[0] != 0x00 || buf[1] != 0x40)
+    if (len < AMICA_DM_PROBE_SIZE || !dmCompareAddr16(buf, 0, fmt->addr))
         return DM_PROBE_SCORE_FALSE;
     
     // Interpaint Hi-Res gives a false positive
@@ -266,22 +258,6 @@
 }
 
 
-static int fmtProbeKoalaPaint(const Uint8 *buf, const size_t len)
-{
-    if (len == 10003 && buf[0] == 0x00 && buf[1] == 0x60)
-        return DM_PROBE_SCORE_AVG;
-    return DM_PROBE_SCORE_FALSE;
-}
-
-
-static int fmtProbeTruePaint(const Uint8 *buf, const size_t len)
-{
-    if (len == 19434 && buf[0] == 0x00 && buf[1] == 0x9c)
-        return DM_PROBE_SCORE_GOOD;
-    return DM_PROBE_SCORE_FALSE;
-}
-
-
 static BOOL fmtTruePaintSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len)
 {
     (void) op;
@@ -296,7 +272,7 @@
 DMC64ImageFormat dmC64ImageFormats[] =
 {
     {
-        DM_C64IFMT_MC, ".drp", "DrazPaint 2.0 (packed)",
+        DM_C64IFMT_MC, ".drp", "DrazPaint 2.0 (packed)", 0x5800, -1,
         fmtProbeDrazPaint20Packed, fmtDecodeDrazPaintPacked,
         NULL, NULL, NULL,
         4,
@@ -309,7 +285,7 @@
     },
 
     {
-        DM_C64IFMT_MC_ILACE, ".dlp", "DrazLace 1.0 (packed)",
+        DM_C64IFMT_MC_ILACE, ".dlp", "DrazLace 1.0 (packed)", 0x5800, -1,
         fmtProbeDrazLace10Packed, fmtDecodeDrazPaintPacked,
         NULL, NULL, NULL,
         6,
@@ -324,8 +300,8 @@
     },
     
     {
-        DM_C64IFMT_MC, ".drp", "DrazPaint (unpacked)",
-        fmtProbeDrazPaint, NULL,
+        DM_C64IFMT_MC, ".drp", "DrazPaint (unpacked)", 0x5800, 10051,
+        NULL, NULL,
         NULL, NULL, NULL,
         4,
         {
@@ -337,8 +313,8 @@
     },
 
     {
-        DM_C64IFMT_MC_ILACE, ".drl", "DrazLace 1.0 (unpacked)",
-        fmtProbeDrazLace10, NULL,
+        DM_C64IFMT_MC_ILACE, ".drl", "DrazLace 1.0 (unpacked)", 0x5800, 18242,
+        NULL, NULL,
         NULL, NULL, NULL,
         6,
         {
@@ -352,8 +328,8 @@
     },
     
     {
-        DM_C64IFMT_MC_ILACE, ".mci", "Truepaint (unpacked)",
-        fmtProbeTruePaint, NULL,
+        DM_C64IFMT_MC_ILACE, ".mci", "Truepaint (unpacked)", 0x9c00, 19434,
+        NULL, NULL,
         NULL, NULL, NULL,
         6,
         {
@@ -368,8 +344,8 @@
     },
     
     {
-        DM_C64IFMT_MC, ".kla", "Koala Paint (unpacked)",
-        fmtProbeKoalaPaint, NULL,
+        DM_C64IFMT_MC, ".kla", "Koala Paint (unpacked)", 0x6000, 10003,
+        NULL, NULL,
         NULL, NULL, NULL,
         4,
         {
@@ -381,7 +357,7 @@
     },
 
     {
-        DM_C64IFMT_MC, ".ami", "Amica Paint (packed)",
+        DM_C64IFMT_MC, ".ami", "Amica Paint (packed)", 0x4000, -1,
         fmtProbeAmicaPaintPacked, fmtDecodeAmicaPaintPacked,
         NULL, NULL, NULL,
         4,
@@ -405,7 +381,16 @@
     for (i = 0; i < ndmC64ImageFormats; i++)
     {
         DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
-        int score = fmt->probe(buf, len);
+        int score = DM_PROBE_SCORE_FALSE;
+        if (fmt->probe == NULL && fmt->size > 0 && fmt->addr > 0)
+        {
+            // Generic probe just checks matching size and load address
+            if (len == fmt->size && dmCompareAddr16(buf, 0, fmt->addr))
+                score = DM_PROBE_SCORE_GOOD;
+        }
+        else
+            score = fmt->probe(buf, len, fmt);
+
         if (score > scoreMax)
         {
             scoreMax = score;
--- a/lib64gfx.h	Mon Nov 19 15:06:01 2012 +0200
+++ b/lib64gfx.h	Mon Nov 19 15:52:44 2012 +0200
@@ -128,7 +128,9 @@
     int  type;
     char *extension;
     char *name;
-    int  (*probe)(const Uint8 *buf, const size_t len);
+    size_t addr; // Loading address (0 if no loading address)
+    size_t size; // Size, including loading address. Only used in encoding, if even there (0 if no static size)
+    int  (*probe)(const Uint8 *buf, const size_t len, const struct _DMC64ImageFormat *fmt);
     int  (*decode)(DMC64Image *img, const Uint8 *buf, const size_t len, const struct _DMC64ImageFormat *fmt);
     int  (*encode)(DMC64Image *img, Uint8 **buf, size_t *len, const struct _DMC64ImageFormat *fmt);
     int  (*convertFrom)(DMImage *, DMC64Image *);