]> git.cworth.org Git - apitrace/blobdiff - image/image_pnm.cpp
gui: Fix debug build.
[apitrace] / image / image_pnm.cpp
index 55130cb12866859a61efcdfd211a473ad63ba919..58e2cd359058f8ffc9f014fb3db4a697b83447f1 100644 (file)
@@ -62,9 +62,13 @@ Image::writePNM(std::ostream &os, const char *comment) const
         if (channels == 1) {
             identifier = "Pf";
             outChannels = 1;
-        } else {
+        } else if (channels <= 3) {
             identifier = "PF";
-            outChannels = 3;
+            outChannels = 4;
+        } else {
+            // Non-standard extension for 4 floats
+            identifier = "PX";
+            outChannels = 4;
         }
         break;
     default:
@@ -80,6 +84,8 @@ Image::writePNM(std::ostream &os, const char *comment) const
 
     if (channelType == TYPE_UNORM8) {
         os << "255" << "\n";
+    } else {
+        os << "1" << "\n";
     }
 
     const unsigned char *row;
@@ -97,7 +103,7 @@ Image::writePNM(std::ostream &os, const char *comment) const
          * Need to add/remove channels, one pixel at a time.
          */
 
-        unsigned char *tmp = new unsigned char[width*bytesPerPixel];
+        unsigned char *tmp = new unsigned char[width*outChannels*bytesPerChannel];
 
         if (channelType == TYPE_UNORM8) {
             /*
@@ -172,7 +178,7 @@ Image::writePNM(std::ostream &os, const char *comment) const
                         *dst++ = 0;
                     }
                 }
-                os.write((const char *)tmp, width*bytesPerPixel);
+                os.write((const char *)tmp, width*outChannels*bytesPerChannel);
             }
         }
 
@@ -233,6 +239,10 @@ readPNMHeader(const char *buffer, size_t bufferSize, PNMInfo &info)
         info.channels = 3;
         info.channelType = TYPE_FLOAT;
         break;
+    case 'X':
+        info.channels = 4;
+        info.channelType = TYPE_FLOAT;
+        break;
     default:
         return NULL;
     }
@@ -262,10 +272,8 @@ readPNMHeader(const char *buffer, size_t bufferSize, PNMInfo &info)
     bufferSize -= nextBuffer - currentBuffer;
     currentBuffer = nextBuffer;
 
-    if (info.channelType == TYPE_UNORM8) {
-        // skip over "255\n" at end of header
-        nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
-    }
+    // skip scale factor / endianness line
+    nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
 
     // return start of image data
     return nextBuffer;