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