1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Image Reader Management (Image Reader)                              */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "gx_api.h"
27 #include "gx_system.h"
28 #include "gx_utility.h"
29 #include "gx_image_reader.h"
30 
31 #if defined(GX_SOFTWARE_DECODER_SUPPORT)
32 
33 extern VOID _gx_image_reader_rgb2gray(GX_PIXEL *pixel, GX_UBYTE *gray);
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_image_reader_8bit_alpha_write                   PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function writes a pixel to output pixemap data structure.      */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    image_reader                          Image reader control block.   */
51 /*    pixel                                 Pixel to write.               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _gx_image_reader_pixel_write_callback_set                           */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
70 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_gx_image_reader_8bit_alpha_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)74 static UINT _gx_image_reader_8bit_alpha_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
75 {
76 GX_UBYTE *palpha;
77 
78     if (!image_reader -> gx_image_reader_size_testing)
79     {
80         palpha = (GX_UBYTE *)image_reader -> gx_image_reader_putdata;
81 
82         *palpha = pixel -> gx_pixel_alpha;
83     }
84 
85     image_reader -> gx_image_reader_putdata += 1;
86 
87     return GX_SUCCESS;
88 }
89 
90 /**************************************************************************/
91 /*                                                                        */
92 /*  FUNCTION                                               RELEASE        */
93 /*                                                                        */
94 /*    _gx_image_reader_8bit_palette_write                 PORTABLE C      */
95 /*                                                           6.1          */
96 /*  AUTHOR                                                                */
97 /*                                                                        */
98 /*    Kenneth Maxwell, Microsoft Corporation                              */
99 /*                                                                        */
100 /*  DESCRIPTION                                                           */
101 /*                                                                        */
102 /*    This function writes a palette index to output pixemap data         */
103 /*    structure.                                                          */
104 /*                                                                        */
105 /*  INPUT                                                                 */
106 /*                                                                        */
107 /*    image_reader                          Image reader control block.   */
108 /*    pixel                                 Pixel to write.               */
109 /*                                                                        */
110 /*  OUTPUT                                                                */
111 /*                                                                        */
112 /*    None                                                                */
113 /*                                                                        */
114 /*  CALLS                                                                 */
115 /*                                                                        */
116 /*    None                                                                */
117 /*                                                                        */
118 /*  CALLED BY                                                             */
119 /*                                                                        */
120 /*    _gx_image_reader_pixel_write_callback_set                           */
121 /*                                                                        */
122 /*  RELEASE HISTORY                                                       */
123 /*                                                                        */
124 /*    DATE              NAME                      DESCRIPTION             */
125 /*                                                                        */
126 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
127 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
128 /*                                            resulting in version 6.1    */
129 /*                                                                        */
130 /**************************************************************************/
_gx_image_reader_8bit_palette_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)131 static UINT _gx_image_reader_8bit_palette_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
132 {
133 GX_UBYTE *pLine;
134 INT       palindex;
135 
136     if (!image_reader -> gx_image_reader_size_testing)
137     {
138         pLine = (GX_UBYTE *)image_reader -> gx_image_reader_putdata;
139 
140         if ((image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA) && (pixel -> gx_pixel_alpha < 128))
141         {
142             *pLine = GX_TRANSPARENT_COLOR;
143         }
144         else
145         {
146             /* Find the palette color that is nearest to the wanted color. */
147             _gx_image_reader_nearest_palette_color_get(image_reader, pixel, &palindex);
148 
149             *pLine = (GX_UBYTE)palindex;
150         }
151     }
152 
153     image_reader -> gx_image_reader_putdata += 1;
154 
155     return GX_SUCCESS;
156 }
157 
158 /**************************************************************************/
159 /*                                                                        */
160 /*  FUNCTION                                               RELEASE        */
161 /*                                                                        */
162 /*    _gx_image_reader_24xrgb_pixel_write                 PORTABLE C      */
163 /*                                                           6.1          */
164 /*  AUTHOR                                                                */
165 /*                                                                        */
166 /*    Kenneth Maxwell, Microsoft Corporation                              */
167 /*                                                                        */
168 /*  DESCRIPTION                                                           */
169 /*                                                                        */
170 /*    This function writes a pixel to output pixelmap data structure.     */
171 /*                                                                        */
172 /*  INPUT                                                                 */
173 /*                                                                        */
174 /*    image_reader                          Image reader control block.   */
175 /*    pixel                                 Pixel to write.               */
176 /*                                                                        */
177 /*  OUTPUT                                                                */
178 /*                                                                        */
179 /*    Completion Status                                                   */
180 /*                                                                        */
181 /*  CALLS                                                                 */
182 /*                                                                        */
183 /*    None                                                                */
184 /*                                                                        */
185 /*  CALLED BY                                                             */
186 /*                                                                        */
187 /*    _gx_image_reader_pixel_write_callback_set                           */
188 /*                                                                        */
189 /*  RELEASE HISTORY                                                       */
190 /*                                                                        */
191 /*    DATE              NAME                      DESCRIPTION             */
192 /*                                                                        */
193 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
194 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
195 /*                                            resulting in version 6.1    */
196 /*                                                                        */
197 /**************************************************************************/
_gx_image_reader_24xrgb_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)198 static UINT _gx_image_reader_24xrgb_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
199 {
200 GX_COLOR *pLine;
201 
202     if (!image_reader -> gx_image_reader_size_testing)
203     {
204         pLine = (GX_COLOR *)image_reader -> gx_image_reader_putdata;
205 
206         (*pLine) = (GX_COLOR)((pixel -> gx_pixel_red << 16));
207         (*pLine) = (GX_COLOR)((*pLine) | ((ULONG)(pixel -> gx_pixel_green) << 8));
208         (*pLine) = (GX_COLOR)((*pLine) | pixel -> gx_pixel_blue);
209         (*pLine) = (GX_COLOR)((*pLine) | 0xff000000);
210     }
211 
212     image_reader -> gx_image_reader_putdata += 4;
213 
214     return GX_SUCCESS;
215 }
216 
217 /**************************************************************************/
218 /*                                                                        */
219 /*  FUNCTION                                               RELEASE        */
220 /*                                                                        */
221 /*    _gx_image_reader_24xrgb_rotated_pixel_write         PORTABLE C      */
222 /*                                                           6.1.4        */
223 /*  AUTHOR                                                                */
224 /*                                                                        */
225 /*    Kenneth Maxwell, Microsoft Corporation                              */
226 /*                                                                        */
227 /*  DESCRIPTION                                                           */
228 /*                                                                        */
229 /*    This function writes a pixel to output rotated pixelmap data        */
230 /*    structure.                                                          */
231 /*                                                                        */
232 /*  INPUT                                                                 */
233 /*                                                                        */
234 /*    image_reader                          Image reader control block.   */
235 /*    pixel                                 Pixel to write.               */
236 /*                                                                        */
237 /*  OUTPUT                                                                */
238 /*                                                                        */
239 /*    Completion Status                                                   */
240 /*                                                                        */
241 /*  CALLS                                                                 */
242 /*                                                                        */
243 /*    None                                                                */
244 /*                                                                        */
245 /*  CALLED BY                                                             */
246 /*                                                                        */
247 /*    _gx_image_reader_pixel_write_callback_set                           */
248 /*                                                                        */
249 /*  RELEASE HISTORY                                                       */
250 /*                                                                        */
251 /*    DATE              NAME                      DESCRIPTION             */
252 /*                                                                        */
253 /*  02-02-2021     Kenneth Maxwell          Initial Version 6.1.4         */
254 /*                                                                        */
255 /**************************************************************************/
_gx_image_reader_24xrgb_rotated_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)256 static UINT _gx_image_reader_24xrgb_rotated_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
257 {
258 GX_COLOR *pLine;
259 
260     if (!image_reader -> gx_image_reader_size_testing)
261     {
262         pLine = (GX_COLOR *)image_reader -> gx_image_reader_putdata;
263 
264         (*pLine) = (GX_COLOR)((pixel -> gx_pixel_red << 16));
265         (*pLine) = (GX_COLOR)((*pLine) | ((ULONG)(pixel -> gx_pixel_green) << 8));
266         (*pLine) = (GX_COLOR)((*pLine) | pixel -> gx_pixel_blue);
267         (*pLine) = (GX_COLOR)((*pLine) | ((ULONG)(pixel -> gx_pixel_alpha) << 24));
268     }
269 
270     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ROTATE_CW)
271     {
272         image_reader -> gx_image_reader_putdata -= (image_reader -> gx_image_reader_image_height << 2);
273     }
274     else
275     {
276         image_reader -> gx_image_reader_putdata += (image_reader -> gx_image_reader_image_height << 2);
277     }
278 
279     return GX_SUCCESS;
280 }
281 
282 /**************************************************************************/
283 /*                                                                        */
284 /*  FUNCTION                                               RELEASE        */
285 /*                                                                        */
286 /*    _gx_image_reader_32argb_pixel_write                 PORTABLE C      */
287 /*                                                           6.1          */
288 /*  AUTHOR                                                                */
289 /*                                                                        */
290 /*    Kenneth Maxwell, Microsoft Corporation                              */
291 /*                                                                        */
292 /*  DESCRIPTION                                                           */
293 /*                                                                        */
294 /*    This function writes a pixel to output pixelmap data structure.     */
295 /*                                                                        */
296 /*  INPUT                                                                 */
297 /*                                                                        */
298 /*    image_reader                          Image reader control block.   */
299 /*    pixel                                 Pixel to write.               */
300 /*                                                                        */
301 /*  OUTPUT                                                                */
302 /*                                                                        */
303 /*    Completion Status                                                   */
304 /*                                                                        */
305 /*  CALLS                                                                 */
306 /*                                                                        */
307 /*    None                                                                */
308 /*                                                                        */
309 /*  CALLED BY                                                             */
310 /*                                                                        */
311 /*    _gx_image_reader_pixel_write_callback_set                           */
312 /*                                                                        */
313 /*  RELEASE HISTORY                                                       */
314 /*                                                                        */
315 /*    DATE              NAME                      DESCRIPTION             */
316 /*                                                                        */
317 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
318 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
319 /*                                            resulting in version 6.1    */
320 /*                                                                        */
321 /**************************************************************************/
_gx_image_reader_32argb_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)322 static UINT _gx_image_reader_32argb_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
323 {
324 GX_COLOR *pLine;
325 
326     if (!image_reader -> gx_image_reader_size_testing)
327     {
328         pLine = (GX_COLOR *)image_reader -> gx_image_reader_putdata;
329 
330         (*pLine) = (GX_COLOR)(pixel -> gx_pixel_alpha << 24);
331         (*pLine) = (GX_COLOR)((*pLine) | ((ULONG)(pixel -> gx_pixel_red) << 16));
332         (*pLine) = (GX_COLOR)((*pLine) | ((ULONG)(pixel -> gx_pixel_green) << 8));
333         (*pLine) = (GX_COLOR)((*pLine) | pixel -> gx_pixel_blue);
334     }
335 
336     image_reader -> gx_image_reader_putdata += 4;
337 
338     return GX_SUCCESS;
339 }
340 
341 /**************************************************************************/
342 /*                                                                        */
343 /*  FUNCTION                                               RELEASE        */
344 /*                                                                        */
345 /*    _gx_image_reader_565rgb_rle_pixel_write             PORTABLE C      */
346 /*                                                           6.1          */
347 /*  AUTHOR                                                                */
348 /*                                                                        */
349 /*    Kenneth Maxwell, Microsoft Corporation                              */
350 /*                                                                        */
351 /*  DESCRIPTION                                                           */
352 /*                                                                        */
353 /*    This function writes a pixel to output pixemap data structure.      */
354 /*                                                                        */
355 /*  INPUT                                                                 */
356 /*                                                                        */
357 /*    image_reader                          Image reader control block.   */
358 /*    pixel                                 Pixel to write.               */
359 /*                                                                        */
360 /*  OUTPUT                                                                */
361 /*                                                                        */
362 /*    Completion Status                                                   */
363 /*                                                                        */
364 /*  CALLS                                                                 */
365 /*                                                                        */
366 /*    None                                                                */
367 /*                                                                        */
368 /*  CALLED BY                                                             */
369 /*                                                                        */
370 /*    _gx_image_reader_pixel_write_callback_set                           */
371 /*                                                                        */
372 /*  RELEASE HISTORY                                                       */
373 /*                                                                        */
374 /*    DATE              NAME                      DESCRIPTION             */
375 /*                                                                        */
376 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
377 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
378 /*                                            resulting in version 6.1    */
379 /*                                                                        */
380 /**************************************************************************/
_gx_image_reader_565rgb_rle_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)381 static UINT _gx_image_reader_565rgb_rle_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
382 {
383 USHORT   *pLine;
384 GX_UBYTE *pAlpha;
385 
386     if (!image_reader -> gx_image_reader_size_testing)
387     {
388         pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
389 
390         if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
391         {
392             pAlpha = (GX_UBYTE *)image_reader -> gx_image_reader_putdata;
393 
394             /* Skip count. */
395             pAlpha++;
396             pLine++;
397 
398             (*pAlpha) = pixel -> gx_pixel_alpha;
399         }
400 
401         pixel -> gx_pixel_red &= 0xf8;
402         pixel -> gx_pixel_green &= 0xfc;
403         pixel -> gx_pixel_blue &= 0xf8;
404 
405         (*pLine) = (USHORT)(pixel -> gx_pixel_red << 8);
406         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 3));
407         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_blue >> 3));
408     }
409 
410     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
411     {
412         image_reader -> gx_image_reader_putdata += 4;
413     }
414     else
415     {
416         image_reader -> gx_image_reader_putdata += 2;
417     }
418 
419     return GX_SUCCESS;
420 }
421 
422 /**************************************************************************/
423 /*                                                                        */
424 /*  FUNCTION                                               RELEASE        */
425 /*                                                                        */
426 /*    _gx_image_reader_565rgb_pixel_write                 PORTABLE C      */
427 /*                                                           6.1          */
428 /*  AUTHOR                                                                */
429 /*                                                                        */
430 /*    Kenneth Maxwell, Microsoft Corporation                              */
431 /*                                                                        */
432 /*  DESCRIPTION                                                           */
433 /*                                                                        */
434 /*    This function writes a pixel to output pixemap data structure.      */
435 /*                                                                        */
436 /*  INPUT                                                                 */
437 /*                                                                        */
438 /*    image_reader                          Image reader control block.   */
439 /*    pixel                                 Pixel to write.               */
440 /*                                                                        */
441 /*  OUTPUT                                                                */
442 /*                                                                        */
443 /*    Completion Status                                                   */
444 /*                                                                        */
445 /*  CALLS                                                                 */
446 /*                                                                        */
447 /*    None                                                                */
448 /*                                                                        */
449 /*  CALLED BY                                                             */
450 /*                                                                        */
451 /*    _gx_image_reader_pixel_write_callback_set                           */
452 /*                                                                        */
453 /*  RELEASE HISTORY                                                       */
454 /*                                                                        */
455 /*    DATE              NAME                      DESCRIPTION             */
456 /*                                                                        */
457 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
458 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
459 /*                                            resulting in version 6.1    */
460 /*                                                                        */
461 /**************************************************************************/
_gx_image_reader_565rgb_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)462 static UINT _gx_image_reader_565rgb_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
463 {
464 USHORT   *pLine;
465 GX_UBYTE *pAlpha;
466 
467     pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
468 
469     pixel -> gx_pixel_red &= 0xf8;
470     pixel -> gx_pixel_green &= 0xfc;
471     pixel -> gx_pixel_blue &= 0xf8;
472 
473     (*pLine) = (USHORT)(pixel -> gx_pixel_red << 8);
474     (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 3));
475     (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_blue >> 3));
476 
477     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
478     {
479         pAlpha = image_reader -> gx_image_reader_putauxdata;
480 
481         (*pAlpha) = pixel -> gx_pixel_alpha;
482     }
483 
484     image_reader -> gx_image_reader_putauxdata += 1;
485     image_reader -> gx_image_reader_putdata += 2;
486 
487     return GX_SUCCESS;
488 }
489 
490 /**************************************************************************/
491 /*                                                                        */
492 /*  FUNCTION                                               RELEASE        */
493 /*                                                                        */
494 /*    _gx_image_reader_565rgb_rotated_pixel_write         PORTABLE C      */
495 /*                                                           6.1.5        */
496 /*  AUTHOR                                                                */
497 /*                                                                        */
498 /*    Kenneth Maxwell, Microsoft Corporation                              */
499 /*                                                                        */
500 /*  DESCRIPTION                                                           */
501 /*                                                                        */
502 /*    This function rotates a pixel to output pixemap data structure.     */
503 /*                                                                        */
504 /*  INPUT                                                                 */
505 /*                                                                        */
506 /*    image_reader                          Image reader control block.   */
507 /*    pixel                                 Pixel to write.               */
508 /*                                                                        */
509 /*  OUTPUT                                                                */
510 /*                                                                        */
511 /*    Completion Status                                                   */
512 /*                                                                        */
513 /*  CALLS                                                                 */
514 /*                                                                        */
515 /*    None                                                                */
516 /*                                                                        */
517 /*  CALLED BY                                                             */
518 /*                                                                        */
519 /*    _gx_image_reader_pixel_write_callback_set                           */
520 /*                                                                        */
521 /*  RELEASE HISTORY                                                       */
522 /*                                                                        */
523 /*    DATE              NAME                      DESCRIPTION             */
524 /*                                                                        */
525 /*  12-31-2020     Kenneth Maxwell          Initial Version 6.1.3         */
526 /*  03-02-2021     Ting Zhu                 Modified comment(s),          */
527 /*                                            improved logic,             */
528 /*                                            resulting in version 6.1.5  */
529 /*                                                                        */
530 /**************************************************************************/
_gx_image_reader_565rgb_rotated_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)531 static UINT _gx_image_reader_565rgb_rotated_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
532 {
533 USHORT   *pLine;
534 GX_UBYTE *pAlpha;
535 
536     if(!image_reader -> gx_image_reader_size_testing)
537     {
538         pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
539 
540         pixel -> gx_pixel_red &= 0xf8;
541         pixel -> gx_pixel_green &= 0xfc;
542         pixel -> gx_pixel_blue &= 0xf8;
543 
544         (*pLine) = (USHORT)(pixel -> gx_pixel_red << 8);
545         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 3));
546         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_blue >> 3));
547 
548         if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
549         {
550             pAlpha = image_reader -> gx_image_reader_putauxdata;
551 
552             (*pAlpha) = pixel -> gx_pixel_alpha;
553         }
554     }
555 
556     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ROTATE_CW)
557     {
558         image_reader -> gx_image_reader_putauxdata -= image_reader -> gx_image_reader_image_height;
559         image_reader -> gx_image_reader_putdata -= (image_reader -> gx_image_reader_image_height << 1);
560     }
561     else
562     {
563         image_reader -> gx_image_reader_putauxdata += image_reader -> gx_image_reader_image_height;
564         image_reader -> gx_image_reader_putdata += (image_reader -> gx_image_reader_image_height << 1);
565     }
566 
567     return GX_SUCCESS;
568 }
569 
570 /**************************************************************************/
571 /*                                                                        */
572 /*  FUNCTION                                               RELEASE        */
573 /*                                                                        */
574 /*    _gx_image_reader_1555xrgb_rle_pixel_write           PORTABLE C      */
575 /*                                                           6.1          */
576 /*  AUTHOR                                                                */
577 /*                                                                        */
578 /*    Kenneth Maxwell, Microsoft Corporation                              */
579 /*                                                                        */
580 /*  DESCRIPTION                                                           */
581 /*                                                                        */
582 /*    This function writes an 1555xrgb format pixel to output pixemap     */
583 /*    data structure.                                                     */
584 /*                                                                        */
585 /*  INPUT                                                                 */
586 /*                                                                        */
587 /*    image_reader                          Image reader control block.   */
588 /*    pixel                                 Pixel to write.               */
589 /*                                                                        */
590 /*  OUTPUT                                                                */
591 /*                                                                        */
592 /*    Completion Status                                                   */
593 /*                                                                        */
594 /*  CALLS                                                                 */
595 /*                                                                        */
596 /*    None                                                                */
597 /*                                                                        */
598 /*  CALLED BY                                                             */
599 /*                                                                        */
600 /*    _gx_image_reader_pixel_write_callback_set                           */
601 /*                                                                        */
602 /*  RELEASE HISTORY                                                       */
603 /*                                                                        */
604 /*    DATE              NAME                      DESCRIPTION             */
605 /*                                                                        */
606 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
607 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
608 /*                                            resulting in version 6.1    */
609 /*                                                                        */
610 /**************************************************************************/
_gx_image_reader_1555xrgb_rle_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)611 static UINT _gx_image_reader_1555xrgb_rle_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
612 {
613 USHORT   *pLine;
614 GX_UBYTE *pAlpha;
615 
616     if (!image_reader -> gx_image_reader_size_testing)
617     {
618         pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
619 
620         if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
621         {
622             pAlpha = (GX_UBYTE *)image_reader -> gx_image_reader_putdata;
623 
624             /* Skip count. */
625             pAlpha++;
626             pLine++;
627 
628             (*pAlpha) = pixel -> gx_pixel_alpha;
629         }
630 
631         pixel -> gx_pixel_red &= 0xf8;
632         pixel -> gx_pixel_green &= 0xf8;
633         pixel -> gx_pixel_blue &= 0xf8;
634 
635         (*pLine) = (USHORT)(pixel -> gx_pixel_red << 7);
636         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 2));
637         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_blue >> 3));
638     }
639 
640     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
641     {
642         image_reader -> gx_image_reader_putdata += 4;
643     }
644     else
645     {
646         image_reader -> gx_image_reader_putdata += 2;
647     }
648 
649     return GX_SUCCESS;
650 }
651 
652 /**************************************************************************/
653 /*                                                                        */
654 /*  FUNCTION                                               RELEASE        */
655 /*                                                                        */
656 /*    _gx_image_reader_1555xrgb_pixel_write               PORTABLE C      */
657 /*                                                           6.1          */
658 /*  AUTHOR                                                                */
659 /*                                                                        */
660 /*    Kenneth Maxwell, Microsoft Corporation                              */
661 /*                                                                        */
662 /*  DESCRIPTION                                                           */
663 /*                                                                        */
664 /*    This function writes an 1555xrgb format pixel to output pixemap     */
665 /*    data structure.                                                     */
666 /*                                                                        */
667 /*  INPUT                                                                 */
668 /*                                                                        */
669 /*    image_reader                          Image reader control block.   */
670 /*    pixel                                 Pixel to write.               */
671 /*                                                                        */
672 /*  OUTPUT                                                                */
673 /*                                                                        */
674 /*    Completion Status                                                   */
675 /*                                                                        */
676 /*  CALLS                                                                 */
677 /*                                                                        */
678 /*    None                                                                */
679 /*                                                                        */
680 /*  CALLED BY                                                             */
681 /*                                                                        */
682 /*    _gx_image_reader_pixel_write_callback_set                           */
683 /*                                                                        */
684 /*  RELEASE HISTORY                                                       */
685 /*                                                                        */
686 /*    DATE              NAME                      DESCRIPTION             */
687 /*                                                                        */
688 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
689 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
690 /*                                            resulting in version 6.1    */
691 /*                                                                        */
692 /**************************************************************************/
_gx_image_reader_1555xrgb_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)693 static UINT _gx_image_reader_1555xrgb_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
694 {
695 USHORT   *pLine;
696 GX_UBYTE *pAlpha;
697 
698     pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
699 
700     pixel -> gx_pixel_red &= 0xf8;
701     pixel -> gx_pixel_green &= 0xf8;
702     pixel -> gx_pixel_blue &= 0xf8;
703 
704     (*pLine) = (USHORT)(pixel -> gx_pixel_red << 7);
705     (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 2));
706     (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_blue >> 3));
707 
708     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ALPHA)
709     {
710         pAlpha = image_reader -> gx_image_reader_putauxdata;
711 
712         (*pAlpha) = pixel -> gx_pixel_alpha;
713     }
714 
715     image_reader -> gx_image_reader_putauxdata += 1;
716     image_reader -> gx_image_reader_putdata += 2;
717 
718     return GX_SUCCESS;
719 }
720 
721 /**************************************************************************/
722 /*                                                                        */
723 /*  FUNCTION                                               RELEASE        */
724 /*                                                                        */
725 /*    _gx_image_reader_4444argb_pixel_write               PORTABLE C      */
726 /*                                                           6.1          */
727 /*  AUTHOR                                                                */
728 /*                                                                        */
729 /*    Kenneth Maxwell, Microsoft Corporation                              */
730 /*                                                                        */
731 /*  DESCRIPTION                                                           */
732 /*                                                                        */
733 /*    This function writes an 4444argb format pixel (no transparency) to  */
734 /*    output pixemap data structure.                                      */
735 /*                                                                        */
736 /*  INPUT                                                                 */
737 /*                                                                        */
738 /*    image_reader                          Image reader control block.   */
739 /*    pixel                                 Pixel to write.               */
740 /*                                                                        */
741 /*  OUTPUT                                                                */
742 /*                                                                        */
743 /*    Completion Status                                                   */
744 /*                                                                        */
745 /*  CALLS                                                                 */
746 /*                                                                        */
747 /*    None                                                                */
748 /*                                                                        */
749 /*  CALLED BY                                                             */
750 /*                                                                        */
751 /*    _gx_image_reader_pixel_write_callback_set                           */
752 /*                                                                        */
753 /*  RELEASE HISTORY                                                       */
754 /*                                                                        */
755 /*    DATE              NAME                      DESCRIPTION             */
756 /*                                                                        */
757 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
758 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
759 /*                                            resulting in version 6.1    */
760 /*                                                                        */
761 /**************************************************************************/
_gx_image_reader_4444argb_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)762 static UINT _gx_image_reader_4444argb_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
763 {
764 USHORT *pLine;
765 
766     if (!image_reader -> gx_image_reader_size_testing)
767     {
768         pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
769 
770         pixel -> gx_pixel_alpha &= 0xf0;
771         pixel -> gx_pixel_red &= 0xf0;
772         pixel -> gx_pixel_green &= 0xf0;
773         pixel -> gx_pixel_blue &= 0xf0;
774 
775         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_red << 4));
776         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 0));
777         (*pLine) = (USHORT)((*pLine) | pixel -> gx_pixel_blue >> 4);
778         (*pLine) = (USHORT)((*pLine) | 0xf000);
779     }
780 
781     image_reader -> gx_image_reader_putdata += 2;
782 
783     return GX_SUCCESS;
784 }
785 
786 /**************************************************************************/
787 /*                                                                        */
788 /*  FUNCTION                                               RELEASE        */
789 /*                                                                        */
790 /*    _gx_image_reader_4444argb_transparent_write         PORTABLE C      */
791 /*                                                           6.1          */
792 /*  AUTHOR                                                                */
793 /*                                                                        */
794 /*    Kenneth Maxwell, Microsoft Corporation                              */
795 /*                                                                        */
796 /*  DESCRIPTION                                                           */
797 /*                                                                        */
798 /*    This function writes an 4444argb format pixel (with transparency)   */
799 /*    to output pixemap data structure.                                   */
800 /*                                                                        */
801 /*  INPUT                                                                 */
802 /*                                                                        */
803 /*    image_reader                          Image reader control block.   */
804 /*    pixel                                 Pixel to write.               */
805 /*                                                                        */
806 /*  OUTPUT                                                                */
807 /*                                                                        */
808 /*    Completion Status                                                   */
809 /*                                                                        */
810 /*  CALLS                                                                 */
811 /*                                                                        */
812 /*    None                                                                */
813 /*                                                                        */
814 /*  CALLED BY                                                             */
815 /*                                                                        */
816 /*    _gx_image_reader_pixel_write_callback_set                           */
817 /*                                                                        */
818 /*  RELEASE HISTORY                                                       */
819 /*                                                                        */
820 /*    DATE              NAME                      DESCRIPTION             */
821 /*                                                                        */
822 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
823 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
824 /*                                            resulting in version 6.1    */
825 /*                                                                        */
826 /**************************************************************************/
_gx_image_reader_4444argb_transparent_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)827 static UINT _gx_image_reader_4444argb_transparent_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
828 {
829 USHORT *pLine;
830 
831     if (!image_reader -> gx_image_reader_size_testing)
832     {
833         pLine = (USHORT *)image_reader -> gx_image_reader_putdata;
834 
835         pixel -> gx_pixel_alpha &= 0xf0;
836         pixel -> gx_pixel_red &= 0xf0;
837         pixel -> gx_pixel_green &= 0xf0;
838         pixel -> gx_pixel_blue &= 0xf0;
839 
840         (*pLine) = (USHORT)(pixel -> gx_pixel_alpha << 8);
841         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_red << 4));
842         (*pLine) = (USHORT)((*pLine) | (pixel -> gx_pixel_green << 0));
843         (*pLine) = (USHORT)((*pLine) | pixel -> gx_pixel_blue >> 4);
844     }
845 
846     image_reader -> gx_image_reader_putdata += 2;
847 
848     return GX_SUCCESS;
849 }
850 
851 /**************************************************************************/
852 /*                                                                        */
853 /*  FUNCTION                                               RELEASE        */
854 /*                                                                        */
855 /*    _gx_image_reader_4bit_grayscale_pixel_write         PORTABLE C      */
856 /*                                                           6.1          */
857 /*  AUTHOR                                                                */
858 /*                                                                        */
859 /*    Kenneth Maxwell, Microsoft Corporation                              */
860 /*                                                                        */
861 /*  DESCRIPTION                                                           */
862 /*                                                                        */
863 /*    This function writes an 4bit grayscale format pixel                 */
864 /*    (no transparency)to output pixelmap data structure.                 */
865 /*                                                                        */
866 /*  INPUT                                                                 */
867 /*                                                                        */
868 /*    image_reader                          Image reader control block.   */
869 /*    pixel                                 Pixel to write.               */
870 /*                                                                        */
871 /*  OUTPUT                                                                */
872 /*                                                                        */
873 /*    Completion Status                                                   */
874 /*                                                                        */
875 /*  CALLS                                                                 */
876 /*                                                                        */
877 /*    _gx_image_reader_rgb2gray             Convert RGB value to grayscale*/
878 /*                                                                        */
879 /*  CALLED BY                                                             */
880 /*                                                                        */
881 /*    _gx_image_reader_pixel_write_callback_set                           */
882 /*                                                                        */
883 /*  RELEASE HISTORY                                                       */
884 /*                                                                        */
885 /*    DATE              NAME                      DESCRIPTION             */
886 /*                                                                        */
887 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
888 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
889 /*                                            resulting in version 6.1    */
890 /*                                                                        */
891 /**************************************************************************/
_gx_image_reader_4bit_grayscale_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)892 static UINT _gx_image_reader_4bit_grayscale_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
893 {
894 GX_UBYTE *pLine;
895 GX_UBYTE  gray;
896 GX_UBYTE  color;
897 
898     if (!image_reader -> gx_image_reader_size_testing)
899     {
900         _gx_image_reader_rgb2gray(pixel, &gray);
901         gray >>= 4;
902 
903         color = (GX_UBYTE)(gray | (gray << 4));
904         pixel -> gx_pixel_red = color;
905         pixel -> gx_pixel_green = color;
906         pixel -> gx_pixel_blue = color;
907 
908         pLine = image_reader -> gx_image_reader_putdata;
909 
910         if (image_reader -> gx_image_reader_putdata_mask & 0xf0)
911         {
912             (*pLine) = (GX_UBYTE)(gray << 4);
913         }
914         else
915         {
916             (*pLine) |= gray;
917         }
918     }
919 
920     image_reader -> gx_image_reader_putdata_mask >>= 4;
921 
922     if (!image_reader -> gx_image_reader_putdata_mask)
923     {
924         image_reader -> gx_image_reader_putdata++;
925         image_reader -> gx_image_reader_putdata_mask = 0xf0;
926     }
927 
928     return GX_SUCCESS;
929 }
930 
931 /**************************************************************************/
932 /*                                                                        */
933 /*  FUNCTION                                               RELEASE        */
934 /*                                                                        */
935 /*    _gx_image_reader_4bit_grayscale_transparent_write   PORTABLE C      */
936 /*                                                           6.1          */
937 /*  AUTHOR                                                                */
938 /*                                                                        */
939 /*    Kenneth Maxwell, Microsoft Corporation                              */
940 /*                                                                        */
941 /*  DESCRIPTION                                                           */
942 /*                                                                        */
943 /*    This function writes an 4bit grayscale format pixel                 */
944 /*    (with transparency)to output pixelmap data structure.               */
945 /*                                                                        */
946 /*  INPUT                                                                 */
947 /*                                                                        */
948 /*    image_reader                          Image reader control block.   */
949 /*    pixel                                 Pixel to write.               */
950 /*                                                                        */
951 /*  OUTPUT                                                                */
952 /*                                                                        */
953 /*    Completion Status                                                   */
954 /*                                                                        */
955 /*  CALLS                                                                 */
956 /*                                                                        */
957 /*    _gx_image_reader_rgb2gray             Convert RGB value to grayscale*/
958 /*                                                                        */
959 /*  CALLED BY                                                             */
960 /*                                                                        */
961 /*    _gx_image_reader_pixel_write_callback_set                           */
962 /*                                                                        */
963 /*  RELEASE HISTORY                                                       */
964 /*                                                                        */
965 /*    DATE              NAME                      DESCRIPTION             */
966 /*                                                                        */
967 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
968 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
969 /*                                            resulting in version 6.1    */
970 /*                                                                        */
971 /**************************************************************************/
_gx_image_reader_4bit_grayscale_transparent_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)972 static UINT _gx_image_reader_4bit_grayscale_transparent_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
973 {
974 GX_UBYTE *pLine;
975 GX_UBYTE *pAuxData;
976 GX_UBYTE  gray;
977 GX_UBYTE  color;
978 
979     if (!image_reader -> gx_image_reader_size_testing)
980     {
981         _gx_image_reader_rgb2gray(pixel, &gray);
982         gray >>= 4;
983 
984         color = (GX_UBYTE)(gray | (gray << 4));
985         pixel -> gx_pixel_red = color;
986         pixel -> gx_pixel_green = color;
987         pixel -> gx_pixel_blue = color;
988 
989         pLine = image_reader -> gx_image_reader_putdata;
990 
991         if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_COMPRESS)
992         {
993             /* with compression, with transparent: 1 byte color
994                0x00-0x0f: pixel value
995                0xff:      transparent value */
996             if (pixel -> gx_pixel_alpha < 128)
997             {
998                 (*pLine) = GX_TRANSPARENT_COLOR;
999             }
1000             else
1001             {
1002                 (*pLine) = gray;
1003             }
1004         }
1005         else
1006         {
1007             /* without compression, with transparent: half byte color,
1008                1-bit transparent go into auxdata. */
1009             if (image_reader -> gx_image_reader_putdata_mask & 0xf0)
1010             {
1011                 (*pLine) = (GX_UBYTE)(gray << 4);
1012             }
1013             else
1014             {
1015                 (*pLine) |= gray;
1016             }
1017 
1018             pAuxData = image_reader -> gx_image_reader_putauxdata;
1019 
1020             if (pixel -> gx_pixel_alpha > 128)
1021             {
1022                 (*pAuxData) &= (GX_UBYTE)(~image_reader -> gx_image_reader_putauxdata_mask);
1023             }
1024             else
1025             {
1026                 (*pAuxData) |= image_reader -> gx_image_reader_putauxdata_mask;
1027             }
1028         }
1029     }
1030 
1031     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_COMPRESS)
1032     {
1033         image_reader -> gx_image_reader_putdata++;
1034     }
1035     else
1036     {
1037 
1038         image_reader -> gx_image_reader_putdata_mask >>= 4;
1039 
1040         if (!image_reader -> gx_image_reader_putdata_mask)
1041         {
1042             image_reader -> gx_image_reader_putdata++;
1043             image_reader -> gx_image_reader_putdata_mask = 0xf0;
1044         }
1045 
1046         image_reader -> gx_image_reader_putauxdata_mask >>= 1;
1047         if (!image_reader -> gx_image_reader_putauxdata_mask)
1048         {
1049             image_reader -> gx_image_reader_putauxdata++;
1050             image_reader -> gx_image_reader_putauxdata_mask = 0x80;
1051         }
1052     }
1053 
1054     return GX_SUCCESS;
1055 }
1056 
1057 /**************************************************************************/
1058 /*                                                                        */
1059 /*  FUNCTION                                               RELEASE        */
1060 /*                                                                        */
1061 /*    _gx_image_reader_monochrome_pixel_write             PORTABLE C      */
1062 /*                                                           6.1          */
1063 /*  AUTHOR                                                                */
1064 /*                                                                        */
1065 /*    Kenneth Maxwell, Microsoft Corporation                              */
1066 /*                                                                        */
1067 /*  DESCRIPTION                                                           */
1068 /*                                                                        */
1069 /*    This function writes a monochrome format pixel (no transparency) to */
1070 /*    output pixelmap data structure.                                     */
1071 /*                                                                        */
1072 /*  INPUT                                                                 */
1073 /*                                                                        */
1074 /*    image_reader                          Image reader control block.   */
1075 /*    pixel                                 Pixel to write.               */
1076 /*                                                                        */
1077 /*  OUTPUT                                                                */
1078 /*                                                                        */
1079 /*    Completion Status                                                   */
1080 /*                                                                        */
1081 /*  CALLS                                                                 */
1082 /*                                                                        */
1083 /*    _gx_image_reader_rgb2gray             Convert RGB value to grayscale*/
1084 /*                                                                        */
1085 /*  CALLED BY                                                             */
1086 /*                                                                        */
1087 /*    _gx_image_reader_pixel_write_callback_set                           */
1088 /*                                                                        */
1089 /*  RELEASE HISTORY                                                       */
1090 /*                                                                        */
1091 /*    DATE              NAME                      DESCRIPTION             */
1092 /*                                                                        */
1093 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
1094 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
1095 /*                                            resulting in version 6.1    */
1096 /*                                                                        */
1097 /**************************************************************************/
_gx_image_reader_monochrome_pixel_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)1098 static UINT _gx_image_reader_monochrome_pixel_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
1099 {
1100 GX_UBYTE *pLine;
1101 GX_UBYTE  gray;
1102 GX_UBYTE  color;
1103 
1104     pLine = image_reader -> gx_image_reader_putdata;
1105 
1106     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_COMPRESS)
1107     {
1108         /* [7-bit counter][1-bit color]*/
1109         if (!image_reader -> gx_image_reader_size_testing)
1110         {
1111             _gx_image_reader_rgb2gray(pixel, &gray);
1112             if (gray > image_reader -> gx_image_reader_mono_shreshold)
1113             {
1114                 (*pLine) |= 0x01;
1115             }
1116             else
1117             {
1118                 (*pLine) &= 0xfe;
1119             }
1120         }
1121         image_reader -> gx_image_reader_putdata++;
1122     }
1123     else
1124     {
1125         /* [1-bit color]*/
1126         _gx_image_reader_rgb2gray(pixel, &gray);
1127         if (gray > image_reader -> gx_image_reader_mono_shreshold)
1128         {
1129             (*pLine) |= image_reader -> gx_image_reader_putdata_mask;
1130             color = 255;
1131         }
1132         else
1133         {
1134             (*pLine) &= (GX_UBYTE)(~image_reader -> gx_image_reader_putdata_mask);
1135             color = 0;
1136         }
1137 
1138         pixel -> gx_pixel_red = color;
1139         pixel -> gx_pixel_green = color;
1140         pixel -> gx_pixel_blue = color;
1141         pixel -> gx_pixel_alpha = 0xff;
1142 
1143         image_reader -> gx_image_reader_putdata_mask >>= 1;
1144 
1145         if (!image_reader -> gx_image_reader_putdata_mask)
1146         {
1147             image_reader -> gx_image_reader_putdata++;
1148             image_reader -> gx_image_reader_putdata_mask = 0x80;
1149         }
1150     }
1151 
1152     return GX_SUCCESS;
1153 }
1154 
1155 /**************************************************************************/
1156 /*                                                                        */
1157 /*  FUNCTION                                               RELEASE        */
1158 /*                                                                        */
1159 /*    _gx_image_reader_monochrome_transparent_write       PORTABLE C      */
1160 /*                                                           6.1          */
1161 /*  AUTHOR                                                                */
1162 /*                                                                        */
1163 /*    Kenneth Maxwell, Microsoft Corporation                              */
1164 /*                                                                        */
1165 /*  DESCRIPTION                                                           */
1166 /*                                                                        */
1167 /*    This function writes a monochrome format pixel (with transparency)  */
1168 /*    to output pixelmap data structure.                                  */
1169 /*                                                                        */
1170 /*  INPUT                                                                 */
1171 /*                                                                        */
1172 /*    image_reader                          Image reader control block.   */
1173 /*    pixel                                 Pixel to write.               */
1174 /*                                                                        */
1175 /*  OUTPUT                                                                */
1176 /*                                                                        */
1177 /*    Completion Status                                                   */
1178 /*                                                                        */
1179 /*  CALLS                                                                 */
1180 /*                                                                        */
1181 /*    _gx_image_reader_rgb2gray             Convert RGB value to grayscale*/
1182 /*                                                                        */
1183 /*  CALLED BY                                                             */
1184 /*                                                                        */
1185 /*    _gx_image_reader_pixel_write_callback_set                           */
1186 /*                                                                        */
1187 /*  RELEASE HISTORY                                                       */
1188 /*                                                                        */
1189 /*    DATE              NAME                      DESCRIPTION             */
1190 /*                                                                        */
1191 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
1192 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
1193 /*                                            resulting in version 6.1    */
1194 /*                                                                        */
1195 /**************************************************************************/
_gx_image_reader_monochrome_transparent_write(GX_IMAGE_READER * image_reader,GX_PIXEL * pixel)1196 static UINT _gx_image_reader_monochrome_transparent_write(GX_IMAGE_READER *image_reader, GX_PIXEL *pixel)
1197 {
1198 GX_UBYTE *pLine;
1199 GX_UBYTE  gray;
1200 GX_UBYTE  trans_mask;
1201 GX_UBYTE  color;
1202 
1203     pLine = image_reader -> gx_image_reader_putdata;
1204 
1205     if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_COMPRESS)
1206     {
1207         /* [6-bit counter][1-bit color][1-bit transparency]*/
1208         if (!image_reader -> gx_image_reader_size_testing)
1209         {
1210             if (pixel -> gx_pixel_alpha < 128)
1211             {
1212                 /* Mark as transparency. */
1213                 (*pLine) &= 0xfc;
1214             }
1215             else
1216             {
1217                 (*pLine) |= 0x01;
1218 
1219                 /* Write color: 0 or 1. */
1220                 _gx_image_reader_rgb2gray(pixel, &gray);
1221                 if (gray > image_reader -> gx_image_reader_mono_shreshold)
1222                 {
1223                     (*pLine) |= 0x02;
1224                 }
1225                 else
1226                 {
1227                     (*pLine) &= 0xfd;
1228                 }
1229             }
1230         }
1231         image_reader -> gx_image_reader_putdata++;
1232     }
1233     else
1234     {
1235         /* [1-bit color][1-bit transparency]*/
1236         trans_mask = image_reader -> gx_image_reader_putdata_mask >> 1;
1237         if (pixel -> gx_pixel_alpha < 128)
1238         {
1239             /* Mark as transparency. */
1240             (*pLine) &= (GX_UBYTE)(~trans_mask);
1241             (*pLine) &= (GX_UBYTE)(~image_reader -> gx_image_reader_putdata_mask);
1242         }
1243         else
1244         {
1245             (*pLine) |= trans_mask;
1246 
1247             _gx_image_reader_rgb2gray(pixel, &gray);
1248             if (gray > image_reader -> gx_image_reader_mono_shreshold)
1249             {
1250                 (*pLine) |= image_reader -> gx_image_reader_putdata_mask;
1251                 color = 255;
1252             }
1253             else
1254             {
1255                 (*pLine) &= (GX_UBYTE)(~image_reader -> gx_image_reader_putdata_mask);
1256                 color = 0;
1257             }
1258             pixel -> gx_pixel_red = color;
1259             pixel -> gx_pixel_green = color;
1260             pixel -> gx_pixel_blue = color;
1261         }
1262 
1263         image_reader -> gx_image_reader_putdata_mask >>= 2;
1264 
1265         if (!image_reader -> gx_image_reader_putdata_mask)
1266         {
1267             image_reader -> gx_image_reader_putdata++;
1268             image_reader -> gx_image_reader_putdata_mask = 0x80;
1269         }
1270     }
1271 
1272     return GX_SUCCESS;
1273 }
1274 
1275 /**************************************************************************/
1276 /*                                                                        */
1277 /*  FUNCTION                                               RELEASE        */
1278 /*                                                                        */
1279 /*    _gx_image_reader_pixel_rotated_write_callback_set   PORTABLE C      */
1280 /*                                                           6.1.4        */
1281 /*  AUTHOR                                                                */
1282 /*                                                                        */
1283 /*    Kenneth Maxwell, Microsoft Corporation                              */
1284 /*                                                                        */
1285 /*  DESCRIPTION                                                           */
1286 /*                                                                        */
1287 /*    This function sets pixel write callback of the image reader when    */
1288 /*    rotation mode is set.                                               */
1289 /*                                                                        */
1290 /*  INPUT                                                                 */
1291 /*                                                                        */
1292 /*    image_reader                          Image reader control block    */
1293 /*    outmap                                Outpu pixelmap.               */
1294 /*                                                                        */
1295 /*  OUTPUT                                                                */
1296 /*                                                                        */
1297 /*    Completion Status                                                   */
1298 /*                                                                        */
1299 /*  CALLS                                                                 */
1300 /*                                                                        */
1301 /*    _gx_image_reader_565rgb_rotated_pixel_write                         */
1302 /*                                          Write 565rgb format pixel in  */
1303 /*                                            rotation mode               */
1304 /*    _gx_image_reader_24xrgb_rotated_pixel_write                         */
1305 /*                                          Write 24xrgn format pixel in  */
1306 /*                                            rotation mode               */
1307 /*                                                                        */
1308 /*  CALLED BY                                                             */
1309 /*                                                                        */
1310 /*    _gx_image_reader_pixel_write_callback_set                           */
1311 /*                                                                        */
1312 /*  RELEASE HISTORY                                                       */
1313 /*                                                                        */
1314 /*    DATE              NAME                      DESCRIPTION             */
1315 /*                                                                        */
1316 /*  12-31-2020     Kenneth Maxwell          Initial Version 6.1.3         */
1317 /*  02-02-2021     Kenneth Maxwell          Modified comment(s),          */
1318 /*                                            added 24xrgb format support,*/
1319 /*                                            resulting in version 6.1.4  */
1320 /*                                                                        */
1321 /**************************************************************************/
_gx_image_reader_pixel_rotated_write_callback_set(GX_IMAGE_READER * image_reader,GX_PIXELMAP * outmap)1322 static UINT _gx_image_reader_pixel_rotated_write_callback_set(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap)
1323 {
1324     switch (outmap -> gx_pixelmap_format)
1325     {
1326     case GX_COLOR_FORMAT_565RGB:
1327         image_reader -> gx_image_reader_pixel_write = _gx_image_reader_565rgb_rotated_pixel_write;
1328 
1329         if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ROTATE_CW)
1330         {
1331             outmap -> gx_pixelmap_flags |= GX_PIXELMAP_ROTATED_90;
1332 
1333             image_reader -> gx_image_reader_putdatarow = (GX_UBYTE *)outmap -> gx_pixelmap_data;
1334             image_reader -> gx_image_reader_putdatarow += (outmap -> gx_pixelmap_width - 1) * (outmap -> gx_pixelmap_height << 1);
1335 
1336             if (outmap -> gx_pixelmap_aux_data)
1337             {
1338                 image_reader -> gx_image_reader_putauxdatarow = (GX_UBYTE *)outmap -> gx_pixelmap_aux_data;
1339                 image_reader -> gx_image_reader_putauxdatarow += (outmap -> gx_pixelmap_width - 1) * outmap -> gx_pixelmap_height;
1340             }
1341 
1342             image_reader -> gx_image_reader_putdatarow_stride = 2;
1343             image_reader -> gx_image_reader_putauxdatarow_stride = 1;
1344         }
1345         else
1346         {
1347             outmap -> gx_pixelmap_flags |= GX_PIXELMAP_ROTATED_270;
1348 
1349             image_reader -> gx_image_reader_putdatarow = (GX_UBYTE *)outmap -> gx_pixelmap_data;
1350             image_reader -> gx_image_reader_putdatarow += (outmap -> gx_pixelmap_height - 1) * 2;
1351 
1352             if (outmap -> gx_pixelmap_aux_data)
1353             {
1354                 image_reader -> gx_image_reader_putauxdatarow = (GX_UBYTE *)outmap -> gx_pixelmap_aux_data;
1355                 image_reader -> gx_image_reader_putauxdatarow += (outmap -> gx_pixelmap_height - 1);
1356             }
1357 
1358             image_reader -> gx_image_reader_putdatarow_stride = -2;
1359             image_reader -> gx_image_reader_putauxdatarow_stride = -1;
1360         }
1361         break;
1362 
1363     case GX_COLOR_FORMAT_24XRGB:
1364         image_reader -> gx_image_reader_pixel_write = _gx_image_reader_24xrgb_rotated_pixel_write;
1365 
1366         if (image_reader -> gx_image_reader_mode & GX_IMAGE_READER_MODE_ROTATE_CW)
1367         {
1368             outmap -> gx_pixelmap_flags |= GX_PIXELMAP_ROTATED_90;
1369 
1370             image_reader -> gx_image_reader_putdatarow = (GX_UBYTE *)outmap -> gx_pixelmap_data;
1371             image_reader -> gx_image_reader_putdatarow += (outmap -> gx_pixelmap_width - 1) * (outmap -> gx_pixelmap_height << 2);
1372 
1373             image_reader -> gx_image_reader_putdatarow_stride = 4;
1374         }
1375         else
1376         {
1377             outmap -> gx_pixelmap_flags |= GX_PIXELMAP_ROTATED_270;
1378 
1379             image_reader -> gx_image_reader_putdatarow = (GX_UBYTE *)outmap -> gx_pixelmap_data;
1380             image_reader -> gx_image_reader_putdatarow += ((outmap -> gx_pixelmap_height - 1) << 2);
1381 
1382             image_reader -> gx_image_reader_putdatarow_stride = -4;
1383         }
1384         break;
1385 
1386     default:
1387         return GX_NOT_SUPPORTED;
1388     }
1389 
1390     return GX_SUCCESS;
1391 }
1392 
1393 /**************************************************************************/
1394 /*                                                                        */
1395 /*  FUNCTION                                               RELEASE        */
1396 /*                                                                        */
1397 /*    _gx_image_reader_pixel_write_callback_set           PORTABLE C      */
1398 /*                                                           6.1.3        */
1399 /*  AUTHOR                                                                */
1400 /*                                                                        */
1401 /*    Kenneth Maxwell, Microsoft Corporation                              */
1402 /*                                                                        */
1403 /*  DESCRIPTION                                                           */
1404 /*                                                                        */
1405 /*    This function sets pixel write callback of the image reader.        */
1406 /*                                                                        */
1407 /*  INPUT                                                                 */
1408 /*                                                                        */
1409 /*    image_reader                          Image reader control block    */
1410 /*    outmap                                Outpu pixelmap.               */
1411 /*                                                                        */
1412 /*  OUTPUT                                                                */
1413 /*                                                                        */
1414 /*    Completion Status                                                   */
1415 /*                                                                        */
1416 /*  CALLS                                                                 */
1417 /*                                                                        */
1418 /*    _gx_image_reader_565rgb_rle_pixel_write                             */
1419 /*                                          Write 565 rgb fromat pixel    */
1420 /*                                            in compress mode            */
1421 /*    _gx_image_reader_565rgb_pixel_write   Write 565 rgb format pixel    */
1422 /*    _gx_image_reader_1555xrgb_rle_pixel_write                           */
1423 /*                                          Write 1555xrgb format pixel   */
1424 /*                                            in compress mode            */
1425 /*    _gx_image_reader_1555xrgb_pixel_write Write 1555xrgb format pixel   */
1426 /*    _gx_image_reader_4444argb_transparent_write                         */
1427 /*                                          Write 4444argb format pixel   */
1428 /*                                            with transparency           */
1429 /*    _gx_image_reader_4444argb_pixel_write Write 4444argb format pixel   */
1430 /*    _gx_image_reader_32argb_pixel_write   Write 32argb format pixel     */
1431 /*    _gx_image_reader_24xrgb_pixel_write   Write 24xrgb format pixel     */
1432 /*    _gx_image_reader_8bit_alpha_write     Write 8bit alphamap format    */
1433 /*                                            pixel                       */
1434 /*    _gx_image_reader_8bit_palette_write   Write 8bit palette format     */
1435 /*                                            pixel                       */
1436 /*    _gx_image_reader_4bit_grayscale_transparent_write                   */
1437 /*                                          Write 4bit grayscale format   */
1438 /*                                            pixel with transparency     */
1439 /*    _gx_image_reader_4bit_grayscale_pixel_write                         */
1440 /*                                          Write 4bit grayscale format   */
1441 /*                                            pixel                       */
1442 /*    _gx_image_reader_monochrome_transparent_write                       */
1443 /*                                          Write 1bpp format pixel with  */
1444 /*                                            transparency                */
1445 /*    _gx_image_reader_monochrome_pixel_write                             */
1446 /*                                          Write 1bpp format pixel       */
1447 /*    _gx_system_memory_allocator           Application defined memory    */
1448 /*                                            allocator function          */
1449 /*    _gx_system_memory_free                Application defined memory    */
1450 /*                                            free function               */
1451 /*                                                                        */
1452 /*  CALLED BY                                                             */
1453 /*                                                                        */
1454 /*    _gx_image_reader_colorspace_convert                                 */
1455 /*                                                                        */
1456 /*  RELEASE HISTORY                                                       */
1457 /*                                                                        */
1458 /*    DATE              NAME                      DESCRIPTION             */
1459 /*                                                                        */
1460 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
1461 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
1462 /*                                            resulting in version 6.1    */
1463 /*  12-31-2020     Kenneth Maxwell          Modified comment(s),          */
1464 /*                                            supported rotation mode,    */
1465 /*                                            resulting in version 6.1.3  */
1466 /*                                                                        */
1467 /**************************************************************************/
_gx_image_reader_pixel_write_callback_set(GX_IMAGE_READER * image_reader,GX_PIXELMAP * outmap)1468 UINT _gx_image_reader_pixel_write_callback_set(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap)
1469 {
1470     /* Allocate memory for data of the output pixelmap. */
1471     if (outmap -> gx_pixelmap_data_size)
1472     {
1473         outmap -> gx_pixelmap_data = (GX_UBYTE *)_gx_system_memory_allocator(outmap -> gx_pixelmap_data_size);
1474 
1475         if (outmap -> gx_pixelmap_data == GX_NULL)
1476         {
1477             return GX_SYSTEM_MEMORY_ERROR;
1478         }
1479 
1480         memset((void *)outmap -> gx_pixelmap_data, 0, outmap -> gx_pixelmap_data_size);
1481 
1482         image_reader -> gx_image_reader_putdata = (GX_UBYTE *)outmap -> gx_pixelmap_data;
1483     }
1484 
1485     /* Allocate memory for aux data of the output pixelmap. */
1486     if (outmap -> gx_pixelmap_aux_data_size)
1487     {
1488         outmap -> gx_pixelmap_aux_data = (GX_UBYTE *)_gx_system_memory_allocator(outmap -> gx_pixelmap_aux_data_size);
1489 
1490         if (outmap -> gx_pixelmap_aux_data == GX_NULL)
1491         {
1492             _gx_system_memory_free((void *)outmap -> gx_pixelmap_data);
1493             outmap -> gx_pixelmap_data = GX_NULL;
1494             return GX_SYSTEM_MEMORY_ERROR;
1495         }
1496 
1497         memset((void *)outmap -> gx_pixelmap_aux_data, 0, outmap -> gx_pixelmap_aux_data_size);
1498 
1499         image_reader -> gx_image_reader_putauxdata = (GX_UBYTE *)outmap -> gx_pixelmap_aux_data;
1500     }
1501 
1502     if (image_reader -> gx_image_reader_mode & (GX_IMAGE_READER_MODE_ROTATE_CW | GX_IMAGE_READER_MODE_ROTATE_CCW))
1503     {
1504         return _gx_image_reader_pixel_rotated_write_callback_set(image_reader, outmap);
1505     }
1506 
1507     /* Set pixel write callback.  */
1508     switch (outmap -> gx_pixelmap_format)
1509     {
1510     case GX_COLOR_FORMAT_565RGB:
1511         if (outmap -> gx_pixelmap_flags & GX_PIXELMAP_COMPRESSED)
1512         {
1513             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_565rgb_rle_pixel_write;
1514         }
1515         else
1516         {
1517             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_565rgb_pixel_write;
1518         }
1519         break;
1520 
1521     case GX_COLOR_FORMAT_1555XRGB:
1522         if (outmap -> gx_pixelmap_flags & GX_PIXELMAP_COMPRESSED)
1523         {
1524             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_1555xrgb_rle_pixel_write;
1525         }
1526         else
1527         {
1528             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_1555xrgb_pixel_write;
1529         }
1530         break;
1531 
1532     case GX_COLOR_FORMAT_4444ARGB:
1533         if (outmap -> gx_pixelmap_flags & GX_PIXELMAP_ALPHA)
1534         {
1535             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_4444argb_transparent_write;
1536         }
1537         else
1538         {
1539             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_4444argb_pixel_write;
1540         }
1541         break;
1542 
1543     case GX_COLOR_FORMAT_32ARGB:
1544     case GX_COLOR_FORMAT_24XRGB:
1545         if (outmap -> gx_pixelmap_flags & GX_PIXELMAP_ALPHA)
1546         {
1547             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_32argb_pixel_write;
1548         }
1549         else
1550         {
1551             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_24xrgb_pixel_write;
1552         }
1553         break;
1554 
1555     case GX_COLOR_FORMAT_8BIT_ALPHAMAP:
1556         image_reader -> gx_image_reader_pixel_write = _gx_image_reader_8bit_alpha_write;
1557         break;
1558 
1559     case GX_COLOR_FORMAT_8BIT_PALETTE:
1560         image_reader -> gx_image_reader_pixel_write = _gx_image_reader_8bit_palette_write;
1561         break;
1562 
1563     case GX_COLOR_FORMAT_4BIT_GRAY:
1564         if (outmap -> gx_pixelmap_flags & GX_PIXELMAP_TRANSPARENT)
1565         {
1566             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_4bit_grayscale_transparent_write;
1567         }
1568         else
1569         {
1570             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_4bit_grayscale_pixel_write;
1571         }
1572         image_reader -> gx_image_reader_putdata_mask = 0xf0;
1573         image_reader -> gx_image_reader_putauxdata_mask = 0x80;
1574         break;
1575 
1576     case GX_COLOR_FORMAT_MONOCHROME:
1577         if (outmap -> gx_pixelmap_flags & GX_PIXELMAP_TRANSPARENT)
1578         {
1579             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_monochrome_transparent_write;
1580         }
1581         else
1582         {
1583             image_reader -> gx_image_reader_pixel_write = _gx_image_reader_monochrome_pixel_write;
1584         }
1585         image_reader -> gx_image_reader_putdata_mask = 0x80;
1586         break;
1587 
1588     default:
1589         return GX_NOT_SUPPORTED;
1590     }
1591 
1592     return GX_SUCCESS;
1593 }
1594 #endif
1595 
1596