"Fossies" - the Fresh Open Source Software Archive

Member "mapgenerator-1.1.2/src/read_png.cpp" (26 Oct 2003, 3330 Bytes) of package /linux/www/old/mapgenerator-1.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "read_png.cpp" see the Fossies "Dox" file reference documentation.

    1 //
    2 // Copyright 2001 Jim Hourihan
    3 //
    4 // This library is free software; you can redistribute it and/or
    5 // modify it under the terms of the GNU Library General Public
    6 // License as published by the Free Software Foundation; either
    7 // version 2 of the License, or (at your option) any later version.
    8 //
    9 // This library is distributed in the hope that it will be useful,
   10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12 // Library General Public License for more details.
   13 //
   14 // You should have received a copy of the GNU Library General Public
   15 // License along with this library; if not, write to the Free Software
   16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   17 // USA.
   18 //
   19 #include <stdio.h>
   20 #include <png.h>
   21 #include "Frame_Buffer.h"
   22 
   23 extern "C" {
   24 #include <png.h>
   25 }
   26 
   27 // g++ read_jpeg.cxx -o read_jpeg -ljpeg -lm
   28 
   29 Frame_Buffer* read_png(const char *filename)
   30 {
   31     FILE *fp;
   32     if ( !(fp = fopen(filename, "rb")) )
   33     {
   34     fprintf(stderr, "can't open %s\n", filename);
   35     return 0;
   36     }
   37 
   38     
   39 //    png_structp png_ptr = png_create_read_struct
   40 //  (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
   41 //   user_error_fn, user_warning_fn);
   42     png_structp png_ptr = png_create_read_struct
   43     (PNG_LIBPNG_VER_STRING, (png_voidp)0, 0, 0);
   44 
   45     if (!png_ptr) return 0;
   46 
   47     png_infop info_ptr = png_create_info_struct(png_ptr);
   48 
   49     if (!info_ptr)
   50     {
   51     png_destroy_read_struct(&png_ptr,
   52                 (png_infopp)NULL, 
   53                 (png_infopp)NULL);
   54     return 0;
   55     }
   56 
   57     png_infop end_info = png_create_info_struct(png_ptr);
   58     if (!end_info)
   59     {
   60     png_destroy_read_struct(&png_ptr, &info_ptr,
   61                 (png_infopp)NULL);
   62     return 0;
   63     }
   64 
   65     //
   66     //  Wicked (ick) longjmp error handler
   67     //
   68 
   69     if (setjmp(png_ptr->jmpbuf))
   70     {
   71     png_destroy_read_struct(&png_ptr, &info_ptr,
   72                 &end_info);
   73     fclose(fp);
   74     return 0;
   75     }
   76     
   77     png_init_io(png_ptr, fp);
   78     png_set_sig_bytes(png_ptr, 0);
   79     png_read_info(png_ptr, info_ptr);
   80 
   81     
   82     int color_type = png_get_color_type(png_ptr, info_ptr);
   83     int bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
   84 
   85     if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) 
   86     png_set_palette_to_rgb(png_ptr);
   87 
   88     if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 
   89     png_set_gray_1_2_4_to_8(png_ptr);
   90     
   91     if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) 
   92     png_set_tRNS_to_alpha(png_ptr);
   93 
   94     png_set_strip_16(png_ptr);
   95 
   96     if (color_type & PNG_COLOR_MASK_ALPHA)
   97     png_set_strip_alpha(png_ptr);
   98 
   99     if (bit_depth < 8)
  100     png_set_packing(png_ptr);
  101 
  102     if (color_type == PNG_COLOR_TYPE_GRAY ||
  103     color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  104     png_set_gray_to_rgb(png_ptr);
  105 
  106     png_read_update_info(png_ptr, info_ptr);
  107 
  108     int width       = png_get_image_width(png_ptr,info_ptr);
  109     int height      = png_get_image_height(png_ptr,info_ptr);
  110     int components  = png_get_channels(png_ptr,info_ptr);
  111     int scanline_bytes = components * width;
  112 
  113     Frame_Buffer *fb = new Frame_Buffer(width,height,components);
  114 
  115     for (int i=0; i<height; i++)
  116     {
  117     png_bytep row_pointers = (png_bytep)(fb->buffer() + scanline_bytes*i);
  118     png_read_row(png_ptr, row_pointers, NULL);
  119     }
  120 
  121     png_read_end(png_ptr, end_info);
  122     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  123        
  124     return fb;
  125 }