2 * Copyright (c) 2003-2004, Yannick Verschueren
3 * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
37 #define JPIP_JPIP 0x6a706970
39 #define JP2_JP 0x6a502020
40 #define JP2_FTYP 0x66747970
41 #define JP2_JP2H 0x6a703268
42 #define JP2_IHDR 0x69686472
43 #define JP2_COLR 0x636f6c72
44 #define JP2_JP2C 0x6a703263
45 #define JP2_URL 0x75726c20
46 #define JP2_DBTL 0x6474626c
47 #define JP2_BPCC 0x62706363
48 #define JP2 0x6a703220
51 void jp2_write_url(char *Idx_file)
56 sprintf(str, "%s", Idx_file);
59 cio_write(JP2_URL, 4); // DBTL
60 cio_write(0,1); // VERS
61 cio_write(0,3); // FLAG
63 for (i=0; i<strlen(str); i++) {
69 cio_write(len,4); // L
73 void jp2_write_dbtl(char *Idx_file)
79 cio_write(JP2_DBTL, 4); // DBTL
80 cio_write(1,2); // NDR : Only 1
82 jp2_write_url(Idx_file); // URL Box
86 cio_write(len,4); // L
90 int jp2_write_ihdr(j2k_image_t *j2k_img)
93 int depth_0,depth, sign, BPC_ok=1;
97 cio_write(JP2_IHDR, 4); // IHDR
99 cio_write(j2k_img->y1-j2k_img->x0,4); // HEIGHT
100 cio_write(j2k_img->x1-j2k_img->x0,4); // WIDTH
101 cio_write(j2k_img->numcomps,2); // NC
103 depth_0=j2k_img->comps[0].prec-1;
104 sign=j2k_img->comps[0].sgnd;
106 for(i=1;i<j2k_img->numcomps;i++)
108 depth=j2k_img->comps[i].prec-1;
109 sign=j2k_img->comps[i].sgnd;
110 if(depth_0!=depth) BPC_ok=0;
114 cio_write(depth_0+(sign<<7),1);
118 cio_write(7,1); // C : Always 7
119 cio_write(1,1); // UnkC, colorspace unknow
120 cio_write(0,1); // IPR, no intellectual property
124 cio_write(len,4); // L
130 void jp2_write_bpcc(j2k_image_t *j2k_img)
136 cio_write(JP2_BPCC, 4); // BPCC
138 for(i=0;i<j2k_img->numcomps;i++)
139 cio_write(j2k_img->comps[i].prec-1+(j2k_img->comps[i].sgnd<<7),1);
143 cio_write(len,4); // L
147 void jp2_write_colr(int BPC_ok, j2k_image_t *j2k_img)
153 cio_write(JP2_COLR, 4); // COLR
155 if ((j2k_img->numcomps==1 || j2k_img->numcomps==3) && (BPC_ok && j2k_img->comps[0].prec==8))
160 cio_write(meth,1); // METH
161 cio_write(0,1); // PREC
162 cio_write(0,1); // APPROX
165 cio_write(j2k_img->numcomps>1?16:17,4); // EnumCS
168 cio_write(0,1); // PROFILE (??)
172 cio_write(len,4); // L
182 void jp2_write_jp2h(j2k_image_t *j2k_img)
184 int len, lenp, BPC_ok;
188 cio_write(JP2_JP2H, 4); /* JP2H */
190 BPC_ok=jp2_write_ihdr(j2k_img);
193 jp2_write_bpcc(j2k_img);
194 jp2_write_colr(BPC_ok, j2k_img);
198 cio_write(len,4); /* L */
208 void jp2_write_ftyp()
214 cio_write(JP2_FTYP, 4); /* FTYP */
216 cio_write(JP2,4); /* BR */
217 cio_write(0,4); /* MinV */
218 cio_write(JP2,4); /* CL0 : JP2 */
219 cio_write(JPIP_JPIP,4); /* CL1 : JPIP */
223 cio_write(len,4); /* L */
233 void jp2_read_ftyp(int length)
235 int BR, MinV, type, i;
237 BR = cio_read(4); /* BR */
238 MinV = cio_read(4); /* MinV */
241 for (i=length/4;i>0;i--)
242 type = cio_read(4); /* CLi : JP2, JPIP */
245 int jp2_write_jp2c(char *J2K_file)
247 int len, lenp, totlen, i;
253 cio_write(JP2_JP2C, 4); // JP2C
255 src=fopen(J2K_file, "rb");
256 fseek(src, 0, SEEK_END);
258 fseek(src, 0, SEEK_SET);
260 j2kfile=(char*)malloc(totlen);
261 fread(j2kfile, 1, totlen, src);
264 for (i=0;i<totlen;i++)
265 cio_write(j2kfile[i],1);
269 cio_write(len,4); // L
280 cio_write(JP2_JP, 4); // JP
281 cio_write(0x0d0a870a,4);
284 cio_write(len,4); // L
291 * JPEG 2000 signature
293 * return 1 if error else 0
297 if (0x0d0a870a!=cio_read(4))