fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / FileIO / OSGMNGImageFileType.cpp
bloba927012429b3b38e7a6a3e68d0080e7a46fdadf3
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //-------------------------------
40 // Includes
41 //-------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #ifdef OSG_SGI_LIB
49 #include <limits>
50 #endif
52 #ifdef MNG_LIB
53 #include <mng.h>
54 #endif
56 #include "OSGMNGImageFileType.h"
57 #include "OSGLog.h"
59 #ifndef OSG_DO_DOC
60 # ifdef OSG_WITH_MNG
61 # define OSG_MNG_ARG(ARG) ARG
62 # else
63 # define OSG_MNG_ARG(ARG)
64 # endif
65 #else
66 # define OSG_MNG_ARG(ARG) ARG
67 #endif
69 // Static Class Varible implementations:
70 static const OSG::Char8 *suffixArray[] =
72 "mng"
75 OSG_BEGIN_NAMESPACE
77 /*! \class MNGImageFileType
79 Image File Type to read/write and store/restore Image objects as
80 MNG data.
82 To be able to load MNG images you need the MNG library,
83 (check the Prerequisites page on www.opensg.org).
84 The lib comes with all Linux distributions.
86 You have to --enable-mng in the configure line to enable
87 the singleton object.
91 MNGImageFileType MNGImageFileType::_the("video/x-mng",
92 suffixArray,
93 sizeof(suffixArray));
96 //-------------------------------------------------------------------------
97 /*! Tries to fill the image object with the data read from
98 the given fileName. Returns true on success.
101 bool MNGImageFileType::read( Image *,
102 std::istream &,
103 const std::string &)
106 #ifdef MNG_LIB
110 png_structp png_ptr;
111 png_infop info_ptr;
112 png_uint_32 width, wc, height, h, i;
113 png_byte bit_depth, channels, color_type;
114 png_bytep *row_pointers, base;
115 FILE *fd;
116 bool retCode;
118 if ((fd = fopen(fileName, "rb")) == 0) {
119 cerr << "Could not open file " << fileName << std::endl;
120 return false;
123 png_ptr = png_create_read_struct(MNG_LIBMNG_VER_STRING, 0, 0, 0);
124 if (!png_ptr) {
125 fclose(fd);
126 return false;
129 info_ptr = png_create_info_struct(png_ptr);
130 if (!info_ptr) {
131 fclose(fd);
132 png_destroy_read_struct(&png_ptr, 0, 0);
133 return false;
136 if (setjmp(png_ptr->jmpbuf)) {
137 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
138 fclose(fd);
139 return false;
142 png_init_io(png_ptr, fd);
144 png_read_info(png_ptr, info_ptr);
146 width = png_get_image_width(png_ptr, info_ptr);
147 height = png_get_image_height(png_ptr, info_ptr);
148 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
149 channels = png_get_channels(png_ptr, info_ptr);
150 color_type = png_get_color_type(png_ptr, info_ptr);
152 if (image.set(width, height, channels)) {
154 // Convert paletted images to RGB
155 if (color_type == MNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
156 png_set_expand(png_ptr);
157 // Convert < 8 bit to 8 bit
158 if (color_type == MNG_COLOR_TYPE_GRAY && bit_depth < 8)
159 png_set_expand(png_ptr);
160 // Add a full alpha channel if there is transparency
161 // information in a tRNS chunk
162 if (png_get_valid(png_ptr, info_ptr, MNG_INFO_tRNS))
163 png_set_expand(png_ptr);
165 // Convert 16 bit to 8 bit
166 if (bit_depth == 16)
167 png_set_strip_16(png_ptr);
169 // Calculate the row pointers
170 row_pointers = new png_bytep[height];
171 wc = width * channels;
172 h = height - 1;
173 base = image.data();
174 for (i = 0; i < height; ++i)
175 row_pointers[i] = base + (h - i) * wc;
177 // Read the image data
178 png_read_image(png_ptr, row_pointers);
180 delete [] row_pointers;
182 retCode = true;
184 else
185 retCode = false;
187 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
189 fclose(fd);
191 return retCode;
195 SWARNING << getMimeType()
196 << " write is not implemented "
197 << endLog;
199 #else
201 SWARNING << getMimeType()
202 << " read is not compiled into the current binary "
203 << std::endl;
205 return false;
207 #endif
211 //-------------------------------------------------------------------------
212 /*! Tries to write the image object to the given fileName.
213 Returns true on success.
216 bool MNGImageFileType::write(const Image *,
217 std::ostream &,
218 const std::string &)
221 #ifdef MNG_LIB
223 SWARNING << getMimeType()
224 << " write is not implemented "
225 << endLog;
227 #else
229 SWARNING << getMimeType()
230 << " write is not compiled into the current binary "
231 << endLog;
233 return false;
235 #endif
239 //-------------------------------------------------------------------------
241 Tries to determine the mime type of the data provided by an input stream
242 by searching for magic bytes. Returns the mime type or an empty string
243 when the function could not determine the mime type.
246 std::string MNGImageFileType::determineMimetypeFromStream(std::istream &is)
248 char filecode[4];
250 is.read(filecode, 4);
251 is.seekg(-4, std::ios::cur);
253 if (strncmp(filecode, "\x8aMNG", 4) == 0)
254 return std::string(getMimeType());
255 if (strncmp(filecode, "\x8aJNG", 4) == 0)
256 return std::string(getMimeType());
258 return std::string();
261 //-------------------------------------------------------------------------
262 /*! Constructor used for the singleton object
265 MNGImageFileType::MNGImageFileType(const Char8 *mimeType,
266 const Char8 *suffixArray[],
267 UInt16 suffixByteCount) :
269 Inherited(mimeType, suffixArray, suffixByteCount)
273 //-------------------------------------------------------------------------
274 /*! Destructor
277 MNGImageFileType::~MNGImageFileType (void )
281 OSG_END_NAMESPACE