Debian Codegraph @

src/pngSave.cpp

raw symbols ◀ ▶ SCIP
4 Debian patches touch this file
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"
21
•22
•23 void downsample2x2(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 downsample2x2NoAlpha(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 trace(const char *st, const char *st2=NULL){
54 static FILE *f=NULL;
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 donttrace(const char*x){};
62 void donttrace(const char*x, const char*y){};
63 #define TRACE donttrace
64 //#define TRACE trace
65 void handle_error(struct png_struct_def *png,const char *error) {
66 TRACE("Error:\n", error);
67 }
68
•69 bool PNGSaveWithAlpha( const char * filename, const unsigned char * data, int sx, int sy, int reverse ){
70 int type=4; int depth=8;
71 assert(filename);
72 assert(data);
73 TRACE("Start\n");
74 FILE *fp;
75 fp=fopen(filename, "wb+");
76 if(!fp)
77 return false;
78 TRACE("File open (wb+)\n");
79
80 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
81 //NULL, NULL, NULL);
82 NULL, //&handle_error,
83 &handle_error,
84 &handle_error);
85 TRACE("png_create_write\n");
86 if (png_ptr == NULL) {
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 TRACE("png_create_info\n");
94 if (png_info_ptr == NULL) {
95 fclose(fp);
96 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
97 TRACE("png_destroy_write\n");
98 return false;
99 }
100
101 png_init_io(png_ptr, fp);
102 TRACE("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_GRAY; break;
110 case 2: pixel_type = PNG_COLOR_TYPE_GRAY_ALPHA; break;
111 case 3: pixel_type = PNG_COLOR_TYPE_RGB; break;
112 case 4: pixel_type = PNG_COLOR_TYPE_RGB_ALPHA; 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_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
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) StartProgress("Saving PNG", sy);
126 for(int r = sy -1; r >= 0; r--) {
127 if (r%100) { if (!UpdateProgress(sy-r)) { fclose(fp); return false; } }
128 png_write_rows(png_ptr, (png_byte **)(&(rows[r])), 1);
129 }
130 EndProgress();
131 } else {
132 png_write_rows(png_ptr, (png_byte **)(rows), sy);
133 }
134 delete []rows;
135 TRACE("png_write_rows\n");
136 png_write_end(png_ptr, png_info_ptr);
137 TRACE("png_write_end\n");
138 png_destroy_write_struct(&png_ptr, &png_info_ptr);
139 TRACE("png_destroy_write\n");
140 fclose(fp);
141 return true;
142 }