-if (DEBUG) printf("Entree Step 8\n");
-
- /* This is an important step since it will release a good deal of memory. */
-
- jpeg_destroy_decompress(&cinfo);
-
- /* After finish_decompress, we can close the input file.
- * Here we postpone it until after no more JPEG errors are possible,
- * so as to simplify the setjmp error logic above. (Actually, I don't
- * think that jpeg_destroy can do an error exit, but why assume anything...)
- */
+ // Make a one-row-high sample array that will go away when done with image
+ buffer = (*cinfo.mem->alloc_sarray)
+ ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
+
+ // Step 6: while (scan lines remain to be read)
+
+ // Save the buffer in case of suspension to be able to reuse it later:
+ SampBuffer = buffer;
+ }
+ else
+ {
+ // Suspension: re-use the buffer:
+ buffer = (JSAMPARRAY)SampBuffer;
+ }
+ int bufsize = cinfo.output_width * cinfo.output_components;
+ size_t rowsize = bufsize * sizeof(JSAMPLE);
+
+ while (cinfo.output_scanline < cinfo.output_height)
+ {
+ if ( jpeg_read_scanlines(&cinfo, buffer, 1) == 0 )
+ {
+ // Suspension in jpeg_read_scanlines
+ statesuspension = 3;
+ return true;
+ }
+// The ijg has no notion of big endian, therefore always swap the jpeg stream
+#if (defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)) && (CMAKE_BITS_IN_JSAMPLE != 8)
+ uint16_t *buffer16 = (uint16_t*)*buffer;
+ uint16_t *pimage16 = (uint16_t*)pImage;
+ for(unsigned int i=0;i<rowsize/2;i++)
+ pimage16[i] = (buffer16[i] >> 8) | (buffer16[i] << 8 );
+#else
+ memcpy( pImage, *buffer,rowsize);
+#endif //GDCM_WORDS_BIGENDIAN
+ pImage+=rowsize;
+ }
+
+ // Step 7: Finish decompression
+ if ( jpeg_finish_decompress(&cinfo) == FALSE )
+ {
+ // Suspension: jpeg_finish_decompress
+ statesuspension = 4;
+ }
+
+ // Step 8: Release JPEG decompression object
+ jpeg_destroy_decompress(&cinfo);