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
29extern "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 */
39typedef 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 */
49typedef 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
64#undef __DEFINE_WPRINT_PLATFORM_TYPES__
65
66/*
67 * Define an image which can be decoded into a stream
68 */
69typedef struct {
70    // file information
71    const char *mime_type;
72    FILE *imgfile;
73
74    // interfaces
75    const struct _image_decode_ifc_st *decode_ifc;
76    const ifc_wprint_t *wprint_ifc;
77
78    // image dimensions
79    unsigned int width;
80    unsigned int height;
81    unsigned int sampled_width;
82    unsigned int sampled_height;
83
84    // printable area information
85    unsigned int printable_width;
86    unsigned int printable_height;
87    unsigned int print_resolution;
88    unsigned int render_flags;
89
90    // output information
91    unsigned int output_width;
92    unsigned int output_height;
93    int num_components;
94    int pdf_render_resolution;
95
96    // memory optimization parameters
97    unsigned int stripe_height;
98    unsigned int concurrent_stripes;
99    unsigned int output_rows;
100
101    // scaling parameters
102    unsigned int scaled_sample_size;
103    unsigned int scaled_width;
104    unsigned int scaled_height;
105    int unscaled_start_row;
106    int unscaled_end_row;
107    unsigned int unscaled_rows_needed;
108    unsigned char *unscaled_rows;
109    unsigned int mixed_memory_needed;
110    unsigned char *mixed_memory;
111    unsigned char scaling_needed;
112    scaler_config_t scaler_config;
113
114    // padding parameters
115    unsigned int output_padding_top;
116    unsigned int output_padding_bottom;
117    unsigned int output_padding_left;
118    unsigned int output_padding_right;
119    unsigned int padding_options;
120
121    // decoding information
122    wprint_rotation_t rotation;
123    unsigned int row_offset;
124    unsigned int col_offset;
125    int swath_start;
126    int rows_cached;
127    unsigned char **output_cache;
128    int output_swath_start;
129    decoder_data_t decoder_data;
130} wprint_image_info_t;
131
132/*
133 * Defines an interface for decoding images
134 */
135typedef struct _image_decode_ifc_st {
136    /*
137     * Prepare for decoding of the specified image
138     */
139    void (*init)(wprint_image_info_t *image_info);
140
141    /*
142     * Prepare for decoding of an image
143     */
144    status_t (*get_hdr)(wprint_image_info_t *image_info);
145
146    /*
147     * Supply image data at the specified row
148     */
149    unsigned char *(*decode_row)(wprint_image_info_t *, int row);
150
151    /*
152     * Release all resources related to the image
153     */
154    status_t (*cleanup)(wprint_image_info_t *image_info);
155
156    /*
157     * Return OK if subsampling is supported by this decoder
158     */
159    status_t (*supports_subsampling)(wprint_image_info_t *image_info);
160
161    /*
162     * Return resolution in DPI
163     */
164    int (*native_units)(wprint_image_info_t *image_info);
165} image_decode_ifc_t;
166
167/*
168 * Return the appropriate decoding object corresponding to the image
169 */
170const image_decode_ifc_t *wprint_image_get_decode_ifc(wprint_image_info_t *image_info);
171
172/*
173 * Initializes image_info with supplied parameters
174 */
175void wprint_image_setup(wprint_image_info_t *image_info, const char *mime_type,
176        const ifc_wprint_t *wprint_ifc, unsigned int output_resolution, int pdf_render_resolution);
177
178/*
179 * Open an initialized image from a file
180 */
181status_t wprint_image_get_info(FILE *imgfile, wprint_image_info_t *image_info);
182
183/*
184 * Configure image_info parameters as supplied
185 */
186status_t wprint_image_set_output_properties(wprint_image_info_t *image_info,
187        wprint_rotation_t rotation, unsigned int printable_width, unsigned int printable_height,
188        unsigned int top_margin, unsigned int left_margin, unsigned int right_margin,
189        unsigned int bottom_margin, unsigned int render_flags, unsigned int max_decode_stripe,
190        unsigned int concurrent_stripes, unsigned int padding_options);
191
192/*
193 * Return true if the image is wider than it is high (landscape orientation)
194 */
195bool wprint_image_is_landscape(wprint_image_info_t *image_info);
196
197/*
198 * Return the size required to render the image
199 */
200int wprint_image_get_output_buff_size(wprint_image_info_t *image_info);
201
202/*
203 * Return the full image width, including any padding
204 */
205int wprint_image_get_width(wprint_image_info_t *image_info);
206
207/*
208 * Return the full image height, including any padding
209 */
210int wprint_image_get_height(wprint_image_info_t *image_info);
211
212/*
213 * Decode a single stripe of data into rgb_pixels, storing height rendered and returning
214 * bytes processed or 0/negative on error.
215 */
216int wprint_image_decode_stripe(wprint_image_info_t *image_info, int start_row, int *height,
217        unsigned char *rgb_pixels);
218
219/*
220 * Compute and allocate memory in preparation for decoding row data, returning the number of rows
221 */
222int wprint_image_compute_rows_to_cache(wprint_image_info_t *image_info);
223
224/*
225 * Return the current number of cached rows
226 */
227int wprint_image_input_rows_cached(wprint_image_info_t *image_info);
228
229/*
230 * Free all image resources
231 */
232void wprint_image_cleanup(wprint_image_info_t *image_info);
233
234#ifdef __cplusplus
235}
236#endif
237
238#define __DEFINE_WPRINT_PLATFORM_METHODS__
239
240#include "wprint_image_platform.h"
241
242#undef __DEFINE_WPRINT_PLATFORM_METHODS__
243
244#endif // __WPRINT_IMAGE__