Amended for dealing with backup images for revisions > 2.
authorPat Thoyts <patthoyts@users.sourceforge.net>
Thu, 19 Jan 2023 15:24:03 +0000 (15:24 +0000)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Thu, 19 Jan 2023 15:24:03 +0000 (15:24 +0000)
srfdump.c

index 56ffb7f6ef824d7bfe9d6704b037f3ec15166e83..7bf83004924b23342ab02c21052b4d846251c763 100644 (file)
--- a/srfdump.c
+++ b/srfdump.c
@@ -33,6 +33,7 @@ static size_t fcopy(FILE *src, FILE *dst, size_t count)
     return total;
 }
 
+// For a single image the table just contains the end of the image.
 static uint32_t * ReadImageTable(SurfaceFileHeader *hdrPtr, FILE *fp)
 {
     uint32_t *table = NULL;
@@ -147,10 +148,17 @@ static int SrfDump(FILE *fp)
         free(triangles);
     }
 
+    uint32_t image_table = ftell(fp);
+    printf("  image table: %#08x\n", image_table);
     uint32_t *offsets = ReadImageTable(&hdr, fp);
     uint32_t table_base = ftell(fp);
     if (offsets)
     {
+        if (hdr.num_images < 10)
+        {
+            for (uint32_t n = 0; n < hdr.num_images + 1; ++n)
+                printf("    %lu: %#08x\n", n, offsets[n]);
+        }
         printf("  image base: %#08x\n", table_base);
 #ifdef WIN32
         _tmkdir(_T("images"));
@@ -167,20 +175,32 @@ static int SrfDump(FILE *fp)
                 exit(1);
             }
 
-            // this calculated image size is off by 4 bytes. why?
-            uint32_t image_size = offsets[n + 1] - offsets[n] - sizeof(SurfaceImageInfo);
+            uint32_t image_section_size = offsets[n + 1] - offsets[n] - sizeof(SurfaceImageInfo);
             SurfaceImageInfo info = { 0 };
             fseek(fp, offsets[n], SEEK_SET);
             fread(&info, sizeof(SurfaceImageInfo), 1, fp);
             size_t len = fcopy(fp, fp2, info.image_size);
             fclose(fp2);
+            // For revision > 2 we have the size of the 'original' image if present and the image data.
+            if (hdr.revision > 2)
+            {
+                uint32_t backup_size = 0;
+                fread(&backup_size, sizeof(uint32_t), 1, fp);
+                fseek(fp, backup_size, SEEK_CUR);
+                len += sizeof(backup_size) + backup_size; // account for 'backup image' and its size
+                printf("  orig:%u: %u\n", n, backup_size);
+            }
 
-            printf("  img:%d: %#08x @(%.3f,%.3f,%.3f) fov:%.3f,%.3f res:%u,%u off:%d,%d siz:%#08x %d\n",
-                n, offsets[n], 
+            printf("  img:%u: %#08x @(%.3f,%.3f,%.3f) fov:%.3f,%.3f res:%u,%u off:%d,%d siz:%#08x\n",
+                n, offsets[n],
                 info.point.x, info.point.y, info.point.z,
                 info.image_width_microns, info.image_height_microns,
                 info.resolution_x, info.resolution_y,
-                info.xoffset, info.yoffset, info.image_size, len-image_size);
+                info.xoffset, info.yoffset, info.image_size);
+            if ((len - image_section_size) != 0)
+            {
+                printf("  image size error of %zu bytes\n", (len - image_section_size));
+            }
         }
         free(offsets);
     }