| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #include <png.h> |
|---|
| 9 | #include "gx.h" |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | int 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 | { |
|---|
| 37 | error: |
|---|
| 38 | |
|---|
| 39 | png_destroy_write_struct( &png, &info ); |
|---|
| 40 | return 0; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | png_init_io( png, fp ); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | #if 0 |
|---|
| 47 | |
|---|
| 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 | |
|---|
| 55 | |
|---|
| 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 | |
|---|
| 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 | |
|---|