add OPENGL_CFLAGS to CPPFLAGS for OpenBSD
[gnash.git] / libbase / GnashImageJpeg.h
blob8417b276e0e6952c314ec5305d59592cf3b33c1f
1 // GnashImageJpeg.h: Jpeg reader, for Gnash.
2 //
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 // Original version by Thatcher Ulrich <tu@tulrich.com> 2002
23 #ifndef GNASH_IMAGE_JPEG_H
24 #define GNASH_IMAGE_JPEG_H
26 #include <csetjmp>
28 #include "dsodefs.h"
29 #include "GnashImage.h"
31 // jpeglib.h redefines HAVE_STDLIB_H. This silences
32 // the warnings, but it's not good.
33 #undef HAVE_STDLIB_H
34 extern "C" {
35 #include <jpeglib.h>
37 #undef HAVE_STDLIB_H
39 // Forward declarations
40 namespace gnash { class IOChannel; }
42 namespace gnash {
43 namespace image {
45 /// Class for reading JPEG image data.
47 /// This uses the IJG jpeglib to implement the Input interface.
48 class JpegInput : public Input
51 private:
53 const char* _errorOccurred;
55 std::jmp_buf _jmpBuf;
57 // State needed for input.
58 jpeg_decompress_struct m_cinfo;
59 jpeg_error_mgr m_jerr;
61 bool _compressorOpened;
63 public:
65 /// Construct a JpegInput object to read from an IOChannel.
67 /// @param in The stream to read JPEG data from. Ownership is shared
68 /// between caller and JpegInput, so it is freed
69 /// automatically when the last owner is destroyed.
70 DSOEXPORT JpegInput(boost::shared_ptr<IOChannel> in);
72 /// Read the JPEG header information only.
74 /// @param maxHeaderBytes The maximum number of bytes to read before
75 /// Stopping. If the header is shorter, we stop
76 /// early.
77 void DSOEXPORT readHeader(unsigned int maxHeaderBytes);
79 ~JpegInput();
81 /// Begin processing the image data.
82 void read();
84 /// Discard any data sitting in our input buffer.
86 /// Use this before/after reading headers or partial image
87 /// data, to avoid screwing up future reads.
88 DSOEXPORT void discardPartialBuffer();
90 /// Complete processing the image and clean up.
92 /// This should close / free all resources from libjpeg.
93 void finishImage();
95 /// Get the image's height in pixels.
97 /// @return The height of the image in pixels.
98 size_t getHeight() const;
100 /// Get the image's width in pixels.
102 /// @return The width of the image in pixels.
103 size_t getWidth() const;
105 /// Get number of components (channels)
107 /// @return The number of components, e.g. 3 for RGB
108 size_t getComponents() const;
110 /// Read a scanline's worth of image data into the given buffer.
112 /// The amount of data read is getWidth() * getComponents().
114 /// @param rgbData The buffer for writing raw RGB data to.
115 void readScanline(unsigned char* rgbData);
117 /// Create a JpegInput and transfer ownership to the caller.
119 /// @param in The IOChannel to read JPEG data from.
120 static std::auto_ptr<Input> create(boost::shared_ptr<IOChannel> in)
122 std::auto_ptr<Input> ret(new JpegInput(in));
123 // might throw an exception (I guess)
124 if (ret.get()) ret->read();
125 return ret;
128 /// \brief
129 /// For reading SWF JPEG2-style image data, using pre-loaded
130 /// headers stored in the given JpegInput object.
132 /// @param loader The JpegInput object to use for reading the
133 /// data. This should have been constructed with
134 /// createSWFJpeg2HeaderOnly().
135 DSOEXPORT static std::auto_ptr<GnashImage> readSWFJpeg2WithTables(
136 JpegInput& loader);
138 /// Create a JPEG 'loader' object by reading a JPEG header.
140 /// This is for reusing the header information for different JPEGs images.
142 /// @param in The channel to read JPEG header data from.
143 /// @param maxHeaderBytes The maximum number of bytes to read.
144 static std::auto_ptr<JpegInput> createSWFJpeg2HeaderOnly(
145 boost::shared_ptr<IOChannel> in, unsigned int maxHeaderBytes)
147 std::auto_ptr<JpegInput> ret (new JpegInput(in));
148 // might throw an exception
149 if (ret.get()) ret->readHeader(maxHeaderBytes);
150 return ret;
153 /// This function is called when libjpeg encounters an error.
155 /// It is needed to avoid memory corruption during stack unwinding by
156 /// freeing libjpeg resources correctly before throwing an exception.
158 /// @param msg An error message for logging.
159 void errorOccurred(const char* msg);
164 // Class for writing JPEG image data.
165 class JpegOutput : public Output
168 public:
170 /// Constract a JpegOutput for writing to an IOChannel
172 /// @param out The gnash::IOChannel to write the image to
173 /// @param width The width of the resulting image
174 /// @param height The height of the resulting image.
175 /// @param quality The quality of the created image, from 1-100.
176 JpegOutput(boost::shared_ptr<IOChannel> out, size_t width,
177 size_t height, int quality);
179 ~JpegOutput();
181 /// Write RGB image data using the parameters supplied at construction.
183 /// @param rgbData The raw RGB image data to write as a JPEG.
184 virtual void writeImageRGB(const unsigned char* rgbData);
186 /// Write RGBA image data using the parameters supplied at construction.
188 /// Note: transparency is ignored because JPEG doesn't support it!
190 /// @param rgbaData The raw RGBA image data to write as a JPEG.
191 virtual void writeImageRGBA(const unsigned char* rgbaData);
193 /// Create a JpegOutput, transferring ownership to the caller.
195 /// @param out The gnash::IOChannel to write the image to
196 /// @param width The width of the resulting image
197 /// @param height The height of the resulting image.
198 /// @param quality The quality of the created image, from 1-100.
199 static std::auto_ptr<Output> create(boost::shared_ptr<IOChannel> out,
200 size_t width, size_t height, int quality);
202 private:
204 jpeg_compress_struct m_cinfo;
205 jpeg_error_mgr m_jerr;
209 } // namespace image
210 } // namespace gnash
212 #endif // JPEG_H
214 // Local Variables:
215 // mode: C++
216 // c-basic-offset: 8
217 // tab-width: 8
218 // indent-tabs-mode: t
219 // End: