| 1 |
/* |
| 2 |
PNGWRAPPER! |
| 3 |
|
| 4 |
This file is included in QuteMol project |
| 5 |
as temporary hack waiting for WxWidgets to support |
| 6 |
alpha channel in PNG writing. |
| 7 |
|
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <math.h> |
| 13 |
#include <assert.h> |
| 14 |
#include <vector> |
| 15 |
#include <iostream> |
| 16 |
using namespace std; |
| 17 |
|
| 18 |
#include <png.h> |
| 19 |
|
| 20 |
#include "progress.h" |
| 21 |
|
| 22 |
typedef Byte byte; |
| 23 |
|
| 24 |
|
| 25 |
void downsample2x2(byte * data, int sx, int sy){ |
| 26 |
int j=0; |
| 27 |
for (int y=0; y<sy/2; y++) |
| 28 |
for (int x=0; x<sx/2; x++) |
| 29 |
for (int c=0; c <4; c++) { |
| 30 |
data[j++]=( |
| 31 |
int(data[((y*2+0)*sx+(x*2+0))*4+c])+ |
| 32 |
int(data[((y*2+0)*sx+(x*2+1))*4+c])+ |
| 33 |
int(data[((y*2+1)*sx+(x*2+0))*4+c])+ |
| 34 |
int(data[((y*2+1)*sx+(x*2+1))*4+c])+ |
| 35 |
2 |
| 36 |
)/4; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
void downsample2x2NoAlpha(byte * data, int sx, int sy){ |
| 41 |
int j=0; |
| 42 |
for (int y=0; y<sy/2; y++) |
| 43 |
for (int x=0; x<sx/2; x++) |
| 44 |
for (int c=0; c <3; c++) { |
| 45 |
data[j++]=( |
| 46 |
int(data[((y*2+0)*sx+(x*2+0))*3+c])+ |
| 47 |
int(data[((y*2+0)*sx+(x*2+1))*3+c])+ |
| 48 |
int(data[((y*2+1)*sx+(x*2+0))*3+c])+ |
| 49 |
int(data[((y*2+1)*sx+(x*2+1))*3+c])+ |
| 50 |
2 |
| 51 |
)/4; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void trace(const char *st, const char *st2=NULL){ |
| 56 |
static FILE *f=NULL; |
| 57 |
if (!f) f=fopen("trace.txt","wt"); |
| 58 |
fprintf(f,st); |
| 59 |
if (st2) fprintf(f,st2); |
| 60 |
fflush(f); |
| 61 |
} |
| 62 |
|
| 63 |
void donttrace(char*x){}; |
| 64 |
void donttrace( const char*x, const char*y){}; |
| 65 |
#define TRACE donttrace |
| 66 |
//#define TRACE trace |
| 67 |
void handle_error(struct png_struct_def *png,const char *error) { |
| 68 |
TRACE("Error:\n", error); |
| 69 |
} |
| 70 |
|
| 71 |
bool PNGSaveWithAlpha( const char * filename, const byte * const data, int sx, int sy, int reverse ){ |
| 72 |
int type=4; int depth=8; |
| 73 |
assert(filename); |
| 74 |
assert(data); |
| 75 |
TRACE("Start\n"); |
| 76 |
FILE *fp; |
| 77 |
fp=fopen(filename, "wb+"); |
| 78 |
if(!fp) |
| 79 |
return false; |
| 80 |
TRACE("File open (wb+)\n"); |
| 81 |
|
| 82 |
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
| 83 |
//NULL, NULL, NULL); |
| 84 |
NULL, //&handle_error, |
| 85 |
&handle_error, |
| 86 |
&handle_error); |
| 87 |
TRACE("png_create_write\n"); |
| 88 |
if (png_ptr == NULL) { |
| 89 |
fclose(fp); |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
// Allocate/initialize the image information data. REQUIRED |
| 94 |
png_infop png_info_ptr = png_create_info_struct(png_ptr); |
| 95 |
TRACE("png_create_info\n"); |
| 96 |
if (png_info_ptr == NULL) { |
| 97 |
fclose(fp); |
| 98 |
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
| 99 |
TRACE("png_destroy_write\n"); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
png_init_io(png_ptr, fp); |
| 104 |
TRACE("png_init_io\n"); |
| 105 |
//type: 1 -> PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16) |
| 106 |
// 2 -> PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16) |
| 107 |
// 3 -> PNG_COLOR_TYPE_RGB (bit_depths 8, 16) |
| 108 |
// 4 -> PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16) |
| 109 |
int pixel_type = 1; |
| 110 |
switch(type) { |
| 111 |
case 1: pixel_type = PNG_COLOR_TYPE_GRAY; break; |
| 112 |
case 2: pixel_type = PNG_COLOR_TYPE_GRAY_ALPHA; break; |
| 113 |
case 3: pixel_type = PNG_COLOR_TYPE_RGB; break; |
| 114 |
case 4: pixel_type = PNG_COLOR_TYPE_RGB_ALPHA; break; |
| 115 |
} |
| 116 |
//TRACE("sx %d, sy %d, depth %d, pixel_type %d\n", sx, sy, depth, pixel_type); |
| 117 |
png_set_IHDR(png_ptr, png_info_ptr, sx, sy, depth, pixel_type, |
| 118 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 119 |
//TRACE("png_set_IHDR\n"); |
| 120 |
png_write_info(png_ptr, png_info_ptr); |
| 121 |
//TRACE("png_write_info\n"); |
| 122 |
//TRACE("uffa");*/ |
| 123 |
char **rows = new char *[sy]; |
| 124 |
for(int i = 0; i < sy; i++) |
| 125 |
rows[i] = (char *)data + i * type * (depth/8) * sx; |
| 126 |
if(reverse) { |
| 127 |
if (sy>200) StartProgress("Saving PNG", sy); |
| 128 |
for(int r = sy -1; r >= 0; r--) { |
| 129 |
if (r%100) { if (!UpdateProgress(sy-r)) { fclose(fp); return false; } } |
| 130 |
png_write_rows(png_ptr, (png_byte **)(&(rows[r])), 1); |
| 131 |
} |
| 132 |
EndProgress(); |
| 133 |
} else { |
| 134 |
png_write_rows(png_ptr, (png_byte **)(rows), sy); |
| 135 |
} |
| 136 |
delete []rows; |
| 137 |
TRACE("png_write_rows\n"); |
| 138 |
png_write_end(png_ptr, png_info_ptr); |
| 139 |
TRACE("png_write_end\n"); |
| 140 |
png_destroy_write_struct(&png_ptr, &png_info_ptr); |
| 141 |
TRACE("png_destroy_write\n"); |
| 142 |
fclose(fp); |
| 143 |
return true; |
| 144 |
} |