/src/FreeRDP/libfreerdp/codec/planar.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * RDP6 Planar Codec |
4 | | * |
5 | | * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2016 Armin Novak <armin.novak@thincast.com> |
7 | | * Copyright 2016 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <winpr/crt.h> |
25 | | #include <winpr/wtypes.h> |
26 | | #include <winpr/assert.h> |
27 | | #include <winpr/print.h> |
28 | | |
29 | | #include <freerdp/primitives.h> |
30 | | #include <freerdp/log.h> |
31 | | #include <freerdp/codec/bitmap.h> |
32 | | #include <freerdp/codec/planar.h> |
33 | | |
34 | | #define TAG FREERDP_TAG("codec") |
35 | | |
36 | | #define PLANAR_ALIGN(val, align) \ |
37 | 0 | ((val) % (align) == 0) ? (val) : ((val) + (align) - (val) % (align)) |
38 | | |
39 | | typedef struct |
40 | | { |
41 | | /** |
42 | | * controlByte: |
43 | | * [0-3]: nRunLength |
44 | | * [4-7]: cRawBytes |
45 | | */ |
46 | | BYTE controlByte; |
47 | | BYTE* rawValues; |
48 | | } RDP6_RLE_SEGMENT; |
49 | | |
50 | | typedef struct |
51 | | { |
52 | | UINT32 cSegments; |
53 | | RDP6_RLE_SEGMENT* segments; |
54 | | } RDP6_RLE_SEGMENTS; |
55 | | |
56 | | typedef struct |
57 | | { |
58 | | /** |
59 | | * formatHeader: |
60 | | * [0-2]: Color Loss Level (CLL) |
61 | | * [3] : Chroma Subsampling (CS) |
62 | | * [4] : Run Length Encoding (RLE) |
63 | | * [5] : No Alpha (NA) |
64 | | * [6-7]: Reserved |
65 | | */ |
66 | | BYTE formatHeader; |
67 | | } RDP6_BITMAP_STREAM; |
68 | | |
69 | | struct S_BITMAP_PLANAR_CONTEXT |
70 | | { |
71 | | UINT32 maxWidth; |
72 | | UINT32 maxHeight; |
73 | | UINT32 maxPlaneSize; |
74 | | |
75 | | BOOL AllowSkipAlpha; |
76 | | BOOL AllowRunLengthEncoding; |
77 | | BOOL AllowColorSubsampling; |
78 | | BOOL AllowDynamicColorFidelity; |
79 | | |
80 | | UINT32 ColorLossLevel; |
81 | | |
82 | | BYTE* planes[4]; |
83 | | BYTE* planesBuffer; |
84 | | |
85 | | BYTE* deltaPlanes[4]; |
86 | | BYTE* deltaPlanesBuffer; |
87 | | |
88 | | BYTE* rlePlanes[4]; |
89 | | BYTE* rlePlanesBuffer; |
90 | | |
91 | | BYTE* pTempData; |
92 | | UINT32 nTempStep; |
93 | | |
94 | | BOOL bgr; |
95 | | BOOL topdown; |
96 | | }; |
97 | | |
98 | | static INLINE UINT32 planar_invert_format(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL alpha, |
99 | | UINT32 DstFormat) |
100 | 0 | { |
101 | |
|
102 | 0 | if (planar->bgr && alpha) |
103 | 0 | { |
104 | 0 | switch (DstFormat) |
105 | 0 | { |
106 | 0 | case PIXEL_FORMAT_ARGB32: |
107 | 0 | DstFormat = PIXEL_FORMAT_ABGR32; |
108 | 0 | break; |
109 | 0 | case PIXEL_FORMAT_XRGB32: |
110 | 0 | DstFormat = PIXEL_FORMAT_XBGR32; |
111 | 0 | break; |
112 | 0 | case PIXEL_FORMAT_ABGR32: |
113 | 0 | DstFormat = PIXEL_FORMAT_ARGB32; |
114 | 0 | break; |
115 | 0 | case PIXEL_FORMAT_XBGR32: |
116 | 0 | DstFormat = PIXEL_FORMAT_XRGB32; |
117 | 0 | break; |
118 | 0 | case PIXEL_FORMAT_BGRA32: |
119 | 0 | DstFormat = PIXEL_FORMAT_RGBA32; |
120 | 0 | break; |
121 | 0 | case PIXEL_FORMAT_BGRX32: |
122 | 0 | DstFormat = PIXEL_FORMAT_RGBX32; |
123 | 0 | break; |
124 | 0 | case PIXEL_FORMAT_RGBA32: |
125 | 0 | DstFormat = PIXEL_FORMAT_BGRA32; |
126 | 0 | break; |
127 | 0 | case PIXEL_FORMAT_RGBX32: |
128 | 0 | DstFormat = PIXEL_FORMAT_BGRX32; |
129 | 0 | break; |
130 | 0 | case PIXEL_FORMAT_RGB24: |
131 | 0 | DstFormat = PIXEL_FORMAT_BGR24; |
132 | 0 | break; |
133 | 0 | case PIXEL_FORMAT_BGR24: |
134 | 0 | DstFormat = PIXEL_FORMAT_RGB24; |
135 | 0 | break; |
136 | 0 | case PIXEL_FORMAT_RGB16: |
137 | 0 | DstFormat = PIXEL_FORMAT_BGR16; |
138 | 0 | break; |
139 | 0 | case PIXEL_FORMAT_BGR16: |
140 | 0 | DstFormat = PIXEL_FORMAT_RGB16; |
141 | 0 | break; |
142 | 0 | case PIXEL_FORMAT_ARGB15: |
143 | 0 | DstFormat = PIXEL_FORMAT_ABGR15; |
144 | 0 | break; |
145 | 0 | case PIXEL_FORMAT_RGB15: |
146 | 0 | DstFormat = PIXEL_FORMAT_BGR15; |
147 | 0 | break; |
148 | 0 | case PIXEL_FORMAT_ABGR15: |
149 | 0 | DstFormat = PIXEL_FORMAT_ARGB15; |
150 | 0 | break; |
151 | 0 | case PIXEL_FORMAT_BGR15: |
152 | 0 | DstFormat = PIXEL_FORMAT_RGB15; |
153 | 0 | break; |
154 | 0 | default: |
155 | 0 | break; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | return DstFormat; |
159 | 0 | } |
160 | | |
161 | | static INLINE BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT plane, |
162 | | UINT32 width, UINT32 height, |
163 | | BYTE* outPlane, |
164 | | UINT32* WINPR_RESTRICT dstSize); |
165 | | static INLINE BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, |
166 | | UINT32 width, UINT32 height, |
167 | | BYTE* WINPR_RESTRICT outPlane); |
168 | | |
169 | | static INLINE INT32 planar_skip_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, |
170 | | UINT32 nWidth, UINT32 nHeight) |
171 | 0 | { |
172 | 0 | UINT32 used = 0; |
173 | 0 | BYTE controlByte = 0; |
174 | |
|
175 | 0 | WINPR_ASSERT(pSrcData); |
176 | | |
177 | 0 | for (UINT32 y = 0; y < nHeight; y++) |
178 | 0 | { |
179 | 0 | for (UINT32 x = 0; x < nWidth;) |
180 | 0 | { |
181 | 0 | int cRawBytes = 0; |
182 | 0 | int nRunLength = 0; |
183 | |
|
184 | 0 | if (used >= SrcSize) |
185 | 0 | { |
186 | 0 | WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, |
187 | 0 | SrcSize); |
188 | 0 | return -1; |
189 | 0 | } |
190 | | |
191 | 0 | controlByte = pSrcData[used++]; |
192 | 0 | nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte); |
193 | 0 | cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte); |
194 | |
|
195 | 0 | if (nRunLength == 1) |
196 | 0 | { |
197 | 0 | nRunLength = cRawBytes + 16; |
198 | 0 | cRawBytes = 0; |
199 | 0 | } |
200 | 0 | else if (nRunLength == 2) |
201 | 0 | { |
202 | 0 | nRunLength = cRawBytes + 32; |
203 | 0 | cRawBytes = 0; |
204 | 0 | } |
205 | |
|
206 | 0 | used += cRawBytes; |
207 | 0 | x += cRawBytes; |
208 | 0 | x += nRunLength; |
209 | |
|
210 | 0 | if (x > nWidth) |
211 | 0 | { |
212 | 0 | WLog_ERR(TAG, "planar plane x %" PRIu32 " exceeds width %" PRIu32, x, nWidth); |
213 | 0 | return -1; |
214 | 0 | } |
215 | | |
216 | 0 | if (used > SrcSize) |
217 | 0 | { |
218 | 0 | WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, |
219 | 0 | INT32_MAX); |
220 | 0 | return -1; |
221 | 0 | } |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | 0 | if (used > INT32_MAX) |
226 | 0 | { |
227 | 0 | WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, SrcSize); |
228 | 0 | return -1; |
229 | 0 | } |
230 | 0 | return (INT32)used; |
231 | 0 | } |
232 | | |
233 | | static INLINE INT32 planar_decompress_plane_rle_only(const BYTE* WINPR_RESTRICT pSrcData, |
234 | | UINT32 SrcSize, BYTE* WINPR_RESTRICT pDstData, |
235 | | UINT32 nWidth, UINT32 nHeight) |
236 | 0 | { |
237 | 0 | UINT32 pixel = 0; |
238 | 0 | UINT32 cRawBytes = 0; |
239 | 0 | UINT32 nRunLength = 0; |
240 | 0 | INT32 deltaValue = 0; |
241 | 0 | BYTE controlByte = 0; |
242 | 0 | BYTE* currentScanline = NULL; |
243 | 0 | BYTE* previousScanline = NULL; |
244 | 0 | const BYTE* srcp = pSrcData; |
245 | |
|
246 | 0 | WINPR_ASSERT(nHeight <= INT32_MAX); |
247 | 0 | WINPR_ASSERT(nWidth <= INT32_MAX); |
248 | | |
249 | 0 | previousScanline = NULL; |
250 | |
|
251 | 0 | for (INT32 y = 0; y < (INT32)nHeight; y++) |
252 | 0 | { |
253 | 0 | BYTE* dstp = &pDstData[(1ULL * (y) * (INT32)nWidth)]; |
254 | 0 | pixel = 0; |
255 | 0 | currentScanline = dstp; |
256 | |
|
257 | 0 | for (INT32 x = 0; x < (INT32)nWidth;) |
258 | 0 | { |
259 | 0 | controlByte = *srcp; |
260 | 0 | srcp++; |
261 | |
|
262 | 0 | if ((srcp - pSrcData) > SrcSize * 1ll) |
263 | 0 | { |
264 | 0 | WLog_ERR(TAG, "error reading input buffer"); |
265 | 0 | return -1; |
266 | 0 | } |
267 | | |
268 | 0 | nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte); |
269 | 0 | cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte); |
270 | |
|
271 | 0 | if (nRunLength == 1) |
272 | 0 | { |
273 | 0 | nRunLength = cRawBytes + 16; |
274 | 0 | cRawBytes = 0; |
275 | 0 | } |
276 | 0 | else if (nRunLength == 2) |
277 | 0 | { |
278 | 0 | nRunLength = cRawBytes + 32; |
279 | 0 | cRawBytes = 0; |
280 | 0 | } |
281 | |
|
282 | 0 | if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 1ll) |
283 | 0 | { |
284 | 0 | WLog_ERR(TAG, "too many pixels in scanline"); |
285 | 0 | return -1; |
286 | 0 | } |
287 | | |
288 | 0 | if (!previousScanline) |
289 | 0 | { |
290 | | /* first scanline, absolute values */ |
291 | 0 | while (cRawBytes > 0) |
292 | 0 | { |
293 | 0 | pixel = *srcp; |
294 | 0 | srcp++; |
295 | 0 | *dstp = pixel; |
296 | 0 | dstp++; |
297 | 0 | x++; |
298 | 0 | cRawBytes--; |
299 | 0 | } |
300 | |
|
301 | 0 | while (nRunLength > 0) |
302 | 0 | { |
303 | 0 | *dstp = pixel; |
304 | 0 | dstp++; |
305 | 0 | x++; |
306 | 0 | nRunLength--; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | else |
310 | 0 | { |
311 | | /* delta values relative to previous scanline */ |
312 | 0 | while (cRawBytes > 0) |
313 | 0 | { |
314 | 0 | deltaValue = *srcp; |
315 | 0 | srcp++; |
316 | |
|
317 | 0 | if (deltaValue & 1) |
318 | 0 | { |
319 | 0 | deltaValue = deltaValue >> 1; |
320 | 0 | deltaValue = deltaValue + 1; |
321 | 0 | pixel = -deltaValue; |
322 | 0 | } |
323 | 0 | else |
324 | 0 | { |
325 | 0 | deltaValue = deltaValue >> 1; |
326 | 0 | pixel = deltaValue; |
327 | 0 | } |
328 | |
|
329 | 0 | deltaValue = previousScanline[x] + pixel; |
330 | 0 | *dstp = deltaValue; |
331 | 0 | dstp++; |
332 | 0 | x++; |
333 | 0 | cRawBytes--; |
334 | 0 | } |
335 | |
|
336 | 0 | while (nRunLength > 0) |
337 | 0 | { |
338 | 0 | deltaValue = previousScanline[x] + pixel; |
339 | 0 | *dstp = deltaValue; |
340 | 0 | dstp++; |
341 | 0 | x++; |
342 | 0 | nRunLength--; |
343 | 0 | } |
344 | 0 | } |
345 | 0 | } |
346 | | |
347 | 0 | previousScanline = currentScanline; |
348 | 0 | } |
349 | | |
350 | 0 | return (INT32)(srcp - pSrcData); |
351 | 0 | } |
352 | | |
353 | | static INLINE INT32 planar_decompress_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, |
354 | | BYTE* WINPR_RESTRICT pDstData, INT32 nDstStep, |
355 | | UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, |
356 | | UINT32 nHeight, UINT32 nChannel, BOOL vFlip) |
357 | 0 | { |
358 | 0 | UINT32 pixel = 0; |
359 | 0 | UINT32 cRawBytes = 0; |
360 | 0 | UINT32 nRunLength = 0; |
361 | 0 | INT32 deltaValue = 0; |
362 | 0 | INT32 beg = 0; |
363 | 0 | INT32 end = 0; |
364 | 0 | INT32 inc = 0; |
365 | 0 | BYTE controlByte = 0; |
366 | 0 | BYTE* currentScanline = NULL; |
367 | 0 | BYTE* previousScanline = NULL; |
368 | 0 | const BYTE* srcp = pSrcData; |
369 | |
|
370 | 0 | WINPR_ASSERT(nHeight <= INT32_MAX); |
371 | 0 | WINPR_ASSERT(nWidth <= INT32_MAX); |
372 | 0 | WINPR_ASSERT(nDstStep <= INT32_MAX); |
373 | | |
374 | 0 | previousScanline = NULL; |
375 | |
|
376 | 0 | if (vFlip) |
377 | 0 | { |
378 | 0 | beg = (INT32)nHeight - 1; |
379 | 0 | end = -1; |
380 | 0 | inc = -1; |
381 | 0 | } |
382 | 0 | else |
383 | 0 | { |
384 | 0 | beg = 0; |
385 | 0 | end = (INT32)nHeight; |
386 | 0 | inc = 1; |
387 | 0 | } |
388 | |
|
389 | 0 | for (INT32 y = beg; y != end; y += inc) |
390 | 0 | { |
391 | 0 | BYTE* dstp = &pDstData[((nYDst + y) * nDstStep) + (nXDst * 4) + nChannel]; |
392 | 0 | pixel = 0; |
393 | 0 | currentScanline = dstp; |
394 | |
|
395 | 0 | for (INT32 x = 0; x < (INT32)nWidth;) |
396 | 0 | { |
397 | 0 | controlByte = *srcp; |
398 | 0 | srcp++; |
399 | |
|
400 | 0 | if ((srcp - pSrcData) > SrcSize * 1ll) |
401 | 0 | { |
402 | 0 | WLog_ERR(TAG, "error reading input buffer"); |
403 | 0 | return -1; |
404 | 0 | } |
405 | | |
406 | 0 | nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte); |
407 | 0 | cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte); |
408 | |
|
409 | 0 | if (nRunLength == 1) |
410 | 0 | { |
411 | 0 | nRunLength = cRawBytes + 16; |
412 | 0 | cRawBytes = 0; |
413 | 0 | } |
414 | 0 | else if (nRunLength == 2) |
415 | 0 | { |
416 | 0 | nRunLength = cRawBytes + 32; |
417 | 0 | cRawBytes = 0; |
418 | 0 | } |
419 | |
|
420 | 0 | if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 4ll) |
421 | 0 | { |
422 | 0 | WLog_ERR(TAG, "too many pixels in scanline"); |
423 | 0 | return -1; |
424 | 0 | } |
425 | | |
426 | 0 | if (!previousScanline) |
427 | 0 | { |
428 | | /* first scanline, absolute values */ |
429 | 0 | while (cRawBytes > 0) |
430 | 0 | { |
431 | 0 | pixel = *srcp; |
432 | 0 | srcp++; |
433 | 0 | *dstp = pixel; |
434 | 0 | dstp += 4; |
435 | 0 | x++; |
436 | 0 | cRawBytes--; |
437 | 0 | } |
438 | |
|
439 | 0 | while (nRunLength > 0) |
440 | 0 | { |
441 | 0 | *dstp = pixel; |
442 | 0 | dstp += 4; |
443 | 0 | x++; |
444 | 0 | nRunLength--; |
445 | 0 | } |
446 | 0 | } |
447 | 0 | else |
448 | 0 | { |
449 | | /* delta values relative to previous scanline */ |
450 | 0 | while (cRawBytes > 0) |
451 | 0 | { |
452 | 0 | deltaValue = *srcp; |
453 | 0 | srcp++; |
454 | |
|
455 | 0 | if (deltaValue & 1) |
456 | 0 | { |
457 | 0 | deltaValue = deltaValue >> 1; |
458 | 0 | deltaValue = deltaValue + 1; |
459 | 0 | pixel = -deltaValue; |
460 | 0 | } |
461 | 0 | else |
462 | 0 | { |
463 | 0 | deltaValue = deltaValue >> 1; |
464 | 0 | pixel = deltaValue; |
465 | 0 | } |
466 | |
|
467 | 0 | deltaValue = previousScanline[4LL * x] + pixel; |
468 | 0 | *dstp = deltaValue; |
469 | 0 | dstp += 4; |
470 | 0 | x++; |
471 | 0 | cRawBytes--; |
472 | 0 | } |
473 | |
|
474 | 0 | while (nRunLength > 0) |
475 | 0 | { |
476 | 0 | deltaValue = previousScanline[4LL * x] + pixel; |
477 | 0 | *dstp = deltaValue; |
478 | 0 | dstp += 4; |
479 | 0 | x++; |
480 | 0 | nRunLength--; |
481 | 0 | } |
482 | 0 | } |
483 | 0 | } |
484 | | |
485 | 0 | previousScanline = currentScanline; |
486 | 0 | } |
487 | | |
488 | 0 | return (INT32)(srcp - pSrcData); |
489 | 0 | } |
490 | | |
491 | | static INLINE INT32 planar_set_plane(BYTE bValue, BYTE* pDstData, INT32 nDstStep, UINT32 nXDst, |
492 | | UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 nChannel, |
493 | | BOOL vFlip) |
494 | 0 | { |
495 | 0 | INT32 beg = 0; |
496 | 0 | INT32 end = 0; |
497 | 0 | INT32 inc = 0; |
498 | |
|
499 | 0 | WINPR_ASSERT(nHeight <= INT32_MAX); |
500 | 0 | WINPR_ASSERT(nWidth <= INT32_MAX); |
501 | 0 | WINPR_ASSERT(nDstStep <= INT32_MAX); |
502 | | |
503 | 0 | if (vFlip) |
504 | 0 | { |
505 | 0 | beg = (INT32)nHeight - 1; |
506 | 0 | end = -1; |
507 | 0 | inc = -1; |
508 | 0 | } |
509 | 0 | else |
510 | 0 | { |
511 | 0 | beg = 0; |
512 | 0 | end = (INT32)nHeight; |
513 | 0 | inc = 1; |
514 | 0 | } |
515 | |
|
516 | 0 | for (INT32 y = beg; y != end; y += inc) |
517 | 0 | { |
518 | 0 | BYTE* dstp = &pDstData[((nYDst + y) * nDstStep) + (nXDst * 4) + nChannel]; |
519 | |
|
520 | 0 | for (INT32 x = 0; x < (INT32)nWidth; ++x) |
521 | 0 | { |
522 | 0 | *dstp = bValue; |
523 | 0 | dstp += 4; |
524 | 0 | } |
525 | 0 | } |
526 | |
|
527 | 0 | return 0; |
528 | 0 | } |
529 | | |
530 | | static INLINE BOOL writeLine(BYTE** WINPR_RESTRICT ppRgba, UINT32 DstFormat, UINT32 width, |
531 | | const BYTE** WINPR_RESTRICT ppR, const BYTE** WINPR_RESTRICT ppG, |
532 | | const BYTE** WINPR_RESTRICT ppB, const BYTE** WINPR_RESTRICT ppA) |
533 | 0 | { |
534 | 0 | WINPR_ASSERT(ppRgba); |
535 | 0 | WINPR_ASSERT(ppR); |
536 | 0 | WINPR_ASSERT(ppG); |
537 | 0 | WINPR_ASSERT(ppB); |
538 | | |
539 | 0 | switch (DstFormat) |
540 | 0 | { |
541 | 0 | case PIXEL_FORMAT_BGRA32: |
542 | 0 | for (UINT32 x = 0; x < width; x++) |
543 | 0 | { |
544 | 0 | *(*ppRgba)++ = *(*ppB)++; |
545 | 0 | *(*ppRgba)++ = *(*ppG)++; |
546 | 0 | *(*ppRgba)++ = *(*ppR)++; |
547 | 0 | *(*ppRgba)++ = *(*ppA)++; |
548 | 0 | } |
549 | |
|
550 | 0 | return TRUE; |
551 | | |
552 | 0 | case PIXEL_FORMAT_BGRX32: |
553 | 0 | for (UINT32 x = 0; x < width; x++) |
554 | 0 | { |
555 | 0 | *(*ppRgba)++ = *(*ppB)++; |
556 | 0 | *(*ppRgba)++ = *(*ppG)++; |
557 | 0 | *(*ppRgba)++ = *(*ppR)++; |
558 | 0 | *(*ppRgba)++ = 0xFF; |
559 | 0 | } |
560 | |
|
561 | 0 | return TRUE; |
562 | | |
563 | 0 | default: |
564 | 0 | if (ppA) |
565 | 0 | { |
566 | 0 | for (UINT32 x = 0; x < width; x++) |
567 | 0 | { |
568 | 0 | BYTE alpha = *(*ppA)++; |
569 | 0 | UINT32 color = |
570 | 0 | FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha); |
571 | 0 | FreeRDPWriteColor(*ppRgba, DstFormat, color); |
572 | 0 | *ppRgba += FreeRDPGetBytesPerPixel(DstFormat); |
573 | 0 | } |
574 | 0 | } |
575 | 0 | else |
576 | 0 | { |
577 | 0 | const BYTE alpha = 0xFF; |
578 | |
|
579 | 0 | for (UINT32 x = 0; x < width; x++) |
580 | 0 | { |
581 | 0 | UINT32 color = |
582 | 0 | FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha); |
583 | 0 | FreeRDPWriteColor(*ppRgba, DstFormat, color); |
584 | 0 | *ppRgba += FreeRDPGetBytesPerPixel(DstFormat); |
585 | 0 | } |
586 | 0 | } |
587 | |
|
588 | 0 | return TRUE; |
589 | 0 | } |
590 | 0 | } |
591 | | |
592 | | static INLINE BOOL planar_decompress_planes_raw(const BYTE* WINPR_RESTRICT pSrcData[4], |
593 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
594 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, |
595 | | UINT32 nWidth, UINT32 nHeight, BOOL vFlip, |
596 | | UINT32 totalHeight) |
597 | 0 | { |
598 | 0 | INT32 beg = 0; |
599 | 0 | INT32 end = 0; |
600 | 0 | INT32 inc = 0; |
601 | 0 | const BYTE* pR = pSrcData[0]; |
602 | 0 | const BYTE* pG = pSrcData[1]; |
603 | 0 | const BYTE* pB = pSrcData[2]; |
604 | 0 | const BYTE* pA = pSrcData[3]; |
605 | 0 | const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat); |
606 | |
|
607 | 0 | if (vFlip) |
608 | 0 | { |
609 | 0 | beg = nHeight - 1; |
610 | 0 | end = -1; |
611 | 0 | inc = -1; |
612 | 0 | } |
613 | 0 | else |
614 | 0 | { |
615 | 0 | beg = 0; |
616 | 0 | end = nHeight; |
617 | 0 | inc = 1; |
618 | 0 | } |
619 | |
|
620 | 0 | if (nYDst + nHeight > totalHeight) |
621 | 0 | { |
622 | 0 | WLog_ERR(TAG, |
623 | 0 | "planar plane destination Y %" PRIu32 " + height %" PRIu32 |
624 | 0 | " exceeds totalHeight %" PRIu32, |
625 | 0 | nYDst, nHeight, totalHeight); |
626 | 0 | return FALSE; |
627 | 0 | } |
628 | | |
629 | 0 | if ((nXDst + nWidth) * bpp > nDstStep) |
630 | 0 | { |
631 | 0 | WLog_ERR(TAG, |
632 | 0 | "planar plane destination (X %" PRIu32 " + width %" PRIu32 ") * bpp %" PRIu32 |
633 | 0 | " exceeds stride %" PRIu32, |
634 | 0 | nXDst, nWidth, bpp, nDstStep); |
635 | 0 | return FALSE; |
636 | 0 | } |
637 | | |
638 | 0 | for (INT32 y = beg; y != end; y += inc) |
639 | 0 | { |
640 | 0 | BYTE* pRGB = NULL; |
641 | |
|
642 | 0 | if (y > (INT64)nHeight) |
643 | 0 | { |
644 | 0 | WLog_ERR(TAG, "planar plane destination Y %" PRId32 " exceeds height %" PRIu32, y, |
645 | 0 | nHeight); |
646 | 0 | return FALSE; |
647 | 0 | } |
648 | | |
649 | 0 | pRGB = &pDstData[((nYDst + y) * nDstStep) + (nXDst * bpp)]; |
650 | |
|
651 | 0 | if (!writeLine(&pRGB, DstFormat, nWidth, &pR, &pG, &pB, &pA)) |
652 | 0 | return FALSE; |
653 | 0 | } |
654 | | |
655 | 0 | return TRUE; |
656 | 0 | } |
657 | | |
658 | | static BOOL planar_subsample_expand(const BYTE* WINPR_RESTRICT plane, size_t planeLength, |
659 | | UINT32 nWidth, UINT32 nHeight, UINT32 nPlaneWidth, |
660 | | UINT32 nPlaneHeight, BYTE* WINPR_RESTRICT deltaPlane) |
661 | 0 | { |
662 | 0 | size_t pos = 0; |
663 | 0 | WINPR_UNUSED(planeLength); |
664 | |
|
665 | 0 | WINPR_ASSERT(plane); |
666 | 0 | WINPR_ASSERT(deltaPlane); |
667 | | |
668 | 0 | if (nWidth > nPlaneWidth * 2) |
669 | 0 | { |
670 | 0 | WLog_ERR(TAG, "planar subsample width %" PRIu32 " > PlaneWidth %" PRIu32 " * 2", nWidth, |
671 | 0 | nPlaneWidth); |
672 | 0 | return FALSE; |
673 | 0 | } |
674 | | |
675 | 0 | if (nHeight > nPlaneHeight * 2) |
676 | 0 | { |
677 | 0 | WLog_ERR(TAG, "planar subsample height %" PRIu32 " > PlaneHeight %" PRIu32 " * 2", nHeight, |
678 | 0 | nPlaneHeight); |
679 | 0 | return FALSE; |
680 | 0 | } |
681 | | |
682 | 0 | for (size_t y = 0; y < nHeight; y++) |
683 | 0 | { |
684 | 0 | const BYTE* src = plane + y / 2 * nPlaneWidth; |
685 | |
|
686 | 0 | for (UINT32 x = 0; x < nWidth; x++) |
687 | 0 | { |
688 | 0 | deltaPlane[pos++] = src[x / 2]; |
689 | 0 | } |
690 | 0 | } |
691 | |
|
692 | 0 | return TRUE; |
693 | 0 | } |
694 | | |
695 | | BOOL planar_decompress(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, |
696 | | const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 nSrcWidth, |
697 | | UINT32 nSrcHeight, BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
698 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth, |
699 | | UINT32 nDstHeight, BOOL vFlip) |
700 | 0 | { |
701 | 0 | BOOL cs = 0; |
702 | 0 | BOOL rle = 0; |
703 | 0 | UINT32 cll = 0; |
704 | 0 | BOOL alpha = 0; |
705 | 0 | BOOL useAlpha = FALSE; |
706 | 0 | INT32 status = 0; |
707 | 0 | const BYTE* srcp = NULL; |
708 | 0 | UINT32 subSize = 0; |
709 | 0 | UINT32 subWidth = 0; |
710 | 0 | UINT32 subHeight = 0; |
711 | 0 | UINT32 planeSize = 0; |
712 | 0 | INT32 rleSizes[4] = { 0, 0, 0, 0 }; |
713 | 0 | UINT32 rawSizes[4]; |
714 | 0 | UINT32 rawWidths[4]; |
715 | 0 | UINT32 rawHeights[4]; |
716 | 0 | BYTE FormatHeader = 0; |
717 | 0 | const BYTE* planes[4] = { 0 }; |
718 | 0 | const UINT32 w = MIN(nSrcWidth, nDstWidth); |
719 | 0 | const UINT32 h = MIN(nSrcHeight, nDstHeight); |
720 | 0 | const primitives_t* prims = primitives_get(); |
721 | |
|
722 | 0 | WINPR_ASSERT(planar); |
723 | 0 | WINPR_ASSERT(prims); |
724 | | |
725 | 0 | if (nDstStep <= 0) |
726 | 0 | nDstStep = nDstWidth * FreeRDPGetBytesPerPixel(DstFormat); |
727 | |
|
728 | 0 | srcp = pSrcData; |
729 | |
|
730 | 0 | if (!pSrcData) |
731 | 0 | { |
732 | 0 | WLog_ERR(TAG, "Invalid argument pSrcData=NULL"); |
733 | 0 | return FALSE; |
734 | 0 | } |
735 | | |
736 | 0 | if (!pDstData) |
737 | 0 | { |
738 | 0 | WLog_ERR(TAG, "Invalid argument pDstData=NULL"); |
739 | 0 | return FALSE; |
740 | 0 | } |
741 | | |
742 | 0 | FormatHeader = *srcp++; |
743 | 0 | cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK); |
744 | 0 | cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) ? TRUE : FALSE; |
745 | 0 | rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) ? TRUE : FALSE; |
746 | 0 | alpha = (FormatHeader & PLANAR_FORMAT_HEADER_NA) ? FALSE : TRUE; |
747 | |
|
748 | 0 | DstFormat = planar_invert_format(planar, alpha, DstFormat); |
749 | |
|
750 | 0 | if (alpha) |
751 | 0 | useAlpha = FreeRDPColorHasAlpha(DstFormat); |
752 | | |
753 | | // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle, |
754 | | // alpha); |
755 | |
|
756 | 0 | if (!cll && cs) |
757 | 0 | { |
758 | 0 | WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data"); |
759 | 0 | return FALSE; /* Chroma subsampling requires YCoCg */ |
760 | 0 | } |
761 | | |
762 | 0 | subWidth = (nSrcWidth / 2) + (nSrcWidth % 2); |
763 | 0 | subHeight = (nSrcHeight / 2) + (nSrcHeight % 2); |
764 | 0 | planeSize = nSrcWidth * nSrcHeight; |
765 | 0 | subSize = subWidth * subHeight; |
766 | |
|
767 | 0 | if (!cs) |
768 | 0 | { |
769 | 0 | rawSizes[0] = planeSize; /* LumaOrRedPlane */ |
770 | 0 | rawWidths[0] = nSrcWidth; |
771 | 0 | rawHeights[0] = nSrcHeight; |
772 | 0 | rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */ |
773 | 0 | rawWidths[1] = nSrcWidth; |
774 | 0 | rawHeights[1] = nSrcHeight; |
775 | 0 | rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */ |
776 | 0 | rawWidths[2] = nSrcWidth; |
777 | 0 | rawHeights[2] = nSrcHeight; |
778 | 0 | rawSizes[3] = planeSize; /* AlphaPlane */ |
779 | 0 | rawWidths[3] = nSrcWidth; |
780 | 0 | rawHeights[3] = nSrcHeight; |
781 | 0 | } |
782 | 0 | else /* Chroma Subsampling */ |
783 | 0 | { |
784 | 0 | rawSizes[0] = planeSize; /* LumaOrRedPlane */ |
785 | 0 | rawWidths[0] = nSrcWidth; |
786 | 0 | rawHeights[0] = nSrcHeight; |
787 | 0 | rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */ |
788 | 0 | rawWidths[1] = subWidth; |
789 | 0 | rawHeights[1] = subHeight; |
790 | 0 | rawSizes[2] = subSize; /* GreenChromaOrBluePlane */ |
791 | 0 | rawWidths[2] = subWidth; |
792 | 0 | rawHeights[2] = subHeight; |
793 | 0 | rawSizes[3] = planeSize; /* AlphaPlane */ |
794 | 0 | rawWidths[3] = nSrcWidth; |
795 | 0 | rawHeights[3] = nSrcHeight; |
796 | 0 | } |
797 | |
|
798 | 0 | const size_t diff = srcp - pSrcData; |
799 | 0 | if (SrcSize < diff) |
800 | 0 | { |
801 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff); |
802 | 0 | return FALSE; |
803 | 0 | } |
804 | | |
805 | 0 | if (!rle) /* RAW */ |
806 | 0 | { |
807 | |
|
808 | 0 | UINT32 base = planeSize * 3; |
809 | 0 | if (cs) |
810 | 0 | base = planeSize + planeSize / 2; |
811 | |
|
812 | 0 | if (alpha) |
813 | 0 | { |
814 | 0 | if ((SrcSize - diff) < (planeSize + base)) |
815 | 0 | { |
816 | 0 | WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff, |
817 | 0 | (planeSize + base)); |
818 | 0 | return FALSE; |
819 | 0 | } |
820 | | |
821 | 0 | planes[3] = srcp; /* AlphaPlane */ |
822 | 0 | planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */ |
823 | 0 | planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */ |
824 | 0 | planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */ |
825 | |
|
826 | 0 | if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize]) |
827 | 0 | { |
828 | 0 | WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2], |
829 | 0 | &pSrcData[SrcSize]); |
830 | 0 | return FALSE; |
831 | 0 | } |
832 | 0 | } |
833 | 0 | else |
834 | 0 | { |
835 | 0 | if ((SrcSize - diff) < base) |
836 | 0 | { |
837 | 0 | WLog_ERR(TAG, "plane size mismatch %" PRIu32 " < %" PRIu32, SrcSize - diff, base); |
838 | 0 | return FALSE; |
839 | 0 | } |
840 | | |
841 | 0 | planes[0] = srcp; /* LumaOrRedPlane */ |
842 | 0 | planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */ |
843 | 0 | planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */ |
844 | |
|
845 | 0 | if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize]) |
846 | 0 | { |
847 | 0 | WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2], |
848 | 0 | &pSrcData[SrcSize]); |
849 | 0 | return FALSE; |
850 | 0 | } |
851 | 0 | } |
852 | 0 | } |
853 | 0 | else /* RLE */ |
854 | 0 | { |
855 | 0 | if (alpha) |
856 | 0 | { |
857 | 0 | planes[3] = srcp; |
858 | 0 | rleSizes[3] = planar_skip_plane_rle(planes[3], SrcSize - diff, rawWidths[3], |
859 | 0 | rawHeights[3]); /* AlphaPlane */ |
860 | |
|
861 | 0 | if (rleSizes[3] < 0) |
862 | 0 | return FALSE; |
863 | | |
864 | 0 | planes[0] = planes[3] + rleSizes[3]; |
865 | 0 | } |
866 | 0 | else |
867 | 0 | planes[0] = srcp; |
868 | | |
869 | 0 | const size_t diff0 = (planes[0] - pSrcData); |
870 | 0 | if (SrcSize < diff0) |
871 | 0 | { |
872 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0); |
873 | 0 | return FALSE; |
874 | 0 | } |
875 | 0 | rleSizes[0] = planar_skip_plane_rle(planes[0], SrcSize - diff0, rawWidths[0], |
876 | 0 | rawHeights[0]); /* RedPlane */ |
877 | |
|
878 | 0 | if (rleSizes[0] < 0) |
879 | 0 | return FALSE; |
880 | | |
881 | 0 | planes[1] = planes[0] + rleSizes[0]; |
882 | |
|
883 | 0 | const size_t diff1 = (planes[1] - pSrcData); |
884 | 0 | if (SrcSize < diff1) |
885 | 0 | { |
886 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1); |
887 | 0 | return FALSE; |
888 | 0 | } |
889 | 0 | rleSizes[1] = planar_skip_plane_rle(planes[1], SrcSize - diff1, rawWidths[1], |
890 | 0 | rawHeights[1]); /* GreenPlane */ |
891 | |
|
892 | 0 | if (rleSizes[1] < 1) |
893 | 0 | return FALSE; |
894 | | |
895 | 0 | planes[2] = planes[1] + rleSizes[1]; |
896 | 0 | const size_t diff2 = (planes[2] - pSrcData); |
897 | 0 | if (SrcSize < diff2) |
898 | 0 | { |
899 | 0 | WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff); |
900 | 0 | return FALSE; |
901 | 0 | } |
902 | 0 | rleSizes[2] = planar_skip_plane_rle(planes[2], SrcSize - diff2, rawWidths[2], |
903 | 0 | rawHeights[2]); /* BluePlane */ |
904 | |
|
905 | 0 | if (rleSizes[2] < 1) |
906 | 0 | return FALSE; |
907 | 0 | } |
908 | | |
909 | 0 | if (!cll) /* RGB */ |
910 | 0 | { |
911 | 0 | UINT32 TempFormat = 0; |
912 | 0 | BYTE* pTempData = pDstData; |
913 | 0 | UINT32 nTempStep = nDstStep; |
914 | 0 | UINT32 nTotalHeight = nYDst + nDstHeight; |
915 | |
|
916 | 0 | if (useAlpha) |
917 | 0 | TempFormat = PIXEL_FORMAT_BGRA32; |
918 | 0 | else |
919 | 0 | TempFormat = PIXEL_FORMAT_BGRX32; |
920 | |
|
921 | 0 | TempFormat = planar_invert_format(planar, alpha, TempFormat); |
922 | |
|
923 | 0 | if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight)) |
924 | 0 | { |
925 | 0 | pTempData = planar->pTempData; |
926 | 0 | nTempStep = planar->nTempStep; |
927 | 0 | nTotalHeight = planar->maxHeight; |
928 | 0 | } |
929 | |
|
930 | 0 | if (!rle) /* RAW */ |
931 | 0 | { |
932 | 0 | if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst, |
933 | 0 | nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight)) |
934 | 0 | return FALSE; |
935 | | |
936 | 0 | if (alpha) |
937 | 0 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3]; |
938 | 0 | else /* NoAlpha */ |
939 | 0 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2]; |
940 | |
|
941 | 0 | if ((SrcSize - (srcp - pSrcData)) == 1) |
942 | 0 | srcp++; /* pad */ |
943 | 0 | } |
944 | 0 | else /* RLE */ |
945 | 0 | { |
946 | 0 | status = |
947 | 0 | planar_decompress_plane_rle(planes[0], rleSizes[0], pTempData, nTempStep, nXDst, |
948 | 0 | nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */ |
949 | |
|
950 | 0 | if (status < 0) |
951 | 0 | return FALSE; |
952 | | |
953 | 0 | status = planar_decompress_plane_rle(planes[1], rleSizes[1], pTempData, nTempStep, |
954 | 0 | nXDst, nYDst, nSrcWidth, nSrcHeight, 1, |
955 | 0 | vFlip); /* GreenPlane */ |
956 | |
|
957 | 0 | if (status < 0) |
958 | 0 | return FALSE; |
959 | | |
960 | 0 | status = |
961 | 0 | planar_decompress_plane_rle(planes[2], rleSizes[2], pTempData, nTempStep, nXDst, |
962 | 0 | nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */ |
963 | |
|
964 | 0 | if (status < 0) |
965 | 0 | return FALSE; |
966 | | |
967 | 0 | srcp += rleSizes[0] + rleSizes[1] + rleSizes[2]; |
968 | |
|
969 | 0 | if (useAlpha) |
970 | 0 | { |
971 | 0 | status = planar_decompress_plane_rle(planes[3], rleSizes[3], pTempData, nTempStep, |
972 | 0 | nXDst, nYDst, nSrcWidth, nSrcHeight, 3, |
973 | 0 | vFlip); /* AlphaPlane */ |
974 | 0 | } |
975 | 0 | else |
976 | 0 | status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth, |
977 | 0 | nSrcHeight, 3, vFlip); |
978 | |
|
979 | 0 | if (status < 0) |
980 | 0 | return FALSE; |
981 | | |
982 | 0 | if (alpha) |
983 | 0 | srcp += rleSizes[3]; |
984 | 0 | } |
985 | | |
986 | 0 | if (pTempData != pDstData) |
987 | 0 | { |
988 | 0 | if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h, |
989 | 0 | pTempData, TempFormat, nTempStep, nXDst, nYDst, NULL, |
990 | 0 | FREERDP_FLIP_NONE)) |
991 | 0 | { |
992 | 0 | WLog_ERR(TAG, "planar image copy failed"); |
993 | 0 | return FALSE; |
994 | 0 | } |
995 | 0 | } |
996 | 0 | } |
997 | 0 | else /* YCoCg */ |
998 | 0 | { |
999 | 0 | UINT32 TempFormat = 0; |
1000 | 0 | BYTE* pTempData = planar->pTempData; |
1001 | 0 | UINT32 nTempStep = planar->nTempStep; |
1002 | 0 | UINT32 nTotalHeight = planar->maxHeight; |
1003 | 0 | BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep]; |
1004 | |
|
1005 | 0 | if (useAlpha) |
1006 | 0 | TempFormat = PIXEL_FORMAT_BGRA32; |
1007 | 0 | else |
1008 | 0 | TempFormat = PIXEL_FORMAT_BGRX32; |
1009 | |
|
1010 | 0 | if (!pTempData) |
1011 | 0 | return FALSE; |
1012 | | |
1013 | 0 | if (rle) /* RLE encoded data. Decode and handle it like raw data. */ |
1014 | 0 | { |
1015 | 0 | BYTE* rleBuffer[4] = { 0 }; |
1016 | |
|
1017 | 0 | if (!planar->rlePlanesBuffer) |
1018 | 0 | return FALSE; |
1019 | | |
1020 | 0 | rleBuffer[3] = planar->rlePlanesBuffer; /* AlphaPlane */ |
1021 | 0 | rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */ |
1022 | 0 | rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */ |
1023 | 0 | rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */ |
1024 | 0 | if (useAlpha) |
1025 | 0 | { |
1026 | 0 | status = |
1027 | 0 | planar_decompress_plane_rle_only(planes[3], rleSizes[3], rleBuffer[3], |
1028 | 0 | rawWidths[3], rawHeights[3]); /* AlphaPlane */ |
1029 | |
|
1030 | 0 | if (status < 0) |
1031 | 0 | return FALSE; |
1032 | 0 | } |
1033 | | |
1034 | 0 | if (alpha) |
1035 | 0 | srcp += rleSizes[3]; |
1036 | |
|
1037 | 0 | status = planar_decompress_plane_rle_only(planes[0], rleSizes[0], rleBuffer[0], |
1038 | 0 | rawWidths[0], rawHeights[0]); /* LumaPlane */ |
1039 | |
|
1040 | 0 | if (status < 0) |
1041 | 0 | return FALSE; |
1042 | | |
1043 | 0 | status = |
1044 | 0 | planar_decompress_plane_rle_only(planes[1], rleSizes[1], rleBuffer[1], rawWidths[1], |
1045 | 0 | rawHeights[1]); /* OrangeChromaPlane */ |
1046 | |
|
1047 | 0 | if (status < 0) |
1048 | 0 | return FALSE; |
1049 | | |
1050 | 0 | status = |
1051 | 0 | planar_decompress_plane_rle_only(planes[2], rleSizes[2], rleBuffer[2], rawWidths[2], |
1052 | 0 | rawHeights[2]); /* GreenChromaPlane */ |
1053 | |
|
1054 | 0 | if (status < 0) |
1055 | 0 | return FALSE; |
1056 | | |
1057 | 0 | planes[0] = rleBuffer[0]; |
1058 | 0 | planes[1] = rleBuffer[1]; |
1059 | 0 | planes[2] = rleBuffer[2]; |
1060 | 0 | planes[3] = rleBuffer[3]; |
1061 | 0 | } |
1062 | | |
1063 | | /* RAW */ |
1064 | 0 | { |
1065 | 0 | if (cs) |
1066 | 0 | { /* Chroma subsampling for Co and Cg: |
1067 | | * Each pixel contains the value that should be expanded to |
1068 | | * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */ |
1069 | 0 | if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight, |
1070 | 0 | rawWidths[1], rawHeights[1], planar->deltaPlanes[0])) |
1071 | 0 | return FALSE; |
1072 | | |
1073 | 0 | planes[1] = planar->deltaPlanes[0]; |
1074 | 0 | rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */ |
1075 | 0 | rawWidths[1] = nSrcWidth; |
1076 | 0 | rawHeights[1] = nSrcHeight; |
1077 | |
|
1078 | 0 | if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight, |
1079 | 0 | rawWidths[2], rawHeights[2], planar->deltaPlanes[1])) |
1080 | 0 | return FALSE; |
1081 | | |
1082 | 0 | planes[2] = planar->deltaPlanes[1]; |
1083 | 0 | rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */ |
1084 | 0 | rawWidths[2] = nSrcWidth; |
1085 | 0 | rawHeights[2] = nSrcHeight; |
1086 | 0 | } |
1087 | | |
1088 | 0 | if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst, |
1089 | 0 | nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight)) |
1090 | 0 | return FALSE; |
1091 | | |
1092 | 0 | if (alpha) |
1093 | 0 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3]; |
1094 | 0 | else /* NoAlpha */ |
1095 | 0 | srcp += rawSizes[0] + rawSizes[1] + rawSizes[2]; |
1096 | |
|
1097 | 0 | if ((SrcSize - (srcp - pSrcData)) == 1) |
1098 | 0 | srcp++; /* pad */ |
1099 | 0 | } |
1100 | | |
1101 | 0 | WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R); |
1102 | 0 | int rc = prims->YCoCgToRGB_8u_AC4R(pTempData, nTempStep, dst, DstFormat, nDstStep, w, h, |
1103 | 0 | cll, useAlpha); |
1104 | 0 | if (rc != PRIMITIVES_SUCCESS) |
1105 | 0 | { |
1106 | 0 | WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc); |
1107 | 0 | return FALSE; |
1108 | 0 | } |
1109 | 0 | } |
1110 | | |
1111 | 0 | WINPR_UNUSED(srcp); |
1112 | 0 | return TRUE; |
1113 | 0 | } |
1114 | | |
1115 | | static INLINE BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, |
1116 | | const BYTE* WINPR_RESTRICT data, UINT32 format, |
1117 | | UINT32 width, UINT32 height, UINT32 scanline, |
1118 | | BYTE* WINPR_RESTRICT planes[4]) |
1119 | 0 | { |
1120 | 0 | WINPR_ASSERT(planar); |
1121 | | |
1122 | 0 | if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX)) |
1123 | 0 | return FALSE; |
1124 | | |
1125 | 0 | if (scanline == 0) |
1126 | 0 | scanline = width * FreeRDPGetBytesPerPixel(format); |
1127 | |
|
1128 | 0 | if (planar->topdown) |
1129 | 0 | { |
1130 | 0 | UINT32 k = 0; |
1131 | 0 | for (UINT32 i = 0; i < height; i++) |
1132 | 0 | { |
1133 | 0 | const BYTE* pixel = &data[1ULL * scanline * i]; |
1134 | |
|
1135 | 0 | for (UINT32 j = 0; j < width; j++) |
1136 | 0 | { |
1137 | 0 | const UINT32 color = FreeRDPReadColor(pixel, format); |
1138 | 0 | pixel += FreeRDPGetBytesPerPixel(format); |
1139 | 0 | FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k], |
1140 | 0 | &planes[0][k], NULL); |
1141 | 0 | k++; |
1142 | 0 | } |
1143 | 0 | } |
1144 | 0 | } |
1145 | 0 | else |
1146 | 0 | { |
1147 | 0 | UINT32 k = 0; |
1148 | |
|
1149 | 0 | for (INT64 i = (INT64)height - 1; i >= 0; i--) |
1150 | 0 | { |
1151 | 0 | const BYTE* pixel = &data[1ULL * scanline * (UINT32)i]; |
1152 | |
|
1153 | 0 | for (UINT32 j = 0; j < width; j++) |
1154 | 0 | { |
1155 | 0 | const UINT32 color = FreeRDPReadColor(pixel, format); |
1156 | 0 | pixel += FreeRDPGetBytesPerPixel(format); |
1157 | 0 | FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k], |
1158 | 0 | &planes[0][k], NULL); |
1159 | 0 | k++; |
1160 | 0 | } |
1161 | 0 | } |
1162 | 0 | } |
1163 | 0 | return TRUE; |
1164 | 0 | } |
1165 | | |
1166 | | static INLINE UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer, |
1167 | | UINT32 cRawBytes, UINT32 nRunLength, |
1168 | | BYTE* WINPR_RESTRICT pOutBuffer, |
1169 | | UINT32 outBufferSize) |
1170 | 0 | { |
1171 | 0 | const BYTE* pInput = NULL; |
1172 | 0 | BYTE* pOutput = NULL; |
1173 | 0 | BYTE controlByte = 0; |
1174 | 0 | UINT32 nBytesToWrite = 0; |
1175 | 0 | pInput = pInBuffer; |
1176 | 0 | pOutput = pOutBuffer; |
1177 | |
|
1178 | 0 | if (!cRawBytes && !nRunLength) |
1179 | 0 | return 0; |
1180 | | |
1181 | 0 | if (nRunLength < 3) |
1182 | 0 | { |
1183 | 0 | cRawBytes += nRunLength; |
1184 | 0 | nRunLength = 0; |
1185 | 0 | } |
1186 | |
|
1187 | 0 | while (cRawBytes) |
1188 | 0 | { |
1189 | 0 | if (cRawBytes < 16) |
1190 | 0 | { |
1191 | 0 | if (nRunLength > 15) |
1192 | 0 | { |
1193 | 0 | if (nRunLength < 18) |
1194 | 0 | { |
1195 | 0 | controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes); |
1196 | 0 | nRunLength -= 13; |
1197 | 0 | cRawBytes = 0; |
1198 | 0 | } |
1199 | 0 | else |
1200 | 0 | { |
1201 | 0 | controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes); |
1202 | 0 | nRunLength -= 15; |
1203 | 0 | cRawBytes = 0; |
1204 | 0 | } |
1205 | 0 | } |
1206 | 0 | else |
1207 | 0 | { |
1208 | 0 | controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes); |
1209 | 0 | nRunLength = 0; |
1210 | 0 | cRawBytes = 0; |
1211 | 0 | } |
1212 | 0 | } |
1213 | 0 | else |
1214 | 0 | { |
1215 | 0 | controlByte = PLANAR_CONTROL_BYTE(0, 15); |
1216 | 0 | cRawBytes -= 15; |
1217 | 0 | } |
1218 | |
|
1219 | 0 | if (outBufferSize < 1) |
1220 | 0 | return 0; |
1221 | | |
1222 | 0 | outBufferSize--; |
1223 | 0 | *pOutput = controlByte; |
1224 | 0 | pOutput++; |
1225 | 0 | nBytesToWrite = (controlByte >> 4); |
1226 | |
|
1227 | 0 | if (nBytesToWrite) |
1228 | 0 | { |
1229 | 0 | if (outBufferSize < nBytesToWrite) |
1230 | 0 | return 0; |
1231 | | |
1232 | 0 | outBufferSize -= nBytesToWrite; |
1233 | 0 | CopyMemory(pOutput, pInput, nBytesToWrite); |
1234 | 0 | pOutput += nBytesToWrite; |
1235 | 0 | pInput += nBytesToWrite; |
1236 | 0 | } |
1237 | 0 | } |
1238 | | |
1239 | 0 | while (nRunLength) |
1240 | 0 | { |
1241 | 0 | if (nRunLength > 47) |
1242 | 0 | { |
1243 | 0 | if (nRunLength < 50) |
1244 | 0 | { |
1245 | 0 | controlByte = PLANAR_CONTROL_BYTE(2, 13); |
1246 | 0 | nRunLength -= 45; |
1247 | 0 | } |
1248 | 0 | else |
1249 | 0 | { |
1250 | 0 | controlByte = PLANAR_CONTROL_BYTE(2, 15); |
1251 | 0 | nRunLength -= 47; |
1252 | 0 | } |
1253 | 0 | } |
1254 | 0 | else if (nRunLength > 31) |
1255 | 0 | { |
1256 | 0 | controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32)); |
1257 | 0 | nRunLength = 0; |
1258 | 0 | } |
1259 | 0 | else if (nRunLength > 15) |
1260 | 0 | { |
1261 | 0 | controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16)); |
1262 | 0 | nRunLength = 0; |
1263 | 0 | } |
1264 | 0 | else |
1265 | 0 | { |
1266 | 0 | controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0); |
1267 | 0 | nRunLength = 0; |
1268 | 0 | } |
1269 | |
|
1270 | 0 | if (outBufferSize < 1) |
1271 | 0 | return 0; |
1272 | | |
1273 | 0 | --outBufferSize; |
1274 | 0 | *pOutput = controlByte; |
1275 | 0 | pOutput++; |
1276 | 0 | } |
1277 | | |
1278 | 0 | return (pOutput - pOutBuffer); |
1279 | 0 | } |
1280 | | |
1281 | | static INLINE UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer, |
1282 | | UINT32 inBufferSize, |
1283 | | BYTE* WINPR_RESTRICT pOutBuffer, |
1284 | | UINT32 outBufferSize) |
1285 | 0 | { |
1286 | 0 | BYTE symbol = 0; |
1287 | 0 | const BYTE* pInput = NULL; |
1288 | 0 | BYTE* pOutput = NULL; |
1289 | 0 | const BYTE* pBytes = NULL; |
1290 | 0 | UINT32 cRawBytes = 0; |
1291 | 0 | UINT32 nRunLength = 0; |
1292 | 0 | UINT32 bSymbolMatch = 0; |
1293 | 0 | UINT32 nBytesWritten = 0; |
1294 | 0 | UINT32 nTotalBytesWritten = 0; |
1295 | 0 | symbol = 0; |
1296 | 0 | cRawBytes = 0; |
1297 | 0 | nRunLength = 0; |
1298 | 0 | pInput = pInBuffer; |
1299 | 0 | pOutput = pOutBuffer; |
1300 | 0 | nTotalBytesWritten = 0; |
1301 | |
|
1302 | 0 | if (!outBufferSize) |
1303 | 0 | return 0; |
1304 | | |
1305 | 0 | do |
1306 | 0 | { |
1307 | 0 | if (!inBufferSize) |
1308 | 0 | break; |
1309 | | |
1310 | 0 | bSymbolMatch = (symbol == *pInput) ? TRUE : FALSE; |
1311 | 0 | symbol = *pInput; |
1312 | 0 | pInput++; |
1313 | 0 | inBufferSize--; |
1314 | |
|
1315 | 0 | if (nRunLength && !bSymbolMatch) |
1316 | 0 | { |
1317 | 0 | if (nRunLength < 3) |
1318 | 0 | { |
1319 | 0 | cRawBytes += nRunLength; |
1320 | 0 | nRunLength = 0; |
1321 | 0 | } |
1322 | 0 | else |
1323 | 0 | { |
1324 | 0 | pBytes = pInput - (cRawBytes + nRunLength + 1); |
1325 | 0 | nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength, |
1326 | 0 | pOutput, outBufferSize); |
1327 | 0 | nRunLength = 0; |
1328 | |
|
1329 | 0 | if (!nBytesWritten || (nBytesWritten > outBufferSize)) |
1330 | 0 | return nRunLength; |
1331 | | |
1332 | 0 | nTotalBytesWritten += nBytesWritten; |
1333 | 0 | outBufferSize -= nBytesWritten; |
1334 | 0 | pOutput += nBytesWritten; |
1335 | 0 | cRawBytes = 0; |
1336 | 0 | } |
1337 | 0 | } |
1338 | | |
1339 | 0 | nRunLength += bSymbolMatch; |
1340 | 0 | cRawBytes += (!bSymbolMatch) ? TRUE : FALSE; |
1341 | 0 | } while (outBufferSize); |
1342 | | |
1343 | 0 | if (cRawBytes || nRunLength) |
1344 | 0 | { |
1345 | 0 | pBytes = pInput - (cRawBytes + nRunLength); |
1346 | 0 | nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength, |
1347 | 0 | pOutput, outBufferSize); |
1348 | |
|
1349 | 0 | if (!nBytesWritten) |
1350 | 0 | return 0; |
1351 | | |
1352 | 0 | nTotalBytesWritten += nBytesWritten; |
1353 | 0 | } |
1354 | | |
1355 | 0 | if (inBufferSize) |
1356 | 0 | return 0; |
1357 | | |
1358 | 0 | return nTotalBytesWritten; |
1359 | 0 | } |
1360 | | |
1361 | | BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width, |
1362 | | UINT32 height, BYTE* WINPR_RESTRICT outPlane, |
1363 | | UINT32* WINPR_RESTRICT dstSize) |
1364 | 0 | { |
1365 | 0 | UINT32 index = 0; |
1366 | 0 | const BYTE* pInput = NULL; |
1367 | 0 | BYTE* pOutput = NULL; |
1368 | 0 | UINT32 outBufferSize = 0; |
1369 | 0 | UINT32 nBytesWritten = 0; |
1370 | 0 | UINT32 nTotalBytesWritten = 0; |
1371 | |
|
1372 | 0 | if (!outPlane) |
1373 | 0 | return FALSE; |
1374 | | |
1375 | 0 | index = 0; |
1376 | 0 | pInput = inPlane; |
1377 | 0 | pOutput = outPlane; |
1378 | 0 | outBufferSize = *dstSize; |
1379 | 0 | nTotalBytesWritten = 0; |
1380 | |
|
1381 | 0 | while (outBufferSize) |
1382 | 0 | { |
1383 | 0 | nBytesWritten = |
1384 | 0 | freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize); |
1385 | |
|
1386 | 0 | if ((!nBytesWritten) || (nBytesWritten > outBufferSize)) |
1387 | 0 | return FALSE; |
1388 | | |
1389 | 0 | outBufferSize -= nBytesWritten; |
1390 | 0 | nTotalBytesWritten += nBytesWritten; |
1391 | 0 | pOutput += nBytesWritten; |
1392 | 0 | pInput += width; |
1393 | 0 | index++; |
1394 | |
|
1395 | 0 | if (index >= height) |
1396 | 0 | break; |
1397 | 0 | } |
1398 | | |
1399 | 0 | *dstSize = nTotalBytesWritten; |
1400 | 0 | return TRUE; |
1401 | 0 | } |
1402 | | |
1403 | | static INLINE BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4], |
1404 | | UINT32 width, UINT32 height, |
1405 | | BYTE* WINPR_RESTRICT outPlanes, |
1406 | | UINT32* WINPR_RESTRICT dstSizes, |
1407 | | BOOL skipAlpha) |
1408 | 0 | { |
1409 | 0 | UINT32 outPlanesSize = width * height * 4; |
1410 | | |
1411 | | /* AlphaPlane */ |
1412 | 0 | if (skipAlpha) |
1413 | 0 | { |
1414 | 0 | dstSizes[0] = 0; |
1415 | 0 | } |
1416 | 0 | else |
1417 | 0 | { |
1418 | 0 | dstSizes[0] = outPlanesSize; |
1419 | |
|
1420 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes, |
1421 | 0 | &dstSizes[0])) |
1422 | 0 | return FALSE; |
1423 | | |
1424 | 0 | outPlanes += dstSizes[0]; |
1425 | 0 | outPlanesSize -= dstSizes[0]; |
1426 | 0 | } |
1427 | | |
1428 | | /* LumaOrRedPlane */ |
1429 | 0 | dstSizes[1] = outPlanesSize; |
1430 | |
|
1431 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes, |
1432 | 0 | &dstSizes[1])) |
1433 | 0 | return FALSE; |
1434 | | |
1435 | 0 | outPlanes += dstSizes[1]; |
1436 | 0 | outPlanesSize -= dstSizes[1]; |
1437 | | /* OrangeChromaOrGreenPlane */ |
1438 | 0 | dstSizes[2] = outPlanesSize; |
1439 | |
|
1440 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes, |
1441 | 0 | &dstSizes[2])) |
1442 | 0 | return FALSE; |
1443 | | |
1444 | 0 | outPlanes += dstSizes[2]; |
1445 | 0 | outPlanesSize -= dstSizes[2]; |
1446 | | /* GreenChromeOrBluePlane */ |
1447 | 0 | dstSizes[3] = outPlanesSize; |
1448 | |
|
1449 | 0 | if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes, |
1450 | 0 | &dstSizes[3])) |
1451 | 0 | return FALSE; |
1452 | | |
1453 | 0 | return TRUE; |
1454 | 0 | } |
1455 | | |
1456 | | BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width, |
1457 | | UINT32 height, BYTE* WINPR_RESTRICT outPlane) |
1458 | 0 | { |
1459 | 0 | char s2c = 0; |
1460 | 0 | BYTE* outPtr = NULL; |
1461 | 0 | const BYTE* srcPtr = NULL; |
1462 | 0 | const BYTE* prevLinePtr = NULL; |
1463 | |
|
1464 | 0 | if (!outPlane) |
1465 | 0 | { |
1466 | 0 | if (width * height == 0) |
1467 | 0 | return NULL; |
1468 | | |
1469 | 0 | if (!(outPlane = (BYTE*)calloc(height, width))) |
1470 | 0 | return NULL; |
1471 | 0 | } |
1472 | | |
1473 | | // first line is copied as is |
1474 | 0 | CopyMemory(outPlane, inPlane, width); |
1475 | 0 | outPtr = outPlane + width; |
1476 | 0 | srcPtr = inPlane + width; |
1477 | 0 | prevLinePtr = inPlane; |
1478 | |
|
1479 | 0 | for (UINT32 y = 1; y < height; y++) |
1480 | 0 | { |
1481 | 0 | for (UINT32 x = 0; x < width; x++, outPtr++, srcPtr++, prevLinePtr++) |
1482 | 0 | { |
1483 | 0 | INT32 delta = *srcPtr - *prevLinePtr; |
1484 | 0 | s2c = (delta >= 0) ? (char)delta : (char)(~((BYTE)(-delta)) + 1); |
1485 | 0 | s2c = (s2c >= 0) ? (char)((UINT32)s2c << 1) |
1486 | 0 | : (char)(((UINT32)(~((BYTE)s2c) + 1) << 1) - 1); |
1487 | 0 | *outPtr = (BYTE)s2c; |
1488 | 0 | } |
1489 | 0 | } |
1490 | |
|
1491 | 0 | return outPlane; |
1492 | 0 | } |
1493 | | |
1494 | | static INLINE BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4], |
1495 | | UINT32 width, UINT32 height, |
1496 | | BYTE* WINPR_RESTRICT outPlanes[4]) |
1497 | 0 | { |
1498 | 0 | for (UINT32 i = 0; i < 4; i++) |
1499 | 0 | { |
1500 | 0 | outPlanes[i] = |
1501 | 0 | freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]); |
1502 | |
|
1503 | 0 | if (!outPlanes[i]) |
1504 | 0 | return FALSE; |
1505 | 0 | } |
1506 | | |
1507 | 0 | return TRUE; |
1508 | 0 | } |
1509 | | |
1510 | | BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context, |
1511 | | const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width, |
1512 | | UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData, |
1513 | | UINT32* WINPR_RESTRICT pDstSize) |
1514 | 0 | { |
1515 | 0 | UINT32 size = 0; |
1516 | 0 | BYTE* dstp = NULL; |
1517 | 0 | UINT32 planeSize = 0; |
1518 | 0 | UINT32 dstSizes[4] = { 0 }; |
1519 | 0 | BYTE FormatHeader = 0; |
1520 | |
|
1521 | 0 | if (!context || !context->rlePlanesBuffer) |
1522 | 0 | return NULL; |
1523 | | |
1524 | 0 | if (context->AllowSkipAlpha) |
1525 | 0 | FormatHeader |= PLANAR_FORMAT_HEADER_NA; |
1526 | |
|
1527 | 0 | planeSize = width * height; |
1528 | |
|
1529 | 0 | if (!context->AllowSkipAlpha) |
1530 | 0 | format = planar_invert_format(context, TRUE, format); |
1531 | |
|
1532 | 0 | if (!freerdp_split_color_planes(context, data, format, width, height, scanline, |
1533 | 0 | context->planes)) |
1534 | 0 | return NULL; |
1535 | | |
1536 | 0 | if (context->AllowRunLengthEncoding) |
1537 | 0 | { |
1538 | 0 | if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height, |
1539 | 0 | context->deltaPlanes)) |
1540 | 0 | return NULL; |
1541 | | |
1542 | 0 | if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height, |
1543 | 0 | context->rlePlanesBuffer, dstSizes, |
1544 | 0 | context->AllowSkipAlpha)) |
1545 | 0 | return NULL; |
1546 | | |
1547 | 0 | { |
1548 | 0 | int offset = 0; |
1549 | 0 | FormatHeader |= PLANAR_FORMAT_HEADER_RLE; |
1550 | 0 | context->rlePlanes[0] = &context->rlePlanesBuffer[offset]; |
1551 | 0 | offset += dstSizes[0]; |
1552 | 0 | context->rlePlanes[1] = &context->rlePlanesBuffer[offset]; |
1553 | 0 | offset += dstSizes[1]; |
1554 | 0 | context->rlePlanes[2] = &context->rlePlanesBuffer[offset]; |
1555 | 0 | offset += dstSizes[2]; |
1556 | 0 | context->rlePlanes[3] = &context->rlePlanesBuffer[offset]; |
1557 | |
|
1558 | | #if defined(WITH_DEBUG_CODECS) |
1559 | | WLog_DBG(TAG, |
1560 | | "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32 |
1561 | | " / %" PRIu32 "] ", |
1562 | | dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize); |
1563 | | #endif |
1564 | 0 | } |
1565 | 0 | } |
1566 | | |
1567 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1568 | 0 | { |
1569 | 0 | if (!context->AllowRunLengthEncoding) |
1570 | 0 | return NULL; |
1571 | | |
1572 | 0 | if (context->rlePlanes[0] == NULL) |
1573 | 0 | return NULL; |
1574 | | |
1575 | 0 | if (context->rlePlanes[1] == NULL) |
1576 | 0 | return NULL; |
1577 | | |
1578 | 0 | if (context->rlePlanes[2] == NULL) |
1579 | 0 | return NULL; |
1580 | | |
1581 | 0 | if (context->rlePlanes[3] == NULL) |
1582 | 0 | return NULL; |
1583 | 0 | } |
1584 | | |
1585 | 0 | if (!dstData) |
1586 | 0 | { |
1587 | 0 | size = 1; |
1588 | |
|
1589 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA)) |
1590 | 0 | { |
1591 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1592 | 0 | size += dstSizes[0]; |
1593 | 0 | else |
1594 | 0 | size += planeSize; |
1595 | 0 | } |
1596 | |
|
1597 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1598 | 0 | size += (dstSizes[1] + dstSizes[2] + dstSizes[3]); |
1599 | 0 | else |
1600 | 0 | size += (planeSize * 3); |
1601 | |
|
1602 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE)) |
1603 | 0 | size++; |
1604 | |
|
1605 | 0 | dstData = malloc(size); |
1606 | |
|
1607 | 0 | if (!dstData) |
1608 | 0 | return NULL; |
1609 | | |
1610 | 0 | *pDstSize = size; |
1611 | 0 | } |
1612 | | |
1613 | 0 | dstp = dstData; |
1614 | 0 | *dstp = FormatHeader; /* FormatHeader */ |
1615 | 0 | dstp++; |
1616 | | |
1617 | | /* AlphaPlane */ |
1618 | |
|
1619 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA)) |
1620 | 0 | { |
1621 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1622 | 0 | { |
1623 | 0 | CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */ |
1624 | 0 | dstp += dstSizes[0]; |
1625 | 0 | } |
1626 | 0 | else |
1627 | 0 | { |
1628 | 0 | CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */ |
1629 | 0 | dstp += planeSize; |
1630 | 0 | } |
1631 | 0 | } |
1632 | | |
1633 | | /* LumaOrRedPlane */ |
1634 | |
|
1635 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1636 | 0 | { |
1637 | 0 | CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */ |
1638 | 0 | dstp += dstSizes[1]; |
1639 | 0 | } |
1640 | 0 | else |
1641 | 0 | { |
1642 | 0 | CopyMemory(dstp, context->planes[1], planeSize); /* Red */ |
1643 | 0 | dstp += planeSize; |
1644 | 0 | } |
1645 | | |
1646 | | /* OrangeChromaOrGreenPlane */ |
1647 | |
|
1648 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1649 | 0 | { |
1650 | 0 | CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */ |
1651 | 0 | dstp += dstSizes[2]; |
1652 | 0 | } |
1653 | 0 | else |
1654 | 0 | { |
1655 | 0 | CopyMemory(dstp, context->planes[2], planeSize); /* Green */ |
1656 | 0 | dstp += planeSize; |
1657 | 0 | } |
1658 | | |
1659 | | /* GreenChromeOrBluePlane */ |
1660 | |
|
1661 | 0 | if (FormatHeader & PLANAR_FORMAT_HEADER_RLE) |
1662 | 0 | { |
1663 | 0 | CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */ |
1664 | 0 | dstp += dstSizes[3]; |
1665 | 0 | } |
1666 | 0 | else |
1667 | 0 | { |
1668 | 0 | CopyMemory(dstp, context->planes[3], planeSize); /* Blue */ |
1669 | 0 | dstp += planeSize; |
1670 | 0 | } |
1671 | | |
1672 | | /* Pad1 (1 byte) */ |
1673 | |
|
1674 | 0 | if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE)) |
1675 | 0 | { |
1676 | 0 | *dstp = 0; |
1677 | 0 | dstp++; |
1678 | 0 | } |
1679 | |
|
1680 | 0 | size = (dstp - dstData); |
1681 | 0 | *pDstSize = size; |
1682 | 0 | return dstData; |
1683 | 0 | } |
1684 | | |
1685 | | BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context, |
1686 | | UINT32 width, UINT32 height) |
1687 | 0 | { |
1688 | 0 | if (!context) |
1689 | 0 | return FALSE; |
1690 | | |
1691 | 0 | context->bgr = FALSE; |
1692 | 0 | context->maxWidth = PLANAR_ALIGN(width, 4); |
1693 | 0 | context->maxHeight = PLANAR_ALIGN(height, 4); |
1694 | 0 | { |
1695 | 0 | const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight; |
1696 | 0 | if (tmp > UINT32_MAX) |
1697 | 0 | return FALSE; |
1698 | 0 | context->maxPlaneSize = tmp; |
1699 | 0 | } |
1700 | | |
1701 | 0 | if (context->maxWidth > UINT32_MAX / 4) |
1702 | 0 | return FALSE; |
1703 | 0 | context->nTempStep = context->maxWidth * 4; |
1704 | |
|
1705 | 0 | memset(context->planes, 0, sizeof(context->planes)); |
1706 | 0 | memset(context->rlePlanes, 0, sizeof(context->rlePlanes)); |
1707 | 0 | memset(context->deltaPlanes, 0, sizeof(context->deltaPlanes)); |
1708 | |
|
1709 | 0 | if (context->maxPlaneSize > 0) |
1710 | 0 | { |
1711 | 0 | void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32); |
1712 | 0 | if (!tmp) |
1713 | 0 | return FALSE; |
1714 | 0 | context->planesBuffer = tmp; |
1715 | |
|
1716 | 0 | tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32); |
1717 | 0 | if (!tmp) |
1718 | 0 | return FALSE; |
1719 | 0 | context->pTempData = tmp; |
1720 | |
|
1721 | 0 | tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32); |
1722 | 0 | if (!tmp) |
1723 | 0 | return FALSE; |
1724 | 0 | context->deltaPlanesBuffer = tmp; |
1725 | |
|
1726 | 0 | tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32); |
1727 | 0 | if (!tmp) |
1728 | 0 | return FALSE; |
1729 | 0 | context->rlePlanesBuffer = tmp; |
1730 | |
|
1731 | 0 | context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize]; |
1732 | 0 | context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize]; |
1733 | 0 | context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize]; |
1734 | 0 | context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize]; |
1735 | 0 | context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize]; |
1736 | 0 | context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize]; |
1737 | 0 | context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize]; |
1738 | 0 | context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize]; |
1739 | 0 | } |
1740 | 0 | return TRUE; |
1741 | 0 | } |
1742 | | |
1743 | | BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth, |
1744 | | UINT32 maxHeight) |
1745 | 0 | { |
1746 | 0 | BITMAP_PLANAR_CONTEXT* context = |
1747 | 0 | (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32); |
1748 | |
|
1749 | 0 | if (!context) |
1750 | 0 | return NULL; |
1751 | | |
1752 | 0 | if (flags & PLANAR_FORMAT_HEADER_NA) |
1753 | 0 | context->AllowSkipAlpha = TRUE; |
1754 | |
|
1755 | 0 | if (flags & PLANAR_FORMAT_HEADER_RLE) |
1756 | 0 | context->AllowRunLengthEncoding = TRUE; |
1757 | |
|
1758 | 0 | if (flags & PLANAR_FORMAT_HEADER_CS) |
1759 | 0 | context->AllowColorSubsampling = TRUE; |
1760 | |
|
1761 | 0 | context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK; |
1762 | |
|
1763 | 0 | if (context->ColorLossLevel) |
1764 | 0 | context->AllowDynamicColorFidelity = TRUE; |
1765 | |
|
1766 | 0 | if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight)) |
1767 | 0 | { |
1768 | 0 | WINPR_PRAGMA_DIAG_PUSH |
1769 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
1770 | 0 | freerdp_bitmap_planar_context_free(context); |
1771 | 0 | WINPR_PRAGMA_DIAG_POP |
1772 | 0 | return NULL; |
1773 | 0 | } |
1774 | | |
1775 | 0 | return context; |
1776 | 0 | } |
1777 | | |
1778 | | void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context) |
1779 | 0 | { |
1780 | 0 | if (!context) |
1781 | 0 | return; |
1782 | | |
1783 | 0 | winpr_aligned_free(context->pTempData); |
1784 | 0 | winpr_aligned_free(context->planesBuffer); |
1785 | 0 | winpr_aligned_free(context->deltaPlanesBuffer); |
1786 | 0 | winpr_aligned_free(context->rlePlanesBuffer); |
1787 | 0 | winpr_aligned_free(context); |
1788 | 0 | } |
1789 | | |
1790 | | void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr) |
1791 | 0 | { |
1792 | 0 | WINPR_ASSERT(planar); |
1793 | 0 | planar->bgr = bgr; |
1794 | 0 | } |
1795 | | |
1796 | | void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown) |
1797 | 0 | { |
1798 | 0 | WINPR_ASSERT(planar); |
1799 | 0 | planar->topdown = topdown; |
1800 | 0 | } |