1 // GnashImageJpeg.h: Jpeg reader, for Gnash.
3 // Copyright (C) 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 // Original version by Thatcher Ulrich <tu@tulrich.com> 2002
24 #ifndef GNASH_IMAGE_JPEG_H
25 #define GNASH_IMAGE_JPEG_H
30 #include "GnashImage.h"
32 // jpeglib.h redefines HAVE_STDLIB_H. This silences
33 // the warnings, but it's not good.
40 // Forward declarations
41 namespace gnash
{ class IOChannel
; }
46 /// Class for reading JPEG image data.
48 /// This uses the IJG jpeglib to implement the Input interface.
49 class DSOEXPORT JpegInput
: public Input
54 const char* _errorOccurred
;
58 // State needed for input.
59 jpeg_decompress_struct m_cinfo
;
60 jpeg_error_mgr m_jerr
;
62 bool _compressorOpened
;
66 /// Construct a JpegInput object to read from an IOChannel.
68 /// @param in The stream to read JPEG data from. Ownership is shared
69 /// between caller and JpegInput, so it is freed
70 /// automatically when the last owner is destroyed.
71 DSOEXPORT
JpegInput(std::shared_ptr
<IOChannel
> in
);
73 /// Read the JPEG header information only.
75 /// @param maxHeaderBytes The maximum number of bytes to read before
76 /// Stopping. If the header is shorter, we stop
78 void DSOEXPORT
readHeader(unsigned int maxHeaderBytes
);
82 /// Begin processing the image data.
85 /// Discard any data sitting in our input buffer.
87 /// Use this before/after reading headers or partial image
88 /// data, to avoid screwing up future reads.
89 DSOEXPORT
void discardPartialBuffer();
91 /// Complete processing the image and clean up.
93 /// This should close / free all resources from libjpeg.
96 /// Get the image's height in pixels.
98 /// @return The height of the image in pixels.
99 size_t getHeight() const;
101 /// Get the image's width in pixels.
103 /// @return The width of the image in pixels.
104 size_t getWidth() const;
106 /// Get number of components (channels)
108 /// @return The number of components, e.g. 3 for RGB
109 size_t getComponents() const;
111 /// Read a scanline's worth of image data into the given buffer.
113 /// The amount of data read is getWidth() * getComponents().
115 /// @param rgbData The buffer for writing raw RGB data to.
116 void readScanline(unsigned char* rgbData
);
118 /// Create a JpegInput and transfer ownership to the caller.
120 /// @param in The IOChannel to read JPEG data from.
121 static std::unique_ptr
<Input
> create(std::shared_ptr
<IOChannel
> in
)
123 std::unique_ptr
<Input
> ret(new JpegInput(in
));
124 // might throw an exception (I guess)
125 if (ret
.get()) ret
->read();
130 /// For reading SWF JPEG2-style image data, using pre-loaded
131 /// headers stored in the given JpegInput object.
133 /// @param loader The JpegInput object to use for reading the
134 /// data. This should have been constructed with
135 /// createSWFJpeg2HeaderOnly().
136 DSOEXPORT
static std::unique_ptr
<GnashImage
> readSWFJpeg2WithTables(
139 /// Create a JPEG 'loader' object by reading a JPEG header.
141 /// This is for reusing the header information for different JPEGs images.
143 /// @param in The channel to read JPEG header data from.
144 /// @param maxHeaderBytes The maximum number of bytes to read.
145 static std::unique_ptr
<JpegInput
> createSWFJpeg2HeaderOnly(
146 std::shared_ptr
<IOChannel
> in
, unsigned int maxHeaderBytes
)
148 std::unique_ptr
<JpegInput
> ret (new JpegInput(in
));
149 // might throw an exception
150 if (ret
.get()) ret
->readHeader(maxHeaderBytes
);
154 /// This function is called when libjpeg encounters an error.
156 /// It is needed to avoid memory corruption during stack unwinding by
157 /// freeing libjpeg resources correctly before throwing an exception.
159 /// @param msg An error message for logging.
160 void errorOccurred(const char* msg
);
165 // Class for writing JPEG image data.
166 class JpegOutput
: public Output
171 /// Constract a JpegOutput for writing to an IOChannel
173 /// @param out The gnash::IOChannel to write the image to
174 /// @param width The width of the resulting image
175 /// @param height The height of the resulting image.
176 /// @param quality The quality of the created image, from 1-100.
177 JpegOutput(std::shared_ptr
<IOChannel
> out
, size_t width
,
178 size_t height
, int quality
);
182 /// Write RGB image data using the parameters supplied at construction.
184 /// @param rgbData The raw RGB image data to write as a JPEG.
185 virtual void writeImageRGB(const unsigned char* rgbData
);
187 /// Write RGBA image data using the parameters supplied at construction.
189 /// Note: transparency is ignored because JPEG doesn't support it!
191 /// @param rgbaData The raw RGBA image data to write as a JPEG.
192 virtual void writeImageRGBA(const unsigned char* rgbaData
);
194 /// Create a JpegOutput, transferring ownership to the caller.
196 /// @param out The gnash::IOChannel to write the image to
197 /// @param width The width of the resulting image
198 /// @param height The height of the resulting image.
199 /// @param quality The quality of the created image, from 1-100.
200 static std::unique_ptr
<Output
> create(std::shared_ptr
<IOChannel
> out
,
201 size_t width
, size_t height
, int quality
);
205 jpeg_compress_struct m_cinfo
;
206 jpeg_error_mgr m_jerr
;
219 // indent-tabs-mode: t