bitmapJpeg.cpp

Engine/source/gfx/bitmap/loaders/bitmapJpeg.cpp

More...

Classes:

Public Defines

define
MAX_HEIGHT() 4096

Public Variables

struct _privateRegisterJPG

Public Functions

jpegErrorFn(void * client_data)
jpegReadDataFn(void * client_data, U8 * data, S32 length)
jpegWriteDataFn(void * client_data, U8 * data, S32 length)
bool
sReadJPG(Stream & stream, GBitmap * bitmap)
bool
sWriteJPG(GBitmap * bitmap, Stream & stream, U32 compressionLevel)

Detailed Description

Public Defines

MAX_HEIGHT() 4096

Public Variables

struct _privateRegisterJPG sStaticRegisterJPG 

Public Functions

jpegErrorFn(void * client_data)

jpegFlushDataFn(void * )

jpegReadDataFn(void * client_data, U8 * data, S32 length)

jpegWriteDataFn(void * client_data, U8 * data, S32 length)

sReadJPG(Stream & stream, GBitmap * bitmap)

sWriteJPG(GBitmap * bitmap, Stream & stream, U32 compressionLevel)

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