1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2003 Michael David Adams.
5  * All rights reserved.
6  */
7 
8 /* __START_OF_JASPER_LICENSE__
9  *
10  * JasPer License Version 2.0
11  *
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  *
16  * All rights reserved.
17  *
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  *
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  *
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  *
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  *
61  * __END_OF_JASPER_LICENSE__
62  */
63 
64 /*
65  * Image Class
66  *
67  * $Id$
68  */
69 
70 #ifndef JAS_IMAGE_H
71 #define JAS_IMAGE_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 #include <jasper/jas_config.h>
78 #include <jasper/jas_stream.h>
79 #include <jasper/jas_seq.h>
80 #include <jasper/jas_cm.h>
81 #include <stdio.h>
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 /******************************************************************************\
88 * Constants.
89 \******************************************************************************/
90 
91 /*
92  * Miscellaneous constants.
93  */
94 
95 /* The threshold at which image data is no longer stored in memory. */
96 #define JAS_IMAGE_INMEMTHRESH	(32 * 1024 * 1024)
97 
98 /*
99  * Component types
100  */
101 
102 #define	JAS_IMAGE_CT_UNKNOWN	0x10000
103 #define	JAS_IMAGE_CT_COLOR(n)	((n) & 0x7fff)
104 #define	JAS_IMAGE_CT_OPACITY	0x08000
105 
106 #define	JAS_IMAGE_CT_RGB_R	0
107 #define	JAS_IMAGE_CT_RGB_G	1
108 #define	JAS_IMAGE_CT_RGB_B	2
109 
110 #define	JAS_IMAGE_CT_YCBCR_Y	0
111 #define	JAS_IMAGE_CT_YCBCR_CB	1
112 #define	JAS_IMAGE_CT_YCBCR_CR	2
113 
114 #define	JAS_IMAGE_CT_GRAY_Y	0
115 
116 /******************************************************************************\
117 * Simple types.
118 \******************************************************************************/
119 
120 /* Image coordinate. */
121 typedef int_fast32_t jas_image_coord_t;
122 
123 /* Color space (e.g., RGB, YCbCr). */
124 typedef int_fast16_t jas_image_colorspc_t;
125 
126 /* Component type (e.g., color, opacity). */
127 typedef int_fast32_t jas_image_cmpttype_t;
128 
129 /* Component sample data format (e.g., real/integer, signedness, precision). */
130 typedef int_fast16_t jas_image_smpltype_t;
131 
132 /******************************************************************************\
133 * Image class and supporting classes.
134 \******************************************************************************/
135 
136 /* Image component class. */
137 
138 typedef struct {
139 
140 	jas_image_coord_t tlx_;
141 	/* The x-coordinate of the top-left corner of the component. */
142 
143 	jas_image_coord_t tly_;
144 	/* The y-coordinate of the top-left corner of the component. */
145 
146 	jas_image_coord_t hstep_;
147 	/* The horizontal sampling period in units of the reference grid. */
148 
149 	jas_image_coord_t vstep_;
150 	/* The vertical sampling period in units of the reference grid. */
151 
152 	jas_image_coord_t width_;
153 	/* The component width in samples. */
154 
155 	jas_image_coord_t height_;
156 	/* The component height in samples. */
157 
158 #ifdef FIX_ME
159 	int smpltype_;
160 #else
161 	int prec_;
162 	/* The precision of the sample data (i.e., the number of bits per
163 	sample).  If the samples are signed values, this quantity
164 	includes the sign bit. */
165 
166 	int sgnd_;
167 	/* The signedness of the sample data. */
168 #endif
169 
170 	jas_stream_t *stream_;
171 	/* The stream containing the component data. */
172 
173 	int cps_;
174 	/* The number of characters per sample in the stream. */
175 
176 	jas_image_cmpttype_t type_;
177 	/* The type of component (e.g., opacity, red, green, blue, luma). */
178 
179 } jas_image_cmpt_t;
180 
181 /* Image class. */
182 
183 typedef struct {
184 
185 	jas_image_coord_t tlx_;
186 	/* The x-coordinate of the top-left corner of the image bounding box. */
187 
188 	jas_image_coord_t tly_;
189 	/* The y-coordinate of the top-left corner of the image bounding box. */
190 
191 	jas_image_coord_t brx_;
192 	/* The x-coordinate of the bottom-right corner of the image bounding
193 	  box (plus one). */
194 
195 	jas_image_coord_t bry_;
196 	/* The y-coordinate of the bottom-right corner of the image bounding
197 	  box (plus one). */
198 
199 	int numcmpts_;
200 	/* The number of components. */
201 
202 	int maxcmpts_;
203 	/* The maximum number of components that this image can have (i.e., the
204 	  allocated size of the components array). */
205 
206 	jas_image_cmpt_t **cmpts_;
207 	/* Per-component information. */
208 
209 	jas_clrspc_t clrspc_;
210 
211 	jas_cmprof_t *cmprof_;
212 
213 	bool inmem_;
214 
215 } jas_image_t;
216 
217 /* Component parameters class. */
218 /* This data type exists solely/mainly for the purposes of the
219   jas_image_create function. */
220 
221 typedef struct {
222 
223 	jas_image_coord_t tlx;
224 	/* The x-coordinate of the top-left corner of the component. */
225 
226 	jas_image_coord_t tly;
227 	/* The y-coordinate of the top-left corner of the component. */
228 
229 	jas_image_coord_t hstep;
230 	/* The horizontal sampling period in units of the reference grid. */
231 
232 	jas_image_coord_t vstep;
233 	/* The vertical sampling period in units of the reference grid. */
234 
235 	jas_image_coord_t width;
236 	/* The width of the component in samples. */
237 
238 	jas_image_coord_t height;
239 	/* The height of the component in samples. */
240 
241 #ifdef FIX_ME
242 	int smpltype;
243 #else
244 	int prec;
245 	/* The precision of the component sample data. */
246 
247 	int sgnd;
248 	/* The signedness of the component sample data. */
249 #endif
250 
251 } jas_image_cmptparm_t;
252 
253 /******************************************************************************\
254 * File format related classes.
255 \******************************************************************************/
256 
257 #define	JAS_IMAGE_MAXFMTS	32
258 /* The maximum number of image data formats supported. */
259 
260 /* Image format-dependent operations. */
261 
262 typedef struct {
263 
264 	jas_image_t *(*decode)(jas_stream_t *in, char *opts);
265 	/* Decode image data from a stream. */
266 
267 	int (*encode)(jas_image_t *image, jas_stream_t *out, char *opts);
268 	/* Encode image data to a stream. */
269 
270 	int (*validate)(jas_stream_t *in);
271 	/* Determine if stream data is in a particular format. */
272 
273 } jas_image_fmtops_t;
274 
275 /* Image format information. */
276 
277 typedef struct {
278 
279 	int id;
280 	/* The ID for this format. */
281 
282 	char *name;
283 	/* The name by which this format is identified. */
284 
285 	char *ext;
286 	/* The file name extension associated with this format. */
287 
288 	char *desc;
289 	/* A brief description of the format. */
290 
291 	jas_image_fmtops_t ops;
292 	/* The operations for this format. */
293 
294 } jas_image_fmtinfo_t;
295 
296 /******************************************************************************\
297 * Image operations.
298 \******************************************************************************/
299 
300 /* Create an image. */
301 jas_image_t *jas_image_create(int numcmpts,
302   jas_image_cmptparm_t *cmptparms, jas_clrspc_t clrspc);
303 
304 /* Create an "empty" image. */
305 jas_image_t *jas_image_create0(void);
306 
307 /* Clone an image. */
308 jas_image_t *jas_image_copy(jas_image_t *image);
309 
310 /* Deallocate any resources associated with an image. */
311 void jas_image_destroy(jas_image_t *image);
312 
313 /* Get the width of the image in units of the image reference grid. */
314 #define jas_image_width(image) \
315 	((image)->brx_ - (image)->tlx_)
316 
317 /* Get the height of the image in units of the image reference grid. */
318 #define	jas_image_height(image) \
319 	((image)->bry_ - (image)->tly_)
320 
321 /* Get the x-coordinate of the top-left corner of the image bounding box
322   on the reference grid. */
323 #define jas_image_tlx(image) \
324 	((image)->tlx_)
325 
326 /* Get the y-coordinate of the top-left corner of the image bounding box
327   on the reference grid. */
328 #define jas_image_tly(image) \
329 	((image)->tly_)
330 
331 /* Get the x-coordinate of the bottom-right corner of the image bounding box
332   on the reference grid (plus one). */
333 #define jas_image_brx(image) \
334 	((image)->brx_)
335 
336 /* Get the y-coordinate of the bottom-right corner of the image bounding box
337   on the reference grid (plus one). */
338 #define jas_image_bry(image) \
339 	((image)->bry_)
340 
341 /* Get the number of image components. */
342 #define	jas_image_numcmpts(image) \
343 	((image)->numcmpts_)
344 
345 /* Get the color model used by the image. */
346 #define	jas_image_clrspc(image) \
347 	((image)->clrspc_)
348 
349 /* Set the color model for an image. */
350 #define jas_image_setclrspc(image, clrspc) \
351 	((image)->clrspc_ = (clrspc))
352 
353 #define jas_image_cmpttype(image, cmptno) \
354 	((image)->cmpts_[(cmptno)]->type_)
355 #define jas_image_setcmpttype(image, cmptno, type) \
356 	((image)->cmpts_[(cmptno)]->type_ = (type))
357 
358 /* Get the width of a component. */
359 #define	jas_image_cmptwidth(image, cmptno) \
360 	((image)->cmpts_[cmptno]->width_)
361 
362 /* Get the height of a component. */
363 #define	jas_image_cmptheight(image, cmptno) \
364 	((image)->cmpts_[cmptno]->height_)
365 
366 /* Get the signedness of the sample data for a component. */
367 #define	jas_image_cmptsgnd(image, cmptno) \
368 	((image)->cmpts_[cmptno]->sgnd_)
369 
370 /* Get the precision of the sample data for a component. */
371 #define	jas_image_cmptprec(image, cmptno) \
372 	((image)->cmpts_[cmptno]->prec_)
373 
374 /* Get the horizontal subsampling factor for a component. */
375 #define	jas_image_cmpthstep(image, cmptno) \
376 	((image)->cmpts_[cmptno]->hstep_)
377 
378 /* Get the vertical subsampling factor for a component. */
379 #define	jas_image_cmptvstep(image, cmptno) \
380 	((image)->cmpts_[cmptno]->vstep_)
381 
382 /* Get the x-coordinate of the top-left corner of a component. */
383 #define	jas_image_cmpttlx(image, cmptno) \
384 	((image)->cmpts_[cmptno]->tlx_)
385 
386 /* Get the y-coordinate of the top-left corner of a component. */
387 #define	jas_image_cmpttly(image, cmptno) \
388 	((image)->cmpts_[cmptno]->tly_)
389 
390 /* Get the x-coordinate of the bottom-right corner of a component
391   (plus "one"). */
392 #define	jas_image_cmptbrx(image, cmptno) \
393 	((image)->cmpts_[cmptno]->tlx_ + (image)->cmpts_[cmptno]->width_ * \
394 	  (image)->cmpts_[cmptno]->hstep_)
395 
396 /* Get the y-coordinate of the bottom-right corner of a component
397   (plus "one"). */
398 #define	jas_image_cmptbry(image, cmptno) \
399 	((image)->cmpts_[cmptno]->tly_ + (image)->cmpts_[cmptno]->height_ * \
400 	  (image)->cmpts_[cmptno]->vstep_)
401 
402 /* Get the raw size of an image (i.e., the nominal size of the image without
403   any compression. */
404 uint_fast32_t jas_image_rawsize(jas_image_t *image);
405 
406 /* Create an image from a stream in some specified format. */
407 jas_image_t *jas_image_decode(jas_stream_t *in, int fmt, char *optstr);
408 
409 /* Write an image to a stream in a specified format. */
410 int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt,
411   char *optstr);
412 
413 /* Read a rectangular region of an image component. */
414 /* The position and size of the rectangular region to be read is specified
415 relative to the component's coordinate system. */
416 int jas_image_readcmpt(jas_image_t *image, int cmptno,
417   jas_image_coord_t x, jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
418   jas_matrix_t *data);
419 
420 /* Write a rectangular region of an image component. */
421 int jas_image_writecmpt(jas_image_t *image, int cmptno,
422   jas_image_coord_t x, jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
423   jas_matrix_t *data);
424 
425 /* Delete a component from an image. */
426 void jas_image_delcmpt(jas_image_t *image, int cmptno);
427 
428 /* Add a component to an image. */
429 int jas_image_addcmpt(jas_image_t *image, int cmptno,
430   jas_image_cmptparm_t *cmptparm);
431 
432 /* Copy a component from one image to another. */
433 int jas_image_copycmpt(jas_image_t *dstimage, int dstcmptno,
434   jas_image_t *srcimage, int srccmptno);
435 
436 #define	JAS_IMAGE_CDT_GETSGND(dtype) (((dtype) >> 7) & 1)
437 #define	JAS_IMAGE_CDT_SETSGND(dtype) (((dtype) & 1) << 7)
438 #define	JAS_IMAGE_CDT_GETPREC(dtype) ((dtype) & 0x7f)
439 #define	JAS_IMAGE_CDT_SETPREC(dtype) ((dtype) & 0x7f)
440 
441 #define	jas_image_cmptdtype(image, cmptno) \
442 	(JAS_IMAGE_CDT_SETSGND((image)->cmpts_[cmptno]->sgnd_) | JAS_IMAGE_CDT_SETPREC((image)->cmpts_[cmptno]->prec_))
443 
444 int jas_image_depalettize(jas_image_t *image, int cmptno, int numlutents,
445   int_fast32_t *lutents, int dtype, int newcmptno);
446 
447 int jas_image_readcmptsample(jas_image_t *image, int cmptno, int x, int y);
448 void jas_image_writecmptsample(jas_image_t *image, int cmptno, int x, int y,
449   int_fast32_t v);
450 
451 int jas_image_getcmptbytype(jas_image_t *image, int ctype);
452 
453 /******************************************************************************\
454 * Image format-related operations.
455 \******************************************************************************/
456 
457 /* Clear the table of image formats. */
458 void jas_image_clearfmts(void);
459 
460 /* Add entry to table of image formats. */
461 int jas_image_addfmt(int id, char *name, char *ext, char *desc,
462   jas_image_fmtops_t *ops);
463 
464 /* Get the ID for the image format with the specified name. */
465 int jas_image_strtofmt(char *s);
466 
467 /* Get the name of the image format with the specified ID. */
468 char *jas_image_fmttostr(int fmt);
469 
470 /* Lookup image format information by the format ID. */
471 jas_image_fmtinfo_t *jas_image_lookupfmtbyid(int id);
472 
473 /* Lookup image format information by the format name. */
474 jas_image_fmtinfo_t *jas_image_lookupfmtbyname(const char *name);
475 
476 /* Guess the format of an image file based on its name. */
477 int jas_image_fmtfromname(char *filename);
478 
479 /* Get the format of image data in a stream. */
480 int jas_image_getfmt(jas_stream_t *in);
481 
482 
483 #define	jas_image_cmprof(image)	((image)->cmprof_)
484 int jas_image_ishomosamp(jas_image_t *image);
485 int jas_image_sampcmpt(jas_image_t *image, int cmptno, int newcmptno,
486   jas_image_coord_t ho, jas_image_coord_t vo, jas_image_coord_t hs,
487   jas_image_coord_t vs, int sgnd, int prec);
488 int jas_image_writecmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
489   jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
490   long *buf);
491 int jas_image_readcmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
492   jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
493   long *buf);
494 
495 #define	jas_image_setcmprof(image, cmprof) ((image)->cmprof_ = cmprof)
496 jas_image_t *jas_image_chclrspc(jas_image_t *image, jas_cmprof_t *outprof,
497   int intent);
498 void jas_image_dump(jas_image_t *image, FILE *out);
499 
500 /******************************************************************************\
501 * Image format-dependent operations.
502 \******************************************************************************/
503 
504 #if !defined(EXCLUDE_JPG_SUPPORT)
505 /* Format-dependent operations for JPG support. */
506 jas_image_t *jpg_decode(jas_stream_t *in, char *optstr);
507 int jpg_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
508 int jpg_validate(jas_stream_t *in);
509 #endif
510 
511 #if !defined(EXCLUDE_MIF_SUPPORT)
512 /* Format-dependent operations for MIF support. */
513 jas_image_t *mif_decode(jas_stream_t *in, char *optstr);
514 int mif_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
515 int mif_validate(jas_stream_t *in);
516 #endif
517 
518 #if !defined(EXCLUDE_PNM_SUPPORT)
519 /* Format-dependent operations for PNM support. */
520 jas_image_t *pnm_decode(jas_stream_t *in, char *optstr);
521 int pnm_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
522 int pnm_validate(jas_stream_t *in);
523 #endif
524 
525 #if !defined(EXCLUDE_RAS_SUPPORT)
526 /* Format-dependent operations for Sun Rasterfile support. */
527 jas_image_t *ras_decode(jas_stream_t *in, char *optstr);
528 int ras_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
529 int ras_validate(jas_stream_t *in);
530 #endif
531 
532 #if !defined(EXCLUDE_BMP_SUPPORT)
533 /* Format-dependent operations for BMP support. */
534 jas_image_t *bmp_decode(jas_stream_t *in, char *optstr);
535 int bmp_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
536 int bmp_validate(jas_stream_t *in);
537 #endif
538 
539 #if !defined(EXCLUDE_JP2_SUPPORT)
540 /* Format-dependent operations for JP2 support. */
541 jas_image_t *jp2_decode(jas_stream_t *in, char *optstr);
542 int jp2_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
543 int jp2_validate(jas_stream_t *in);
544 #endif
545 
546 #if !defined(EXCLUDE_JPC_SUPPORT)
547 /* Format-dependent operations for JPEG-2000 code stream support. */
548 jas_image_t *jpc_decode(jas_stream_t *in, char *optstr);
549 int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
550 int jpc_validate(jas_stream_t *in);
551 #endif
552 
553 #if !defined(EXCLUDE_PGX_SUPPORT)
554 /* Format-dependent operations for PGX support. */
555 jas_image_t *pgx_decode(jas_stream_t *in, char *optstr);
556 int pgx_encode(jas_image_t *image, jas_stream_t *out, char *optstr);
557 int pgx_validate(jas_stream_t *in);
558 #endif
559 
560 #ifdef __cplusplus
561 }
562 #endif
563 
564 #endif
565