clsPng.cpp
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "clsPng.h"
#include"clsCrSurf.h"
namespace DmapCore_30
{
clsPng::clsPng(void)
{
b = 0;
//b3=0;
//p1=0;
patta = 0;
}
clsPng::~clsPng(void)
{
// if(p1){delete p1;p1=0;}
if (patta){ delete patta; patta = 0; };
if (b){ free(b); b = 0; }
// if(b3){free(b3);b3=0;}
}
int clsPng::ReadPng(const char * fileName, unsigned int colorThis_)
{
try
{
// b = (unsigned char *)malloc(1024);
unsigned int colorThis = colorThis_; ((char *)&colorThis)[3] = 0;
cairo_surface_t* image = cairo_image_surface_create_from_png(fileName);
clsCrSurf clsCS(image);
int width_image = cairo_image_surface_get_width(image); int height_image = cairo_image_surface_get_height(image);
if (width_image == 0 || height_image == 0)return 1;
{
unsigned char* b3 = cairo_image_surface_get_data(image);
b = (unsigned char *)malloc(4 * width_image*height_image);
memcpy(b, b3, 4 * width_image*height_image);
}
for (int j = 0; j < height_image; j++)
{
int *c = ((int *)(b + 4 * (width_image * j)));
for (int i = 0; i < width_image; i++, c++)
{
if (((unsigned char *)c)[3] == 0)
{
c[0] = colorThis;
}
}
}
int beforCount = 0, endCount = 0;
for (int j = 0; j < height_image; j++)
{
bool found = false;
int *c = ((int *)(b + 4 * (width_image * j)));
for (int i = 0; i < width_image; i++, c++)
{
if (((unsigned char *)c)[3] == 0)continue;
found = true; break;
}
if (found)break;
beforCount++;
}
for (int j = height_image - 1; j >= 0; j--)
{
bool found = false;
int *c = ((int *)(b + 4 * (width_image * j)));
for (int i = 0; i < width_image; i++, c++)
{
if (((unsigned char *)c)[3] == 0)continue;
found = true; break;
}
if (found)break;
endCount++;
}
if (beforCount + endCount >= height_image){ return 1; }
if (beforCount>0)
{
int hTemp = height_image - (beforCount + endCount);
memmove(b, b + beforCount*width_image * 4, hTemp*width_image * 4);
height_image = hTemp;
}
agg::rendering_buffer rbuf_img_aa;
rbuf_img_aa.attach(b, width_image, height_image, (width_image)* 4);
pattern_src_brightness_to_alpha_rgba8 p1(rbuf_img_aa);
patta = new agg::line_image_pattern<agg::pattern_filter_bilinear_rgba8>(fltr, p1);
}
catch (...)
{
return (1);
}
return 0;
/*
// globeLock.Log1Message("1","aa");
char sz[2000];
int len=strlen(fileName);
for(int i=0;i<=len;i++)
{
if(fileName[i]=='/')
{
sz[i]='\\';
}
else
{
sz[i]=fileName[i];
}
}
clsPngRead read;
ZeroFile zeroFile;
zeroFile.fp=fopen(sz,"rb");
FILE *fp = zeroFile.fp;
if (!fp)
{
return (1);
}
//globeLock.Log1Message("2","aa");
try
{
int k=read.readpng_init(fp,&width,&height);
png_uint_16 rr=0,gg=0,bb=0;
k=read.readpng_get_bgcolor(&rr,&gg,&bb);
//png_uint_32 rowbytes;
b=read.readpng_get_image(0,&channels,&rowbytes);
if(channels==3&&b)
{
int rowbytes1=4*rowbytes/3;
png_byte *b1=(png_byte *)malloc(width*height*4);
for(unsigned long j=0;j<height;j++)
{
for(unsigned long i=0;i<width;i++)
{
(b1+rowbytes1*j+i*4)[0]=(b+rowbytes*j+i*3)[0];
(b1+rowbytes1*j+i*4)[1]=(b+rowbytes*j+i*3)[1];
(b1+rowbytes1*j+i*4)[2]=(b+rowbytes*j+i*3)[2];
(b1+rowbytes1*j+i*4)[3]=255;
}
}
free(b);
b=b1;
rowbytes=4*rowbytes/3;
}
read.readpng_cleanup(0);
//globeLock.Log1Message("3","aa");
unsigned long j;
for(j=0;j<height;j++)
{
for(unsigned long i=0;i<width;i++)
{
png_byte rrr=(b+rowbytes*j+i*4)[0];
//png_byte ggg=(icon+rowbytes*j+i*4)[1];
png_byte ggg=(b+rowbytes*j+i*4)[1];
png_byte bbb=(b+rowbytes*j+i*4)[2];
//unsigned char aa[100];
//memcpy(aa,b,sizeof(aa));
if(rrr==0&&ggg==0&&bbb==0)
{
rrr=1;
}
else if(rrr==255&&ggg==255&&bbb==255)
{
//rrr=254;
}
(b+rowbytes*j+i*4)[0]=bbb;
//ggg(icon+rowbytes*j+i*4)[1];
(b+rowbytes*j+i*4)[2]=rrr;
if(read.color_type==6)
{
if((b+rowbytes*j+i*4)[3]==0)
{
(b+rowbytes*j+i*4)[0]=255;
(b+rowbytes*j+i*4)[1]=255;
(b+rowbytes*j+i*4)[2]=255;
}
}
}
}
//globeLock.Log1Message("4","aa");
png_byte *b3=(png_byte *)malloc(width*height*3);
png_byte *t=b3;
for(j=0;j<height;j++)
{
for(unsigned long i=0;i<width;i++)
{
png_byte rrr=(b+rowbytes*j+i*4)[0];
png_byte ggg=(b+rowbytes*j+i*4)[1];
png_byte bbb=(b+rowbytes*j+i*4)[2];
if((b+rowbytes*j+i*4)[3]==0)
{
rrr=ggg=bbb=255;
}
t[0]=rrr;t++;
t[0]=ggg;t++;
t[0]=bbb;t++;
}
}
agg::rendering_buffer rbuf_img_aa;
rbuf_img_aa.attach(b3,width,height,width * 3);pattern_src_brightness_to_alpha_rgba8 p1(rbuf_img_aa);
//;p1=new pattern_src_brightness_to_alpha_rgba8(rbuf_img_aa);
patta=new agg::line_image_pattern<agg::pattern_filter_bilinear_rgba8>(fltr,p1);
free(b);b=b3;
//free(p1);p1=0;
//globeLock.Log1Message("5","aa");
return 0;
}
catch(...)
{
return (1);
}
*/
//return 0;
}
/*
{
clsPngRead read;
FILE *fp = fopen("D:\\beijing\\temp\\2.png", "r+b");
if (!fp)
{
return (ERROR);
}
unsigned long width,height;
int k=read.readpng_init(fp,&width,&height);
png_uint_16 rr=0,gg=0,bb=0;
k=read.readpng_get_bgcolor(&rr,&gg,&bb);
int channels;png_uint_32 rowbytes;
png_byte *b=read.readpng_get_image(0,&channels,&rowbytes);
HDC hdc=::GetDC(0);
int cc=RGB(255,255,255);
for(int j=0;j<height;j++);
{
png_byte *brow=b+j*rowbytes;
for(int i=0;i<width;i++,brow+=channels)
{
int c=RGB(brow[0],brow[1],brow[2]);
if(c!=cc){
// ::SetPixel(hdc,i,j,c);
}
}
}
width=width;
}
*/
}