• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef __WPRINT_IMAGE__
20 #define __WPRINT_IMAGE__
21 
22 #include <stdio.h>
23 #include "mime_types.h"
24 #include "wprint_scaler.h"
25 #include "wprint_debug.h"
26 #include "ifc_wprint.h"
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
32 
33 #define BYTES_PER_PIXEL(X)  ((X)*3)
34 #define BITS_PER_CHANNEL    8
35 
36 /*
37  * Rotations to apply while decoding
38  */
39 typedef enum {
40     ROT_0 = 0,
41     ROT_90,
42     ROT_180,
43     ROT_270,
44 } wprint_rotation_t;
45 
46 /*
47  * Location(s) for padding to be applied while decoding
48  */
49 typedef enum {
50     PAD_TOP = (1 << 0),
51     PAD_LEFT = (1 << 1),
52     PAD_RIGHT = (1 << 2),
53     PAD_BOTTOM = (1 << 3),
54 } wprint_padding_t;
55 
56 #define PAD_NONE  (0)
57 #define PAD_PRINT (PAD_TOP | PAD_LEFT)
58 #define PAD_ALL   (PAD_TOP | PAD_LEFT | PAD_RIGHT | PAD_BOTTOM)
59 
60 #define __DEFINE_WPRINT_PLATFORM_TYPES__
61 
62 #include "wprint_image_platform.h"
63 #include "lib_wprint.h"
64 
65 #undef __DEFINE_WPRINT_PLATFORM_TYPES__
66 
67 /*
68  * Define an image which can be decoded into a stream
69  */
70 typedef struct {
71     // file information
72     const char *mime_type;
73     FILE *imgfile;
74 
75     // interfaces
76     const struct _image_decode_ifc_st *decode_ifc;
77     const ifc_wprint_t *wprint_ifc;
78 
79     // image dimensions
80     unsigned int width;
81     unsigned int height;
82     unsigned int sampled_width;
83     unsigned int sampled_height;
84 
85     // printable area information
86     unsigned int printable_width;
87     unsigned int printable_height;
88     unsigned int print_resolution;
89     unsigned int render_flags;
90 
91     // output information
92     unsigned int output_width;
93     unsigned int output_height;
94     int num_components;
95     int pdf_render_resolution;
96 
97     // memory optimization parameters
98     unsigned int stripe_height;
99     unsigned int concurrent_stripes;
100     unsigned int output_rows;
101 
102     // scaling parameters
103     unsigned int scaled_sample_size;
104     unsigned int scaled_width;
105     unsigned int scaled_height;
106     int unscaled_start_row;
107     int unscaled_end_row;
108     unsigned int unscaled_rows_needed;
109     unsigned char *unscaled_rows;
110     unsigned int mixed_memory_needed;
111     unsigned char *mixed_memory;
112     unsigned char scaling_needed;
113     scaler_config_t scaler_config;
114 
115     // padding parameters
116     unsigned int output_padding_top;
117     unsigned int output_padding_bottom;
118     unsigned int output_padding_left;
119     unsigned int output_padding_right;
120     unsigned int padding_options;
121 
122     // decoding information
123     wprint_rotation_t rotation;
124     unsigned int row_offset;
125     unsigned int col_offset;
126     int swath_start;
127     int rows_cached;
128     unsigned char **output_cache;
129     int output_swath_start;
130     decoder_data_t decoder_data;
131 } wprint_image_info_t;
132 
133 /*
134  * Defines an interface for decoding images
135  */
136 typedef struct _image_decode_ifc_st {
137     /*
138      * Prepare for decoding of the specified image
139      */
140     void (*init)(wprint_image_info_t *image_info);
141 
142     /*
143      * Prepare for decoding of an image
144      */
145     status_t (*get_hdr)(wprint_image_info_t *image_info);
146 
147     /*
148      * Supply image data at the specified row
149      */
150     unsigned char *(*decode_row)(wprint_image_info_t *, int row);
151 
152     /*
153      * Release all resources related to the image
154      */
155     status_t (*cleanup)(wprint_image_info_t *image_info);
156 
157     /*
158      * Return OK if subsampling is supported by this decoder
159      */
160     status_t (*supports_subsampling)(wprint_image_info_t *image_info);
161 
162     /*
163      * Return resolution in DPI
164      */
165     int (*native_units)(wprint_image_info_t *image_info);
166 } image_decode_ifc_t;
167 
168 /*
169  * Return the appropriate decoding object corresponding to the image
170  */
171 const image_decode_ifc_t *wprint_image_get_decode_ifc(wprint_image_info_t *image_info);
172 
173 /*
174  * Initializes image_info with supplied parameters
175  */
176 void wprint_image_setup(wprint_image_info_t *image_info, const char *mime_type,
177         const ifc_wprint_t *wprint_ifc, unsigned int output_resolution, int pdf_render_resolution);
178 
179 /*
180  * Open an initialized image from a file
181  */
182 status_t wprint_image_get_info(FILE *imgfile, wprint_image_info_t *image_info);
183 
184 /*
185  * Configure image_info parameters as supplied
186  */
187 status_t wprint_image_set_output_properties(wprint_image_info_t *image_info,
188         wprint_rotation_t rotation, unsigned int printable_width, unsigned int printable_height,
189         unsigned int top_margin, unsigned int left_margin, unsigned int right_margin,
190         unsigned int bottom_margin, unsigned int render_flags, unsigned int max_decode_stripe,
191         unsigned int concurrent_stripes, unsigned int padding_options, pcl_t pclenum);
192 
193 /*
194  * Return true if the image is wider than it is high (landscape orientation)
195  */
196 bool wprint_image_is_landscape(wprint_image_info_t *image_info);
197 
198 /*
199  * Return the size required to render the image
200  */
201 int wprint_image_get_output_buff_size(wprint_image_info_t *image_info);
202 
203 /*
204  * Return the full image width, including any padding
205  */
206 int wprint_image_get_width(wprint_image_info_t *image_info);
207 
208 /*
209  * Return the full image height, including any padding
210  */
211 int wprint_image_get_height(wprint_image_info_t *image_info);
212 
213 /*
214  * Decode a single stripe of data into rgb_pixels, storing height rendered and returning
215  * bytes processed or 0/negative on error.
216  */
217 int wprint_image_decode_stripe(wprint_image_info_t *image_info, int start_row, int *height,
218         unsigned char *rgb_pixels);
219 
220 /*
221  * Compute and allocate memory in preparation for decoding row data, returning the number of rows
222  */
223 int wprint_image_compute_rows_to_cache(wprint_image_info_t *image_info);
224 
225 /*
226  * Return the current number of cached rows
227  */
228 int wprint_image_input_rows_cached(wprint_image_info_t *image_info);
229 
230 /*
231  * Free all image resources
232  */
233 void wprint_image_cleanup(wprint_image_info_t *image_info);
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #define __DEFINE_WPRINT_PLATFORM_METHODS__
240 
241 #include "wprint_image_platform.h"
242 
243 #undef __DEFINE_WPRINT_PLATFORM_METHODS__
244 
245 #endif // __WPRINT_IMAGE__