| 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 <libpng/png.h> |
| 19 |
|
| 20 |
#include "progress.h"cxx . . $ `<file>/src/progress.h`/ |
| 21 |
|
| •22 |
|
| •23 |
void downsample2x2cxx . . $ downsample2x2(49a5bbab55ea8abc).(unsigned char *data, int sx, int sy){ |
| 24 |
int j=0; |
| 25 |
for (int y=0; y<sy/2; y++) |
| 26 |
for (int x=0; x<sx/2; x++) |
| 27 |
for (int c=0; c <4; c++) { |
| 28 |
data[j++]=( |
| 29 |
int(data[((y*2+0)*sx+(x*2+0))*4+c])+ |
| 30 |
int(data[((y*2+0)*sx+(x*2+1))*4+c])+ |
| 31 |
int(data[((y*2+1)*sx+(x*2+0))*4+c])+ |
| 32 |
int(data[((y*2+1)*sx+(x*2+1))*4+c])+ |
| 33 |
2 |
| 34 |
)/4; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| •38 |
void downsample2x2NoAlphacxx . . $ downsample2x2NoAlpha(49a5bbab55ea8abc).(unsigned char * data, int sx, int sy){ |
| 39 |
int j=0; |
| 40 |
for (int y=0; y<sy/2; y++) |
| 41 |
for (int x=0; x<sx/2; x++) |
| 42 |
for (int c=0; c <3; c++) { |
| 43 |
data[j++]=( |
| 44 |
int(data[((y*2+0)*sx+(x*2+0))*3+c])+ |
| 45 |
int(data[((y*2+0)*sx+(x*2+1))*3+c])+ |
| 46 |
int(data[((y*2+1)*sx+(x*2+0))*3+c])+ |
| 47 |
int(data[((y*2+1)*sx+(x*2+1))*3+c])+ |
| 48 |
2 |
| 49 |
)/4; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void tracecxx . . $ trace(2fa786934fe80e24).(const char *st, const char *st2=NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!){ |
| 54 |
static FILE *f=NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 55 |
if (!f) f=fopen("trace.txt","wt"); |
| •56 |
fprintf(f,"%s",st); |
| 57 |
if (st2) fprintf(f,"%s",st2); |
| 58 |
fflush(f); |
| 59 |
} |
| 60 |
|
| •61 |
void donttracecxx . . $ donttrace(85c52e162fed56f9).(const char*x){}; |
| 62 |
void donttracecxx . . $ donttrace(2fa786934fe80e24).(const char*x, const char*y){}; |
| 63 |
#define TRACEcxx . . $ `src/pngSave.cpp:63:9`! donttrace |
| 64 |
//#define TRACE trace |
| 65 |
void handle_errorcxx . . $ handle_error(6c268845f29c377d).(struct png_struct_defcxx . . $ png_struct_def# *png,const char *error) { |
| 66 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("Error:\n", error); |
| 67 |
} |
| 68 |
|
| •69 |
bool PNGSaveWithAlphacxx . . $ PNGSaveWithAlpha(f0ecdd44617aa11b).( const char * filename, const unsigned char * data, int sx, int sy, int reverse ){ |
| 70 |
int type=4; int depth=8; |
| 71 |
assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(filename); |
| 72 |
assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(data); |
| 73 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("Start\n"); |
| 74 |
FILE *fp; |
| 75 |
fp=fopen(filename, "wb+"); |
| 76 |
if(!fp) |
| 77 |
return false; |
| 78 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("File open (wb+)\n"); |
| 79 |
|
| 80 |
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRINGcxx . . $ `<external>/15466183176234753646/png.h:277:9`!, |
| 81 |
//NULL, NULL, NULL); |
| 82 |
NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, //&handle_error, |
| 83 |
&handle_errorcxx . . $ handle_error(6c268845f29c377d)., |
| 84 |
&handle_errorcxx . . $ handle_error(6c268845f29c377d).); |
| 85 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_create_write\n"); |
| 86 |
if (png_ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 87 |
fclose(fp); |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
// Allocate/initialize the image information data. REQUIRED |
| 92 |
png_infop png_info_ptr = png_create_info_struct(png_ptr); |
| 93 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_create_info\n"); |
| 94 |
if (png_info_ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 95 |
fclose(fp); |
| 96 |
png_destroy_write_struct(&png_ptr, (png_infopp)NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 97 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_destroy_write\n"); |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
png_init_io(png_ptr, fp); |
| 102 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_init_io\n"); |
| 103 |
//type: 1 -> PNG_COLOR_TYPE_GRAY (bit depths 1, 2, 4, 8, 16) |
| 104 |
// 2 -> PNG_COLOR_TYPE_GRAY_ALPHA (bit depths 8, 16) |
| 105 |
// 3 -> PNG_COLOR_TYPE_RGB (bit_depths 8, 16) |
| 106 |
// 4 -> PNG_COLOR_TYPE_RGB_ALPHA (bit_depths 8, 16) |
| 107 |
int pixel_type = 1; |
| 108 |
switch(type) { |
| 109 |
case 1: pixel_type = PNG_COLOR_TYPE_GRAYcxx . . $ `<external>/15466183176234753646/png.h:666:9`!; break; |
| 110 |
case 2: pixel_type = PNG_COLOR_TYPE_GRAY_ALPHAcxx . . $ `<external>/15466183176234753646/png.h:670:9`!; break; |
| 111 |
case 3: pixel_type = PNG_COLOR_TYPE_RGBcxx . . $ `<external>/15466183176234753646/png.h:668:9`!; break; |
| 112 |
case 4: pixel_type = PNG_COLOR_TYPE_RGB_ALPHAcxx . . $ `<external>/15466183176234753646/png.h:669:9`!; break; |
| 113 |
} |
| 114 |
//TRACE("sx %d, sy %d, depth %d, pixel_type %d\n", sx, sy, depth, pixel_type); |
| 115 |
png_set_IHDR(png_ptr, png_info_ptr, sx, sy, depth, pixel_type, |
| 116 |
PNG_INTERLACE_NONEcxx . . $ `<external>/15466183176234753646/png.h:685:9`!, PNG_COMPRESSION_TYPE_BASEcxx . . $ `<external>/15466183176234753646/png.h:676:9`!, PNG_FILTER_TYPE_BASEcxx . . $ `<external>/15466183176234753646/png.h:680:9`!); |
| 117 |
//TRACE("png_set_IHDR\n"); |
| 118 |
png_write_info(png_ptr, png_info_ptr); |
| 119 |
//TRACE("png_write_info\n"); |
| 120 |
//TRACE("uffa");*/ |
| 121 |
char **rows = new char *[sy]; |
| 122 |
for(int i = 0; i < sy; i++) |
| 123 |
rows[i] = (char *)data + i * type * (depth/8) * sx; |
| 124 |
if(reverse) { |
| 125 |
if (sy>200) StartProgresscxx . . $ StartProgress(34d26762b42c433a).("Saving PNG", sy); |
| 126 |
for(int r = sy -1; r >= 0; r--) { |
| 127 |
if (r%100) { if (!UpdateProgresscxx . . $ UpdateProgress(83c267b20bac841a).(sy-r)) { fclose(fp); return false; } } |
| 128 |
png_write_rows(png_ptr, (png_bytecxx . . $ png_byte# **)(&(rows[r])), 1); |
| 129 |
} |
| 130 |
EndProgresscxx . . $ EndProgress(49f6e7a06ebc5aa8).(); |
| 131 |
} else { |
| 132 |
png_write_rows(png_ptr, (png_bytecxx . . $ png_byte# **)(rows), sy); |
| 133 |
} |
| 134 |
delete []rows; |
| 135 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_write_rows\n"); |
| 136 |
png_write_end(png_ptr, png_info_ptr); |
| 137 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_write_end\n"); |
| 138 |
png_destroy_write_struct(&png_ptr, &png_info_ptr); |
| 139 |
TRACEcxx . . $ `src/pngSave.cpp:63:9`!("png_destroy_write\n"); |
| 140 |
fclose(fp); |
| 141 |
return true; |
| 142 |
} |