Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.     IMGLIB:  An example image loading library for use with SDL
  3.     Copyright (C) 1999  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     5635-34 Springhouse Dr.
  21.     Pleasanton, CA 94588 (USA)
  22.     slouken@devolution.com
  23. */
  24.  
  25. /* This is a PNG image file loading framework */
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29.  
  30. #include "SDL_image.h"
  31.  
  32. #ifdef LOAD_PNG
  33.  
  34. /*=============================================================================
  35.         File: SDL_png.c
  36.      Purpose: A PNG loader and saver for the SDL library      
  37.     Revision:
  38.   Created by: Philippe Lavoie          (2 November 1998)
  39.               lavoie@zeus.genie.uottawa.ca
  40.  Modified by:
  41.  
  42.  Copyright notice:
  43.           Copyright (C) 1998 Philippe Lavoie
  44.  
  45.           This library is free software; you can redistribute it and/or
  46.           modify it under the terms of the GNU Library General Public
  47.           License as published by the Free Software Foundation; either
  48.           version 2 of the License, or (at your option) any later version.
  49.  
  50.           This library is distributed in the hope that it will be useful,
  51.           but WITHOUT ANY WARRANTY; without even the implied warranty of
  52.           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  53.           Library General Public License for more details.
  54.  
  55.           You should have received a copy of the GNU Library General Public
  56.           License along with this library; if not, write to the Free
  57.           Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  58.  
  59.     Comments: The load and save routine are basically the ones you can find
  60.              in the example.c file from the libpng distribution.
  61.  
  62.   Changes:
  63.     5/17/99 - Modified to use the new SDL data sources - Sam Lantinga
  64.  
  65. =============================================================================*/
  66.  
  67. #include "SDL_endian.h"
  68.  
  69. #ifdef macintosh
  70. #define MACOS
  71. #endif
  72. #include <png.h>
  73.  
  74. #define PNG_BYTES_TO_CHECK 4
  75.  
  76. /* See if an image is contained in a data source */
  77. int IMG_isPNG(SDL_RWops *src)
  78. {
  79.    unsigned char buf[PNG_BYTES_TO_CHECK];
  80.  
  81.    /* Read in the signature bytes */
  82.    if (SDL_RWread(src, buf, 1, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK)
  83.       return 0;
  84.  
  85.    /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. */
  86.    return( !png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
  87. }
  88.  
  89. /* Load a PNG type image from an SDL datasource */
  90. static void png_read_data(png_structp ctx, png_bytep area, png_size_t size)
  91. {
  92.         SDL_RWops *src;
  93.  
  94.         src = (SDL_RWops *)png_get_io_ptr(ctx);
  95.         SDL_RWread(src, area, size, 1);
  96. }
  97. SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
  98. {
  99.         SDL_Surface *volatile surface;
  100.         png_structp png_ptr;
  101.         png_infop info_ptr;
  102.         png_uint_32 width, height;
  103.         int bit_depth, color_type, interlace_type;
  104.         Uint32 Rmask;
  105.         Uint32 Gmask;
  106.         Uint32 Bmask;
  107.         Uint32 Amask;
  108.         SDL_Palette *palette;
  109.         png_bytep *volatile row_pointers;
  110.         int row, i;
  111.         volatile int ckey = -1;
  112.         png_color_16 *transv;
  113.  
  114.         /* Initialize the data we will clean up when we're done */
  115.         png_ptr = NULL; info_ptr = NULL; row_pointers = NULL; surface = NULL;
  116.  
  117.         /* Check to make sure we have something to do */
  118.         if ( ! src ) {
  119.                 goto done;
  120.         }
  121.  
  122.         /* Create the PNG loading context structure */
  123.         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  124.                                           NULL,NULL,NULL);
  125.         if (png_ptr == NULL){
  126.                 IMG_SetError("Couldn't allocate memory for PNG file");
  127.                 goto done;
  128.         }
  129.  
  130.          /* Allocate/initialize the memory for image information.  REQUIRED. */
  131.         info_ptr = png_create_info_struct(png_ptr);
  132.         if (info_ptr == NULL) {
  133.                 IMG_SetError("Couldn't create image information for PNG file");
  134.                 goto done;
  135.         }
  136.  
  137.         /* Set error handling if you are using setjmp/longjmp method (this is
  138.          * the normal method of doing things with libpng).  REQUIRED unless you
  139.          * set up your own error handlers in png_create_read_struct() earlier.
  140.          */
  141.         if ( setjmp(png_ptr->jmpbuf) ) {
  142.                 IMG_SetError("Error reading the PNG file.");
  143.                 goto done;
  144.         }
  145.  
  146.         /* Set up the input control */
  147.         png_set_read_fn(png_ptr, src, png_read_data);
  148.  
  149.         /* Read PNG header info */
  150.         png_read_info(png_ptr, info_ptr);
  151.         png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
  152.                         &color_type, &interlace_type, NULL, NULL);
  153.  
  154.         /* tell libpng to strip 16 bit/color files down to 8 bits/color */
  155.         png_set_strip_16(png_ptr) ;
  156.  
  157.         /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
  158.          * byte into separate bytes (useful for paletted and grayscale images).
  159.          */
  160.         png_set_packing(png_ptr);
  161.  
  162.         /* scale greyscale values to the range 0..255 */
  163.         if(color_type == PNG_COLOR_TYPE_GRAY)
  164.                 png_set_expand(png_ptr);
  165.  
  166.         /* For images with a single "transparent colour", set colour key;
  167.            if more than one index has transparency, use full alpha channel */
  168.         if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
  169.                 int num_trans;
  170.                 Uint8 *trans;
  171.                 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
  172.                              &transv);
  173.                 if(color_type == PNG_COLOR_TYPE_PALETTE) {
  174.                     if(num_trans == 1) {
  175.                         /* exactly one transparent value: set colour key */
  176.                         ckey = trans[0];
  177.                     } else
  178.                         png_set_expand(png_ptr);
  179.                 } else
  180.                     ckey = 0; /* actual value will be set later */
  181.         }
  182.  
  183.         if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
  184.                 png_set_gray_to_rgb(png_ptr);
  185.  
  186.         png_read_update_info(png_ptr, info_ptr);
  187.  
  188.         png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
  189.                         &color_type, &interlace_type, NULL, NULL);
  190.  
  191.         /* Allocate the SDL surface to hold the image */
  192.         Rmask = Gmask = Bmask = Amask = 0 ;
  193.         if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
  194.                 if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
  195.                         Rmask = 0x000000FF;
  196.                         Gmask = 0x0000FF00;
  197.                         Bmask = 0x00FF0000;
  198.                         Amask = (info_ptr->channels == 4) ? 0xFF000000 : 0;
  199.                 } else {
  200.                         int s = (info_ptr->channels == 4) ? 0 : 8;
  201.                         Rmask = 0xFF000000 >> s;
  202.                         Gmask = 0x00FF0000 >> s;
  203.                         Bmask = 0x0000FF00 >> s;
  204.                         Amask = 0x000000FF >> s;
  205.                 }
  206.         }
  207.         surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
  208.                         bit_depth*info_ptr->channels, Rmask,Gmask,Bmask,Amask);
  209.         if ( surface == NULL ) {
  210.                 IMG_SetError("Out of memory");
  211.                 goto done;
  212.         }
  213.  
  214.         if(ckey != -1) {
  215.                 if(color_type != PNG_COLOR_TYPE_PALETTE)
  216.                         /* FIXME: Should these be truncated or shifted down? */
  217.                         ckey = SDL_MapRGB(surface->format,
  218.                                          (Uint8)transv->red,
  219.                                          (Uint8)transv->green,
  220.                                          (Uint8)transv->blue);
  221.                 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, ckey);
  222.         }
  223.  
  224.         /* Create the array of pointers to image data */
  225.         row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*height);
  226.         if ( (row_pointers == NULL) ) {
  227.                 IMG_SetError("Out of memory");
  228.                 SDL_FreeSurface(surface);
  229.                 surface = NULL;
  230.                 goto done;
  231.         }
  232.         for (row = 0; row < (int)height; row++) {
  233.                 row_pointers[row] = (png_bytep)
  234.                                 (Uint8 *)surface->pixels + row*surface->pitch;
  235.         }
  236.  
  237.         /* Read the entire image in one go */
  238.         png_read_image(png_ptr, row_pointers);
  239.  
  240.         /* read rest of file, get additional chunks in info_ptr - REQUIRED */
  241.         png_read_end(png_ptr, info_ptr);
  242.  
  243.         /* Load the palette, if any */
  244.         palette = surface->format->palette;
  245.         if ( palette ) {
  246.             if(color_type == PNG_COLOR_TYPE_GRAY) {
  247.                 palette->ncolors = 256;
  248.                 for(i = 0; i < 256; i++) {
  249.                     palette->colors[i].r = i;
  250.                     palette->colors[i].g = i;
  251.                     palette->colors[i].b = i;
  252.                 }
  253.             } else if (info_ptr->num_palette > 0 ) {
  254.                 palette->ncolors = info_ptr->num_palette;
  255.                 for( i=0; i<info_ptr->num_palette; ++i ) {
  256.                     palette->colors[i].b = info_ptr->palette[i].blue;
  257.                     palette->colors[i].g = info_ptr->palette[i].green;
  258.                     palette->colors[i].r = info_ptr->palette[i].red;
  259.                 }
  260.             }
  261.         }
  262.  
  263. done:   /* Clean up and return */
  264.         png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : (png_infopp)0,
  265.                                                                 (png_infopp)0);
  266.         if ( row_pointers ) {
  267.                 free(row_pointers);
  268.         }
  269.         return(surface);
  270. }
  271.  
  272. #else
  273.  
  274. /* See if an image is contained in a data source */
  275. int IMG_isPNG(SDL_RWops *src)
  276. {
  277.         return(0);
  278. }
  279.  
  280. /* Load a PNG type image from an SDL datasource */
  281. SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
  282. {
  283.         return(NULL);
  284. }
  285.  
  286. #endif /* LOAD_PNG */
  287.