Modelib: Fichier source de Png.cpp

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# Png.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00030 */
    00031
    00032
    00033 #include "Png.h"
    00034 #include <png.h>
    00035 #include <iostream>
    00036
    00037 namespace Modelib {
    00038
    00039 using namespace std;
    00040
    00041 PNGImage::~PNGImage()
    00042 {
    00043   cerr << "PNG - destructor" << endl;
    00044   if (fp!=NULL)
    00045      fclose(fp);
    00046   if (image)
    00047   {
    00048     for (unsigned i=0;i<height;i++)
    00049           delete [] image[i];
    00050     delete image;
    00051   }
    00052   delete [] row_pointers;
    00053 }
    00054
    00055
    00056 bool PNGImage::openFileRead() {
    00057   if (fp!=NULL) {
    00058      cerr << "file already opened\n" << endl;
    00059     return false;   // can't open an already opened file
    00060   }
    00061
    00062   fp = fopen(filename.c_str(), "rb");
    00063   if (!fp) {
    00064     cerr << "couldn't open file " << filename << "for reading" << endl;;
    00065     return false;           // couldn't open file
    00066   }
    00067
    00068   cerr << "opened file " <<  filename << endl;
    00069   return true;
    00070 }
    00071
    00072 bool PNGImage::openFileWrite() {
    00073   if (fp!=NULL) {
    00074     cerr << "file already opened" << endl;
    00075     return false;   // can't open an already opened file
    00076   }
    00077
    00078   fp = fopen(filename.c_str(), "wb");
    00079   if (!fp) {
    00080     return false;           // couldn't open file
    00081   }
    00082   return true;
    00083 }
    00084
    00085 bool PNGImage::isPNG(int bytesToCheck = 8) {
    00086   if (fp==NULL) {
    00087     if (openFileRead() == false) return false;
    00088   }
    00089
    00090   if (bytesToCheck > 8) {
    00091     bytesToCheck=8;
    00092     cerr << "bytesToCheck set to 8.  It cannot be more than 8." << endl;
    00093   }
    00094   else if (bytesToCheck < 1) {
    00095     bytesToCheck=1;
    00096     cerr << "bytesToCheck set to 1.  It cannot be less than 1." << endl;
    00097   }
    00098
    00099   // todo: check that fp is at start of file.
    00100
    00101   char header[8];   // 8 is than maximum size that can be checked.
    00102
    00103   int retValue = fread(header, 1, bytesToCheck, fp);
    00104   if (retValue != bytesToCheck) {
    00105     cerr << "couldn't read " << bytesToCheck << "bytes from file" << endl;
    00106   }
    00107
    00108   // use library to check if it's a valid PNG file
    00109   bool valid;
    00110   valid = ( png_sig_cmp((png_byte*)header, 0, bytesToCheck) == 0);
    00111
    00112   return valid;
    00113 }
    00114
    00115
    00116 bool PNGImage::initReadStructs() {
    00117   //  printf("initReadStructs: starting..." << endl;
    00118
    00119   // initialise png_ptr
    00120   //  printf("initReadStructs: initialising png_ptr..." << endl;
    00121   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,    // from png.h
    00122                    NULL, // ptr to struct for user error funct.
    00123                    NULL, // ptr to user error function
    00124                    NULL);// ptr to usr warning function
    00125
    00126   if (png_ptr == NULL) {
    00127     cerr << "initReadStructs: png_ptr was not able to be initialised" << endl;
    00128     return false;
    00129   }
    00130
    00131
    00132   // initialise info_ptr
    00133   //  printf("initReadStructs: initialising info_ptr..." << endl;
    00134   info_ptr = png_create_info_struct(png_ptr);
    00135   if (info_ptr == NULL) {
    00136     cerr << "initReadStructs: info_ptr was not able to be initialised" << endl;
    00137     png_destroy_read_struct(&png_ptr,
    00138                 (png_infopp)NULL,   // ?
    00139                 (png_infopp)NULL);  // ?
    00140     return false;
    00141   }
    00142
    00143   // note: there is no end_info variable because I don't know what
    00144   // it's for yet.  But if we need one then it's memory allocation
    00145   // should happen here (once a variable has been declared in the
    00146   // class).
    00147
    00148   //  printf("initReadStructs: finished" << endl;
    00149   return true;
    00150 }
    00151
    00152
    00153 bool PNGImage::initWriteStructs() {
    00154   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
    00155                     NULL, // ptr to user error struct
    00156                     NULL, // ptr to user error function
    00157                     NULL);// ptr to user warning function
    00158
    00159   if (!png_ptr) {
    00160     cerr << "initWriteStructs: failed to init png_ptr" << endl;
    00161     return false;
    00162   }
    00163
    00164   info_ptr = png_create_info_struct(png_ptr);
    00165   if (!info_ptr) {
    00166     cerr << "initWriteStructs: failed to init info_ptr" << endl;
    00167     png_destroy_write_struct(&png_ptr,
    00168                  (png_infopp)NULL);
    00169     return false;
    00170   }
    00171
    00172   // prepare for errors.  Can't call setjmp() before png_ptr has been
    00173   // allocated because the data inside png_ptr is access by png_jmpbuf!
    00174   if (setjmp(png_jmpbuf(png_ptr))) {  // png_jmpbuf is a macro in pngconf.h
    00175     cerr << "initReadStructs: setjmp returned non-zero (i.e. an error occured.)" << endl;
    00176     png_destroy_read_struct(&png_ptr, &info_ptr, NULL /*end_info*/);
    00177     return false;
    00178   }
    00179   png_init_io(png_ptr, fp);
    00180   cerr << "initWriteStructs: complete" << endl;
    00181   return true;
    00182 }
    00183
    00184
    00185 bool PNGImage::writeHeader() {
    00186   cerr << "writeHeader: starting..." << endl;
    00187   // prepare for errors.
    00188   if (setjmp(png_jmpbuf(png_ptr))) {  // png_jmpbuf is a macro in pngconf.h
    00189     cerr << "writeHeader: setjmp returned non-zero (i.e. an error occured.)" << endl;
    00190     png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
    00191     return false;
    00192   }
    00193
    00194   cerr << "writeHeader: setting header" << endl;
    00195   png_set_IHDR(png_ptr, info_ptr, width, height,
    00196            8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
    00197            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
    00198
    00199   cerr << "writeHeader: writing header" << endl;
    00200   png_write_info(png_ptr, info_ptr);
    00201   cerr << "writeHeader: finished." << endl;
    00202
    00203   return true;
    00204 }
    00205
    00206
    00207 png_byte** PNGImage::writeImage_Start() {
    00208   cerr << "writeImage: starting..." << endl;
    00209   cerr << "image is " << height << " by " << width << endl;
    00210   // prepare for errors.
    00211   if (setjmp(png_jmpbuf(png_ptr))) {  // png_jmpbuf is a macro in pngconf.h
    00212     cerr << "writeImage: setjmp returned non-zero (i.e. an error occured.)" << endl;
    00213     png_destroy_read_struct(&png_ptr, &info_ptr, NULL /*end_info*/);
    00214     return false;
    00215   }
    00216
    00217   for (unsigned k = 0; k < height; k++) {
    00218     row_pointers[k] = image[k];
    00219   }
    00220
    00221   return image;
    00222 }
    00223
    00224
    00225 bool PNGImage::writeImage_End() {
    00226   png_write_image(png_ptr, row_pointers);
    00227   cerr << "writeImage: finished." << endl;
    00228   return true;
    00229 }
    00230
    00231 bool PNGImage::writeEnd() {
    00232   cerr << "writeEnd: starting..." << endl;
    00233
    00234   // prepare for errors.
    00235   if (setjmp(png_jmpbuf(png_ptr))) {  // png_jmpbuf is a macro in pngconf.h
    00236     cerr << "writeEnd: setjmp returned non-zero (i.e. an error occured.)" << endl;
    00237     png_destroy_read_struct(&png_ptr, &info_ptr, NULL /*end_info*/);
    00238     return false;
    00239   }
    00240
    00241   png_write_end(png_ptr, NULL);  // NULL because we don't need to
    00242                  // write any comments, etc.
    00243
    00244   cerr << "writeEnd: finished" << endl;
    00245   return true;
    00246 }
    00247
    00248 }
    00249