bitmapJpeg.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "ljpeg/jpeglib.h"
  23. #include "core/stream/stream.h"
  24. #include "gfx/bitmap/gBitmap.h"
  25. static bool sReadJPG(Stream &stream, GBitmap *bitmap);
  26. static bool sWriteJPG(GBitmap *bitmap, Stream &stream, U32 compressionLevel);
  27. static struct _privateRegisterJPG
  28. {
  29. _privateRegisterJPG()
  30. {
  31. GBitmap::Registration reg;
  32. reg.priority = 50;
  33. reg.extensions.push_back( "jpeg" );
  34. reg.extensions.push_back( "jpg" );
  35. reg.readFunc = sReadJPG;
  36. reg.writeFunc = sWriteJPG;
  37. GBitmap::sRegisterFormat( reg );
  38. }
  39. } sStaticRegisterJPG;
  40. //-------------------------------------- Replacement I/O for standard LIBjpeg
  41. // functions. we don't wanna use
  42. // FILE*'s...
  43. static S32 jpegReadDataFn(void *client_data, U8 *data, S32 length)
  44. {
  45. Stream *stream = (Stream*)client_data;
  46. AssertFatal(stream != NULL, "jpegReadDataFn::No stream.");
  47. S32 pos = stream->getPosition();
  48. if (stream->read(length, data))
  49. return length;
  50. if (stream->getStatus() == Stream::EOS)
  51. return (stream->getPosition()-pos);
  52. else
  53. return 0;
  54. }
  55. //--------------------------------------
  56. static S32 jpegWriteDataFn(void *client_data, U8 *data, S32 length)
  57. {
  58. Stream *stream = (Stream*)client_data;
  59. AssertFatal(stream != NULL, "jpegWriteDataFn::No stream.");
  60. if (stream->write(length, data))
  61. return length;
  62. else
  63. return 0;
  64. }
  65. //--------------------------------------
  66. static S32 jpegFlushDataFn(void *)
  67. {
  68. // do nothing since we can't flush the stream object
  69. return 0;
  70. }
  71. //--------------------------------------
  72. static S32 jpegErrorFn(void *client_data)
  73. {
  74. Stream *stream = (Stream*)client_data;
  75. AssertFatal(stream != NULL, "jpegErrorFn::No stream.");
  76. return (stream->getStatus() != Stream::Ok);
  77. }
  78. //--------------------------------------
  79. static bool sReadJPG(Stream &stream, GBitmap *bitmap)
  80. {
  81. JFREAD = jpegReadDataFn;
  82. JFERROR = jpegErrorFn;
  83. jpeg_decompress_struct cinfo;
  84. jpeg_error_mgr jerr;
  85. // We set up the normal JPEG error routines, then override error_exit.
  86. //cinfo.err = jpeg_std_error(&jerr.pub);
  87. //jerr.pub.error_exit = my_error_exit;
  88. // if (setjmp(jerr.setjmp_buffer))
  89. // {
  90. // // If we get here, the JPEG code has signaled an error.
  91. // // We need to clean up the JPEG object, close the input file, and return.
  92. // jpeg_destroy_decompress(&cinfo);
  93. // return false;
  94. // }
  95. cinfo.err = jpeg_std_error(&jerr); // set up the normal JPEG error routines.
  96. cinfo.client_data = (void*)&stream; // set the stream into the client_data
  97. // Now we can initialize the JPEG decompression object.
  98. jpeg_create_decompress(&cinfo);
  99. jpeg_stdio_src(&cinfo);
  100. // Read file header, set default decompression parameters
  101. jpeg_read_header(&cinfo, true);
  102. GFXFormat format;
  103. switch (cinfo.out_color_space)
  104. {
  105. case JCS_GRAYSCALE: format = GFXFormatA8; break;
  106. case JCS_RGB: format = GFXFormatR8G8B8; break;
  107. default:
  108. jpeg_destroy_decompress(&cinfo);
  109. return false;
  110. }
  111. // Start decompressor
  112. jpeg_start_decompress(&cinfo);
  113. // allocate the bitmap space and init internal variables...
  114. bitmap->allocateBitmap(cinfo.output_width, cinfo.output_height, false, format);
  115. // Set up the row pointers...
  116. U32 rowBytes = cinfo.output_width * cinfo.output_components;
  117. U8* pBase = (U8*)bitmap->getBits();
  118. for (U32 i = 0; i < bitmap->getHeight(); i++)
  119. {
  120. JSAMPROW rowPointer = pBase + (i * rowBytes);
  121. jpeg_read_scanlines(&cinfo, &rowPointer, 1);
  122. }
  123. // Finish decompression
  124. jpeg_finish_decompress(&cinfo);
  125. // Release JPEG decompression object
  126. // This is an important step since it will release a good deal of memory.
  127. jpeg_destroy_decompress(&cinfo);
  128. // We know JPEG's don't have any transparency
  129. bitmap->setHasTransparency(false);
  130. return true;
  131. }
  132. //--------------------------------------------------------------------------
  133. static bool sWriteJPG(GBitmap *bitmap, Stream &stream, U32 compressionLevel)
  134. {
  135. TORQUE_UNUSED(compressionLevel); // compression level not currently hooked up
  136. GFXFormat format = bitmap->getFormat();
  137. // JPEG format does not support transparency so any image
  138. // in Alpha format should be saved as a grayscale which coincides
  139. // with how the readJPEG function will read-in a JPEG. So the
  140. // only formats supported are RGB and Alpha, not RGBA.
  141. AssertFatal(format == GFXFormatR8G8B8 || format == GFXFormatA8,
  142. "GBitmap::writeJPEG: ONLY RGB bitmap writing supported at this time.");
  143. if (format != GFXFormatR8G8B8 && format != GFXFormatA8)
  144. return false;
  145. // maximum image size allowed
  146. #define MAX_HEIGHT 4096
  147. if (bitmap->getHeight() > MAX_HEIGHT)
  148. return false;
  149. // Bind our own stream writing, error, and memory flush functions
  150. // to the jpeg library interface
  151. JFWRITE = jpegWriteDataFn;
  152. JFFLUSH = jpegFlushDataFn;
  153. JFERROR = jpegErrorFn;
  154. // Allocate and initialize our jpeg compression structure and error manager
  155. jpeg_compress_struct cinfo;
  156. jpeg_error_mgr jerr;
  157. cinfo.err = jpeg_std_error(&jerr); // set up the normal JPEG error routines.
  158. cinfo.client_data = (void*)&stream; // set the stream into the client_data
  159. jpeg_create_compress(&cinfo); // allocates a small amount of memory
  160. // specify the destination for the compressed data(our stream)
  161. jpeg_stdio_dest(&cinfo);
  162. // set the image properties
  163. cinfo.image_width = bitmap->getWidth(); // image width
  164. cinfo.image_height = bitmap->getHeight(); // image height
  165. cinfo.input_components = bitmap->getBytesPerPixel(); // samples per pixel(RGB:3, Alpha:1)
  166. switch (format)
  167. {
  168. case GFXFormatA8: // no alpha support in JPEG format, so turn it into a grayscale
  169. cinfo.in_color_space = JCS_GRAYSCALE;
  170. break;
  171. case GFXFormatR8G8B8: // otherwise we are writing in RGB format
  172. cinfo.in_color_space = JCS_RGB;
  173. break;
  174. default:
  175. AssertFatal( false, "Format not handled in GBitmap::writeJPEG() switch" );
  176. break;
  177. }
  178. // use default compression params(75% compression)
  179. jpeg_set_defaults(&cinfo);
  180. // begin JPEG compression cycle
  181. jpeg_start_compress(&cinfo, true);
  182. // Set up the row pointers...
  183. U32 rowBytes = cinfo.image_width * cinfo.input_components;
  184. U8* pBase = (U8*)bitmap->getBits();
  185. for (U32 i = 0; i < bitmap->getHeight(); i++)
  186. {
  187. // write the image data
  188. JSAMPROW rowPointer = pBase + (i * rowBytes);
  189. jpeg_write_scanlines(&cinfo, &rowPointer, 1);
  190. }
  191. // complete the compression cycle
  192. jpeg_finish_compress(&cinfo);
  193. // release the JPEG compression object
  194. jpeg_destroy_compress(&cinfo);
  195. // return success
  196. return true;
  197. }