Avoided ABI breakage
[openjpeg.git] / indexer_JPIP / jp2.c
1 /*
2  * Copyright (c) 2003-2004, Yannick Verschueren
3  * Copyright (c) 2003-2004,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "j2k.h"
33 #include "cio.h"
34 #include "tcd.h"
35 #include "int.h"
36
37 #define JPIP_JPIP 0x6a706970
38
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
49
50
51 void jp2_write_url(char *Idx_file)
52 {
53   int len, lenp, i;
54   char str[256];
55
56   sprintf(str, "%s", Idx_file);
57   lenp=cio_tell();
58   cio_skip(4);
59   cio_write(JP2_URL, 4);  // DBTL
60   cio_write(0,1);          // VERS
61   cio_write(0,3);          // FLAG
62
63   for (i=0; i<strlen(str); i++) {
64         cio_write(str[i], 1);
65     }
66
67   len=cio_tell()-lenp;
68   cio_seek(lenp);
69   cio_write(len,4);         // L
70   cio_seek(lenp+len);
71 }
72
73 void jp2_write_dbtl(char *Idx_file)
74 {
75   int len, lenp;
76
77   lenp=cio_tell();
78   cio_skip(4);
79   cio_write(JP2_DBTL, 4);  // DBTL
80   cio_write(1,2);           // NDR : Only 1
81   
82   jp2_write_url(Idx_file); // URL Box
83
84   len=cio_tell()-lenp;
85   cio_seek(lenp);
86   cio_write(len,4);         // L
87   cio_seek(lenp+len);
88 }
89
90 int jp2_write_ihdr(j2k_image_t *j2k_img)
91 {
92   int len, lenp,i; 
93   int depth_0,depth, sign, BPC_ok=1;
94
95   lenp=cio_tell();
96   cio_skip(4);
97   cio_write(JP2_IHDR, 4);  // IHDR
98
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
102
103   depth_0=j2k_img->comps[0].prec-1;
104   sign=j2k_img->comps[0].sgnd;
105
106   for(i=1;i<j2k_img->numcomps;i++)
107     {
108       depth=j2k_img->comps[i].prec-1;
109       sign=j2k_img->comps[i].sgnd;
110       if(depth_0!=depth) BPC_ok=0;
111     }
112   
113   if (BPC_ok)
114     cio_write(depth_0+(sign<<7),1);
115   else
116     cio_write(255,1);
117
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
121
122   len=cio_tell()-lenp;
123   cio_seek(lenp);
124   cio_write(len,4);         // L
125   cio_seek(lenp+len);
126
127   return BPC_ok;
128 }
129
130 void jp2_write_bpcc(j2k_image_t *j2k_img)
131 {
132   int len, lenp, i;
133   
134   lenp=cio_tell();
135   cio_skip(4);
136   cio_write(JP2_BPCC, 4);  // BPCC
137   
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);
140
141   len=cio_tell()-lenp;
142   cio_seek(lenp);
143   cio_write(len,4);         // L
144   cio_seek(lenp+len);
145 }
146
147 void jp2_write_colr(int BPC_ok, j2k_image_t *j2k_img)
148 {
149   int len, lenp, meth;
150   
151   lenp=cio_tell();
152   cio_skip(4);
153   cio_write(JP2_COLR, 4);  // COLR
154
155   if ((j2k_img->numcomps==1 || j2k_img->numcomps==3) && (BPC_ok && j2k_img->comps[0].prec==8))
156     meth=1;
157   else
158     meth=2;
159
160   cio_write(meth,1);       // METH
161   cio_write(0,1);          // PREC
162   cio_write(0,1);          // APPROX
163   
164   if (meth==1)
165     cio_write(j2k_img->numcomps>1?16:17,4);          // EnumCS
166
167   if (meth==2)
168     cio_write(0,1);        // PROFILE (??) 
169
170   len=cio_tell()-lenp;
171   cio_seek(lenp);
172   cio_write(len,4);         // L
173   cio_seek(lenp+len);
174 }
175
176 /*
177  * Write the JP2H box
178  *
179  * JP2 Header box
180  *
181  */
182 void jp2_write_jp2h(j2k_image_t *j2k_img)
183 {
184   int len, lenp, BPC_ok;
185   
186   lenp=cio_tell();
187   cio_skip(4);
188   cio_write(JP2_JP2H, 4);           /* JP2H */
189
190   BPC_ok=jp2_write_ihdr(j2k_img);
191
192   if (!BPC_ok)
193     jp2_write_bpcc(j2k_img);
194   jp2_write_colr(BPC_ok, j2k_img);
195
196   len=cio_tell()-lenp;
197   cio_seek(lenp);
198   cio_write(len,4);         /* L */
199   cio_seek(lenp+len);
200 }
201
202 /*
203  * Write the FTYP box
204  *
205  * File type box
206  *
207  */
208 void jp2_write_ftyp()
209 {
210   int len, lenp;
211   
212   lenp=cio_tell();
213   cio_skip(4);
214   cio_write(JP2_FTYP, 4);   /* FTYP       */
215
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 */
220
221   len=cio_tell()-lenp;
222   cio_seek(lenp);
223   cio_write(len,4);         /* L          */
224   cio_seek(lenp+len);
225 }
226
227 /*
228  * Read the FTYP box
229  *
230  * File type box
231  *
232  */
233 void jp2_read_ftyp(int length)
234 {
235   int BR, MinV, type, i;
236
237   BR = cio_read(4);         /* BR              */
238   MinV = cio_read(4);       /* MinV            */
239   length-=8;
240   
241   for (i=length/4;i>0;i--)
242     type = cio_read(4);     /* CLi : JP2, JPIP */
243 }
244
245 int jp2_write_jp2c(char *J2K_file)
246 {
247   int len, lenp, totlen, i;
248   FILE *src;
249   char *j2kfile;
250
251   lenp=cio_tell();
252   cio_skip(4);
253   cio_write(JP2_JP2C, 4);  // JP2C
254
255   src=fopen(J2K_file, "rb");
256   fseek(src, 0, SEEK_END);
257   totlen=ftell(src);
258   fseek(src, 0, SEEK_SET);
259   
260   j2kfile=(char*)malloc(totlen);
261   fread(j2kfile, 1, totlen, src);
262   fclose(src);
263
264   for (i=0;i<totlen;i++)
265     cio_write(j2kfile[i],1);
266   
267   len=cio_tell()-lenp;
268   cio_seek(lenp);
269   cio_write(len,4);         // L
270   cio_seek(lenp+len);
271   return lenp;
272 }
273
274 void jp2_write_jp()
275 {
276   int len, lenp;
277   
278   lenp=cio_tell();
279   cio_skip(4);
280   cio_write(JP2_JP, 4);  // JP
281   cio_write(0x0d0a870a,4);
282   len=cio_tell()-lenp;
283   cio_seek(lenp);
284   cio_write(len,4);         // L
285   cio_seek(lenp+len);
286 }
287
288 /*
289  * Read the JP box
290  *
291  * JPEG 2000 signature
292  *
293  * return 1 if error else 0
294  */
295 int jp2_read_jp()
296 {
297   if (0x0d0a870a!=cio_read(4))
298     return 1;
299   else
300     return 0;
301 }