1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //-------------------------------
41 //-------------------------------
46 #include "OSGConfig.h"
56 #include "OSGMNGImageFileType.h"
61 # define OSG_MNG_ARG(ARG) ARG
63 # define OSG_MNG_ARG(ARG)
66 # define OSG_MNG_ARG(ARG) ARG
69 // Static Class Varible implementations:
70 static const OSG::Char8
*suffixArray
[] =
77 /*! \class MNGImageFileType
79 Image File Type to read/write and store/restore Image objects as
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
91 MNGImageFileType
MNGImageFileType::_the("video/x-mng",
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
*,
112 png_uint_32 width, wc, height, h, i;
113 png_byte bit_depth, channels, color_type;
114 png_bytep *row_pointers, base;
118 if ((fd = fopen(fileName, "rb")) == 0) {
119 cerr << "Could not open file " << fileName << std::endl;
123 png_ptr = png_create_read_struct(MNG_LIBMNG_VER_STRING, 0, 0, 0);
129 info_ptr = png_create_info_struct(png_ptr);
132 png_destroy_read_struct(&png_ptr, 0, 0);
136 if (setjmp(png_ptr->jmpbuf)) {
137 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
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
167 png_set_strip_16(png_ptr);
169 // Calculate the row pointers
170 row_pointers = new png_bytep[height];
171 wc = width * channels;
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;
187 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
195 SWARNING
<< getMimeType()
196 << " write is not implemented "
201 SWARNING
<< getMimeType()
202 << " read is not compiled into the current binary "
211 //-------------------------------------------------------------------------
212 /*! Tries to write the image object to the given fileName.
213 Returns true on success.
216 bool MNGImageFileType::write(const Image
*,
223 SWARNING
<< getMimeType()
224 << " write is not implemented "
229 SWARNING
<< getMimeType()
230 << " write is not compiled into the current binary "
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
)
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 //-------------------------------------------------------------------------
277 MNGImageFileType::~MNGImageFileType (void )