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 #include <iostream> 00060 00061 #ifndef OSG_DO_DOC 00062 # ifdef OSG_WITH_MNG 00063 # define OSG_MNG_ARG(ARG) ARG 00064 # else 00065 # define OSG_MNG_ARG(ARG) 00066 # endif 00067 #else 00068 # define OSG_MNG_ARG(ARG) ARG 00069 #endif 00070 00071 OSG_USING_NAMESPACE 00072 00073 00089 /***************************** 00090 * Types 00091 *****************************/ 00092 00093 00094 /***************************** 00095 * Classvariables 00096 *****************************/ 00097 00098 // Static Class Varible implementations: 00099 static const Char8 *suffixArray[] = { 00100 "mng" 00101 }; 00102 00103 MNGImageFileType MNGImageFileType::_the ( "video/x-mng", 00104 suffixArray, sizeof(suffixArray) ); 00105 00106 /******************************** 00107 * Class methodes 00108 *******************************/ 00109 00110 00111 //------------------------------------------------------------------------- 00115 MNGImageFileType& MNGImageFileType::the (void) 00116 { 00117 return _the; 00118 } 00119 00120 /******************************* 00121 *public 00122 *******************************/ 00123 00124 //------------------------------------------------------------------------- 00129 /* 00130 bool MNGImageFileType::read(ImagePtr &OSG_MNG_ARG(image), std::istream &OSG_MNG_ARG(is)) 00131 { 00132 00133 #ifdef MNG_LIB 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 #else 00219 00220 SWARNING << getMimeType() 00221 << " read is not compiled into the current binary " 00222 << std::endl; 00223 00224 return false; 00225 00226 #endif 00227 00228 } 00229 */ 00230 00231 //------------------------------------------------------------------------- 00237 std::string MNGImageFileType::determineMimetypeFromStream(std::istream &is) 00238 { 00239 char filecode[4]; 00240 is.read(filecode, 4); 00241 is.seekg(-4, std::ios::cur); 00242 if (strncmp(filecode, "\x8aMNG", 4) == 0) 00243 return std::string(getMimeType()); 00244 if (strncmp(filecode, "\x8aJNG", 4) == 0) 00245 return std::string(getMimeType()); 00246 return std::string(); 00247 } 00248 00249 //------------------------------------------------------------------------- 00253 MNGImageFileType::MNGImageFileType ( const Char8 *mimeType, 00254 const Char8 *suffixArray[], 00255 UInt16 suffixByteCount) 00256 : ImageFileType ( mimeType, suffixArray, suffixByteCount ) 00257 {} 00258 00259 //------------------------------------------------------------------------- 00263 MNGImageFileType::~MNGImageFileType (void) {}
1.5.5