Home | History | Annotate | Download | only in src
      1 // Copyright 2002-2010 Guillaume Cottenceau.
      2 // This software may be freely redistributed under the terms of
      3 // the X11 license.
      4 //
      5 // Function write_png_file taken slightly modified from
      6 // http://zarb.org/~gc/html/libpng.html
      7 
      8 #include <png.h>
      9 #include <stdarg.h>
     10 #include <stdio.h>
     11 
     12 #include <base/files/file_util.h>
     13 #include <gflags/gflags.h>
     14 
     15 #include "png_helper.h"
     16 
     17 void abort_(const char * s, ...) {
     18   va_list args;
     19   va_start(args, s);
     20   vfprintf(stderr, s, args);
     21   fprintf(stderr, "\n");
     22   va_end(args);
     23   abort();
     24 }
     25 
     26 void write_png_file(const char* file_name, char* pixels, int width, int height)
     27 {
     28   int         x, y;
     29   png_bytep  *row_pointers;
     30   png_structp png_ptr;
     31   png_infop   info_ptr;
     32   png_byte    bit_depth = 8;  // 8 bits per channel RGBA
     33   png_byte    color_type = 6; // RGBA
     34 
     35   row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
     36   char *p = pixels;
     37   for (y=height-1; y>=0; y--) {
     38     row_pointers[y] = (png_byte*) malloc(4*width);
     39     for (x=0; x<width; x++) {
     40       png_byte* pixel = &(row_pointers[y][x*4]);
     41       pixel[0] = *p; p++; // R
     42       pixel[1] = *p; p++; // G
     43       pixel[2] = *p; p++; // B
     44       pixel[3] = *p; p++; // A
     45     }
     46   }
     47 
     48   /* create file */
     49   FILE *fp = fopen(file_name, "wb");
     50   if (!fp)
     51     abort_("[write_png_file] File %s could not be opened for writing",
     52            file_name);
     53   /* initialize stuff */
     54   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
     55   if (!png_ptr)
     56     abort_("[write_png_file] png_create_write_struct failed");
     57   info_ptr = png_create_info_struct(png_ptr);
     58   if (!info_ptr)
     59     abort_("[write_png_file] png_create_info_struct failed");
     60   if (setjmp(png_jmpbuf(png_ptr)))
     61     abort_("[write_png_file] Error during init_io");
     62   png_init_io(png_ptr, fp);
     63 
     64   /* write header */
     65   if (setjmp(png_jmpbuf(png_ptr)))
     66     abort_("[write_png_file] Error during writing header");
     67   png_set_IHDR(png_ptr, info_ptr, width, height,
     68                bit_depth, color_type, PNG_INTERLACE_NONE,
     69                PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
     70   png_write_info(png_ptr, info_ptr);
     71 
     72   /* write bytes */
     73   if (setjmp(png_jmpbuf(png_ptr)))
     74     abort_("[write_png_file] Error during writing bytes");
     75   png_write_image(png_ptr, row_pointers);
     76 
     77   /* end write */
     78   if (setjmp(png_jmpbuf(png_ptr)))
     79     abort_("[write_png_file] Error during end of write");
     80   png_write_end(png_ptr, NULL);
     81 
     82   /* cleanup heap allocation */
     83   for (y=0; y<height; y++)
     84     free(row_pointers[y]);
     85   free(row_pointers);
     86 
     87   fclose(fp);
     88 
     89   // Try to flush saved image to disk such that more data survives a hard crash.
     90   system("/bin/sync");
     91 }
     92