jas_image.h Source File

jas_image.h
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 /* The configuration header file should be included first. */
78 #include <jasper/jas_config.h>
79
80 #include <jasper/jas_stream.h>
81 #include <jasper/jas_seq.h>
82 #include <jasper/jas_cm.h>
83 #include <stdio.h>
84 #include <stdbool.h>
85
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89
90 /******************************************************************************\
91 * Constants.
92 \******************************************************************************/
93
94 /*
95 * Miscellaneous constants.
96 */
97
98 /* Basic units */
99 #define JAS_IMAGE_KIBI (JAS_CAST(size_t, 1024))
100 #define JAS_IMAGE_MEBI (JAS_IMAGE_KIBI * JAS_IMAGE_KIBI)
101
102 /* The threshold at which image data is no longer stored in memory. */
103 #define JAS_IMAGE_INMEMTHRESH (256 * JAS_IMAGE_MEBI)
104
105 /*
106 * Component types
107 */
108
109 #define JAS_IMAGE_CT_UNKNOWN 0x10000
110 #define JAS_IMAGE_CT_COLOR(n) ((n) & 0x7fff)
111 #define JAS_IMAGE_CT_OPACITY 0x08000
112
113 #define JAS_IMAGE_CT_RGB_R 0
114 #define JAS_IMAGE_CT_RGB_G 1
115 #define JAS_IMAGE_CT_RGB_B 2
116
117 #define JAS_IMAGE_CT_YCBCR_Y 0
118 #define JAS_IMAGE_CT_YCBCR_CB 1
119 #define JAS_IMAGE_CT_YCBCR_CR 2
120
121 #define JAS_IMAGE_CT_GRAY_Y 0
122
123 /******************************************************************************\
124 * Simple types.
125 \******************************************************************************/
126
127 /* Image coordinate. */
128 typedef int_fast32_t jas_image_coord_t;
129 #define JAS_IMAGE_COORD_MAX INT_FAST32_MAX
130 #define JAS_IMAGE_COORD_MIN INT_FAST32_MIN
131
132 /* Color space (e.g., RGB, YCbCr). */
133 typedef int_fast16_t jas_image_colorspc_t;
134
135 /* Component type (e.g., color, opacity). */
136 typedef int_fast32_t jas_image_cmpttype_t;
137
138 /* Component sample data format (e.g., real/integer, signedness, precision). */
139 typedef int_fast16_t jas_image_smpltype_t;
140
141 /******************************************************************************\
142 * Image class and supporting classes.
143 \******************************************************************************/
144
145 /* Image component class. */
146
147 typedef struct {
148
149 jas_image_coord_t tlx_;
150 /* The x-coordinate of the top-left corner of the component. */
151
152 jas_image_coord_t tly_;
153 /* The y-coordinate of the top-left corner of the component. */
154
155 jas_image_coord_t hstep_;
156 /* The horizontal sampling period in units of the reference grid. */
157
158 jas_image_coord_t vstep_;
159 /* The vertical sampling period in units of the reference grid. */
160
161 jas_image_coord_t width_;
162 /* The component width in samples. */
163
164 jas_image_coord_t height_;
165 /* The component height in samples. */
166
167 #ifdef FIX_ME
168 int smpltype_;
169 #else
170 int prec_;
171 /* The precision of the sample data (i.e., the number of bits per
172 sample). If the samples are signed values, this quantity
173 includes the sign bit. */
174
175 int sgnd_;
176 /* The signedness of the sample data. */
177 #endif
178
179 jas_stream_t *stream_;
180 /* The stream containing the component data. */
181
182 int cps_;
183 /* The number of characters per sample in the stream. */
184
185 jas_image_cmpttype_t type_;
186 /* The type of component (e.g., opacity, red, green, blue, luma). */
187
188 } jas_image_cmpt_t;
189
190 /* Image class. */
191
192 typedef struct {
193
194 jas_image_coord_t tlx_;
195 /* The x-coordinate of the top-left corner of the image bounding box. */
196
197 jas_image_coord_t tly_;
198 /* The y-coordinate of the top-left corner of the image bounding box. */
199
200 jas_image_coord_t brx_;
201 /* The x-coordinate of the bottom-right corner of the image bounding
202 box (plus one). */
203
204 jas_image_coord_t bry_;
205 /* The y-coordinate of the bottom-right corner of the image bounding
206 box (plus one). */
207
208 int numcmpts_;
209 /* The number of components. */
210
211 int maxcmpts_;
212 /* The maximum number of components that this image can have (i.e., the
213 allocated size of the components array). */
214
215 jas_image_cmpt_t **cmpts_;
216 /* Per-component information. */
217
218 jas_clrspc_t clrspc_;
219
220 jas_cmprof_t *cmprof_;
221
222 // bool inmem_;
223
224 } jas_image_t;
225
226 /* Component parameters class. */
227 /* This data type exists solely/mainly for the purposes of the
228 jas_image_create function. */
229
230 typedef struct {
231
232 jas_image_coord_t tlx;
233 /* The x-coordinate of the top-left corner of the component. */
234
235 jas_image_coord_t tly;
236 /* The y-coordinate of the top-left corner of the component. */
237
238 jas_image_coord_t hstep;
239 /* The horizontal sampling period in units of the reference grid. */
240
241 jas_image_coord_t vstep;
242 /* The vertical sampling period in units of the reference grid. */
243
244 jas_image_coord_t width;
245 /* The width of the component in samples. */
246
247 jas_image_coord_t height;
248 /* The height of the component in samples. */
249
250 #ifdef FIX_ME
251 int smpltype;
252 #else
253 int prec;
254 /* The precision of the component sample data. */
255
256 int sgnd;
257 /* The signedness of the component sample data. */
258 #endif
259
260 } jas_image_cmptparm_t;
261
262 /******************************************************************************\
263 * File format related classes.
264 \******************************************************************************/
265
266 #define JAS_IMAGE_MAXFMTS 32
267 /* The maximum number of image data formats supported. */
268
269 /* Image format-dependent operations. */
270
271 typedef struct {
272
273 jas_image_t *(*decode)(jas_stream_t *in, const char *opts);
274 /* Decode image data from a stream. */
275
276 int (*encode)(jas_image_t *image, jas_stream_t *out, const char *opts);
277 /* Encode image data to a stream. */
278
279 int (*validate)(jas_stream_t *in);
280 /* Determine if stream data is in a particular format. */
281
282 } jas_image_fmtops_t;
283
284 /* Image format information. */
285
286 typedef struct {
287
288 int id;
289 /* The ID for this format. */
290
291 char *name;
292 /* The name by which this format is identified. */
293
294 char *ext;
295 /* The file name extension associated with this format. */
296
297 char *desc;
298 /* A brief description of the format. */
299
300 jas_image_fmtops_t ops;
301 /* The operations for this format. */
302
303 } jas_image_fmtinfo_t;
304
305 /******************************************************************************\
306 * Image operations.
307 \******************************************************************************/
308
309 /* Create an image. */
310 JAS_DLLEXPORT jas_image_t *jas_image_create(int numcmpts,
311 jas_image_cmptparm_t *cmptparms, jas_clrspc_t clrspc);
312
313 /* Create an "empty" image. */
314 jas_image_t *jas_image_create0(void);
315
316 /* Clone an image. */
317 JAS_DLLEXPORT jas_image_t *jas_image_copy(jas_image_t *image);
318
319 /* Deallocate any resources associated with an image. */
320 JAS_DLLEXPORT void jas_image_destroy(jas_image_t *image);
321
322 /* Get the width of the image in units of the image reference grid. */
323 #define jas_image_width(image) \
324 ((image)->brx_ - (image)->tlx_)
325
326 /* Get the height of the image in units of the image reference grid. */
327 #define jas_image_height(image) \
328 ((image)->bry_ - (image)->tly_)
329
330 /* Get the x-coordinate of the top-left corner of the image bounding box
331 on the reference grid. */
332 #define jas_image_tlx(image) \
333 ((image)->tlx_)
334
335 /* Get the y-coordinate of the top-left corner of the image bounding box
336 on the reference grid. */
337 #define jas_image_tly(image) \
338 ((image)->tly_)
339
340 /* Get the x-coordinate of the bottom-right corner of the image bounding box
341 on the reference grid (plus one). */
342 #define jas_image_brx(image) \
343 ((image)->brx_)
344
345 /* Get the y-coordinate of the bottom-right corner of the image bounding box
346 on the reference grid (plus one). */
347 #define jas_image_bry(image) \
348 ((image)->bry_)
349
350 /* Get the number of image components. */
351 #define jas_image_numcmpts(image) \
352 ((image)->numcmpts_)
353
354 /* Get the color model used by the image. */
355 #define jas_image_clrspc(image) \
356 ((image)->clrspc_)
357
358 /* Set the color model for an image. */
359 #define jas_image_setclrspc(image, clrspc) \
360 ((image)->clrspc_ = (clrspc))
361
362 #define jas_image_cmpttype(image, cmptno) \
363 ((image)->cmpts_[(cmptno)]->type_)
364 #define jas_image_setcmpttype(image, cmptno, type) \
365 ((image)->cmpts_[(cmptno)]->type_ = (type))
366
367 /* Get the width of a component. */
368 #define jas_image_cmptwidth(image, cmptno) \
369 ((image)->cmpts_[cmptno]->width_)
370
371 /* Get the height of a component. */
372 #define jas_image_cmptheight(image, cmptno) \
373 ((image)->cmpts_[cmptno]->height_)
374
375 /* Get the signedness of the sample data for a component. */
376 #define jas_image_cmptsgnd(image, cmptno) \
377 ((image)->cmpts_[cmptno]->sgnd_)
378
379 /* Get the precision of the sample data for a component. */
380 #define jas_image_cmptprec(image, cmptno) \
381 ((image)->cmpts_[cmptno]->prec_)
382
383 /* Get the horizontal subsampling factor for a component. */
384 #define jas_image_cmpthstep(image, cmptno) \
385 ((image)->cmpts_[cmptno]->hstep_)
386
387 /* Get the vertical subsampling factor for a component. */
388 #define jas_image_cmptvstep(image, cmptno) \
389 ((image)->cmpts_[cmptno]->vstep_)
390
391 /* Get the x-coordinate of the top-left corner of a component. */
392 #define jas_image_cmpttlx(image, cmptno) \
393 ((image)->cmpts_[cmptno]->tlx_)
394
395 /* Get the y-coordinate of the top-left corner of a component. */
396 #define jas_image_cmpttly(image, cmptno) \
397 ((image)->cmpts_[cmptno]->tly_)
398
399 /* Get the x-coordinate of the bottom-right corner of a component
400 (plus "one"). */
401 #define jas_image_cmptbrx(image, cmptno) \
402 ((image)->cmpts_[cmptno]->tlx_ + (image)->cmpts_[cmptno]->width_ * \
403 (image)->cmpts_[cmptno]->hstep_)
404
405 /* Get the y-coordinate of the bottom-right corner of a component
406 (plus "one"). */
407 #define jas_image_cmptbry(image, cmptno) \
408 ((image)->cmpts_[cmptno]->tly_ + (image)->cmpts_[cmptno]->height_ * \
409 (image)->cmpts_[cmptno]->vstep_)
410
411 // Test if all components are specified at the same positions in space. */
412 JAS_DLLEXPORT bool jas_image_cmpt_domains_same(jas_image_t *image);
413
414 /* Get the raw size of an image (i.e., the nominal size of the image without
415 any compression. */
416 JAS_DLLEXPORT uint_fast32_t jas_image_rawsize(jas_image_t *image);
417
418 /* Create an image from a stream in some specified format. */
419 JAS_DLLEXPORT jas_image_t *jas_image_decode(jas_stream_t *in, int fmt, const char *optstr);
420
421 /* Write an image to a stream in a specified format. */
422 JAS_DLLEXPORT int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt,
423 const char *optstr);
424
425 /* Read a rectangular region of an image component. */
426 /* The position and size of the rectangular region to be read is specified
427 relative to the component's coordinate system. */
428 JAS_DLLEXPORT int jas_image_readcmpt(jas_image_t *image, int cmptno, jas_image_coord_t x,
429 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
430 jas_matrix_t *data);
431
432 /* Write a rectangular region of an image component. */
433 JAS_DLLEXPORT int jas_image_writecmpt(jas_image_t *image, int cmptno, jas_image_coord_t x,
434 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
435 jas_matrix_t *data);
436
437 /* Delete a component from an image. */
438 JAS_DLLEXPORT void jas_image_delcmpt(jas_image_t *image, int cmptno);
439
440 /* Add a component to an image. */
441 JAS_DLLEXPORT int jas_image_addcmpt(jas_image_t *image, int cmptno,
442 jas_image_cmptparm_t *cmptparm);
443
444 /* Copy a component from one image to another. */
445 JAS_DLLEXPORT int jas_image_copycmpt(jas_image_t *dstimage, int dstcmptno,
446 jas_image_t *srcimage, int srccmptno);
447
448 #define JAS_IMAGE_CDT_GETSGND(dtype) (((dtype) >> 7) & 1)
449 #define JAS_IMAGE_CDT_SETSGND(dtype) (((dtype) & 1) << 7)
450 #define JAS_IMAGE_CDT_GETPREC(dtype) ((dtype) & 0x7f)
451 #define JAS_IMAGE_CDT_SETPREC(dtype) ((dtype) & 0x7f)
452
453 #define jas_image_cmptdtype(image, cmptno) \
454 (JAS_IMAGE_CDT_SETSGND((image)->cmpts_[cmptno]->sgnd_) | JAS_IMAGE_CDT_SETPREC((image)->cmpts_[cmptno]->prec_))
455
456 JAS_DLLEXPORT int jas_image_depalettize(jas_image_t *image, int cmptno, int numlutents,
457 int_fast32_t *lutents, int dtype, int newcmptno);
458
459 JAS_DLLEXPORT int jas_image_readcmptsample(jas_image_t *image, int cmptno, int x, int y);
460 JAS_DLLEXPORT void jas_image_writecmptsample(jas_image_t *image, int cmptno, int x, int y,
461 int_fast32_t v);
462
463 JAS_DLLEXPORT int jas_image_getcmptbytype(jas_image_t *image, int ctype);
464
465 /******************************************************************************\
466 * Image format-related operations.
467 \******************************************************************************/
468
469 /* Clear the table of image formats. */
470 JAS_DLLEXPORT void jas_image_clearfmts(void);
471
472 /* Add entry to table of image formats. */
473 JAS_DLLEXPORT int jas_image_addfmt(int id, char *name, char *ext, char *desc,
474 jas_image_fmtops_t *ops);
475
476 /* Get the ID for the image format with the specified name. */
477 JAS_DLLEXPORT int jas_image_strtofmt(char *s);
478
479 /* Get the name of the image format with the specified ID. */
480 JAS_DLLEXPORT char *jas_image_fmttostr(int fmt);
481
482 /* Lookup image format information by the format ID. */
483 JAS_DLLEXPORT jas_image_fmtinfo_t *jas_image_lookupfmtbyid(int id);
484
485 /* Lookup image format information by the format name. */
486 JAS_DLLEXPORT jas_image_fmtinfo_t *jas_image_lookupfmtbyname(const char *name);
487
488 /* Guess the format of an image file based on its name. */
489 JAS_DLLEXPORT int jas_image_fmtfromname(char *filename);
490
491 /* Get the format of image data in a stream. */
492 JAS_DLLEXPORT int jas_image_getfmt(jas_stream_t *in);
493
494
495 #define jas_image_cmprof(image) ((image)->cmprof_)
496 int jas_image_ishomosamp(jas_image_t *image);
497 int jas_image_sampcmpt(jas_image_t *image, int cmptno, int newcmptno,
498 jas_image_coord_t ho, jas_image_coord_t vo, jas_image_coord_t hs,
499 jas_image_coord_t vs, int sgnd, int prec);
500 int jas_image_writecmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
501 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
502 long *buf);
503 int jas_image_readcmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
504 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
505 long *buf);
506
507 #define jas_image_setcmprof(image, cmprof) ((image)->cmprof_ = cmprof)
508 JAS_DLLEXPORT jas_image_t *jas_image_chclrspc(jas_image_t *image, jas_cmprof_t *outprof,
509 int intent);
510 void jas_image_dump(jas_image_t *image, FILE *out);
511
512 /******************************************************************************\
513 * Image format-dependent operations.
514 \******************************************************************************/
515
516 #if !defined(EXCLUDE_JPG_SUPPORT)
517 /* Format-dependent operations for JPG support. */
518 jas_image_t *jpg_decode(jas_stream_t *in, const char *optstr);
519 int jpg_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
520 int jpg_validate(jas_stream_t *in);
521 #endif
522
523 #if !defined(EXCLUDE_MIF_SUPPORT)
524 /* Format-dependent operations for MIF support. */
525 jas_image_t *mif_decode(jas_stream_t *in, const char *optstr);
526 int mif_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
527 int mif_validate(jas_stream_t *in);
528 #endif
529
530 #if !defined(EXCLUDE_PNM_SUPPORT)
531 /* Format-dependent operations for PNM support. */
532 jas_image_t *pnm_decode(jas_stream_t *in, const char *optstr);
533 int pnm_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
534 int pnm_validate(jas_stream_t *in);
535 #endif
536
537 #if !defined(EXCLUDE_RAS_SUPPORT)
538 /* Format-dependent operations for Sun Rasterfile support. */
539 jas_image_t *ras_decode(jas_stream_t *in, const char *optstr);
540 int ras_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
541 int ras_validate(jas_stream_t *in);
542 #endif
543
544 #if !defined(EXCLUDE_BMP_SUPPORT)
545 /* Format-dependent operations for BMP support. */
546 jas_image_t *bmp_decode(jas_stream_t *in, const char *optstr);
547 int bmp_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
548 int bmp_validate(jas_stream_t *in);
549 #endif
550
551 #if !defined(EXCLUDE_JP2_SUPPORT)
552 /* Format-dependent operations for JP2 support. */
553 jas_image_t *jp2_decode(jas_stream_t *in, const char *optstr);
554 int jp2_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
555 int jp2_validate(jas_stream_t *in);
556 #endif
557
558 #if !defined(EXCLUDE_JPC_SUPPORT)
559 /* Format-dependent operations for JPEG-2000 code stream support. */
560 jas_image_t *jpc_decode(jas_stream_t *in, const char *optstr);
561 int jpc_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
562 int jpc_validate(jas_stream_t *in);
563 #endif
564
565 #if !defined(EXCLUDE_PGX_SUPPORT)
566 /* Format-dependent operations for PGX support. */
567 jas_image_t *pgx_decode(jas_stream_t *in, const char *optstr);
568 int pgx_encode(jas_image_t *image, jas_stream_t *out, const char *optstr);
569 int pgx_validate(jas_stream_t *in);
570 #endif
571
572 #ifdef __cplusplus
573 }
574 #endif
575
576 #endif