@@ -288,16 +288,28 @@ bool DecodeToPnm(std::istream& jlsFile, std::ostream& pnmFile)
288288 JLS_ERROR err = JpegLsReadHeaderStream (compressedByteStream, &info);
289289 if (err != OK)
290290 return false ;
291+ jlsFile.seekg (0 );
291292
292293 int maxval = (1 << info.bitspersample ) - 1 ;
293- int id = info.components == 3 ? 6 : 5 ;
294- info.colorTransform = XFORM_BIGENDIAN;
295294
296- pnmFile << ' P' << id << ' ' << info.width << ' ' << info.height << ' ' << maxval << " " << std::endl;
297- ByteStreamInfo pnmStream = {pnmFile.rdbuf (), NULL , 0 };
295+ int bytesPerSample = maxval > 255 ? 2 : 1 ;
296+ std::vector<BYTE> outputBuffer (info.width * info.height * bytesPerSample);
297+ ByteStreamInfo outputInfo = FromByteArray (outputBuffer.data (), outputBuffer.size ());
298+ JpegLsDecodeStream (outputInfo, compressedByteStream, &info);
299+
300+ // PNM format requires most significant byte first (big endian).
301+ if (bytesPerSample == 2 )
302+ {
303+ for (std::vector<BYTE>::iterator i = outputBuffer.begin (); i != outputBuffer.end (); i += 2 )
304+ {
305+ std::iter_swap (i, i + 1 );
306+ }
307+ }
308+
309+ int magicNumber = info.components == 3 ? 6 : 5 ;
310+ pnmFile << ' P' << magicNumber << std::endl << info.width << ' ' << info.height << std::endl << maxval << std::endl;
311+ pnmFile.write (reinterpret_cast <char *>(&outputBuffer[0 ]), outputBuffer.size ());
298312
299- jlsFile.seekg (0 );
300- JpegLsDecodeStream (pnmStream, compressedByteStream, &info);
301313 return true ;
302314}
303315
0 commit comments