netpanzer-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netPanzer-CVS] netpanzer ./ChangeLog src/Lib/2D/PackedSurface....


From: Matthias Braun
Subject: [netPanzer-CVS] netpanzer ./ChangeLog src/Lib/2D/PackedSurface....
Date: Sat, 25 Oct 2003 11:26:07 -0400

CVSROOT:        /cvsroot/netpanzer
Module name:    netpanzer
Branch:         
Changes by:     Matthias Braun <address@hidden> 03/10/25 11:26:06

Modified files:
        .              : ChangeLog 
        src/Lib/2D     : PackedSurface.cpp Surface.cpp 

Log message:
        tried to make .pak and .bmp loader endian-safe

Patches:
Index: netpanzer/ChangeLog
diff -u netpanzer/ChangeLog:1.19 netpanzer/ChangeLog:1.20
--- netpanzer/ChangeLog:1.19    Sat Oct 25 10:52:09 2003
+++ netpanzer/ChangeLog Sat Oct 25 11:26:05 2003
@@ -1,5 +1,6 @@
 25-Oct-2003 by Matthias Braun
 -removed support for .til files and converted them to .bmp
+-tried to make .pak and .bmp loader endian safe
 
 23-Oct-2003 by Ivo Danihelka
 -implemented respawntype="Random" parameter
Index: netpanzer/src/Lib/2D/PackedSurface.cpp
diff -u netpanzer/src/Lib/2D/PackedSurface.cpp:1.18 
netpanzer/src/Lib/2D/PackedSurface.cpp:1.19
--- netpanzer/src/Lib/2D/PackedSurface.cpp:1.18 Mon Oct 13 10:30:07 2003
+++ netpanzer/src/Lib/2D/PackedSurface.cpp      Sat Oct 25 11:26:05 2003
@@ -20,6 +20,7 @@
 #include <vector>
 #include <string>
 #include <algorithm>
+#include <memory>
 
 #include "PackedSurface.hpp"
 #include "Surface.hpp"
