2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
67 * $Id: jas_image.h,v 1.1 2005/05/22 18:33:01 malaterre Exp $
73 /******************************************************************************\
75 \******************************************************************************/
77 #include <jasper/jas_config.h>
78 #include <jasper/jas_stream.h>
79 #include <jasper/jas_seq.h>
80 #include <jasper/jas_cm.h>
87 /******************************************************************************\
89 \******************************************************************************/
92 * Miscellaneous constants.
95 /* The threshold at which image data is no longer stored in memory. */
96 #define JAS_IMAGE_INMEMTHRESH (16 * 1024 * 1024)
102 #define JAS_IMAGE_CT_UNKNOWN 0x10000
103 #define JAS_IMAGE_CT_COLOR(n) ((n) & 0x7fff)
104 #define JAS_IMAGE_CT_OPACITY 0x08000
106 #define JAS_IMAGE_CT_RGB_R 0
107 #define JAS_IMAGE_CT_RGB_G 1
108 #define JAS_IMAGE_CT_RGB_B 2
110 #define JAS_IMAGE_CT_YCBCR_Y 0
111 #define JAS_IMAGE_CT_YCBCR_CB 1
112 #define JAS_IMAGE_CT_YCBCR_CR 2
114 #define JAS_IMAGE_CT_GRAY_Y 0
116 /******************************************************************************\
118 \******************************************************************************/
120 /* Image coordinate. */
121 typedef int_fast32_t jas_image_coord_t;
123 /* Color space (e.g., RGB, YCbCr). */
124 typedef int_fast16_t jas_image_colorspc_t;
126 /* Component type (e.g., color, opacity). */
127 typedef int_fast32_t jas_image_cmpttype_t;
129 /* Component sample data format (e.g., real/integer, signedness, precision). */
130 typedef int_fast16_t jas_image_smpltype_t;
132 /******************************************************************************\
133 * Image class and supporting classes.
134 \******************************************************************************/
136 /* Image component class. */
140 jas_image_coord_t tlx_;
141 /* The x-coordinate of the top-left corner of the component. */
143 jas_image_coord_t tly_;
144 /* The y-coordinate of the top-left corner of the component. */
146 jas_image_coord_t hstep_;
147 /* The horizontal sampling period in units of the reference grid. */
149 jas_image_coord_t vstep_;
150 /* The vertical sampling period in units of the reference grid. */
152 jas_image_coord_t width_;
153 /* The component width in samples. */
155 jas_image_coord_t height_;
156 /* The component height in samples. */
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. */
167 /* The signedness of the sample data. */
170 jas_stream_t *stream_;
171 /* The stream containing the component data. */
174 /* The number of characters per sample in the stream. */
176 jas_image_cmpttype_t type_;
177 /* The type of component (e.g., opacity, red, green, blue, luma). */
185 jas_image_coord_t tlx_;
186 /* The x-coordinate of the top-left corner of the image bounding box. */
188 jas_image_coord_t tly_;
189 /* The y-coordinate of the top-left corner of the image bounding box. */
191 jas_image_coord_t brx_;
192 /* The x-coordinate of the bottom-right corner of the image bounding
195 jas_image_coord_t bry_;
196 /* The y-coordinate of the bottom-right corner of the image bounding
200 /* The number of components. */
203 /* The maximum number of components that this image can have (i.e., the
204 allocated size of the components array). */
206 jas_image_cmpt_t **cmpts_;
207 /* Per-component information. */
209 jas_clrspc_t clrspc_;
211 jas_cmprof_t *cmprof_;
217 /* Component parameters class. */
218 /* This data type exists solely/mainly for the purposes of the
219 jas_image_create function. */
223 jas_image_coord_t tlx;
224 /* The x-coordinate of the top-left corner of the component. */
226 jas_image_coord_t tly;
227 /* The y-coordinate of the top-left corner of the component. */
229 jas_image_coord_t hstep;
230 /* The horizontal sampling period in units of the reference grid. */
232 jas_image_coord_t vstep;
233 /* The vertical sampling period in units of the reference grid. */
235 jas_image_coord_t width;
236 /* The width of the component in samples. */
238 jas_image_coord_t height;
239 /* The height of the component in samples. */
245 /* The precision of the component sample data. */
248 /* The signedness of the component sample data. */
251 } jas_image_cmptparm_t;
253 /******************************************************************************\
254 * File format related classes.
255 \******************************************************************************/
257 #define JAS_IMAGE_MAXFMTS 32
258 /* The maximum number of image data formats supported. */
260 /* Image format-dependent operations. */
264 jas_image_t *(*decode)(jas_stream_t *in, char *opts);
265 /* Decode image data from a stream. */
267 int (*encode)(jas_image_t *image, jas_stream_t *out, char *opts);
268 /* Encode image data to a stream. */
270 int (*validate)(jas_stream_t *in);
271 /* Determine if stream data is in a particular format. */
273 } jas_image_fmtops_t;
275 /* Image format information. */
280 /* The ID for this format. */
283 /* The name by which this format is identified. */
286 /* The file name extension associated with this format. */
289 /* A brief description of the format. */
291 jas_image_fmtops_t ops;
292 /* The operations for this format. */
294 } jas_image_fmtinfo_t;
296 /******************************************************************************\
298 \******************************************************************************/
300 /* Create an image. */
301 jas_image_t *jas_image_create(int numcmpts,
302 jas_image_cmptparm_t *cmptparms, jas_clrspc_t clrspc);
304 /* Create an "empty" image. */
305 jas_image_t *jas_image_create0(void);
307 /* Clone an image. */
308 jas_image_t *jas_image_copy(jas_image_t *image);
310 /* Deallocate any resources associated with an image. */
311 void jas_image_destroy(jas_image_t *image);
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_)
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_)
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) \
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) \
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) \
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) \
341 /* Get the number of image components. */
342 #define jas_image_numcmpts(image) \
345 /* Get the color model used by the image. */
346 #define jas_image_clrspc(image) \
349 /* Set the color model for an image. */
350 #define jas_image_setclrspc(image, clrspc) \
351 ((image)->clrspc_ = (clrspc))
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))
358 /* Get the width of a component. */
359 #define jas_image_cmptwidth(image, cmptno) \
360 ((image)->cmpts_[cmptno]->width_)
362 /* Get the height of a component. */
363 #define jas_image_cmptheight(image, cmptno) \
364 ((image)->cmpts_[cmptno]->height_)
366 /* Get the signedness of the sample data for a component. */
367 #define jas_image_cmptsgnd(image, cmptno) \
368 ((image)->cmpts_[cmptno]->sgnd_)
370 /* Get the precision of the sample data for a component. */
371 #define jas_image_cmptprec(image, cmptno) \
372 ((image)->cmpts_[cmptno]->prec_)
374 /* Get the horizontal subsampling factor for a component. */
375 #define jas_image_cmpthstep(image, cmptno) \
376 ((image)->cmpts_[cmptno]->hstep_)
378 /* Get the vertical subsampling factor for a component. */
379 #define jas_image_cmptvstep(image, cmptno) \
380 ((image)->cmpts_[cmptno]->vstep_)
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_)
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_)
390 /* Get the x-coordinate of the bottom-right corner of a component
392 #define jas_image_cmptbrx(image, cmptno) \
393 ((image)->cmpts_[cmptno]->tlx_ + (image)->cmpts_[cmptno]->width_ * \
394 (image)->cmpts_[cmptno]->hstep_)
396 /* Get the y-coordinate of the bottom-right corner of a component
398 #define jas_image_cmptbry(image, cmptno) \
399 ((image)->cmpts_[cmptno]->tly_ + (image)->cmpts_[cmptno]->height_ * \
400 (image)->cmpts_[cmptno]->vstep_)
402 /* Get the raw size of an image (i.e., the nominal size of the image without
404 uint_fast32_t jas_image_rawsize(jas_image_t *image);
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);
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,
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,
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,
425 /* Delete a component from an image. */
426 void jas_image_delcmpt(jas_image_t *image, int cmptno);
428 /* Add a component to an image. */
429 int jas_image_addcmpt(jas_image_t *image, int cmptno,
430 jas_image_cmptparm_t *cmptparm);
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);
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)
441 #define jas_image_cmptdtype(image, cmptno) \
442 (JAS_IMAGE_CDT_SETSGND((image)->cmpts_[cmptno]->sgnd_) | JAS_IMAGE_CDT_SETPREC((image)->cmpts_[cmptno]->prec_))
444 int jas_image_depalettize(jas_image_t *image, int cmptno, int numlutents,
445 int_fast32_t *lutents, int dtype, int newcmptno);
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,
451 int jas_image_getcmptbytype(jas_image_t *image, int ctype);
453 /******************************************************************************\
454 * Image format-related operations.
455 \******************************************************************************/
457 /* Clear the table of image formats. */
458 void jas_image_clearfmts(void);
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);
464 /* Get the ID for the image format with the specified name. */
465 int jas_image_strtofmt(char *s);
467 /* Get the name of the image format with the specified ID. */
468 char *jas_image_fmttostr(int fmt);
470 /* Lookup image format information by the format ID. */
471 jas_image_fmtinfo_t *jas_image_lookupfmtbyid(int id);
473 /* Lookup image format information by the format name. */
474 jas_image_fmtinfo_t *jas_image_lookupfmtbyname(const char *name);
476 /* Guess the format of an image file based on its name. */
477 int jas_image_fmtfromname(char *filename);
479 /* Get the format of image data in a stream. */
480 int jas_image_getfmt(jas_stream_t *in);
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,
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,
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,
498 void jas_image_dump(jas_image_t *image, FILE *out);
500 /******************************************************************************\
501 * Image format-dependent operations.
502 \******************************************************************************/
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);
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);
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);
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);
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);
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);
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);
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);