-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.h
executable file
·393 lines (327 loc) · 9.16 KB
/
codec.h
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#ifndef __CODEC_H__
#define __CODEC_H__
#include <string>
#include <linux/fb.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
using namespace std;
#include "hui.h"
//#include "jpeg.h"
//#include "jpegcodec.h"
//#include "w55fa92_blt.h"
//#include "blt.h"
#include "hulib.h"
#include "alpha.h"
#ifdef CONFIG_DEBUG_AREACOPY
#define debug_areacopy(a,...) fprintf(stderr,a,##__VA_ARGS__)
#else
#define debug_areacopy(a,...)
#endif
extern int snapcache;
int bmpCodec_to_image(image * sobj, const char *filename);
int jpegCodec_to_image(image * sobj, const char *filename);
int pngCodec_to_image(image * sobj, const char *filepath);
int pngEndec_to_image(const char *file_name, image * graph);
int codec_to_Image(image * enode, const char * filename);
void Render_img_to_buf(void * buf, image * img, int width, int height);
void ProcArea(image * dst_img, image * rsc_img, int & src_x, int & src_y, int & cp_width, int & cp_height, int & dst_x, int & dst_y);
void AreaCopy(image * dst_img, image * src_img, int src_x, int src_y, int cp_width, int cp_height, int dst_x, int dst_y);
int fill_image(image * img, int a = 255, int r = 128, int g = 128, int b = 128);
int access_Image(const char * filename);
typedef struct
{
unsigned char u8Blue;
unsigned char u8Green;
unsigned char u8Red;
unsigned char u8Alpha;
} IMG_PIX;
class image: virtual public Mutex
{
protected:
void * pSrcBuffer;
unsigned long SrcSize;
unsigned int u32Width; // crop width
unsigned int u32Height; // crop height
unsigned int u32Stride;
int transp; //透明度
public:
friend void Render_img_to_img(image * dst, image * src, int src_x, int src_y, int cp_width, int cp_height, int dst_x, int dst_y);
friend int image_write_to_snap(image * img, const char * rawpath);
friend int image_read_from_snap(image * img, const char * rawpath);
friend void AreaCopy(image * dst_img, image * src_img, int src_x, int src_y, int cp_width, int cp_height, int dst_x, int dst_y);
void * GetAddr() const
{
return pSrcBuffer;
}
int getSize() const
{
return SrcSize;
}
int GetImageWidth() const
{
return u32Width;
}
int GetImageHeight() const
{
return u32Height;
}
int GetImageStride()
{
return u32Stride;
}
void setTransp(int n)
{
lock();
transp = n;
unlock();
}
void dump_from_buf(const void * buf, int width, int height)
{
lock();
SetBuffer(width, height);
memcpy(pSrcBuffer, buf, SrcSize);
unlock();
}
void dump_to_buf(void * buf)
{
lock();
memcpy(buf, pSrcBuffer, SrcSize);
unlock();
}
int isNULL()
{
return (pSrcBuffer == NULL);
}
image& operator=(const image &img)
{
if (SrcSize < img.SrcSize)
{
destroy();
}
SetBuffer(img.u32Width, img.u32Height);
memcpy(pSrcBuffer, img.pSrcBuffer, SrcSize);
}
// inline void SetPix(int pos, int r, int g, int b, int a)
// {
// IMG_PIX * tmp = GetPix(pos);
// tmp->u8Red = r;
// tmp->u8Green = g;
// tmp->u8Blue = b;
// tmp->u8Alpha = a;
// }
// inline void SetPix(int x, int y, int r, int g, int b, int a)
// {
// IMG_PIX * tmp = GetPix(x,y);
// tmp->u8Red = r;
// tmp->u8Green = g;
// tmp->u8Blue = b;
// tmp->u8Alpha = a;
// }
inline IMG_PIX * GetPix(int x, int y)
{
if (x > u32Width || y > u32Height)
{
huErrExitf("Get pixels beyond the range x=%d y=%d width=%d height=%d\r\n", x, y, u32Width, u32Height);
}
return GetPix(y * u32Width + x);
}
inline IMG_PIX * GetPix(int pos)
{
if ((pos * 4) > SrcSize)
{
huErrExitf("Get pixels beyond the range pos=%d width=%d height=%d\r\n", pos, u32Width, u32Height);
}
return ((IMG_PIX *) pSrcBuffer + pos);
}
unsigned long SrcGPUAddr()
{
return (unsigned long) pSrcBuffer;
}
int Load(const char * path)
{
return codec_to_Image(this,path);//装载图片
}
int Save(const char * file)
{
return pngEndec_to_image(file, this);
}
virtual void * alloc_memory(size_t size)
{
return malloc(size);
}
virtual void free_memory(void * buf)
{
free(buf);
}
virtual void destroy()
{
if (pSrcBuffer != NULL)
{
debug("destory image pSrcBuffer %dx%d\r\n", u32Width, u32Height);
free_memory(pSrcBuffer);
pSrcBuffer = NULL;
SrcSize = 0;
u32Width = 0; // crop width
u32Height = 0; // crop height
u32Stride = 0;
}
}
int ReSetBuffer(int width, int height)
{
lock();
if(width * height == 0)
{
log_e("undefined width height is zero\n");
}
int dep = 4;
int tmpsize = width * height * dep;
log_d("ReSetBuffer %d %d %d\n",width,height,tmpsize);
void * tmpbuf = alloc_memory(tmpsize);
if (tmpbuf == NULL)
errexitf("ReSetBuffer image alloc_memory failed: width=%d height=%d\n", width, height);
for (int y=0; y < u32Height; y++)
{
memcpy((unsigned int *) tmpbuf + (y * width),(unsigned int *) pSrcBuffer + (y * u32Width),u32Stride);
}
destroy();
pSrcBuffer = tmpbuf;
SrcSize = tmpsize;
u32Width = width;
u32Height = height;
u32Stride = width * dep;
unlock();
return 0;
}
int SetBuffer(int width, int height)
{
//path.format("SetBuffer-%dx%d",width,height);
lock();
if (width * height == 0)
{
log_w("undefined width height is zero\n");
}
int dep = 4;
int tmpsize = width * height * dep;
if (tmpsize > SrcSize)
{
destroy();
}
if (pSrcBuffer == NULL)
{
log_d("SetBuffer %d %d %d\n",width,height,tmpsize);
pSrcBuffer = alloc_memory(tmpsize);
if (pSrcBuffer == NULL)
{
errexitf("image alloc_memory failed: width=%d height=%d\n", width, height);
}
}
SrcSize = tmpsize;
u32Width = width;
u32Height = height;
u32Stride = width * dep;
cleanBuf();
unlock();
return 0;
}
void DrawPoint(int x, int y, int color)
{
*((unsigned int *) pSrcBuffer + y * u32Width + x) = color;
}
void DrawBrush(int x, int y, int color)
{
DrawPoint(x, y, color);
DrawPoint(x, y + 1, color);
DrawPoint(x, y + 2, color);
DrawPoint(x, y - 1, color);
DrawPoint(x, y - 2, color);
DrawPoint(x + 1, y, color);
DrawPoint(x + 2, y, color);
DrawPoint(x - 1, y, color);
DrawPoint(x - 2, y, color);
}
void AreaCmp(image * img, int src_x, int src_y, int cp_width, int cp_height, int dst_x, int dst_y)
{
int x;
int y;
for (y = 0; y < cp_height; y++)
{
for (x = 0; x < cp_width; x++)
{
if (*((unsigned int *) pSrcBuffer + (y + dst_y) * u32Width + dst_x + x)
== *((unsigned int *) img->pSrcBuffer + (y + src_y) * img->u32Width + src_x + x))
{
*((unsigned int *) pSrcBuffer + (y + dst_y) * u32Width + dst_x + x) = 0;
}
}
}
}
void cleanBuf()
{
if (pSrcBuffer)
memset(pSrcBuffer, 0, SrcSize);
}
void debug_info()
{
printf("u32Width=%d\r\n", u32Width);
printf("u32Height=%d\r\n", u32Height);
printf("u32Stride=%d\r\n", u32Stride);
printf("SrcGPUAddr=0x%x\r\n", (int)SrcGPUAddr());
}
virtual void RenderFrom(image * img, int x, int y)
{
Render_img_to_img(this, img, 0, 0, img->u32Width, img->u32Height, x, y);
}
virtual void RenderFrom(image * src_img, int src_x, int src_y, int cp_width, int cp_height, int dst_x, int dst_y)
{
ProcArea(this, src_img, src_x, src_y, cp_width, cp_height, dst_x, dst_y);
Render_img_to_img(this, src_img, src_x, src_y, cp_width, cp_height, dst_x, dst_y);
}
virtual void RenderTo(image * img, int x, int y)
{
Render_img_to_img(img, this, 0, 0, img->u32Width, img->u32Height, x, y);
}
virtual void RenderTo(image * dst_img, int src_x, int src_y, int cp_width,
int cp_height, int dst_x, int dst_y)
{
ProcArea(dst_img, this, src_x, src_y, cp_width, cp_height, dst_x,
dst_y);
Render_img_to_img(dst_img, this, src_x, src_y, cp_width, cp_height,
dst_x, dst_y);
}
virtual void AreaCopyFrom(image * img, int x, int y)
{
::AreaCopy(this, img, 0, 0, img->u32Width, img->u32Height, x, y);
}
virtual void AreaCopyFrom(image * src_img, int src_x, int src_y,
int cp_width, int cp_height, int dst_x, int dst_y)
{
::AreaCopy(this, src_img, src_x, src_y, cp_width, cp_height, dst_x,
dst_y);
}
virtual void AreaCopyTo(image * img, int x, int y)
{
::AreaCopy(img, this, 0, 0, img->u32Width, img->u32Height, x, y);
}
virtual void AreaCopyTo(image * dst_img, int src_x, int src_y, int cp_width,
int cp_height, int dst_x, int dst_y)
{
::AreaCopy(dst_img, this, src_x, src_y, cp_width, cp_height, dst_x,
dst_y);
}
image()
{
pSrcBuffer = NULL;
SrcSize = 0;
u32Width = 0; // crop width
u32Height = 0; // crop height
u32Stride = 0;
transp = 100; //控件透明度-默认不透明
//s_sblitop.transformation = &stransformation;
}
virtual ~image()
{
destroy();
}
};
#endif //__CODEC_H__