1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** GUIX Component                                                        */
17 /**                                                                       */
18 /**   Image Reader Management (Image Reader)                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  COMPONENT DEFINITION                                   RELEASE        */
27 /*                                                                        */
28 /*    gx_image_reader.h                                   PORTABLE C      */
29 /*                                                           6.2.0        */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Kenneth Maxwell, Microsoft Corporation                              */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the GUIX image reader management component,       */
37 /*    including all data types and external references.  It is assumed    */
38 /*    that gx_api.h and gx_port.h have already been included.             */
39 /*                                                                        */
40 /*  RELEASE HISTORY                                                       */
41 /*                                                                        */
42 /*    DATE              NAME                      DESCRIPTION             */
43 /*                                                                        */
44 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
45 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
46 /*                                            resulting in version 6.1    */
47 /*  06-02-2021     Ting Zhu                 Modified comment(s), and      */
48 /*                                            improved png decoding       */
49 /*                                            performance,                */
50 /*                                            resulting in version 6.1.7  */
51 /*  10-31-2022     Kenneth Maxwell          Modified comment(s),          */
52 /*                                            added definitions for fixed */
53 /*                                            size table dimensions,      */
54 /*                                            resulting in version 6.2.0  */
55 /*                                                                        */
56 /**************************************************************************/
57 #if defined(GX_SOFTWARE_DECODER_SUPPORT)
58 #ifndef GX_IMAGE_READER_H
59 #define GX_IMAGE_READER_H
60 
61 #define GX_IMAGE_FORMAT_1BPP                    0xf0
62 #define GX_IMAGE_FORMAT_2BPP                    0xf1
63 #define GX_IMAGE_FORMAT_4BPP                    0xf2
64 #define GX_IMAGE_FORMAT_8BPP                    0xf3
65 #define GX_IMAGE_FORMAT_16BPP_GRAY              0xf4 /* Internal format: 16bits for each gray value. */
66 #define GX_IMAGE_FORMAT_16BPP_GRAY_ALPHA        0xf5 /* Internal format: gray: alpha stream, 8 bits. */
67 #define GX_IMAGE_FORMAT_32BPP_GRAY_ALPHA        0xf6 /* Internal format: gray: alpha steam, 16bits. */
68 #define GX_IMAGE_FORMAT_24BPP                   0xf7 /* Internal format: r:g:b stream, 8bits for each channel. */
69 #define GX_IMAGE_FORMAT_32BPP                   0xf8 /* Internal format: r:g:b:a strem, 8bits for each channel. */
70 #define GX_IMAGE_FORMAT_48BPP                   0xf9 /* Internal format: r:g:b stream, 16bits for each channel. */
71 #define GX_IMAGE_FORMAT_64BPP                   0xfa /* Internal format: r:g:b:a stream, 16bits for each channel */
72 
73 #define GX_TRANSPARENT_COLOR                    0xff
74 
75 #define GX_PNG_CRC_TABLE_SIZE                   256
76 #define GX_PNG_HUFFMAN_LIT_TABLE_SIZE           286
77 #define GX_PNG_HUFFMAN_LIT_CODE_LEN_TABLE_SIZE  288
78 #define GX_PNG_HUFFMAN_DIST_CODE_LEN_TABLE_SIZE 32
79 #define GX_PNG_HUFFMAN_DIST_TABLE_SIZE          30
80 #define GX_PNG_PALETTE_TABLE_SIZE               256
81 #define GX_PNG_SCRATCH_BUFFER_SIZE              (GX_PNG_CRC_TABLE_SIZE +                   \
82                                                  GX_PNG_HUFFMAN_LIT_TABLE_SIZE +           \
83                                                  GX_PNG_HUFFMAN_LIT_CODE_LEN_TABLE_SIZE +  \
84                                                  GX_PNG_HUFFMAN_DIST_CODE_LEN_TABLE_SIZE + \
85                                                  GX_PNG_PALETTE_TABLE_SIZE)
86 #define JPG_MAX_COMPONENTS                       3
87 #define HUFF_TABLE_DIMENSION                     2
88 #define JPG_QUANT_TABLE_DIMENSION                4
89 
90 /* Control block used internally for jpeg reader.  */
91 
92 typedef struct GX_JPEG_INFO_STRUCT
93 {
94     UINT             (*gx_jpeg_mcu_draw)(struct GX_JPEG_INFO_STRUCT *, INT, INT);
95     USHORT           gx_jpeg_width;
96     USHORT           gx_jpeg_height;
97     INT              gx_jpeg_num_of_components;
98     GX_UBYTE         gx_jpeg_sample_factor[JPG_MAX_COMPONENTS];
99     GX_UBYTE         gx_jpeg_component_id[JPG_MAX_COMPONENTS];
100     GX_UBYTE         gx_jpeg_qantization_table_index[JPG_MAX_COMPONENTS];
101     GX_UBYTE         gx_jpeg_dc_table_index[JPG_MAX_COMPONENTS];
102     GX_UBYTE         gx_jpeg_ac_table_index[JPG_MAX_COMPONENTS];
103     INT              gx_jpeg_quantization_table[JPG_QUANT_TABLE_DIMENSION][64];
104     INT             *gx_jpeg_huffman_table[HUFF_TABLE_DIMENSION][HUFF_TABLE_DIMENSION];
105     GX_VALUE         gx_jpeg_huffman_bits_count[HUFF_TABLE_DIMENSION][HUFF_TABLE_DIMENSION][16];
106     INT              gx_jpeg_restart_interval;
107     GX_UBYTE         gx_jpeg_Y_block[256];
108     GX_UBYTE         gx_jpeg_Cr_block[64];
109     GX_UBYTE         gx_jpeg_Cb_block[64];
110     INT              gx_jpeg_pre_dc[JPG_MAX_COMPONENTS];
111     INT              gx_jpeg_vecter[64];
112     GX_UBYTE        *gx_jpeg_data;
113     INT              gx_jpeg_data_size;
114     INT              gx_jpeg_data_index;
115     GX_UBYTE        *gx_jpeg_decoded_data;
116     UINT             gx_jpeg_decoded_data_size;
117     GX_DRAW_CONTEXT *gx_jpeg_draw_context;
118     INT              gx_jpeg_draw_xpos;
119     INT              gx_jpeg_draw_ypos;
120 } GX_JPEG_INFO;
121 
122 /* control block used internally for png reader */
123 typedef struct GX_PNG_STRUCT
124 {
125     GX_UBYTE *gx_png_data;
126     INT       gx_png_data_size;
127     INT       gx_png_data_index;
128     GX_UBYTE *gx_png_decoded_data;
129     INT       gx_png_decoded_data_len;
130     INT       gx_png_width;
131     INT       gx_png_height;
132     GX_UBYTE  gx_png_bit_depth;
133     INT       gx_png_color_type;
134     GX_UBYTE  gx_png_bpp; /* bits per pixel */
135     INT       gx_png_compression_method;
136     INT       gx_png_filter_method;
137     INT       gx_png_interlace_method;
138     UINT     *gx_png_crc_table;   /* 256 */
139     INT       gx_png_gamma;
140     INT       gx_png_huffman_clen_table[20];
141     INT       gx_png_huffman_clen_bits_count[17];
142     INT      *gx_png_huffman_lit_table;    /* 286 */
143     INT      *gx_png_huffman_lit_code_len; /* 286 */
144     INT       gx_png_huffman_lit_bits_count[17];
145     INT       gx_png_huffman_dist_table[GX_PNG_HUFFMAN_DIST_TABLE_SIZE];
146     INT      *gx_png_huffman_dist_code_len; /* 32 */
147     INT       gx_png_huffman_dist_bits_count[17];
148     GX_COLOR *gx_png_palette_table;         /* 256 */
149     INT       gx_png_palette_table_size;
150     GX_COLOR *gx_png_trans;
151     INT       gx_png_trans_num;
152     INT       gx_png_trunk_end_index;
153     UINT      gx_png_trunk_crc;
154 } GX_PNG;
155 
156 /* Define API functions for image reader.  */
157 UINT _gx_image_reader_start(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap);
158 UINT _gx_image_reader_colorspace_convert(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap);
159 
160 UINT _gx_image_reader_create(GX_IMAGE_READER *image_reader,
161                              GX_CONST GX_UBYTE *read_data,
162                              INT read_data_size,
163                              GX_UBYTE color_format,
164                              GX_UBYTE mode);
165 
166 /* Define internal function prototypes.  */
167 UINT _gx_image_reader_png_decode(GX_CONST GX_UBYTE *read_data, ULONG read_data_size, GX_PIXELMAP *outmap);
168 UINT _gx_image_reader_jpeg_decode(GX_CONST GX_UBYTE *read_data, ULONG data_size, GX_PIXELMAP *outmap);
169 UINT _gx_image_reader_jpeg_mcu_decode(GX_CONST GX_UBYTE * read_data, ULONG data_size,
170                                       GX_DRAW_CONTEXT * context, INT xpos, INT ypos,
171                                       UINT(draw_function)(GX_JPEG_INFO *, INT, INT));
172 
173 
174 UINT _gx_image_reader_pixel_read_callback_set(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap);
175 UINT _gx_image_reader_pixel_write_callback_set(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap);
176 UINT _gx_image_reader_image_decode(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap);
177 UINT _gx_image_reader_palette_set(GX_IMAGE_READER *image_reader, GX_COLOR *pal, UINT palsize);
178 UINT _gx_image_reader_nearest_palette_color_get(GX_IMAGE_READER *image_reader, GX_PIXEL *want_color, INT *index);
179 VOID _gx_image_reader_rgb2gray(GX_PIXEL *pixel, GX_UBYTE *gray);
180 UINT _gx_image_reader_rle_encode(GX_IMAGE_READER *image_reader, GX_PIXELMAP *pRLEmap);
181 UINT _gx_image_reader_rle_encode_size_get(GX_IMAGE_READER *image_reader, UINT *data_size, UINT *aux_size);
182 VOID _gx_image_reader_rgb2gray(GX_PIXEL *pixel, GX_UBYTE *gray);
183 
184 /* Define error checking shells for API services.  These are only referenced by the
185    application.  */
186 
187 UINT _gxe_image_reader_create(GX_IMAGE_READER *image_reader,
188                               GX_CONST GX_UBYTE  *read_data,
189                               INT read_data_size,
190                               GX_UBYTE color_format,
191                               GX_UBYTE mode);
192 
193 UINT _gxe_image_reader_palette_set(GX_IMAGE_READER *image_reader, GX_COLOR *pal, UINT palsize);
194 UINT _gxe_image_reader_start(GX_IMAGE_READER *image_reader, GX_PIXELMAP *outmap);
195 
196 
197 #endif
198 #endif
199 
200