forked from ClnViewer/Android-fast-screen-capture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascreencap-ABitmapLite.cpp
515 lines (447 loc) · 14.5 KB
/
ascreencap-ABitmapLite.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include "ascreencap.h"
#include "ascreencap-ABitmapLite.h"
#include "extern/lz4/lz4.h"
#define _PIX_FORMAT32_BMP(A,B) \
{ \
uint32_t pos = A; \
_dst[(pos + 2)] = *(B) & 0x000000FF; \
_dst[(pos + 1)] = (*(B) & 0x0000FF00) >> 8; \
_dst[(pos + 0)] = (*(B) & 0x00FF0000) >> 16; \
}
#define _PIX_FORMAT32_SDL(A,B) \
{ \
uint32_t pos = A; \
_dst[(pos + 0)] = (*(B) & 0xFF000000) >> 24; \
_dst[(pos + 1)] = (*(B) & 0x00FF0000) >> 16; \
_dst[(pos + 2)] = (*(B) & 0x0000FF00) >> 8; \
}
#define _PIX_FORMAT16_BMP(A,B) \
{ \
uint32_t pos = A; \
_dst[(pos + 2)] = (255 * (*(B) & 0x001F))/32; \
_dst[(pos + 1)] = (255 * ((*(B) & 0x07E0) >> 5))/64; \
_dst[(pos + 0)] = (255 * ((*(B) & 0xF800) >> 11))/32; \
}
#define _PIX_FORMAT16_SDL(A,B) \
{ \
uint32_t pos = A; \
_dst[(pos + 2)] = (255 * ((*(B) & 0xF800) >> 11))/32; \
_dst[(pos + 1)] = (255 * ((*(B) & 0x07E0) >> 5))/64; \
_dst[(pos + 0)] = (255 * (*(B) & 0x001F))/32; \
}
namespace ACapture
{
ABitmapLite::ABitmapLite()
: ishead(false), issdlcompat(false), rat(0U), rot(0U) {}
ABitmapLite::ABitmapLite(
uint32_t _w, uint32_t _h, uint32_t _s, uint32_t _f, const void *_src, size_t _sz
) : ishead(false), issdlcompat(false), rat(0U), rot(0U)
{
SetData(_w, _h, _s, _f, _src, _sz);
}
ABitmapLite::~ABitmapLite() {}
bool ABitmapLite::TestData(bool issrc)
{
return !(
(!bmpdata.w) ||
(!bmpdata.h) ||
(!bmpdata.s) ||
(!bmpdata.sz) ||
((issrc) && (!bmpdata.src))
);
}
void ABitmapLite::Reset()
{
::memset(&bmpdata, 0, sizeof(bmpdata));
ishead = false;
if (vsrc.size())
vsrc.clear();
if (vdst.size())
vdst.clear();
if (vdstz.size())
vdstz.clear();
}
void ABitmapLite::SetData(
uint32_t _w, uint32_t _h, uint32_t _s, uint32_t _f, const void *_src, size_t _sz
)
{
bmpdata.w = _w;
bmpdata.h = _h;
bmpdata.s = _s;
bmpdata.f = _f;
bmpdata.sz = _sz;
if (TestData(false))
{
bmpdata.b = (bmpdata.sz / bmpdata.w / bmpdata.h);
SetData(_src, _sz);
}
}
void ABitmapLite::SetData(const void *_src, size_t _sz)
{
uint8_t *l_src = static_cast<uint8_t*>(
const_cast<void*>(_src)
);
vsrc.assign(&l_src[0], &l_src[0] + _sz);
bmpdata.sz = _sz;
bmpdata.src = &vsrc[0];
bmpdata.dst = nullptr;
}
uint8_t * ABitmapLite::GetData(size_t *psz)
{
do
{
*psz = 0U;
if (!TestData(true))
break;
if (!convertBmp(issdlcompat))
break;
if (!issdlcompat)
if (!headerBmp())
break;
*psz = ((ishead) ?
(bmpdata.sz + sizeof(ABitmapLite::BMPHEADER)) : bmpdata.sz
);
return ((ishead) ?
&bmpdata.dst[0] : &bmpdata.dst[sizeof(ABitmapLite::BMPHEADER)]
);
}
while (0);
return nullptr;
}
uint8_t * ABitmapLite::GetDataPack(size_t *psz, int32_t fast)
{
do
{
uint8_t *pdst;
if (
(!(pdst = GetData(psz))) ||
(!*psz)
)
break;
int lzrsz,
lzbsz = LZ4_compressBound(static_cast<int>(*psz));
if (!lzbsz)
break;
vdstz.resize(lzbsz + sizeof(STREAMHEADER));
lzrsz = LZ4_compress_fast(
(const char*)pdst,
(char*)&vdstz[sizeof(STREAMHEADER)],
static_cast<int>(*psz),
lzbsz,
fast
);
if (lzrsz <= 0)
break;
vdstz.resize(lzrsz + sizeof(STREAMHEADER));
STREAMHEADER sh = {
BMZ_MAGIC,
static_cast<uint32_t>(*psz),
static_cast<uint32_t>(lzrsz),
bmpdata.w,
bmpdata.h
};
memcpy(&vdstz[0], &sh, sizeof(sh));
*psz = vdstz.size();
bmpdata.dst = &vdstz[0];
return &bmpdata.dst[0];
}
while (0);
*psz = 0U;
return nullptr;
}
uint32_t ABitmapLite::getBpp() const
{
/// TODO: format normalize
switch(bmpdata.f)
{
/// * 32 BPP
case android::PIXEL_FORMAT_RGBA_8888:
case android::PIXEL_FORMAT_RGBX_8888:
case android::PIXEL_FORMAT_BGRA_8888:
/// > 5.1.1 duplicate PIXEL_FORMAT_RGBA_8888
//case android::PIXEL_FORMAT_sRGB_A_8888:
/// 5.1.1 AOSP
//case android::PIXEL_FORMAT_sRGB_X_8888:
return 32;
/// * 24 BPP
case android::PIXEL_FORMAT_RGB_888:
return 24;
/// * 16 BPP
case android::PIXEL_FORMAT_RGB_565:
case android::PIXEL_FORMAT_RGBA_5551:
case android::PIXEL_FORMAT_RGBA_4444:
return 16;
default: return 0;
}
}
uint32_t ABitmapLite::getPadDst(uint32_t _w) const
{
uint32_t pad = 0U;
while (((_w + pad) % 4) != 0)
pad++;
return pad;
}
uint32_t ABitmapLite::getPadSrc() const
{
return ((bmpdata.s > bmpdata.w) ?
static_cast<uint32_t>((bmpdata.s - bmpdata.w) * bmpdata.b) :
0U
);
}
bool ABitmapLite::convertBmp(bool sdlcompat)
{
if (!bmpdata.src)
return false;
const uint32_t
hd = bmpdata.h,
wr = ((rat) ? __extension__ (
{ uint32_t a = (bmpdata.w / rat);
if (bmpdata.w % rat) a++; a; })
: bmpdata.w),
hr = ((rat) ? __extension__ (
{ uint32_t a = (bmpdata.h / rat);
if (bmpdata.h % rat) a++; a; })
: bmpdata.h),
ar = ((rat) ? rat : 1U),
ax = (ar * bmpdata.b),
br = bmpdata.b,
wsz = (bmpdata.w * bmpdata.b),
wdz = (wr * 3U),
pfmt = getBpp(),
spad = getPadSrc(),
dpad = getPadDst(wdz);
if (!pfmt)
return false;
bmpdata.w = ((rat) ? wr : bmpdata.w);
bmpdata.h = ((rat) ? hr : bmpdata.h);
bmpdata.sz = ((wdz + dpad) * hr);
if (!sdlcompat)
{
std::reverse(vsrc.begin(), vsrc.end());
bmpdata.src = &vsrc[0];
}
# if defined(_DEBUG)
__LOG_PRINT("-> convertBmp -> point: %ux%u", bmpdata.w, bmpdata.h);
__LOG_PRINT("-> convertBmp -> sdl/ratio/rotate: %d/%u/%u", sdlcompat, rat, rot);
__LOG_PRINT("-> convertBmp -> format/bpp: %u/%u", pfmt, bmpdata.b);
__LOG_PRINT("-> convertBmp -> wsz/wdz/pads: %u/%u/%u/%u", wsz, wdz, spad, dpad);
__LOG_PRINT("-> convertBmp -> size: %zu", bmpdata.sz);
# endif
vdst.resize(bmpdata.sz + sizeof(ABitmapLite::BMPHEADER));
bmpdata.dst = &vdst[0];
uint8_t *_dst = &vdst[sizeof(ABitmapLite::BMPHEADER)];
uint32_t y;
# pragma omp parallel for private(y) schedule(static)
for(y = 0U; y < hd; y += ar)
{
uint32_t sx = 0U, ssz = ((wsz + spad) * y),
dx = 0U, dsz = ((y / ar) * (wdz + dpad));
std::vector<uint8_t> v(wsz + spad);
v.assign(
&bmpdata.src[ssz],
&bmpdata.src[(ssz + wsz + spad)]
);
std::reverse(v.begin(), v.end());
if ((!rat) && (pfmt == 24))
{
/// TODO: sdl2 format make
memcpy(&_dst[dsz], &v[0], v.size());
continue;
}
for (; sx < v.size(); sx += ax, dx += 3U)
{
switch (pfmt)
{
case 16:
{
uint16_t *pixel16 = (uint16_t*)(&v[sx]);
if (sdlcompat)
_PIX_FORMAT16_SDL((dsz + dx), pixel16)
else
_PIX_FORMAT16_BMP((dsz + dx), pixel16)
break;
}
case 24:
{
/// TODO: sdl2 format make
memcpy(&_dst[(dsz + dx)], &v[sx], br);
break;
}
case 32:
{
uint32_t *pixel32 = (uint32_t*)(&v[sx]);
if (sdlcompat)
_PIX_FORMAT32_SDL((dsz + dx), pixel32)
else
_PIX_FORMAT32_BMP((dsz + dx), pixel32)
break;
}
}
}
}
if (rot)
rotateBmp(rot);
# if defined(_DEBUG_RAW_FILE)
FILE *fp;
static const char *fnameraw = "/data/local/tmp/OutBmp.raw";
__LOG_PRINT("-> convertBmp -> write debug RAW file: %s", fnameraw);
if ((fp = fopen(fnameraw, "w")))
{
int sraw = fwrite(_dst, 1, bmpdata.sz, fp);
fclose(fp);
__LOG_PRINT("-> convertBmp -> wrote to file: %d/%u bytes.", sraw, bmpdata.sz);
}
# endif
return true;
}
void ABitmapLite::rotateBmp(uint32_t angle)
{
switch (angle)
{
case 90:
case 180:
case 270:
case 360: /// mirror mode
{
if (!bmpdata.dst)
return;
break;
}
default:
return;
}
uint32_t y, spad, dpad, ssz;
const uint32_t wr = bmpdata.w, hr = bmpdata.h;
switch (angle)
{
case 90:
case 270:
case 360:
{
spad = getPadDst(wr);
dpad = getPadDst(hr);
ssz = (((wr * 3U) + dpad) * hr);
break;
}
default:
{
spad = dpad = getPadDst(wr);
ssz = bmpdata.sz;
break;
}
break;
}
std::vector<uint8_t> v(ssz + sizeof(ABitmapLite::BMPHEADER));
const uint8_t *p0 = &bmpdata.dst[sizeof(ABitmapLite::BMPHEADER)];
uint8_t *p1 = &v[sizeof(ABitmapLite::BMPHEADER)];
# pragma omp parallel for private(y) schedule(dynamic)
for (y = 0U; y < hr; y++)
{
uint32_t sx, dx;
switch (angle)
{
case 90:
{
dx = (hr - y - 1) + (y * dpad);
sx = ((y) ? ((wr + spad) * y) : wr);
break;
}
case 180:
{
dx = ((wr + dpad) * y);
sx = ((hr - y - 1) * (wr + spad));
break;
}
case 270: // TODO:
{
dx = y + (y * dpad);
sx = ((wr + spad) * y);
break;
}
case 360:
{
dx = y + (y * dpad);
sx = ((wr + spad) * y);
break;
}
default:
{
continue;
}
}
for (uint32_t x = 0U; x < wr; x++)
{
uint32_t ssz, dsz;
switch (angle)
{
case 90:
case 360:
{
dsz = (((hr * x) + dx) * 3);
ssz = ((sx + x) * 3);
break;
}
case 180:
{
dsz = ((dx + x) * 3);
ssz = ((sx + (wr - x - 1)) * 3);
break;
}
case 270: // TODO:
{
dsz = (((hr * x) + dx) * 3);
ssz = ((sx + x) * 3);
break;
}
default:
{
continue;
}
}
p1[dsz + 0] = p0[ssz + 0];
p1[dsz + 1] = p0[ssz + 1];
p1[dsz + 2] = p0[ssz + 2];
}
}
vdst.clear();
vdst.assign(v.begin(), v.end());
bmpdata.dst = &vdst[0];
switch (angle)
{
case 90:
case 270:
case 360:
{
if (bmpdata.w != bmpdata.h)
std::swap(bmpdata.w, bmpdata.h);
break;
}
default:
break;
}
}
bool ABitmapLite::headerBmp()
{
if (!bmpdata.sz)
return false;
ABitmapLite::BMPHEADER bmph{};
bmph.fh.bfType = 0x4D42; // "BM"
bmph.fh.bfSize = bmpdata.sz + sizeof(bmph);
bmph.fh.bfOffBits = sizeof(bmph);
bmph.ih.biSize = sizeof(bmph.ih);
bmph.ih.biWidth = static_cast<int32_t>(bmpdata.w);
bmph.ih.biHeight = static_cast<int32_t>(bmpdata.h);
bmph.ih.biPlanes = 1U;
bmph.ih.biBitCount = 24; //(b * 8);
bmph.ih.biCompression = 0x0000; // BI_RGB
bmph.ih.biSizeImage = 0;
if (!memcpy(&bmpdata.dst[0], &bmph, sizeof(bmph)))
return false;
ishead = true;
# if defined(_DEBUG)
__LOG_PRINT("-> bmph -> point: %ux%u", bmpdata.w, bmpdata.h);
__LOG_PRINT("-> bmph -> size: %u/%zu", bmph.fh.bfSize, bmpdata.sz);
# endif
return true;
}
}