@@ -190,37 +191,40 @@
 //--------------------------------------------------------------------------
 void PackedSurface::load(const char* filename)
 {
-    ReadFile* file = FileSystem::openRead(filename);
+    std::auto_ptr<ReadFile> file (FileSystem::openRead(filename));
 
     free();
-    int version;
-    file->read(&version, sizeof(version), 1);
-    if (version < 1) {
-        delete file;
+    int32_t version = file->readSLE32();
+    if (version < 1)
         throw Exception("Invalid PAK file version: %d", version);
-    }
-    if (version > CURRENT_PAK_VERSION) {
-        delete file;
+    if (version > CURRENT_PAK_VERSION)
         throw Exception("PAK file version of '%s' is newer(%d) than "
                 "the currently supported version(%d)",
                filename, version, CURRENT_PAK_VERSION);
-    }
-    file->read(&pix, sizeof(pix), 1);
+    pix.x = file->readSLE32();
+    pix.y = file->readSLE32();
 
     center = pix / 2;
 
-    file->read(&frameCount, sizeof(frameCount), 1);
-    file->read(&fps, sizeof(fps), 1);
-    file->read(&offset, sizeof(offset), 1);
+    frameCount = file->readSLE32(); 
+    // should be done like following but this isn't backward compatible to the
+    // existing files :-/
+    //fps = float(file->readSLE32()) / 65536;
+    // XXX is this correct?!?
+    int32_t fpsint = file->readSLE32();
+    fps = * ((float*) &fpsint);
+    offset.x = file->readSLE32();
+    offset.y = file->readSLE32();
+    
     rowOffsetTable = (int *) malloc((pix.y * frameCount + 1) * 
sizeof(*rowOffsetTable));
-    if (rowOffsetTable == 0) {
-        delete file;
+    if (rowOffsetTable == 0)
         throw Exception("ERROR: Unable to allocate rowTableOffset for 
PackedSurface.");
+    for(int i=0;i<(pix.y * frameCount+1);i++) {
+        rowOffsetTable[i] = file->readSLE32();
     }
-    file->read(rowOffsetTable, (pix.y*frameCount + 1)*sizeof(*rowOffsetTable), 
1);
+    
     packedDataChunk = (uint8_t *)malloc(rowOffsetTable[pix.y*frameCount]);
     if (packedDataChunk == 0) {
-        delete file;
         throw Exception("ERROR: Unable to allocate packedDataChunk for 
PackedSurface.");
     }
     if(file->read(packedDataChunk, rowOffsetTable[pix.y*frameCount], 1) != 1)
@@ -231,28 +235,31 @@
 
     // Add size of packedDataChunk.
     totalByteCount += pix.y * frameCount;
-
-    delete file;
 }
 
 //--------------------------------------------------------------------------
 void PackedSurface::save(const char* filename) const
 {
-    WriteFile* file = FileSystem::openWrite(filename);
+    std::auto_ptr<WriteFile> file (FileSystem::openWrite(filename));
 
-    int version = CURRENT_PAK_VERSION;
-    file->write(&version, sizeof(version), 1);
-    file->write(&pix, sizeof(pix), 1);
-    file->write(&frameCount, sizeof(frameCount), 1);
-    file->write(&fps, sizeof(fps), 1);
-    file->write(&offset, sizeof(offset), 1);
-    file->write(rowOffsetTable, (pix.y*frameCount + 
1)*sizeof(*rowOffsetTable), 1);
-    if (file->write(packedDataChunk, rowOffsetTable[pix.y*frameCount], 1) != 
1) {
-        delete file;
-        throw Exception("error while writing '%s'.", filename);
+    int32_t version = CURRENT_PAK_VERSION;
+    file->writeSLE32(version);
+    file->writeSLE32(pix.x);
+    file->writeSLE32(pix.y);
+    
+    // XXX bad not endian safe :-/
+    // is this correct?!?
+    file->writeSLE32( *((uint32_t*) (&fps)) );
+
+    file->writeSLE32(offset.x);
+    file->writeSLE32(offset.y);
+    
+    for(int i=0;i<(pix.y*frameCount+1); i++) {
+        file->writeSLE32(rowOffsetTable[i]);
     }
-
-    delete file;
+    
+    if (file->write(packedDataChunk, rowOffsetTable[pix.y*frameCount], 1) != 1)
+        throw Exception("error while writing '%s'.", filename);
 }
 
 //--------------------------------------------------------------------------
Index: netpanzer/src/Lib/2D/Surface.cpp
diff -u netpanzer/src/Lib/2D/Surface.cpp:1.38 
netpanzer/src/Lib/2D/Surface.cpp:1.39
--- netpanzer/src/Lib/2D/Surface.cpp:1.38       Sat Oct 25 10:52:14 2003
+++ netpanzer/src/Lib/2D/Surface.cpp    Sat Oct 25 11:26:05 2003
@@ -120,8 +120,18 @@
     uint16_t    bfReserved1;
     uint16_t    bfReserved2;
     uint32_t   bfOffBits;
+
+    BitmapFileHeader(ReadFile* file);
+};
+
+BitmapFileHeader::BitmapFileHeader(ReadFile* file)
+{
+    bfType = file->readULE16();
+    bfSize = file->readULE32();
+    bfReserved1 = file->readULE16();
+    bfReserved2 = file->readULE16();
+    bfOffBits = file->readULE32();
 }
-__attribute__((packed));
 
 #define BI_RGB      0L
 #define BI_RLE8     1L
@@ -141,8 +151,24 @@
     uint32_t  biYPelsPerMeter;
     uint32_t  biClrUsed;
     uint32_t  biClrImportant;
+
+    BitmapInfoHeader(ReadFile* file);
+};
+
+BitmapInfoHeader::BitmapInfoHeader(ReadFile* file)
+{
+    biSize = file->readULE32();
+    biWidth = file->readULE32();
+    biHeight = file->readULE32();
+    biPlanes = file->readULE16();
+    biBitCount = file->readULE16();
+    biCompression = file->readULE32();
+    biSizeImage = file->readULE32();
+    biXPelsPerMeter = file->readULE32();
+    biYPelsPerMeter = file->readULE32();
+    biClrUsed = file->readULE32();
+    biClrImportant = file->readULE32();
 }
-__attribute__((packed));
 
 class RGBQuad
 {
@@ -2046,79 +2072,6 @@
 
     if (n < 1) return;
 
-    // Align to 4-byte dest
-#if 0
-    while (int(d) & 3)
-    {
-        //assert(((*s << 8) + *i) < 256 * 256);
-        *d = Palette::colorTableLightDark[(*s << 8) + *i];
-
-        n--;
-        i++;
-        s++;
-        d++;
-
-        if (n < 1) return;
-    }
-
-    if (n > 3) {
-        // XXX no msvc assembler
-#ifdef MSVC
-        _asm {
-
-            // load vars into regs
-            push ebp
-            mov ebx, i
-            mov esi, s
-            mov edi, d
-            mov ebp, n
-
-            xor ecx, ecx
-    quad:
-
-            mov cl, [ebx + 2]
-            add ebx, 4
-
-                mov ch, [esi + 2]
-                add edi, 4
-
-                    mov al, quickHack[ecx]
-                    mov cl, [ebx - 4 + 3]
-
-                    mov ch, [esi + 3]
-                    add esi, 4
-
-                        mov ah, quickHack[ecx]
-
-                        shl eax, 16
-                        mov cl, [ebx - 4 + 0]
-
-                        mov ch, [esi - 4 + 0]
-                        sub ebp, 4
-
-                        mov al, quickHack[ecx]
-                        mov cl, [ebx - 4 + 1]
-
-                        mov ch, [esi - 4 + 1]
-
-                        mov ah, quickHack[ecx]
-
-                        // decrement counter and advance pointers
-                        mov [edi-4], eax
-                        ja quad
-
-                        // restore vars for the c code that cleans up
-                        mov eax, ebp
-                        pop ebp
-                        mov i, ebx
-                        mov s, esi
-                        mov d, edi
-                        mov n, eax
-                    }
-#endif
-
-                }
-#endif
     while (n > 0) {
         //assert(((*s << 8) + *i) < 256 * 256);
         *d = Palette::colorTableLightDark[(*s << 8) + *i];
@@ -2695,64 +2648,64 @@
 void Surface::loadBMP(const char *fileName, bool needAlloc /* = true */,
                       void *returnPalette /* = 0 */)
 {
-    BitmapFileHeader file_header;
-    BitmapInfoHeader info_header;
-
     assert(this != 0);
 
     if (needAlloc) free();
 
     std::auto_ptr<ReadFile> file (FileSystem::openRead(fileName));
 
-    if(file->read( &file_header, sizeof(BitmapFileHeader), 1) != 1)
-        throw Exception("Error reading .bmp file %s", fileName);
+    try {
+        BitmapFileHeader file_header(file.get());
 
-    if ( file_header.bfType != 0x4d42 ) // file_header.bfType != "BM"
-        throw Exception("%s is not a valid 8-bit BMP file", fileName);
+        if ( file_header.bfType != 0x4d42 ) // file_header.bfType != "BM"
+            throw Exception("%s is not a valid 8-bit BMP file", fileName);
 
-    if(file->read(&info_header, sizeof(BitmapInfoHeader), 1) != 1)
-        throw Exception("Error reading .bmp file %s", fileName);
+        BitmapInfoHeader info_header(file.get());
+        
+        if ( info_header.biBitCount != 8 )
+            throw Exception("%s is not a 8-bit BMP file", fileName);
 
-    if ( info_header.biBitCount != 8 )
-        throw Exception("%s is not a 8-bit BMP file", fileName);
+        if ( info_header.biCompression != BI_RGB )
+            throw Exception("%s is not a 8-bit UnCompressed BMP file", 
fileName);
 
-    if ( info_header.biCompression != BI_RGB )
-        throw Exception("%s is not a 8-bit UnCompressed BMP file", fileName);
+        if (needAlloc) {
 
-    if (needAlloc) {
+            if (!alloc(info_header.biWidth, info_header.biHeight , false, 
info_header.biWidth, 1) ) {
+                throw Exception("Not enough memory to load BMP image %s", 
fileName);
+            }
 
-        if (!alloc(info_header.biWidth, info_header.biHeight , false, 
info_header.biWidth, 1) ) {
-            throw Exception("Not enough memory to load BMP image %s", 
fileName);
+        } else {
+            // Check and make sure the picture will fit
+            if (pix.x < (long) info_header.biWidth|| pix.y < (long) 
info_header.biHeight )
+                throw Exception("Not enough memory to load BMP image %s", 
fileName);
+        }
+
+        file->seek(file_header.bfOffBits);
+
+        if ( (info_header.biWidth % 4) == 0 ) {
+            if (file->read(mem, pix.x * pix.y, 1) != 1)
+                throw Exception("error while reading bmp image %s", fileName);
+        } else {
+            int padding = ((info_header.biWidth / 4 + 1) * 4) - 
info_header.biWidth;
+
+            PIX buffer[10];
+            int numRows = pix.y;
+
+            //PIX *sPtr = mem;
+
+            for (int row = 0; row < numRows; row++) {
+                if(file->read(mem, pix.x, 1) != 1 ||
+                   file->read(buffer, padding, 1) != 1)
+                    throw Exception("error reading file %s.", fileName);
+                mem += stride;
+            }
         }
 
-    } else {
-        // Check and make sure the picture will fit
-        if (pix.x < (long) info_header.biWidth|| pix.y < (long) 
info_header.biHeight )
-            throw Exception("Not enough memory to load BMP image %s", 
fileName);
+        flipVertical();
+    } catch(std::exception& e) {
+        throw Exception("Error reading .bmp file '%s': %s",
+                fileName, e.what());
     }
-
-    file->seek(file_header.bfOffBits);
-
-    if ( (info_header.biWidth % 4) == 0 ) {
-        if (file->read(mem, pix.x * pix.y, 1) != 1)
-            throw Exception("error while reading bmp image %s", fileName);
-    } else {
-        int padding = ((info_header.biWidth / 4 + 1) * 4) - 
info_header.biWidth;
-
-        PIX buffer[10];
-        int numRows = pix.y;
-
-        //PIX *sPtr = mem;
-
-        for (int row = 0; row < numRows; row++) {
-            if(file->read(mem, pix.x, 1) != 1 ||
-               file->read(buffer, padding, 1) != 1)
-                throw Exception("error reading file %s.", fileName);
-            mem += stride;
-        }
-    }
-
-    flipVertical();
 }
 
 // drawWindowsBorder




reply via email to

[Prev in Thread] Current Thread [Next in Thread]