ccdemos/image.c
branchpyrit
changeset 88 f7edb3b90816
parent 87 1081e3dd3f3e
child 89 fcf1487b398b
equal deleted inserted replaced
87:1081e3dd3f3e 88:f7edb3b90816
     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 = (unsigned char *) malloc(width * height * pixelsize);
       
    47 	return(0);
       
    48 }
       
    49 
       
    50 void 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 	unsigned 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_info_struct(png, &pnginfo);
       
    85 		png_destroy_write_struct(&png, &pnginfo);
       
    86 		return (0);
       
    87 	}
       
    88 
       
    89 	/* predat knihovne PNG ukazatel na soubor */
       
    90 	png_init_io(png, f);
       
    91 
       
    92 	/* parametry PNG */
       
    93 	png_set_compression_level(png, Z_BEST_COMPRESSION);
       
    94 	png_set_IHDR(png, pnginfo, img->width, img->height, 8, PNG_COLOR_TYPE_RGB,
       
    95 		PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
       
    96 		PNG_FILTER_TYPE_DEFAULT);
       
    97 	png_set_sRGB(png, pnginfo, PNG_sRGB_INTENT_PERCEPTUAL);
       
    98 	png_set_sRGB_gAMA_and_cHRM(png, pnginfo, PNG_INFO_sRGB);
       
    99 
       
   100 	/* zapsat hlavicku */
       
   101 	png_write_info(png, pnginfo);
       
   102 
       
   103 	/* zapsat data */
       
   104 	data = img->data;
       
   105 	for (y = 0; y < img->height; y++, data += img->width * img->pixel_size)
       
   106 		png_write_row(png, (png_byte *) data);
       
   107 
       
   108 	/* ukoncit soubor a uvolnit pomocne struktury */
       
   109 	png_write_end(png, pnginfo);
       
   110 	png_destroy_info_struct(png, &pnginfo);
       
   111 	png_destroy_write_struct(&png, 0);
       
   112 
       
   113 	fclose(f);
       
   114 
       
   115 	return (1);
       
   116 }