00001 /*---------------------------------------------------------------------------*\ 00002 * OpenSG * 00003 * * 00004 * * 00005 * Copyright (C) 2000-2002 by the OpenSG Forum * 00006 * * 00007 * www.opensg.org * 00008 * * 00009 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de * 00010 * * 00011 \*---------------------------------------------------------------------------*/ 00012 /*---------------------------------------------------------------------------*\ 00013 * License * 00014 * * 00015 * This library is free software; you can redistribute it and/or modify it * 00016 * under the terms of the GNU Library General Public License as published * 00017 * by the Free Software Foundation, version 2. * 00018 * * 00019 * This library is distributed in the hope that it will be useful, but * 00020 * WITHOUT ANY WARRANTY; without even the implied warranty of * 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00022 * Library General Public License for more details. * 00023 * * 00024 * You should have received a copy of the GNU Library General Public * 00025 * License along with this library; if not, write to the Free Software * 00026 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * 00027 * * 00028 \*---------------------------------------------------------------------------*/ 00029 /*---------------------------------------------------------------------------*\ 00030 * Changes * 00031 * * 00032 * * 00033 * * 00034 * * 00035 * * 00036 * * 00037 \*---------------------------------------------------------------------------*/ 00038 00039 //------------------------------- 00040 // Includes 00041 //------------------------------- 00042 00043 #include <stdlib.h> 00044 #include <stdio.h> 00045 00046 #include "OSGConfig.h" 00047 00048 #ifdef OSG_SGI_LIB 00049 #include <limits> 00050 #endif 00051 00052 #ifdef MNG_LIB 00053 #include <mng.h> 00054 #endif 00055 00056 #include "OSGMNGImageFileType.h" 00057 #include <OSGLog.h> 00058 00059 #ifndef OSG_DO_DOC 00060 # ifdef OSG_WITH_MNG 00061 # define OSG_MNG_ARG(ARG) ARG 00062 # else 00063 # define OSG_MNG_ARG(ARG) 00064 # endif 00065 #else 00066 # define OSG_MNG_ARG(ARG) ARG 00067 #endif 00068 00069 OSG_USING_NAMESPACE 00070 00071 00087 /***************************** 00088 * Types 00089 *****************************/ 00090 00091 00092 /***************************** 00093 * Classvariables 00094 *****************************/ 00095 00096 // Static Class Varible implementations: 00097 static const Char8 *suffixArray[] = { 00098 "mng" 00099 }; 00100 00101 MNGImageFileType MNGImageFileType::_the ( "mng", 00102 suffixArray, sizeof(suffixArray) ); 00103 00104 /******************************** 00105 * Class methodes 00106 *******************************/ 00107 00108 00109 //------------------------------------------------------------------------- 00113 MNGImageFileType& MNGImageFileType::the (void) 00114 { 00115 return _the; 00116 } 00117 00118 /******************************* 00119 *public 00120 *******************************/ 00121 00122 //------------------------------------------------------------------------- 00127 bool MNGImageFileType::read( ImagePtr &OSG_MNG_ARG(image ), 00128 const Char8 *OSG_MNG_ARG(fileName)) 00129 { 00130 00131 #ifdef MNG_LIB 00132 00133 /* 00134 00135 png_structp png_ptr; 00136 png_infop info_ptr; 00137 png_uint_32 width, wc, height, h, i; 00138 png_byte bit_depth, channels, color_type; 00139 png_bytep *row_pointers, base; 00140 FILE *fd; 00141 bool retCode; 00142 00143 if ((fd = fopen(fileName, "rb")) == 0) { 00144 cerr << "Could not open file " << fileName << std::endl; 00145 return false; 00146 } 00147 00148 png_ptr = png_create_read_struct(MNG_LIBMNG_VER_STRING, 0, 0, 0); 00149 if (!png_ptr) { 00150 fclose(fd); 00151 return false; 00152 } 00153 00154 info_ptr = png_create_info_struct(png_ptr); 00155 if (!info_ptr) { 00156 fclose(fd); 00157 png_destroy_read_struct(&png_ptr, 0, 0); 00158 return false; 00159 } 00160 00161 if (setjmp(png_ptr->jmpbuf)) { 00162 png_destroy_read_struct(&png_ptr, &info_ptr, 0); 00163 fclose(fd); 00164 return false; 00165 } 00166 00167 png_init_io(png_ptr, fd); 00168 00169 png_read_info(png_ptr, info_ptr); 00170 00171 width = png_get_image_width(png_ptr, info_ptr); 00172 height = png_get_image_height(png_ptr, info_ptr); 00173 bit_depth = png_get_bit_depth(png_ptr, info_ptr); 00174 channels = png_get_channels(png_ptr, info_ptr); 00175 color_type = png_get_color_type(png_ptr, info_ptr); 00176 00177 if (image.set(width, height, channels)) { 00178 00179 // Convert paletted images to RGB 00180 if (color_type == MNG_COLOR_TYPE_PALETTE && bit_depth <= 8) 00181 png_set_expand(png_ptr); 00182 // Convert < 8 bit to 8 bit 00183 if (color_type == MNG_COLOR_TYPE_GRAY && bit_depth < 8) 00184 png_set_expand(png_ptr); 00185 // Add a full alpha channel if there is transparency 00186 // information in a tRNS chunk 00187 if (png_get_valid(png_ptr, info_ptr, MNG_INFO_tRNS)) 00188 png_set_expand(png_ptr); 00189 00190 // Convert 16 bit to 8 bit 00191 if (bit_depth == 16) 00192 png_set_strip_16(png_ptr); 00193 00194 // Calculate the row pointers 00195 row_pointers = new png_bytep[height]; 00196 wc = width * channels; 00197 h = height - 1; 00198 base = image.data(); 00199 for (i = 0; i < height; ++i) 00200 row_pointers[i] = base + (h - i) * wc; 00201 00202 // Read the image data 00203 png_read_image(png_ptr, row_pointers); 00204 00205 delete [] row_pointers; 00206 00207 retCode = true; 00208 } 00209 else 00210 retCode = false; 00211 00212 png_destroy_read_struct(&png_ptr, &info_ptr, 0); 00213 00214 fclose(fd); 00215 00216 return retCode; 00217 00218 */ 00219 00220 #else 00221 00222 SWARNING << getMimeType() 00223 << " read is not compiled into the current binary " 00224 << std::endl; 00225 00226 return false; 00227 00228 #endif 00229 00230 } 00231 00232 //------------------------------------------------------------------------- 00237 bool MNGImageFileType::write(const ImagePtr &OSG_MNG_ARG(image ), 00238 const Char8 *OSG_MNG_ARG(fileName)) 00239 { 00240 00241 #ifdef MNG_LIB 00242 00243 SWARNING << getMimeType() 00244 << " write is not implemented " 00245 << endLog; 00246 00247 #else 00248 00249 SWARNING << getMimeType() 00250 << " write is not compiled into the current binary " 00251 << endLog; 00252 00253 return false; 00254 00255 #endif 00256 00257 } 00258 00259 //------------------------------------------------------------------------- 00263 MNGImageFileType::MNGImageFileType ( const Char8 *mimeType, 00264 const Char8 *suffixArray[], 00265 UInt16 suffixByteCount) 00266 : ImageFileType ( mimeType, suffixArray, suffixByteCount ) 00267 { 00268 return; 00269 } 00270 00271 //------------------------------------------------------------------------- 00275 MNGImageFileType::MNGImageFileType (const MNGImageFileType &obj ) 00276 : ImageFileType(obj) 00277 { 00278 return; 00279 } 00280 00281 00282 //------------------------------------------------------------------------- 00286 MNGImageFileType::~MNGImageFileType (void ) 00287 { 00288 return; 00289 }
1.4.3