root/trunk/orca/gl/png_save.c

Revision 1, 2.3 kB (checked in by krobillard, 3 years ago)

Import orca & thune.

Line 
1//============================================================================
2//
3// Orca PNG Writer
4//
5//============================================================================
6
7
8#include <png.h>
9#include "gx.h"
10
11
12/**
13  Gets image data through the virtual methods and writes a PNG file.
14*/
15int png_save( FILE* fp, OImage* img )
16{
17    png_structp png;
18    png_infop info;
19    png_bytep row_pointer;
20    int color_type;
21    int bpl;
22    int i;
23
24    png = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
25    if( ! png )
26        return 0;
27
28    info = png_create_info_struct( png );
29    if( ! info )
30    {
31        png_destroy_write_struct( &png, 0 );
32        return 0;
33    }
34
35    if( setjmp( png_jmpbuf( png ) ) )
36    {
37error:
38        /* If we get here, we had a problem writing the file */
39        png_destroy_write_struct( &png, &info );
40        return 0;
41    }
42
43    png_init_io( png, fp ); /* Using standard C streams */
44    //png_set_write_fn(png, (void*) user_io_ptr, user_write_fn, user_io_flush);
45
46#if 0
47    // Set up info for write (func calls png_set_IHDR).
48    func( png, info, data );
49    int png_transforms = PNG_TRANSFORM_IDENTITY;
50    png_write_png( png, info, png_transforms, 0 );
51#else
52
53    /*
54      PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE,
55      PNG_COLOR_TYPE_RGB, or PNG_COLOR_TYPE_RGB_ALPHA.
56    */
57    switch( img->format )
58    {
59        case OR_IMG_GRAY:
60            bpl = img->width;
61            color_type = PNG_COLOR_TYPE_GRAY;
62            break;
63
64        case OR_IMG_RGB:
65            bpl = img->width * 3;
66            color_type = PNG_COLOR_TYPE_RGB;
67            break;
68
69        case OR_IMG_RGBA:
70            bpl = img->width * 4;
71            color_type = PNG_COLOR_TYPE_RGB_ALPHA;
72            break;
73
74        default: goto error;
75    }
76
77    png_set_IHDR( png, info, img->width, img->height, 8, color_type,
78                  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
79                  PNG_FILTER_TYPE_DEFAULT );
80
81    png_write_info( png, info );
82
83    row_pointer = (png_byte*) (img + 1);
84    for( i = 0; i < img->height; i++ )
85    {
86        png_write_row( png, row_pointer );
87        //png_write_rows( png, &row_pointer, 1 );
88        row_pointer += bpl;
89    }
90
91    png_write_end( png, info );
92#endif
93   
94
95    png_destroy_write_struct( &png, &info );
96
97    return 1;
98}
99
100
101/*EOF*/
Note: See TracBrowser for help on using the browser.