ccdemos/image.c
branchpyrit
changeset 15 a0a3e334744f
child 27 e9bb83c2b8b9
equal deleted inserted replaced
14:fc18ac4833f2 15:a0a3e334744f
       
     1 /* This code is part of RGB Image Library
       
     2    see URL: xxx */
       
     3 
       
     4 /**************************************************************************
       
     5 
       
     6 RGB Image Library (rgbimagelib)
       
     7 File: image.c
       
     8 
       
     9 Copyright (c) 2006 Radek Brich
       
    10 
       
    11 Permission is hereby granted, free of charge, to any person
       
    12 obtaining a copy of this software and associated documentation
       
    13 files (the "Software"), to deal in the Software without
       
    14 restriction, including without limitation the rights to use,
       
    15 copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    16 copies of the Software, and to permit persons to whom the
       
    17 Software is furnished to do so, subject to the following
       
    18 conditions:
       
    19 
       
    20 The above copyright notice and this permission notice shall be
       
    21 included in all copies or substantial portions of the Software.
       
    22 
       
    23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
       
    25 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
       
    26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    27 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
       
    28 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
       
    29 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
       
    30 OTHER DEALINGS IN THE SOFTWARE.
       
    31 
       
    32 **************************************************************************/
       
    33 
       
    34 #include <stdio.h>
       
    35 #include <stdlib.h>
       
    36 #include <png.h>
       
    37 
       
    38 #include "image.h"
       
    39 
       
    40 int new_image(struct image **img, int width, int height, int pixelsize)
       
    41 {
       
    42 	*img = (struct image *) malloc(sizeof(struct image));
       
    43 	(*img)->pixel_size = pixelsize;
       
    44 	(*img)->width = width;
       
    45 	(*img)->height = height;
       
    46 	(*img)->data = (char *) malloc(width * height * pixelsize);
       
    47 	return(0);
       
    48 }
       
    49 
       
    50 int destroy_image(struct image **img)
       
    51 {
       
    52 	free((*img)->data);
       
    53 	free(*img);
       
    54 	*img = NULL;
       
    55 }
       
    56 
       
    57 /* funkce pro ulozeni obrazku do PNG souboru (vyuziva knihovnu libpng) */
       
    58 int save_png(const char *fname, struct image *img)
       
    59 {
       
    60 	int y; /* pomocna promenna pro pruchod radku obrazu */
       
    61 	FILE *f;
       
    62 	png_structp png;   /* PNG data */
       
    63 	png_infop pnginfo; /* PNG info */
       
    64 	char *data;
       
    65 
       
    66 	if ((f = fopen(fname, "wb")) == NULL)
       
    67 		return (0);
       
    68 
       
    69 	png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
       
    70 	if (!png) {
       
    71 		fclose(f);
       
    72 		return (0);
       
    73 	}
       
    74 
       
    75 	pnginfo = png_create_info_struct(png);
       
    76 	if (!pnginfo) {
       
    77 		fclose(f);
       
    78 		png_destroy_write_struct(&png, 0);
       
    79 		return (0);
       
    80 	}
       
    81 
       
    82 	if (setjmp(png_jmpbuf(png))) {
       
    83 		fclose(f);
       
    84 		png_destroy_write_struct(&png, &pnginfo);
       
    85 		return (0);
       
    86 	}
       
    87 
       
    88 	/* predat knihovne PNG ukazatel na soubor */
       
    89 	png_init_io(png, f);
       
    90 
       
    91 	/* parametry PNG */
       
    92 	png_set_compression_level(png, Z_BEST_COMPRESSION);
       
    93 	png_set_IHDR(png, pnginfo, img->width, img->height, 8, PNG_COLOR_TYPE_RGB,
       
    94 		PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
       
    95 		PNG_FILTER_TYPE_DEFAULT);
       
    96 	png_set_sRGB(png, pnginfo, PNG_sRGB_INTENT_PERCEPTUAL);
       
    97 	png_set_sRGB_gAMA_and_cHRM(png, pnginfo, PNG_INFO_sRGB);
       
    98 
       
    99 	/* zapsat hlavicku */
       
   100 	png_write_info(png, pnginfo);
       
   101 
       
   102 	/* zapsat data */
       
   103 	data = img->data;
       
   104 	for (y = 0; y < img->height; y++, data += img->width * img->pixel_size)
       
   105 		png_write_row(png, (png_byte *) data);
       
   106 
       
   107 	/* ukoncit soubor a uvolnit pomocne struktury */
       
   108 	png_write_end(png, pnginfo);
       
   109 	png_destroy_write_struct(&png, 0);
       
   110 
       
   111 	fclose(f);
       
   112 
       
   113 	return (1);
       
   114 }