diff options
Diffstat (limited to 'libraries')
43 files changed, 14991 insertions, 0 deletions
diff --git a/libraries/openjpeg-libsl/libopenjpeg/bio.c b/libraries/openjpeg-libsl/libopenjpeg/bio.c new file mode 100644 index 0000000..2a305a7 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/bio.c | |||
@@ -0,0 +1,187 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /** @defgroup BIO BIO - Individual bit input-output stream */ | ||
35 | /*@{*/ | ||
36 | |||
37 | /** @name Local static functions */ | ||
38 | /*@{*/ | ||
39 | |||
40 | /** | ||
41 | Write a bit | ||
42 | @param bio BIO handle | ||
43 | @param b Bit to write (0 or 1) | ||
44 | */ | ||
45 | static void bio_putbit(opj_bio_t *bio, int b); | ||
46 | /** | ||
47 | Read a bit | ||
48 | @param bio BIO handle | ||
49 | @return Returns the read bit | ||
50 | */ | ||
51 | static int bio_getbit(opj_bio_t *bio); | ||
52 | /** | ||
53 | Write a byte | ||
54 | @param bio BIO handle | ||
55 | @return Returns 0 if successful, returns 1 otherwise | ||
56 | */ | ||
57 | static int bio_byteout(opj_bio_t *bio); | ||
58 | /** | ||
59 | Read a byte | ||
60 | @param bio BIO handle | ||
61 | @return Returns 0 if successful, returns 1 otherwise | ||
62 | */ | ||
63 | static int bio_bytein(opj_bio_t *bio); | ||
64 | |||
65 | /*@}*/ | ||
66 | |||
67 | /*@}*/ | ||
68 | |||
69 | /* | ||
70 | ========================================================== | ||
71 | local functions | ||
72 | ========================================================== | ||
73 | */ | ||
74 | |||
75 | static int bio_byteout(opj_bio_t *bio) { | ||
76 | bio->buf = (bio->buf << 8) & 0xffff; | ||
77 | bio->ct = bio->buf == 0xff00 ? 7 : 8; | ||
78 | if (bio->bp >= bio->end) { | ||
79 | return 1; | ||
80 | } | ||
81 | *bio->bp++ = bio->buf >> 8; | ||
82 | return 0; | ||
83 | } | ||
84 | |||
85 | static int bio_bytein(opj_bio_t *bio) { | ||
86 | bio->buf = (bio->buf << 8) & 0xffff; | ||
87 | bio->ct = bio->buf == 0xff00 ? 7 : 8; | ||
88 | if (bio->bp >= bio->end) { | ||
89 | return 1; | ||
90 | } | ||
91 | bio->buf |= *bio->bp++; | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | static void bio_putbit(opj_bio_t *bio, int b) { | ||
96 | if (bio->ct == 0) { | ||
97 | bio_byteout(bio); | ||
98 | } | ||
99 | bio->ct--; | ||
100 | bio->buf |= b << bio->ct; | ||
101 | } | ||
102 | |||
103 | static int bio_getbit(opj_bio_t *bio) { | ||
104 | if (bio->ct == 0) { | ||
105 | bio_bytein(bio); | ||
106 | } | ||
107 | bio->ct--; | ||
108 | return (bio->buf >> bio->ct) & 1; | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | ========================================================== | ||
113 | Bit Input/Output interface | ||
114 | ========================================================== | ||
115 | */ | ||
116 | |||
117 | opj_bio_t* bio_create() { | ||
118 | opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t)); | ||
119 | return bio; | ||
120 | } | ||
121 | |||
122 | void bio_destroy(opj_bio_t *bio) { | ||
123 | if(bio) { | ||
124 | opj_free(bio); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | int bio_numbytes(opj_bio_t *bio) { | ||
129 | return (bio->bp - bio->start); | ||
130 | } | ||
131 | |||
132 | void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) { | ||
133 | bio->start = bp; | ||
134 | bio->end = bp + len; | ||
135 | bio->bp = bp; | ||
136 | bio->buf = 0; | ||
137 | bio->ct = 8; | ||
138 | } | ||
139 | |||
140 | void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) { | ||
141 | bio->start = bp; | ||
142 | bio->end = bp + len; | ||
143 | bio->bp = bp; | ||
144 | bio->buf = 0; | ||
145 | bio->ct = 0; | ||
146 | } | ||
147 | |||
148 | void bio_write(opj_bio_t *bio, int v, int n) { | ||
149 | int i; | ||
150 | for (i = n - 1; i >= 0; i--) { | ||
151 | bio_putbit(bio, (v >> i) & 1); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | int bio_read(opj_bio_t *bio, int n) { | ||
156 | int i, v; | ||
157 | v = 0; | ||
158 | for (i = n - 1; i >= 0; i--) { | ||
159 | v += bio_getbit(bio) << i; | ||
160 | } | ||
161 | return v; | ||
162 | } | ||
163 | |||
164 | int bio_flush(opj_bio_t *bio) { | ||
165 | bio->ct = 0; | ||
166 | if (bio_byteout(bio)) { | ||
167 | return 1; | ||
168 | } | ||
169 | if (bio->ct == 7) { | ||
170 | bio->ct = 0; | ||
171 | if (bio_byteout(bio)) { | ||
172 | return 1; | ||
173 | } | ||
174 | } | ||
175 | return 0; | ||
176 | } | ||
177 | |||
178 | int bio_inalign(opj_bio_t *bio) { | ||
179 | bio->ct = 0; | ||
180 | if ((bio->buf & 0xff) == 0xff) { | ||
181 | if (bio_bytein(bio)) { | ||
182 | return 1; | ||
183 | } | ||
184 | bio->ct = 0; | ||
185 | } | ||
186 | return 0; | ||
187 | } | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/bio.h b/libraries/openjpeg-libsl/libopenjpeg/bio.h new file mode 100644 index 0000000..d0a809c --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/bio.h | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __BIO_H | ||
33 | #define __BIO_H | ||
34 | /** | ||
35 | @file bio.h | ||
36 | @brief Implementation of an individual bit input-output (BIO) | ||
37 | |||
38 | The functions in BIO.C have for goal to realize an individual bit input - output. | ||
39 | */ | ||
40 | |||
41 | /** @defgroup BIO BIO - Individual bit input-output stream */ | ||
42 | /*@{*/ | ||
43 | |||
44 | /** | ||
45 | Individual bit input-output stream (BIO) | ||
46 | */ | ||
47 | typedef struct opj_bio { | ||
48 | /** pointer to the start of the buffer */ | ||
49 | unsigned char *start; | ||
50 | /** pointer to the end of the buffer */ | ||
51 | unsigned char *end; | ||
52 | /** pointer to the present position in the buffer */ | ||
53 | unsigned char *bp; | ||
54 | /** temporary place where each byte is read or written */ | ||
55 | unsigned int buf; | ||
56 | /** coder : number of bits free to write. decoder : number of bits read */ | ||
57 | int ct; | ||
58 | } opj_bio_t; | ||
59 | |||
60 | /** @name Exported functions */ | ||
61 | /*@{*/ | ||
62 | /* ----------------------------------------------------------------------- */ | ||
63 | /** | ||
64 | Create a new BIO handle | ||
65 | @return Returns a new BIO handle if successful, returns NULL otherwise | ||
66 | */ | ||
67 | opj_bio_t* bio_create(); | ||
68 | /** | ||
69 | Destroy a previously created BIO handle | ||
70 | @param bio BIO handle to destroy | ||
71 | */ | ||
72 | void bio_destroy(opj_bio_t *bio); | ||
73 | /** | ||
74 | Number of bytes written. | ||
75 | @param bio BIO handle | ||
76 | @return Returns the number of bytes written | ||
77 | */ | ||
78 | int bio_numbytes(opj_bio_t *bio); | ||
79 | /** | ||
80 | Init encoder | ||
81 | @param bio BIO handle | ||
82 | @param bp Output buffer | ||
83 | @param len Output buffer length | ||
84 | */ | ||
85 | void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len); | ||
86 | /** | ||
87 | Init decoder | ||
88 | @param bio BIO handle | ||
89 | @param bp Input buffer | ||
90 | @param len Input buffer length | ||
91 | */ | ||
92 | void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len); | ||
93 | /** | ||
94 | Write bits | ||
95 | @param bio BIO handle | ||
96 | @param v Value of bits | ||
97 | @param n Number of bits to write | ||
98 | */ | ||
99 | void bio_write(opj_bio_t *bio, int v, int n); | ||
100 | /** | ||
101 | Read bits | ||
102 | @param bio BIO handle | ||
103 | @param n Number of bits to read | ||
104 | @return Returns the corresponding read number | ||
105 | */ | ||
106 | int bio_read(opj_bio_t *bio, int n); | ||
107 | /** | ||
108 | Flush bits | ||
109 | @param bio BIO handle | ||
110 | @return Returns 1 if successful, returns 0 otherwise | ||
111 | */ | ||
112 | int bio_flush(opj_bio_t *bio); | ||
113 | /** | ||
114 | Passes the ending bits (coming from flushing) | ||
115 | @param bio BIO handle | ||
116 | @return Returns 1 if successful, returns 0 otherwise | ||
117 | */ | ||
118 | int bio_inalign(opj_bio_t *bio); | ||
119 | /* ----------------------------------------------------------------------- */ | ||
120 | /*@}*/ | ||
121 | |||
122 | /*@}*/ | ||
123 | |||
124 | #endif /* __BIO_H */ | ||
125 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/cio.c b/libraries/openjpeg-libsl/libopenjpeg/cio.c new file mode 100644 index 0000000..6082e9b --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/cio.c | |||
@@ -0,0 +1,190 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /* ----------------------------------------------------------------------- */ | ||
35 | |||
36 | opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) { | ||
37 | opj_cp_t *cp = NULL; | ||
38 | opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t)); | ||
39 | if(!cio) return NULL; | ||
40 | cio->cinfo = cinfo; | ||
41 | if(buffer && length) { | ||
42 | /* wrap a user buffer containing the encoded image */ | ||
43 | cio->openmode = OPJ_STREAM_READ; | ||
44 | cio->buffer = buffer; | ||
45 | cio->length = length; | ||
46 | } | ||
47 | else if(!buffer && !length && cinfo) { | ||
48 | /* allocate a buffer for the encoded image */ | ||
49 | cio->openmode = OPJ_STREAM_WRITE; | ||
50 | switch(cinfo->codec_format) { | ||
51 | case CODEC_J2K: | ||
52 | cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp; | ||
53 | break; | ||
54 | case CODEC_JP2: | ||
55 | cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp; | ||
56 | break; | ||
57 | default: | ||
58 | opj_free(cio); | ||
59 | return NULL; | ||
60 | } | ||
61 | cio->length = (int) (1.3 * cp->img_size); | ||
62 | cio->buffer = (unsigned char *)opj_malloc(cio->length); | ||
63 | if(!cio->buffer) { | ||
64 | opj_free(cio); | ||
65 | return NULL; | ||
66 | } | ||
67 | } | ||
68 | else { | ||
69 | opj_free(cio); | ||
70 | return NULL; | ||
71 | } | ||
72 | |||
73 | /* Initialize byte IO */ | ||
74 | cio->start = cio->buffer; | ||
75 | cio->end = cio->buffer + cio->length; | ||
76 | cio->bp = cio->buffer; | ||
77 | |||
78 | return cio; | ||
79 | } | ||
80 | |||
81 | void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) { | ||
82 | if(cio) { | ||
83 | if(cio->openmode == OPJ_STREAM_WRITE) { | ||
84 | /* destroy the allocated buffer */ | ||
85 | opj_free(cio->buffer); | ||
86 | } | ||
87 | /* destroy the cio */ | ||
88 | opj_free(cio); | ||
89 | } | ||
90 | } | ||
91 | |||
92 | |||
93 | /* ----------------------------------------------------------------------- */ | ||
94 | |||
95 | /* | ||
96 | * Get position in byte stream. | ||
97 | */ | ||
98 | int OPJ_CALLCONV cio_tell(opj_cio_t *cio) { | ||
99 | return cio->bp - cio->start; | ||
100 | } | ||
101 | |||
102 | /* | ||
103 | * Set position in byte stream. | ||
104 | * | ||
105 | * pos : position, in number of bytes, from the beginning of the stream | ||
106 | */ | ||
107 | void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) { | ||
108 | cio->bp = cio->start + pos; | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | * Number of bytes left before the end of the stream. | ||
113 | */ | ||
114 | int cio_numbytesleft(opj_cio_t *cio) { | ||
115 | return cio->end - cio->bp; | ||
116 | } | ||
117 | |||
118 | /* | ||
119 | * Get pointer to the current position in the stream. | ||
120 | */ | ||
121 | unsigned char *cio_getbp(opj_cio_t *cio) { | ||
122 | return cio->bp; | ||
123 | } | ||
124 | |||
125 | /* | ||
126 | * Write a byte. | ||
127 | */ | ||
128 | bool cio_byteout(opj_cio_t *cio, unsigned char v) { | ||
129 | if (cio->bp >= cio->end) { | ||
130 | opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n"); | ||
131 | return false; | ||
132 | } | ||
133 | *cio->bp++ = v; | ||
134 | return true; | ||
135 | } | ||
136 | |||
137 | /* | ||
138 | * Read a byte. | ||
139 | */ | ||
140 | unsigned char cio_bytein(opj_cio_t *cio) { | ||
141 | if (cio->bp >= cio->end) { | ||
142 | opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n"); | ||
143 | return 0; | ||
144 | } | ||
145 | return *cio->bp++; | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * Write some bytes. | ||
150 | * | ||
151 | * v : value to write | ||
152 | * n : number of bytes to write | ||
153 | */ | ||
154 | unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) { | ||
155 | int i; | ||
156 | for (i = n - 1; i >= 0; i--) { | ||
157 | if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) ) | ||
158 | return 0; | ||
159 | } | ||
160 | return n; | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * Read some bytes. | ||
165 | * | ||
166 | * n : number of bytes to read | ||
167 | * | ||
168 | * return : value of the n bytes read | ||
169 | */ | ||
170 | unsigned int cio_read(opj_cio_t *cio, int n) { | ||
171 | int i; | ||
172 | unsigned int v; | ||
173 | v = 0; | ||
174 | for (i = n - 1; i >= 0; i--) { | ||
175 | v += cio_bytein(cio) << (i << 3); | ||
176 | } | ||
177 | return v; | ||
178 | } | ||
179 | |||
180 | /* | ||
181 | * Skip some bytes. | ||
182 | * | ||
183 | * n : number of bytes to skip | ||
184 | */ | ||
185 | void cio_skip(opj_cio_t *cio, int n) { | ||
186 | cio->bp += n; | ||
187 | } | ||
188 | |||
189 | |||
190 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/cio.h b/libraries/openjpeg-libsl/libopenjpeg/cio.h new file mode 100644 index 0000000..580bf9c --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/cio.h | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __CIO_H | ||
33 | #define __CIO_H | ||
34 | /** | ||
35 | @file cio.h | ||
36 | @brief Implementation of a byte input-output process (CIO) | ||
37 | |||
38 | The functions in CIO.C have for goal to realize a byte input / output process. | ||
39 | */ | ||
40 | |||
41 | /** @defgroup CIO CIO - byte input-output stream */ | ||
42 | /*@{*/ | ||
43 | |||
44 | /** @name Exported functions (see also openjpeg.h) */ | ||
45 | /*@{*/ | ||
46 | /* ----------------------------------------------------------------------- */ | ||
47 | /** | ||
48 | Number of bytes left before the end of the stream | ||
49 | @param cio CIO handle | ||
50 | @return Returns the number of bytes before the end of the stream | ||
51 | */ | ||
52 | int cio_numbytesleft(opj_cio_t *cio); | ||
53 | /** | ||
54 | Get pointer to the current position in the stream | ||
55 | @param cio CIO handle | ||
56 | @return Returns a pointer to the current position | ||
57 | */ | ||
58 | unsigned char *cio_getbp(opj_cio_t *cio); | ||
59 | /** | ||
60 | Write some bytes | ||
61 | @param cio CIO handle | ||
62 | @param v Value to write | ||
63 | @param n Number of bytes to write | ||
64 | @return Returns the number of bytes written or 0 if an error occured | ||
65 | */ | ||
66 | unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n); | ||
67 | /** | ||
68 | Read some bytes | ||
69 | @param cio CIO handle | ||
70 | @param n Number of bytes to read | ||
71 | @return Returns the value of the n bytes read | ||
72 | */ | ||
73 | unsigned int cio_read(opj_cio_t *cio, int n); | ||
74 | /** | ||
75 | Skip some bytes | ||
76 | @param cio CIO handle | ||
77 | @param n Number of bytes to skip | ||
78 | */ | ||
79 | void cio_skip(opj_cio_t *cio, int n); | ||
80 | /* ----------------------------------------------------------------------- */ | ||
81 | /*@}*/ | ||
82 | |||
83 | /*@}*/ | ||
84 | |||
85 | #endif /* __CIO_H */ | ||
86 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/dwt.c b/libraries/openjpeg-libsl/libopenjpeg/dwt.c new file mode 100644 index 0000000..d10de18 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/dwt.c | |||
@@ -0,0 +1,661 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | |||
33 | #include "opj_includes.h" | ||
34 | |||
35 | /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ | ||
36 | /*@{*/ | ||
37 | |||
38 | #define WS(i) v->mem[(i)*2] | ||
39 | #define WD(i) v->mem[(1+(i)*2)] | ||
40 | |||
41 | /** @name Local data structures */ | ||
42 | /*@{*/ | ||
43 | |||
44 | typedef struct dwt_local{ | ||
45 | int * mem ; | ||
46 | int dn ; | ||
47 | int sn ; | ||
48 | int cas ; | ||
49 | } dwt_t ; | ||
50 | |||
51 | /*@}*/ | ||
52 | |||
53 | /** | ||
54 | Virtual function type for wavelet transform in 1-D | ||
55 | */ | ||
56 | typedef void (*DWT1DFN)(dwt_t* v); | ||
57 | |||
58 | /** @name Local static functions */ | ||
59 | /*@{*/ | ||
60 | |||
61 | /** | ||
62 | Forward lazy transform (horizontal) | ||
63 | */ | ||
64 | static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas); | ||
65 | /** | ||
66 | Forward lazy transform (vertical) | ||
67 | */ | ||
68 | static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas); | ||
69 | /** | ||
70 | Inverse lazy transform (horizontal) | ||
71 | */ | ||
72 | static void dwt_interleave_h(dwt_t* h, int *a); | ||
73 | /** | ||
74 | Inverse lazy transform (vertical) | ||
75 | */ | ||
76 | static void dwt_interleave_v(dwt_t* v, int *a, int x); | ||
77 | /** | ||
78 | Forward 5-3 wavelet tranform in 1-D | ||
79 | */ | ||
80 | static void dwt_encode_1(int *a, int dn, int sn, int cas); | ||
81 | /** | ||
82 | Inverse 5-3 wavelet tranform in 1-D | ||
83 | */ | ||
84 | static void dwt_decode_1(dwt_t *v); | ||
85 | /** | ||
86 | Forward 9-7 wavelet transform in 1-D | ||
87 | */ | ||
88 | static void dwt_encode_1_real(int *a, int dn, int sn, int cas); | ||
89 | /** | ||
90 | Inverse 9-7 wavelet transform in 1-D | ||
91 | */ | ||
92 | static void dwt_decode_1_real(dwt_t *v); | ||
93 | /** | ||
94 | FIXME : comment ??? | ||
95 | */ | ||
96 | static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize); | ||
97 | /** | ||
98 | Inverse wavelet tranform in 2-D. | ||
99 | */ | ||
100 | static void dwt_decode_tile(opj_tcd_tilecomp_t * tilec, int stop , DWT1DFN fn); | ||
101 | |||
102 | /*@}*/ | ||
103 | |||
104 | /*@}*/ | ||
105 | |||
106 | #define S(i) a[(i)*2] | ||
107 | #define D(i) a[(1+(i)*2)] | ||
108 | #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i))) | ||
109 | #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i))) | ||
110 | /* new */ | ||
111 | #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i))) | ||
112 | #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i))) | ||
113 | |||
114 | /* <summary> */ | ||
115 | /* This table contains the norms of the 5-3 wavelets for different bands. */ | ||
116 | /* </summary> */ | ||
117 | static const double dwt_norms[4][10] = { | ||
118 | {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, | ||
119 | {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, | ||
120 | {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, | ||
121 | {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93} | ||
122 | }; | ||
123 | |||
124 | /* <summary> */ | ||
125 | /* This table contains the norms of the 9-7 wavelets for different bands. */ | ||
126 | /* </summary> */ | ||
127 | static const double dwt_norms_real[4][10] = { | ||
128 | {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, | ||
129 | {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, | ||
130 | {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, | ||
131 | {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2} | ||
132 | }; | ||
133 | |||
134 | /* | ||
135 | ========================================================== | ||
136 | local functions | ||
137 | ========================================================== | ||
138 | */ | ||
139 | |||
140 | /* <summary> */ | ||
141 | /* Forward lazy transform (horizontal). */ | ||
142 | /* </summary> */ | ||
143 | static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) { | ||
144 | int i; | ||
145 | for (i=0; i<sn; i++) b[i]=a[2*i+cas]; | ||
146 | for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)]; | ||
147 | } | ||
148 | |||
149 | /* <summary> */ | ||
150 | /* Forward lazy transform (vertical). */ | ||
151 | /* </summary> */ | ||
152 | static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) { | ||
153 | int i; | ||
154 | for (i=0; i<sn; i++) b[i*x]=a[2*i+cas]; | ||
155 | for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)]; | ||
156 | } | ||
157 | |||
158 | /* <summary> */ | ||
159 | /* Inverse lazy transform (horizontal). */ | ||
160 | /* </summary> */ | ||
161 | static void dwt_interleave_h(dwt_t* h, int *a) { | ||
162 | int *ai = a; | ||
163 | int *bi = h->mem + h->cas; | ||
164 | int i = h->sn; | ||
165 | while( i-- ) { | ||
166 | *bi = *(ai++); | ||
167 | bi += 2; | ||
168 | } | ||
169 | ai = a + h->sn; | ||
170 | bi = h->mem + 1 - h->cas; | ||
171 | i = h->dn ; | ||
172 | while( i-- ) { | ||
173 | *bi = *(ai++); | ||
174 | bi += 2; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | /* <summary> */ | ||
179 | /* Inverse lazy transform (vertical). */ | ||
180 | /* </summary> */ | ||
181 | static void dwt_interleave_v(dwt_t* v, int *a, int x) { | ||
182 | int *ai = a; | ||
183 | int *bi = v->mem + v->cas; | ||
184 | int i = v->sn; | ||
185 | while( i-- ) { | ||
186 | *bi = *ai; | ||
187 | bi += 2; | ||
188 | ai += x; | ||
189 | } | ||
190 | ai = a + (v->sn * x); | ||
191 | bi = v->mem + 1 - v->cas; | ||
192 | i = v->dn ; | ||
193 | while( i-- ) { | ||
194 | *bi = *ai; | ||
195 | bi += 2; | ||
196 | ai += x; | ||
197 | } | ||
198 | } | ||
199 | |||
200 | |||
201 | /* <summary> */ | ||
202 | /* Forward 5-3 wavelet tranform in 1-D. */ | ||
203 | /* </summary> */ | ||
204 | static void dwt_encode_1(int *a, int dn, int sn, int cas) { | ||
205 | int i; | ||
206 | |||
207 | if (!cas) { | ||
208 | if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ | ||
209 | for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1; | ||
210 | for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2; | ||
211 | } | ||
212 | } else { | ||
213 | if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ | ||
214 | S(0) *= 2; | ||
215 | else { | ||
216 | for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1; | ||
217 | for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2; | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | /* <summary> */ | ||
223 | /* Inverse 5-3 wavelet tranform in 1-D. */ | ||
224 | /* </summary> */ | ||
225 | static void dwt_decode_1_(int *a, int dn, int sn, int cas) { | ||
226 | int i; | ||
227 | |||
228 | if (!cas) { | ||
229 | if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ | ||
230 | for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2; | ||
231 | for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1; | ||
232 | } | ||
233 | } else { | ||
234 | if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ | ||
235 | S(0) /= 2; | ||
236 | else { | ||
237 | for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2; | ||
238 | for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1; | ||
239 | } | ||
240 | } | ||
241 | } | ||
242 | |||
243 | /* <summary> */ | ||
244 | /* Inverse 5-3 wavelet tranform in 1-D. */ | ||
245 | /* </summary> */ | ||
246 | static void dwt_decode_1(dwt_t *v) { | ||
247 | dwt_decode_1_(v->mem, v->dn, v->sn, v->cas); | ||
248 | } | ||
249 | |||
250 | /* <summary> */ | ||
251 | /* Forward 9-7 wavelet transform in 1-D. */ | ||
252 | /* </summary> */ | ||
253 | static void dwt_encode_1_real(int *a, int dn, int sn, int cas) { | ||
254 | int i; | ||
255 | if (!cas) { | ||
256 | if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ | ||
257 | for (i = 0; i < dn; i++) | ||
258 | D(i) -= fix_mul(S_(i) + S_(i + 1), 12993); | ||
259 | for (i = 0; i < sn; i++) | ||
260 | S(i) -= fix_mul(D_(i - 1) + D_(i), 434); | ||
261 | for (i = 0; i < dn; i++) | ||
262 | D(i) += fix_mul(S_(i) + S_(i + 1), 7233); | ||
263 | for (i = 0; i < sn; i++) | ||
264 | S(i) += fix_mul(D_(i - 1) + D_(i), 3633); | ||
265 | for (i = 0; i < dn; i++) | ||
266 | D(i) = fix_mul(D(i), 5038); /*5038 */ | ||
267 | for (i = 0; i < sn; i++) | ||
268 | S(i) = fix_mul(S(i), 6659); /*6660 */ | ||
269 | } | ||
270 | } else { | ||
271 | if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */ | ||
272 | for (i = 0; i < dn; i++) | ||
273 | S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993); | ||
274 | for (i = 0; i < sn; i++) | ||
275 | D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434); | ||
276 | for (i = 0; i < dn; i++) | ||
277 | S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233); | ||
278 | for (i = 0; i < sn; i++) | ||
279 | D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633); | ||
280 | for (i = 0; i < dn; i++) | ||
281 | S(i) = fix_mul(S(i), 5038); /*5038 */ | ||
282 | for (i = 0; i < sn; i++) | ||
283 | D(i) = fix_mul(D(i), 6659); /*6660 */ | ||
284 | } | ||
285 | } | ||
286 | } | ||
287 | |||
288 | static void dwt_decode_sm(dwt_t* v, int k, int n, int x) { | ||
289 | int m = k > n ? n : k; | ||
290 | int l = v->mem[1]; //D(0); | ||
291 | int j; | ||
292 | int i; | ||
293 | for (i = 0; i < m; i++) { | ||
294 | j = l; | ||
295 | WS(i) -= fix_mul( ( l = WD(i) ) + j , x); | ||
296 | } | ||
297 | if( i < k ) { | ||
298 | l = fix_mul( l + l , x ); | ||
299 | for (; i < k; i++) | ||
300 | WS(i) -= l; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | static void dwt_decode_sp(dwt_t* v, int k, int n, int x) { | ||
305 | int m = k > n ? n : k; | ||
306 | int l = v->mem[1]; //D(0); | ||
307 | int j; | ||
308 | int i; | ||
309 | for (i = 0; i < m; i++) { | ||
310 | j = l; | ||
311 | WS(i) += fix_mul( ( l = WD(i) ) + j , x); | ||
312 | } | ||
313 | if( i < k ) { | ||
314 | l = fix_mul( l + l , x ); | ||
315 | for (; i < k; i++) | ||
316 | WS(i) += l; | ||
317 | } | ||
318 | } | ||
319 | |||
320 | static void dwt_decode_dm(dwt_t* v, int k, int n, int x) { | ||
321 | int m = k >= n ? n-1 : k; | ||
322 | int l = v->mem[0]; //S(0); | ||
323 | int i; | ||
324 | int j; | ||
325 | for (i = 0; i < m; i++) { | ||
326 | j = l; | ||
327 | WD(i) -= fix_mul( ( l = WS(i+1) ) + j , x); | ||
328 | } | ||
329 | if( i < k ) { | ||
330 | l = fix_mul( l + l , x ); | ||
331 | for (; i < k; i++) | ||
332 | WD(i) -= l; | ||
333 | } | ||
334 | } | ||
335 | |||
336 | static void dwt_decode_dp(dwt_t* v, int k, int n, int x) { | ||
337 | int m = k >= n ? n-1 : k; | ||
338 | int l = v->mem[0]; //S(0); | ||
339 | int i; | ||
340 | int j; | ||
341 | for (i = 0; i < m; i++) { | ||
342 | j = l; | ||
343 | WD(i) += fix_mul( ( l = WS(i+1) ) + j , x); | ||
344 | } | ||
345 | |||
346 | if( i < k ) { | ||
347 | l = fix_mul( l + l , x ); | ||
348 | for (; i < k; i++) | ||
349 | WD(i) += l; | ||
350 | } | ||
351 | } | ||
352 | |||
353 | |||
354 | /* <summary> */ | ||
355 | /* Inverse 9-7 wavelet transform in 1-D. */ | ||
356 | /* </summary> */ | ||
357 | static void dwt_decode_1_real(dwt_t* v) { | ||
358 | int i; | ||
359 | if (!v->cas) { | ||
360 | if ((v->dn > 0) || (v->sn > 1)) { /* NEW : CASE ONE ELEMENT */ | ||
361 | for (i = 0; i < v->sn; i++) | ||
362 | WS(i) = fix_mul(WS(i), 10078); /* 10076 */ | ||
363 | for (i = 0; i < v->dn; i++) | ||
364 | WD(i) = fix_mul(WD(i), 13318); /* 13320 */ | ||
365 | dwt_decode_sm(v, v->sn, v->dn, 3633); | ||
366 | dwt_decode_dm(v, v->dn, v->sn, 7233); | ||
367 | dwt_decode_sp(v, v->sn, v->dn, 434); | ||
368 | dwt_decode_dp(v, v->dn, v->sn, 12994); | ||
369 | } | ||
370 | } else { | ||
371 | if ((v->sn > 0) || (v->dn > 1)) { /* NEW : CASE ONE ELEMENT */ | ||
372 | for (i = 0; i < v->sn; i++) | ||
373 | WD(i) = fix_mul(WD(i), 10078); /* 10076 */ | ||
374 | for (i = 0; i < v->dn; i++) | ||
375 | WS(i) = fix_mul(WS(i), 13318); /* 13320 */ | ||
376 | dwt_decode_dm(v, v->sn, v->dn, 3633); | ||
377 | dwt_decode_sm(v, v->dn, v->sn, 7233); | ||
378 | dwt_decode_dp(v, v->sn, v->dn, 434); | ||
379 | dwt_decode_sp(v, v->dn, v->sn, 12994); | ||
380 | } | ||
381 | } | ||
382 | } | ||
383 | |||
384 | static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) { | ||
385 | int p, n; | ||
386 | p = int_floorlog2(stepsize) - 13; | ||
387 | n = 11 - int_floorlog2(stepsize); | ||
388 | bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; | ||
389 | bandno_stepsize->expn = numbps - p; | ||
390 | } | ||
391 | |||
392 | /* | ||
393 | ========================================================== | ||
394 | DWT interface | ||
395 | ========================================================== | ||
396 | */ | ||
397 | |||
398 | /* <summary> */ | ||
399 | /* Forward 5-3 wavelet tranform in 2-D. */ | ||
400 | /* </summary> */ | ||
401 | void dwt_encode(opj_tcd_tilecomp_t * tilec) { | ||
402 | int i, j, k; | ||
403 | int *a = NULL; | ||
404 | int *aj = NULL; | ||
405 | int *bj = NULL; | ||
406 | int w, l; | ||
407 | |||
408 | w = tilec->x1-tilec->x0; | ||
409 | l = tilec->numresolutions-1; | ||
410 | a = tilec->data; | ||
411 | |||
412 | for (i = 0; i < l; i++) { | ||
413 | int rw; /* width of the resolution level computed */ | ||
414 | int rh; /* heigth of the resolution level computed */ | ||
415 | int rw1; /* width of the resolution level once lower than computed one */ | ||
416 | int rh1; /* height of the resolution level once lower than computed one */ | ||
417 | int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ | ||
418 | int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ | ||
419 | int dn, sn; | ||
420 | |||
421 | rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0; | ||
422 | rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0; | ||
423 | rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0; | ||
424 | rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0; | ||
425 | |||
426 | cas_row = tilec->resolutions[l - i].x0 % 2; | ||
427 | cas_col = tilec->resolutions[l - i].y0 % 2; | ||
428 | |||
429 | sn = rh1; | ||
430 | dn = rh - rh1; | ||
431 | bj = (int*)opj_malloc(rh * sizeof(int)); | ||
432 | for (j = 0; j < rw; j++) { | ||
433 | aj = a + j; | ||
434 | for (k = 0; k < rh; k++) bj[k] = aj[k*w]; | ||
435 | dwt_encode_1(bj, dn, sn, cas_col); | ||
436 | dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col); | ||
437 | } | ||
438 | opj_free(bj); | ||
439 | |||
440 | sn = rw1; | ||
441 | dn = rw - rw1; | ||
442 | bj = (int*)opj_malloc(rw * sizeof(int)); | ||
443 | for (j = 0; j < rh; j++) { | ||
444 | aj = a + j * w; | ||
445 | for (k = 0; k < rw; k++) bj[k] = aj[k]; | ||
446 | dwt_encode_1(bj, dn, sn, cas_row); | ||
447 | dwt_deinterleave_h(bj, aj, dn, sn, cas_row); | ||
448 | } | ||
449 | opj_free(bj); | ||
450 | } | ||
451 | } | ||
452 | |||
453 | |||
454 | /* <summary> */ | ||
455 | /* Inverse 5-3 wavelet tranform in 2-D. */ | ||
456 | /* </summary> */ | ||
457 | void dwt_decode(opj_tcd_tilecomp_t * tilec, int stop) { | ||
458 | dwt_decode_tile(tilec, stop, &dwt_decode_1); | ||
459 | } | ||
460 | |||
461 | |||
462 | /* <summary> */ | ||
463 | /* Get gain of 5-3 wavelet transform. */ | ||
464 | /* </summary> */ | ||
465 | int dwt_getgain(int orient) { | ||
466 | if (orient == 0) | ||
467 | return 0; | ||
468 | if (orient == 1 || orient == 2) | ||
469 | return 1; | ||
470 | return 2; | ||
471 | } | ||
472 | |||
473 | /* <summary> */ | ||
474 | /* Get norm of 5-3 wavelet. */ | ||
475 | /* </summary> */ | ||
476 | double dwt_getnorm(int level, int orient) { | ||
477 | return dwt_norms[orient][level]; | ||
478 | } | ||
479 | |||
480 | /* <summary> */ | ||
481 | /* Forward 9-7 wavelet transform in 2-D. */ | ||
482 | /* </summary> */ | ||
483 | |||
484 | void dwt_encode_real(opj_tcd_tilecomp_t * tilec) { | ||
485 | int i, j, k; | ||
486 | int *a = NULL; | ||
487 | int *aj = NULL; | ||
488 | int *bj = NULL; | ||
489 | int w, l; | ||
490 | |||
491 | w = tilec->x1-tilec->x0; | ||
492 | l = tilec->numresolutions-1; | ||
493 | a = tilec->data; | ||
494 | |||
495 | for (i = 0; i < l; i++) { | ||
496 | int rw; /* width of the resolution level computed */ | ||
497 | int rh; /* heigth of the resolution level computed */ | ||
498 | int rw1; /* width of the resolution level once lower than computed one */ | ||
499 | int rh1; /* height of the resolution level once lower than computed one */ | ||
500 | int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ | ||
501 | int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ | ||
502 | int dn, sn; | ||
503 | |||
504 | rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0; | ||
505 | rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0; | ||
506 | rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0; | ||
507 | rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0; | ||
508 | |||
509 | cas_row = tilec->resolutions[l - i].x0 % 2; | ||
510 | cas_col = tilec->resolutions[l - i].y0 % 2; | ||
511 | |||
512 | sn = rh1; | ||
513 | dn = rh - rh1; | ||
514 | bj = (int*)opj_malloc(rh * sizeof(int)); | ||
515 | for (j = 0; j < rw; j++) { | ||
516 | aj = a + j; | ||
517 | for (k = 0; k < rh; k++) bj[k] = aj[k*w]; | ||
518 | dwt_encode_1_real(bj, dn, sn, cas_col); | ||
519 | dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col); | ||
520 | } | ||
521 | opj_free(bj); | ||
522 | |||
523 | sn = rw1; | ||
524 | dn = rw - rw1; | ||
525 | bj = (int*)opj_malloc(rw * sizeof(int)); | ||
526 | for (j = 0; j < rh; j++) { | ||
527 | aj = a + j * w; | ||
528 | for (k = 0; k < rw; k++) bj[k] = aj[k]; | ||
529 | dwt_encode_1_real(bj, dn, sn, cas_row); | ||
530 | dwt_deinterleave_h(bj, aj, dn, sn, cas_row); | ||
531 | } | ||
532 | opj_free(bj); | ||
533 | } | ||
534 | } | ||
535 | |||
536 | |||
537 | /* <summary> */ | ||
538 | /* Inverse 9-7 wavelet transform in 2-D. */ | ||
539 | /* </summary> */ | ||
540 | void dwt_decode_real(opj_tcd_tilecomp_t * tilec, int stop) { | ||
541 | dwt_decode_tile(tilec, stop, dwt_decode_1_real); | ||
542 | } | ||
543 | |||
544 | |||
545 | /* <summary> */ | ||
546 | /* Get gain of 9-7 wavelet transform. */ | ||
547 | /* </summary> */ | ||
548 | int dwt_getgain_real(int orient) { | ||
549 | (void)orient; | ||
550 | return 0; | ||
551 | } | ||
552 | |||
553 | /* <summary> */ | ||
554 | /* Get norm of 9-7 wavelet. */ | ||
555 | /* </summary> */ | ||
556 | double dwt_getnorm_real(int level, int orient) { | ||
557 | return dwt_norms_real[orient][level]; | ||
558 | } | ||
559 | |||
560 | void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) { | ||
561 | int numbands, bandno; | ||
562 | numbands = 3 * tccp->numresolutions - 2; | ||
563 | for (bandno = 0; bandno < numbands; bandno++) { | ||
564 | double stepsize; | ||
565 | int resno, level, orient, gain; | ||
566 | |||
567 | resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1); | ||
568 | orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1); | ||
569 | level = tccp->numresolutions - 1 - resno; | ||
570 | gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2)); | ||
571 | if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { | ||
572 | stepsize = 1.0; | ||
573 | } else { | ||
574 | double norm = dwt_norms_real[orient][level]; | ||
575 | stepsize = (1 << (gain)) / norm; | ||
576 | } | ||
577 | dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]); | ||
578 | } | ||
579 | } | ||
580 | |||
581 | |||
582 | /* <summary> */ | ||
583 | /* Determine maximum computed resolution level for inverse wavelet transform */ | ||
584 | /* </summary> */ | ||
585 | static int dwt_decode_max_resolution(opj_tcd_resolution_t* r, int i) { | ||
586 | int mr = 1; | ||
587 | int w; | ||
588 | while( --i ) { | ||
589 | r++; | ||
590 | if( mr < ( w = r->x1 - r->x0 ) ) | ||
591 | mr = w ; | ||
592 | if( mr < ( w = r->y1 - r->y0 ) ) | ||
593 | mr = w ; | ||
594 | } | ||
595 | return mr ; | ||
596 | } | ||
597 | |||
598 | |||
599 | /* <summary> */ | ||
600 | /* Inverse wavelet tranform in 2-D. */ | ||
601 | /* </summary> */ | ||
602 | static void dwt_decode_tile(opj_tcd_tilecomp_t * tilec, int stop, DWT1DFN dwt_1D) { | ||
603 | opj_tcd_resolution_t* tr; | ||
604 | int i, j, k; | ||
605 | int *a = NULL; | ||
606 | int *aj = NULL; | ||
607 | int *m; | ||
608 | int w; //, l; | ||
609 | int rw; /* width of the resolution level computed */ | ||
610 | int rh; /* heigth of the resolution level computed */ | ||
611 | dwt_t h; | ||
612 | dwt_t v; | ||
613 | |||
614 | if( 1 > ( i = tilec->numresolutions - stop ) ) | ||
615 | return ; | ||
616 | |||
617 | tr = tilec->resolutions; | ||
618 | |||
619 | w = tilec->x1-tilec->x0; | ||
620 | a = tilec->data; | ||
621 | |||
622 | m = (int*)opj_malloc(sizeof(int) * (dwt_decode_max_resolution(tr, i)+5)); | ||
623 | h.mem = v.mem = (int*)( (unsigned)m + 16 - ( (unsigned)m % 16 ) ) ; | ||
624 | |||
625 | rw = tr->x1 - tr->x0; | ||
626 | rh = tr->y1 - tr->y0; | ||
627 | |||
628 | while( --i ) { | ||
629 | tr++; | ||
630 | h.sn = rw; | ||
631 | v.sn = rh; | ||
632 | h.dn = ( rw = tr->x1 - tr->x0 ) - h.sn; | ||
633 | v.dn = ( rh = tr->y1 - tr->y0 ) - v.sn; | ||
634 | |||
635 | h.cas = tr->x0 % 2; | ||
636 | v.cas = tr->y0 % 2; | ||
637 | |||
638 | aj = a; | ||
639 | j = rh; | ||
640 | while( j-- ) { | ||
641 | dwt_interleave_h(&h, aj); | ||
642 | (dwt_1D)(&h); | ||
643 | k = rw; | ||
644 | while( k-- ) | ||
645 | aj[k] = h.mem[k]; | ||
646 | aj += w; | ||
647 | } | ||
648 | |||
649 | aj = a; | ||
650 | j = rw; | ||
651 | while( j-- ) { | ||
652 | dwt_interleave_v(&v, aj, w); | ||
653 | (dwt_1D)(&v); | ||
654 | k = rh; | ||
655 | while( k-- ) | ||
656 | aj[k * w] = v.mem[k]; | ||
657 | aj++; | ||
658 | } | ||
659 | } | ||
660 | opj_free(m); | ||
661 | } | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/dwt.h b/libraries/openjpeg-libsl/libopenjpeg/dwt.h new file mode 100644 index 0000000..5c95c76 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/dwt.h | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __DWT_H | ||
33 | #define __DWT_H | ||
34 | /** | ||
35 | @file dwt.h | ||
36 | @brief Implementation of a discrete wavelet transform (DWT) | ||
37 | |||
38 | The functions in DWT.C have for goal to realize forward and inverse discret wavelet | ||
39 | transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in | ||
40 | DWT.C are used by some function in TCD.C. | ||
41 | */ | ||
42 | |||
43 | /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ | ||
44 | /*@{*/ | ||
45 | |||
46 | |||
47 | /** @name Exported functions */ | ||
48 | /*@{*/ | ||
49 | /* ----------------------------------------------------------------------- */ | ||
50 | /** | ||
51 | Forward 5-3 wavelet tranform in 2-D. | ||
52 | Apply a reversible DWT transform to a component of an image. | ||
53 | @param tilec Tile component information (current tile) | ||
54 | */ | ||
55 | void dwt_encode(opj_tcd_tilecomp_t * tilec); | ||
56 | /** | ||
57 | Inverse 5-3 wavelet tranform in 2-D. | ||
58 | Apply a reversible inverse DWT transform to a component of an image. | ||
59 | @param tilec Tile component information (current tile) | ||
60 | @param stop FIXME Number of decoded resolution levels ? | ||
61 | */ | ||
62 | void dwt_decode(opj_tcd_tilecomp_t * tilec, int stop); | ||
63 | /** | ||
64 | Get the gain of a subband for the reversible 5-3 DWT. | ||
65 | @param orient Number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH) | ||
66 | @return Returns 0 if orient = 0, returns 1 if orient = 1 or 2, returns 2 otherwise | ||
67 | */ | ||
68 | int dwt_getgain(int orient); | ||
69 | /** | ||
70 | Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT. | ||
71 | @param level Level of the wavelet function | ||
72 | @param orient Band of the wavelet function | ||
73 | @return Returns the norm of the wavelet function | ||
74 | */ | ||
75 | double dwt_getnorm(int level, int orient); | ||
76 | /** | ||
77 | Forward 9-7 wavelet transform in 2-D. | ||
78 | Apply an irreversible DWT transform to a component of an image. | ||
79 | @param tilec Tile component information (current tile) | ||
80 | */ | ||
81 | void dwt_encode_real(opj_tcd_tilecomp_t * tilec); | ||
82 | /** | ||
83 | Inverse 9-7 wavelet transform in 2-D. | ||
84 | Apply an irreversible inverse DWT transform to a component of an image. | ||
85 | @param tilec Tile component information (current tile) | ||
86 | @param stop FIXME Number of decoded resolution levels ? | ||
87 | */ | ||
88 | void dwt_decode_real(opj_tcd_tilecomp_t * tilec, int stop); | ||
89 | /** | ||
90 | Get the gain of a subband for the irreversible 9-7 DWT. | ||
91 | @param orient Number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH) | ||
92 | @return Returns the gain of the 9-7 wavelet transform | ||
93 | */ | ||
94 | int dwt_getgain_real(int orient); | ||
95 | /** | ||
96 | Get the norm of a wavelet function of a subband at a specified level for the irreversible 9-7 DWT | ||
97 | @param level Level of the wavelet function | ||
98 | @param orient Band of the wavelet function | ||
99 | @return Returns the norm of the 9-7 wavelet | ||
100 | */ | ||
101 | double dwt_getnorm_real(int level, int orient); | ||
102 | /** | ||
103 | FIXME : comment ??? | ||
104 | @param tccp | ||
105 | @param prec | ||
106 | */ | ||
107 | void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec); | ||
108 | /* ----------------------------------------------------------------------- */ | ||
109 | /*@}*/ | ||
110 | |||
111 | /*@}*/ | ||
112 | |||
113 | #endif /* __DWT_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/event.c b/libraries/openjpeg-libsl/libopenjpeg/event.c new file mode 100644 index 0000000..291ff58 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/event.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #include "opj_includes.h" | ||
28 | |||
29 | /* ========================================================== | ||
30 | Utility functions | ||
31 | ==========================================================*/ | ||
32 | |||
33 | #if !defined(_MSC_VER) && !defined(__MINGW32__) | ||
34 | static char* | ||
35 | i2a(unsigned i, char *a, unsigned r) { | ||
36 | if (i/r > 0) a = i2a(i/r,a,r); | ||
37 | *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r]; | ||
38 | return a+1; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | Transforms integer i into an ascii string and stores the result in a; | ||
43 | string is encoded in the base indicated by r. | ||
44 | @param i Number to be converted | ||
45 | @param a String result | ||
46 | @param r Base of value; must be in the range 2 - 36 | ||
47 | @return Returns a | ||
48 | */ | ||
49 | static char * | ||
50 | _itoa(int i, char *a, int r) { | ||
51 | r = ((r < 2) || (r > 36)) ? 10 : r; | ||
52 | if(i < 0) { | ||
53 | *a = '-'; | ||
54 | *i2a(-i, a+1, r) = 0; | ||
55 | } | ||
56 | else *i2a(i, a, r) = 0; | ||
57 | return a; | ||
58 | } | ||
59 | |||
60 | #endif /* !WIN32 */ | ||
61 | |||
62 | /* ----------------------------------------------------------------------- */ | ||
63 | |||
64 | opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) { | ||
65 | if(cinfo) { | ||
66 | opj_event_mgr_t *previous = cinfo->event_mgr; | ||
67 | cinfo->event_mgr = event_mgr; | ||
68 | cinfo->client_data = context; | ||
69 | return previous; | ||
70 | } | ||
71 | |||
72 | return NULL; | ||
73 | } | ||
74 | |||
75 | bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) { | ||
76 | #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */ | ||
77 | opj_msg_callback msg_handler = NULL; | ||
78 | |||
79 | opj_event_mgr_t *event_mgr = cinfo->event_mgr; | ||
80 | if(event_mgr != NULL) { | ||
81 | switch(event_type) { | ||
82 | case EVT_ERROR: | ||
83 | msg_handler = event_mgr->error_handler; | ||
84 | break; | ||
85 | case EVT_WARNING: | ||
86 | msg_handler = event_mgr->warning_handler; | ||
87 | break; | ||
88 | case EVT_INFO: | ||
89 | msg_handler = event_mgr->info_handler; | ||
90 | break; | ||
91 | default: | ||
92 | break; | ||
93 | } | ||
94 | if(msg_handler == NULL) { | ||
95 | return false; | ||
96 | } | ||
97 | } else { | ||
98 | return false; | ||
99 | } | ||
100 | |||
101 | if ((fmt != NULL) && (event_mgr != NULL)) { | ||
102 | va_list arg; | ||
103 | int str_length/*, i, j*/; /* UniPG */ | ||
104 | char message[MSG_SIZE]; | ||
105 | memset(message, 0, MSG_SIZE); | ||
106 | /* initialize the optional parameter list */ | ||
107 | va_start(arg, fmt); | ||
108 | /* check the length of the format string */ | ||
109 | str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt); | ||
110 | /* parse the format string and put the result in 'message' */ | ||
111 | vsprintf(message, fmt, arg); /* UniPG */ | ||
112 | /* deinitialize the optional parameter list */ | ||
113 | va_end(arg); | ||
114 | |||
115 | /* output the message to the user program */ | ||
116 | msg_handler(message, cinfo->client_data); | ||
117 | } | ||
118 | |||
119 | return true; | ||
120 | } | ||
121 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/event.h b/libraries/openjpeg-libsl/libopenjpeg/event.h new file mode 100644 index 0000000..11910b0 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/event.h | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | #ifndef __EVENT_H | ||
27 | #define __EVENT_H | ||
28 | /** | ||
29 | @file event.h | ||
30 | @brief Implementation of a event callback system | ||
31 | |||
32 | The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user. | ||
33 | */ | ||
34 | |||
35 | #define EVT_ERROR 1 /**< Error event type */ | ||
36 | #define EVT_WARNING 2 /**< Warning event type */ | ||
37 | #define EVT_INFO 4 /**< Debug event type */ | ||
38 | |||
39 | /** @defgroup EVENT EVENT - Implementation of a event callback system */ | ||
40 | /*@{*/ | ||
41 | |||
42 | /** @name Exported functions (see also openjpeg.h) */ | ||
43 | /*@{*/ | ||
44 | /* ----------------------------------------------------------------------- */ | ||
45 | /** | ||
46 | Write formatted data to a string and send the string to a user callback. | ||
47 | @param cinfo Codec context info | ||
48 | @param event_type Event type or callback to use to send the message | ||
49 | @param fmt Format-control string (plus optionnal arguments) | ||
50 | @return Returns true if successful, returns false otherwise | ||
51 | */ | ||
52 | bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...); | ||
53 | /* ----------------------------------------------------------------------- */ | ||
54 | /*@}*/ | ||
55 | |||
56 | /*@}*/ | ||
57 | |||
58 | #endif /* __EVENT_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/fix.h b/libraries/openjpeg-libsl/libopenjpeg/fix.h new file mode 100644 index 0000000..bcb2acb --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/fix.h | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | #ifndef __FIX_H | ||
32 | #define __FIX_H | ||
33 | |||
34 | #if defined(_MSC_VER) || defined(__BORLANDC__) | ||
35 | #define int64 __int64 | ||
36 | #else | ||
37 | #define int64 long long | ||
38 | #endif | ||
39 | |||
40 | /** | ||
41 | @file fix.h | ||
42 | @brief Implementation of operations of specific multiplication (FIX) | ||
43 | |||
44 | The functions in FIX.H have for goal to realize specific multiplication. | ||
45 | */ | ||
46 | |||
47 | /** @defgroup FIX FIX - Implementation of operations of specific multiplication */ | ||
48 | /*@{*/ | ||
49 | |||
50 | /** | ||
51 | Multiply two fixed-precision rational numbers. | ||
52 | @param a | ||
53 | @param b | ||
54 | @return Returns a * b | ||
55 | */ | ||
56 | static INLINE int fix_mul(int a, int b) { | ||
57 | int64 temp = (int64) a * (int64) b ; | ||
58 | temp += temp & 4096; | ||
59 | return (int) (temp >> 13) ; | ||
60 | } | ||
61 | |||
62 | /*@}*/ | ||
63 | |||
64 | #endif /* __FIX_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/image.c b/libraries/openjpeg-libsl/libopenjpeg/image.c new file mode 100644 index 0000000..84664e9 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/image.c | |||
@@ -0,0 +1,87 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #include "opj_includes.h" | ||
28 | |||
29 | opj_image_t* opj_image_create0() { | ||
30 | opj_image_t *image = (opj_image_t*)opj_malloc(sizeof(opj_image_t)); | ||
31 | return image; | ||
32 | } | ||
33 | |||
34 | opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { | ||
35 | int compno; | ||
36 | opj_image_t *image = NULL; | ||
37 | |||
38 | image = (opj_image_t*)opj_malloc(sizeof(opj_image_t)); | ||
39 | if(image) { | ||
40 | image->color_space = clrspc; | ||
41 | image->numcomps = numcmpts; | ||
42 | /* allocate memory for the per-component information */ | ||
43 | image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t)); | ||
44 | if(!image->comps) { | ||
45 | opj_image_destroy(image); | ||
46 | return NULL; | ||
47 | } | ||
48 | /* create the individual image components */ | ||
49 | for(compno = 0; compno < numcmpts; compno++) { | ||
50 | opj_image_comp_t *comp = &image->comps[compno]; | ||
51 | comp->dx = cmptparms[compno].dx; | ||
52 | comp->dy = cmptparms[compno].dy; | ||
53 | comp->w = cmptparms[compno].w; | ||
54 | comp->h = cmptparms[compno].h; | ||
55 | comp->x0 = cmptparms[compno].x0; | ||
56 | comp->y0 = cmptparms[compno].y0; | ||
57 | comp->prec = cmptparms[compno].prec; | ||
58 | comp->bpp = cmptparms[compno].bpp; | ||
59 | comp->sgnd = cmptparms[compno].sgnd; | ||
60 | comp->data = (int*)opj_malloc(comp->w * comp->h * sizeof(int)); | ||
61 | if(!comp->data) { | ||
62 | opj_image_destroy(image); | ||
63 | return NULL; | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | return image; | ||
69 | } | ||
70 | |||
71 | void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) { | ||
72 | int i; | ||
73 | if(image) { | ||
74 | if(image->comps) { | ||
75 | /* image components */ | ||
76 | for(i = 0; i < image->numcomps; i++) { | ||
77 | opj_image_comp_t *image_comp = &image->comps[i]; | ||
78 | if(image_comp->data) { | ||
79 | opj_free(image_comp->data); | ||
80 | } | ||
81 | } | ||
82 | opj_free(image->comps); | ||
83 | } | ||
84 | opj_free(image); | ||
85 | } | ||
86 | } | ||
87 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/image.h b/libraries/openjpeg-libsl/libopenjpeg/image.h new file mode 100644 index 0000000..b56de4a --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/image.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | #ifndef __IMAGE_H | ||
27 | #define __IMAGE_H | ||
28 | /** | ||
29 | @file image.h | ||
30 | @brief Implementation of operations on images (IMAGE) | ||
31 | |||
32 | The functions in IMAGE.C have for goal to realize operations on images. | ||
33 | */ | ||
34 | |||
35 | /** @defgroup IMAGE IMAGE - Implementation of operations on images */ | ||
36 | /*@{*/ | ||
37 | |||
38 | /** | ||
39 | Create an empty image | ||
40 | @todo this function should be removed | ||
41 | @return returns an empty image if successful, returns NULL otherwise | ||
42 | */ | ||
43 | opj_image_t* opj_image_create0(); | ||
44 | |||
45 | /*@}*/ | ||
46 | |||
47 | #endif /* __IMAGE_H */ | ||
48 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/int.h b/libraries/openjpeg-libsl/libopenjpeg/int.h new file mode 100644 index 0000000..4e5fe08 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/int.h | |||
@@ -0,0 +1,119 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | #ifndef __INT_H | ||
32 | #define __INT_H | ||
33 | /** | ||
34 | @file int.h | ||
35 | @brief Implementation of operations on integers (INT) | ||
36 | |||
37 | The functions in INT.H have for goal to realize operations on integers. | ||
38 | */ | ||
39 | |||
40 | /** @defgroup INT INT - Implementation of operations on integers */ | ||
41 | /*@{*/ | ||
42 | |||
43 | /** @name Exported functions (see also openjpeg.h) */ | ||
44 | /*@{*/ | ||
45 | /* ----------------------------------------------------------------------- */ | ||
46 | /** | ||
47 | Get the minimum of two integers | ||
48 | @return Returns a if a < b else b | ||
49 | */ | ||
50 | static INLINE int int_min(int a, int b) { | ||
51 | return a < b ? a : b; | ||
52 | } | ||
53 | /** | ||
54 | Get the maximum of two integers | ||
55 | @return Returns a if a > b else b | ||
56 | */ | ||
57 | static INLINE int int_max(int a, int b) { | ||
58 | return (a > b) ? a : b; | ||
59 | } | ||
60 | /** | ||
61 | Clamp an integer inside an interval | ||
62 | @return | ||
63 | <ul> | ||
64 | <li>Returns a if (min < a < max) | ||
65 | <li>Returns max if (a > max) | ||
66 | <li>Returns min if (a < min) | ||
67 | </ul> | ||
68 | */ | ||
69 | static INLINE int int_clamp(int a, int min, int max) { | ||
70 | if (a < min) | ||
71 | return min; | ||
72 | if (a > max) | ||
73 | return max; | ||
74 | return a; | ||
75 | } | ||
76 | /** | ||
77 | @return Get absolute value of integer | ||
78 | */ | ||
79 | static INLINE int int_abs(int a) { | ||
80 | return a < 0 ? -a : a; | ||
81 | } | ||
82 | /** | ||
83 | Divide an integer and round upwards | ||
84 | @return Returns a divided by b | ||
85 | */ | ||
86 | static INLINE int int_ceildiv(int a, int b) { | ||
87 | return (a + b - 1) / b; | ||
88 | } | ||
89 | /** | ||
90 | Divide an integer by a power of 2 and round upwards | ||
91 | @return Returns a divided by 2^b | ||
92 | */ | ||
93 | static INLINE int int_ceildivpow2(int a, int b) { | ||
94 | return (a + (1 << b) - 1) >> b; | ||
95 | } | ||
96 | /** | ||
97 | Divide an integer by a power of 2 and round downwards | ||
98 | @return Returns a divided by 2^b | ||
99 | */ | ||
100 | static INLINE int int_floordivpow2(int a, int b) { | ||
101 | return a >> b; | ||
102 | } | ||
103 | /** | ||
104 | Get logarithm of an integer and round downwards | ||
105 | @return Returns log2(a) | ||
106 | */ | ||
107 | static INLINE int int_floorlog2(int a) { | ||
108 | int l; | ||
109 | for (l = 0; a > 1; l++) { | ||
110 | a >>= 1; | ||
111 | } | ||
112 | return l; | ||
113 | } | ||
114 | /* ----------------------------------------------------------------------- */ | ||
115 | /*@}*/ | ||
116 | |||
117 | /*@}*/ | ||
118 | |||
119 | #endif | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/j2k.c b/libraries/openjpeg-libsl/libopenjpeg/j2k.c new file mode 100644 index 0000000..1ef653e --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/j2k.c | |||
@@ -0,0 +1,2630 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2006-2007, Parvatha Elangovan | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "opj_includes.h" | ||
34 | |||
35 | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ | ||
36 | /*@{*/ | ||
37 | |||
38 | /** @name Local static functions */ | ||
39 | /*@{*/ | ||
40 | |||
41 | /** | ||
42 | Write the SOC marker (Start Of Codestream) | ||
43 | @param j2k J2K handle | ||
44 | */ | ||
45 | static void j2k_write_soc(opj_j2k_t *j2k); | ||
46 | /** | ||
47 | Read the SOC marker (Start of Codestream) | ||
48 | @param j2k J2K handle | ||
49 | */ | ||
50 | static void j2k_read_soc(opj_j2k_t *j2k); | ||
51 | /** | ||
52 | Write the SIZ marker (image and tile size) | ||
53 | @param j2k J2K handle | ||
54 | */ | ||
55 | static void j2k_write_siz(opj_j2k_t *j2k); | ||
56 | /** | ||
57 | Read the SIZ marker (image and tile size) | ||
58 | @param j2k J2K handle | ||
59 | */ | ||
60 | static void j2k_read_siz(opj_j2k_t *j2k); | ||
61 | /** | ||
62 | Write the COM marker (comment) | ||
63 | @param j2k J2K handle | ||
64 | */ | ||
65 | static void j2k_write_com(opj_j2k_t *j2k); | ||
66 | /** | ||
67 | Read the COM marker (comment) | ||
68 | @param j2k J2K handle | ||
69 | */ | ||
70 | static void j2k_read_com(opj_j2k_t *j2k); | ||
71 | /** | ||
72 | Write the value concerning the specified component in the marker COD and COC | ||
73 | @param j2k J2K handle | ||
74 | @param compno Number of the component concerned by the information written | ||
75 | */ | ||
76 | static void j2k_write_cox(opj_j2k_t *j2k, int compno); | ||
77 | /** | ||
78 | Read the value concerning the specified component in the marker COD and COC | ||
79 | @param j2k J2K handle | ||
80 | @param compno Number of the component concerned by the information read | ||
81 | */ | ||
82 | static void j2k_read_cox(opj_j2k_t *j2k, int compno); | ||
83 | /** | ||
84 | Write the COD marker (coding style default) | ||
85 | @param j2k J2K handle | ||
86 | */ | ||
87 | static void j2k_write_cod(opj_j2k_t *j2k); | ||
88 | /** | ||
89 | Read the COD marker (coding style default) | ||
90 | @param j2k J2K handle | ||
91 | */ | ||
92 | static void j2k_read_cod(opj_j2k_t *j2k); | ||
93 | /** | ||
94 | Write the COC marker (coding style component) | ||
95 | @param j2k J2K handle | ||
96 | @param compno Number of the component concerned by the information written | ||
97 | */ | ||
98 | static void j2k_write_coc(opj_j2k_t *j2k, int compno); | ||
99 | /** | ||
100 | Read the COC marker (coding style component) | ||
101 | @param j2k J2K handle | ||
102 | */ | ||
103 | static void j2k_read_coc(opj_j2k_t *j2k); | ||
104 | /** | ||
105 | Write the value concerning the specified component in the marker QCD and QCC | ||
106 | @param j2k J2K handle | ||
107 | @param compno Number of the component concerned by the information written | ||
108 | */ | ||
109 | static void j2k_write_qcx(opj_j2k_t *j2k, int compno); | ||
110 | /** | ||
111 | Read the value concerning the specified component in the marker QCD and QCC | ||
112 | @param j2k J2K handle | ||
113 | @param compno Number of the component concern by the information read | ||
114 | @param len Length of the information in the QCX part of the marker QCD/QCC | ||
115 | */ | ||
116 | static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len); | ||
117 | /** | ||
118 | Write the QCD marker (quantization default) | ||
119 | @param j2k J2K handle | ||
120 | */ | ||
121 | static void j2k_write_qcd(opj_j2k_t *j2k); | ||
122 | /** | ||
123 | Read the QCD marker (quantization default) | ||
124 | @param j2k J2K handle | ||
125 | */ | ||
126 | static void j2k_read_qcd(opj_j2k_t *j2k); | ||
127 | /** | ||
128 | Write the QCC marker (quantization component) | ||
129 | @param j2k J2K handle | ||
130 | @param compno Number of the component concerned by the information written | ||
131 | */ | ||
132 | static void j2k_write_qcc(opj_j2k_t *j2k, int compno); | ||
133 | /** | ||
134 | Read the QCC marker (quantization component) | ||
135 | @param j2k J2K handle | ||
136 | */ | ||
137 | static void j2k_read_qcc(opj_j2k_t *j2k); | ||
138 | /** | ||
139 | Write the POC marker (progression order change) | ||
140 | @param j2k J2K handle | ||
141 | */ | ||
142 | static void j2k_write_poc(opj_j2k_t *j2k); | ||
143 | /** | ||
144 | Read the POC marker (progression order change) | ||
145 | @param j2k J2K handle | ||
146 | */ | ||
147 | static void j2k_read_poc(opj_j2k_t *j2k); | ||
148 | /** | ||
149 | Read the CRG marker (component registration) | ||
150 | @param j2k J2K handle | ||
151 | */ | ||
152 | static void j2k_read_crg(opj_j2k_t *j2k); | ||
153 | /** | ||
154 | Read the TLM marker (tile-part lengths) | ||
155 | @param j2k J2K handle | ||
156 | */ | ||
157 | static void j2k_read_tlm(opj_j2k_t *j2k); | ||
158 | /** | ||
159 | Read the PLM marker (packet length, main header) | ||
160 | @param j2k J2K handle | ||
161 | */ | ||
162 | static void j2k_read_plm(opj_j2k_t *j2k); | ||
163 | /** | ||
164 | Read the PLT marker (packet length, tile-part header) | ||
165 | @param j2k J2K handle | ||
166 | */ | ||
167 | static void j2k_read_plt(opj_j2k_t *j2k); | ||
168 | /** | ||
169 | Read the PPM marker (packet packet headers, main header) | ||
170 | @param j2k J2K handle | ||
171 | */ | ||
172 | static void j2k_read_ppm(opj_j2k_t *j2k); | ||
173 | /** | ||
174 | Read the PPT marker (packet packet headers, tile-part header) | ||
175 | @param j2k J2K handle | ||
176 | */ | ||
177 | static void j2k_read_ppt(opj_j2k_t *j2k); | ||
178 | /** | ||
179 | Write the TLM marker (Mainheader) | ||
180 | @param j2k J2K handle | ||
181 | */ | ||
182 | static void j2k_write_tlm(opj_j2k_t *j2k); | ||
183 | /** | ||
184 | Write the SOT marker (start of tile-part) | ||
185 | @param j2k J2K handle | ||
186 | */ | ||
187 | static void j2k_write_sot(opj_j2k_t *j2k); | ||
188 | /** | ||
189 | Read the SOT marker (start of tile-part) | ||
190 | @param j2k J2K handle | ||
191 | */ | ||
192 | static void j2k_read_sot(opj_j2k_t *j2k); | ||
193 | /** | ||
194 | Write the SOD marker (start of data) | ||
195 | @param j2k J2K handle | ||
196 | @param tile_coder Pointer to a TCD handle | ||
197 | */ | ||
198 | static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder); | ||
199 | /** | ||
200 | Read the SOD marker (start of data) | ||
201 | @param j2k J2K handle | ||
202 | */ | ||
203 | static void j2k_read_sod(opj_j2k_t *j2k); | ||
204 | /** | ||
205 | Write the RGN marker (region-of-interest) | ||
206 | @param j2k J2K handle | ||
207 | @param compno Number of the component concerned by the information written | ||
208 | @param tileno Number of the tile concerned by the information written | ||
209 | */ | ||
210 | static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno); | ||
211 | /** | ||
212 | Read the RGN marker (region-of-interest) | ||
213 | @param j2k J2K handle | ||
214 | */ | ||
215 | static void j2k_read_rgn(opj_j2k_t *j2k); | ||
216 | /** | ||
217 | Write the EOC marker (end of codestream) | ||
218 | @param j2k J2K handle | ||
219 | */ | ||
220 | static void j2k_write_eoc(opj_j2k_t *j2k); | ||
221 | /** | ||
222 | Read the EOC marker (end of codestream) | ||
223 | @param j2k J2K handle | ||
224 | */ | ||
225 | static void j2k_read_eoc(opj_j2k_t *j2k); | ||
226 | /** | ||
227 | Read an unknown marker | ||
228 | @param j2k J2K handle | ||
229 | */ | ||
230 | static void j2k_read_unk(opj_j2k_t *j2k); | ||
231 | |||
232 | /*@}*/ | ||
233 | |||
234 | /*@}*/ | ||
235 | |||
236 | /* ----------------------------------------------------------------------- */ | ||
237 | typedef struct j2k_prog_order{ | ||
238 | OPJ_PROG_ORDER enum_prog; | ||
239 | char str_prog[4]; | ||
240 | }j2k_prog_order_t; | ||
241 | |||
242 | j2k_prog_order_t j2k_prog_order_list[] = { | ||
243 | {CPRL, "CPRL"}, | ||
244 | {LRCP, "LRCP"}, | ||
245 | {PCRL, "PCRL"}, | ||
246 | {RLCP, "RLCP"}, | ||
247 | {RPCL, "RPCL"}, | ||
248 | {-1, ""} | ||
249 | }; | ||
250 | |||
251 | char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){ | ||
252 | j2k_prog_order_t *po; | ||
253 | for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){ | ||
254 | if(po->enum_prog == prg_order){ | ||
255 | break; | ||
256 | } | ||
257 | } | ||
258 | return po->str_prog; | ||
259 | } | ||
260 | |||
261 | static void j2k_check_poc_val(opj_cparameters_t *parameters, int numcomps, int numlayers){ | ||
262 | int index, resno, compno, layno, i; | ||
263 | char loss = 0; | ||
264 | int step_c = 1; | ||
265 | int step_r = numcomps * step_c; | ||
266 | int step_l = parameters->numresolution * step_r; | ||
267 | int array_size = step_l * numlayers * sizeof(int); | ||
268 | int *packet_array = (int *) opj_malloc(array_size); | ||
269 | |||
270 | for (i = 0; i < parameters->numpocs ; i++) { | ||
271 | int layno0 = 0; | ||
272 | if(i > 0) | ||
273 | layno0 = (parameters->POC[i].layno1 > parameters->POC[i-1].layno1 )? parameters->POC[i-1].layno1 : 0; | ||
274 | for (resno = parameters->POC[i].resno0 ; resno < parameters->POC[i].resno1 ; resno++) { | ||
275 | for (compno = parameters->POC[i].compno0 ; compno < parameters->POC[i].compno1 ; compno++) { | ||
276 | for (layno = layno0; layno < parameters->POC[i].layno1 ; layno++) { | ||
277 | index = step_r * resno + step_c * compno + step_l * layno; | ||
278 | packet_array[index]= 1; | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | } | ||
283 | for (resno = 0; resno < parameters->numresolution; resno++) { | ||
284 | for (compno = 0; compno < numcomps; compno++) { | ||
285 | for (layno = 0; layno < numlayers ; layno++) { | ||
286 | index = step_r * resno + step_c * compno + step_l * layno; | ||
287 | if(!( packet_array[index]== 1)){ | ||
288 | loss = 1; | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | } | ||
293 | if(loss == 1) | ||
294 | fprintf(stdout,"Missing packets possible loss of data\n"); | ||
295 | opj_free(packet_array); | ||
296 | } | ||
297 | |||
298 | void j2k_dump_image(FILE *fd, opj_image_t * img) { | ||
299 | int compno; | ||
300 | fprintf(fd, "image {\n"); | ||
301 | fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1); | ||
302 | fprintf(fd, " numcomps=%d\n", img->numcomps); | ||
303 | for (compno = 0; compno < img->numcomps; compno++) { | ||
304 | opj_image_comp_t *comp = &img->comps[compno]; | ||
305 | fprintf(fd, " comp %d {\n", compno); | ||
306 | fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy); | ||
307 | fprintf(fd, " prec=%d\n", comp->prec); | ||
308 | fprintf(fd, " sgnd=%d\n", comp->sgnd); | ||
309 | fprintf(fd, " }\n"); | ||
310 | } | ||
311 | fprintf(fd, "}\n"); | ||
312 | } | ||
313 | |||
314 | void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) { | ||
315 | int tileno, compno, layno, bandno, resno, numbands; | ||
316 | fprintf(fd, "coding parameters {\n"); | ||
317 | fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0); | ||
318 | fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy); | ||
319 | fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th); | ||
320 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | ||
321 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
322 | fprintf(fd, " tile %d {\n", tileno); | ||
323 | fprintf(fd, " csty=%x\n", tcp->csty); | ||
324 | fprintf(fd, " prg=%d\n", tcp->prg); | ||
325 | fprintf(fd, " numlayers=%d\n", tcp->numlayers); | ||
326 | fprintf(fd, " mct=%d\n", tcp->mct); | ||
327 | fprintf(fd, " rates="); | ||
328 | for (layno = 0; layno < tcp->numlayers; layno++) { | ||
329 | fprintf(fd, "%.1f ", tcp->rates[layno]); | ||
330 | } | ||
331 | fprintf(fd, "\n"); | ||
332 | for (compno = 0; compno < img->numcomps; compno++) { | ||
333 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
334 | fprintf(fd, " comp %d {\n", compno); | ||
335 | fprintf(fd, " csty=%x\n", tccp->csty); | ||
336 | fprintf(fd, " numresolutions=%d\n", tccp->numresolutions); | ||
337 | fprintf(fd, " cblkw=%d\n", tccp->cblkw); | ||
338 | fprintf(fd, " cblkh=%d\n", tccp->cblkh); | ||
339 | fprintf(fd, " cblksty=%x\n", tccp->cblksty); | ||
340 | fprintf(fd, " qmfbid=%d\n", tccp->qmfbid); | ||
341 | fprintf(fd, " qntsty=%d\n", tccp->qntsty); | ||
342 | fprintf(fd, " numgbits=%d\n", tccp->numgbits); | ||
343 | fprintf(fd, " roishift=%d\n", tccp->roishift); | ||
344 | fprintf(fd, " stepsizes="); | ||
345 | numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; | ||
346 | for (bandno = 0; bandno < numbands; bandno++) { | ||
347 | fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant, | ||
348 | tccp->stepsizes[bandno].expn); | ||
349 | } | ||
350 | fprintf(fd, "\n"); | ||
351 | |||
352 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
353 | fprintf(fd, " prcw="); | ||
354 | for (resno = 0; resno < tccp->numresolutions; resno++) { | ||
355 | fprintf(fd, "%d ", tccp->prcw[resno]); | ||
356 | } | ||
357 | fprintf(fd, "\n"); | ||
358 | fprintf(fd, " prch="); | ||
359 | for (resno = 0; resno < tccp->numresolutions; resno++) { | ||
360 | fprintf(fd, "%d ", tccp->prch[resno]); | ||
361 | } | ||
362 | fprintf(fd, "\n"); | ||
363 | } | ||
364 | fprintf(fd, " }\n"); | ||
365 | } | ||
366 | fprintf(fd, " }\n"); | ||
367 | } | ||
368 | fprintf(fd, "}\n"); | ||
369 | } | ||
370 | |||
371 | /* ----------------------------------------------------------------------- */ | ||
372 | static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){ | ||
373 | char *prog; | ||
374 | int i; | ||
375 | int tpnum=1,tpend=0; | ||
376 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
377 | prog = j2k_convert_progression_order(tcp->prg); | ||
378 | |||
379 | if(cp->tp_on == 1){ | ||
380 | for(i=0;i<4;i++){ | ||
381 | if(tpend!=1){ | ||
382 | if( cp->tp_flag == prog[i] ){ | ||
383 | tpend=1;cp->tp_pos=i; | ||
384 | } | ||
385 | switch(prog[i]){ | ||
386 | case 'C': | ||
387 | tpnum= tpnum * tcp->pocs[pino].compE; | ||
388 | break; | ||
389 | case 'R': | ||
390 | tpnum= tpnum * tcp->pocs[pino].resE; | ||
391 | break; | ||
392 | case 'P': | ||
393 | tpnum= tpnum * tcp->pocs[pino].prcE; | ||
394 | break; | ||
395 | case 'L': | ||
396 | tpnum= tpnum * tcp->pocs[pino].layE; | ||
397 | break; | ||
398 | } | ||
399 | } | ||
400 | } | ||
401 | }else{ | ||
402 | tpnum=1; | ||
403 | } | ||
404 | return tpnum; | ||
405 | } | ||
406 | |||
407 | /** mem allocation for TLM marker*/ | ||
408 | int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){ | ||
409 | int pino,tileno,maxres=0,totnum_tp=0; | ||
410 | j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int)); | ||
411 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | ||
412 | int cur_totnum_tp = 0; | ||
413 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
414 | for(pino = 0; pino <= tcp->numpocs; pino++) { | ||
415 | int tp_num=0; | ||
416 | opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS); | ||
417 | if(!pi) { return -1;} | ||
418 | tp_num = j2k_get_num_tp(cp,pino,tileno); | ||
419 | totnum_tp = totnum_tp + tp_num; | ||
420 | cur_totnum_tp = cur_totnum_tp + tp_num; | ||
421 | pi_destroy(pi, cp, tileno); | ||
422 | } | ||
423 | j2k->cur_totnum_tp[tileno] = cur_totnum_tp; | ||
424 | } | ||
425 | return totnum_tp; | ||
426 | } | ||
427 | |||
428 | static void j2k_write_soc(opj_j2k_t *j2k) { | ||
429 | opj_cio_t *cio = j2k->cio; | ||
430 | cio_write(cio, J2K_MS_SOC, 2); | ||
431 | } | ||
432 | |||
433 | static void j2k_read_soc(opj_j2k_t *j2k) { | ||
434 | j2k->state = J2K_STATE_MHSIZ; | ||
435 | } | ||
436 | |||
437 | static void j2k_write_siz(opj_j2k_t *j2k) { | ||
438 | int i; | ||
439 | int lenp, len; | ||
440 | |||
441 | opj_cio_t *cio = j2k->cio; | ||
442 | opj_image_t *image = j2k->image; | ||
443 | opj_cp_t *cp = j2k->cp; | ||
444 | |||
445 | cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */ | ||
446 | lenp = cio_tell(cio); | ||
447 | cio_skip(cio, 2); | ||
448 | cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */ | ||
449 | cio_write(cio, image->x1, 4); /* Xsiz */ | ||
450 | cio_write(cio, image->y1, 4); /* Ysiz */ | ||
451 | cio_write(cio, image->x0, 4); /* X0siz */ | ||
452 | cio_write(cio, image->y0, 4); /* Y0siz */ | ||
453 | cio_write(cio, cp->tdx, 4); /* XTsiz */ | ||
454 | cio_write(cio, cp->tdy, 4); /* YTsiz */ | ||
455 | cio_write(cio, cp->tx0, 4); /* XT0siz */ | ||
456 | cio_write(cio, cp->ty0, 4); /* YT0siz */ | ||
457 | cio_write(cio, image->numcomps, 2); /* Csiz */ | ||
458 | for (i = 0; i < image->numcomps; i++) { | ||
459 | cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */ | ||
460 | cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */ | ||
461 | cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */ | ||
462 | } | ||
463 | len = cio_tell(cio) - lenp; | ||
464 | cio_seek(cio, lenp); | ||
465 | cio_write(cio, len, 2); /* Lsiz */ | ||
466 | cio_seek(cio, lenp + len); | ||
467 | } | ||
468 | |||
469 | static void j2k_read_siz(opj_j2k_t *j2k) { | ||
470 | int len, i; | ||
471 | |||
472 | opj_cio_t *cio = j2k->cio; | ||
473 | opj_image_t *image = j2k->image; | ||
474 | opj_cp_t *cp = j2k->cp; | ||
475 | |||
476 | len = cio_read(cio, 2); /* Lsiz */ | ||
477 | cio_read(cio, 2); /* Rsiz (capabilities) */ | ||
478 | image->x1 = cio_read(cio, 4); /* Xsiz */ | ||
479 | image->y1 = cio_read(cio, 4); /* Ysiz */ | ||
480 | image->x0 = cio_read(cio, 4); /* X0siz */ | ||
481 | image->y0 = cio_read(cio, 4); /* Y0siz */ | ||
482 | cp->tdx = cio_read(cio, 4); /* XTsiz */ | ||
483 | cp->tdy = cio_read(cio, 4); /* YTsiz */ | ||
484 | cp->tx0 = cio_read(cio, 4); /* XT0siz */ | ||
485 | cp->ty0 = cio_read(cio, 4); /* YT0siz */ | ||
486 | |||
487 | image->numcomps = cio_read(cio, 2); /* Csiz */ | ||
488 | |||
489 | #ifdef USE_JPWL | ||
490 | if (j2k->cp->correct) { | ||
491 | /* if JPWL is on, we check whether TX errors have damaged | ||
492 | too much the SIZ parameters */ | ||
493 | if (!(image->x1 * image->y1)) { | ||
494 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
495 | "JPWL: bad image size (%d x %d)\n", | ||
496 | image->x1, image->y1); | ||
497 | if (!JPWL_ASSUME || JPWL_ASSUME) { | ||
498 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
499 | return; | ||
500 | } | ||
501 | } | ||
502 | if (image->numcomps != ((len - 38) / 3)) { | ||
503 | opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, | ||
504 | "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", | ||
505 | image->numcomps, ((len - 38) / 3)); | ||
506 | if (!JPWL_ASSUME) { | ||
507 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
508 | return; | ||
509 | } | ||
510 | /* we try to correct */ | ||
511 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"); | ||
512 | if (image->numcomps < ((len - 38) / 3)) { | ||
513 | len = 38 + 3 * image->numcomps; | ||
514 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", | ||
515 | len); | ||
516 | } else { | ||
517 | image->numcomps = ((len - 38) / 3); | ||
518 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", | ||
519 | image->numcomps); | ||
520 | } | ||
521 | } | ||
522 | |||
523 | /* update components number in the jpwl_exp_comps filed */ | ||
524 | cp->exp_comps = image->numcomps; | ||
525 | } | ||
526 | #endif /* USE_JPWL */ | ||
527 | |||
528 | image->comps = (opj_image_comp_t *) opj_malloc(image->numcomps * sizeof(opj_image_comp_t)); | ||
529 | for (i = 0; i < image->numcomps; i++) { | ||
530 | int tmp, w, h; | ||
531 | tmp = cio_read(cio, 1); /* Ssiz_i */ | ||
532 | image->comps[i].prec = (tmp & 0x7f) + 1; | ||
533 | image->comps[i].sgnd = tmp >> 7; | ||
534 | image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */ | ||
535 | image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */ | ||
536 | |||
537 | #ifdef USE_JPWL | ||
538 | if (j2k->cp->correct) { | ||
539 | /* if JPWL is on, we check whether TX errors have damaged | ||
540 | too much the SIZ parameters, again */ | ||
541 | if (!(image->comps[i].dx * image->comps[i].dy)) { | ||
542 | opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, | ||
543 | "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", | ||
544 | i, i, image->comps[i].dx, image->comps[i].dy); | ||
545 | if (!JPWL_ASSUME) { | ||
546 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
547 | return; | ||
548 | } | ||
549 | /* we try to correct */ | ||
550 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"); | ||
551 | if (!image->comps[i].dx) { | ||
552 | image->comps[i].dx = 1; | ||
553 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", | ||
554 | i, image->comps[i].dx); | ||
555 | } | ||
556 | if (!image->comps[i].dy) { | ||
557 | image->comps[i].dy = 1; | ||
558 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", | ||
559 | i, image->comps[i].dy); | ||
560 | } | ||
561 | } | ||
562 | |||
563 | } | ||
564 | #endif /* USE_JPWL */ | ||
565 | |||
566 | |||
567 | /* TODO: unused ? */ | ||
568 | w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx); | ||
569 | h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy); | ||
570 | |||
571 | image->comps[i].resno_decoded = 0; /* number of resolution decoded */ | ||
572 | image->comps[i].factor = 0; /* reducing factor per component */ | ||
573 | } | ||
574 | |||
575 | cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx); | ||
576 | cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy); | ||
577 | |||
578 | #ifdef USE_JPWL | ||
579 | if (j2k->cp->correct) { | ||
580 | /* if JPWL is on, we check whether TX errors have damaged | ||
581 | too much the SIZ parameters */ | ||
582 | if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) { | ||
583 | opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, | ||
584 | "JPWL: bad number of tiles (%d x %d)\n", | ||
585 | cp->tw, cp->th); | ||
586 | if (!JPWL_ASSUME) { | ||
587 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
588 | return; | ||
589 | } | ||
590 | /* we try to correct */ | ||
591 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"); | ||
592 | if (cp->tw < 1) { | ||
593 | cp->tw= 1; | ||
594 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n", | ||
595 | cp->tw); | ||
596 | } | ||
597 | if (cp->tw > cp->max_tiles) { | ||
598 | cp->tw= 1; | ||
599 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n" | ||
600 | "- setting %d tiles in x => HYPOTHESIS!!!\n", | ||
601 | cp->max_tiles, cp->tw); | ||
602 | } | ||
603 | if (cp->th < 1) { | ||
604 | cp->th= 1; | ||
605 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n", | ||
606 | cp->th); | ||
607 | } | ||
608 | if (cp->th > cp->max_tiles) { | ||
609 | cp->th= 1; | ||
610 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n", | ||
611 | "- setting %d tiles in y => HYPOTHESIS!!!\n", | ||
612 | cp->max_tiles, cp->th); | ||
613 | } | ||
614 | } | ||
615 | } | ||
616 | #endif /* USE_JPWL */ | ||
617 | |||
618 | cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t)); | ||
619 | cp->tileno = (int *) opj_malloc(cp->tw * cp->th * sizeof(int)); | ||
620 | cp->tileno_size = 0; | ||
621 | |||
622 | #ifdef USE_JPWL | ||
623 | if (j2k->cp->correct) { | ||
624 | if (!cp->tcps) { | ||
625 | opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, | ||
626 | "JPWL: could not alloc tcps field of cp\n"); | ||
627 | if (!JPWL_ASSUME || JPWL_ASSUME) { | ||
628 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
629 | return; | ||
630 | } | ||
631 | } | ||
632 | } | ||
633 | #endif /* USE_JPWL */ | ||
634 | |||
635 | for (i = 0; i < cp->tw * cp->th; i++) { | ||
636 | cp->tcps[i].POC = 0; | ||
637 | cp->tcps[i].numpocs = 0; | ||
638 | cp->tcps[i].first = 1; | ||
639 | } | ||
640 | |||
641 | /* Initialization for PPM marker */ | ||
642 | cp->ppm = 0; | ||
643 | cp->ppm_data = NULL; | ||
644 | cp->ppm_data_first = NULL; | ||
645 | cp->ppm_previous = 0; | ||
646 | cp->ppm_store = 0; | ||
647 | |||
648 | j2k->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps); | ||
649 | for (i = 0; i < cp->tw * cp->th; i++) { | ||
650 | cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps); | ||
651 | } | ||
652 | j2k->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * sizeof(unsigned char *)); | ||
653 | j2k->tile_len = (int *) opj_malloc(cp->tw * cp->th * sizeof(int)); | ||
654 | j2k->state = J2K_STATE_MH; | ||
655 | } | ||
656 | |||
657 | static void j2k_write_com(opj_j2k_t *j2k) { | ||
658 | unsigned int i; | ||
659 | int lenp, len; | ||
660 | |||
661 | if(j2k->cp->comment) { | ||
662 | opj_cio_t *cio = j2k->cio; | ||
663 | char *comment = j2k->cp->comment; | ||
664 | |||
665 | cio_write(cio, J2K_MS_COM, 2); | ||
666 | lenp = cio_tell(cio); | ||
667 | cio_skip(cio, 2); | ||
668 | cio_write(cio, 0, 2); | ||
669 | for (i = 0; i < strlen(comment); i++) { | ||
670 | cio_write(cio, comment[i], 1); | ||
671 | } | ||
672 | len = cio_tell(cio) - lenp; | ||
673 | cio_seek(cio, lenp); | ||
674 | cio_write(cio, len, 2); | ||
675 | cio_seek(cio, lenp + len); | ||
676 | } | ||
677 | } | ||
678 | |||
679 | static void j2k_read_com(opj_j2k_t *j2k) { | ||
680 | int len; | ||
681 | |||
682 | opj_cio_t *cio = j2k->cio; | ||
683 | |||
684 | len = cio_read(cio, 2); | ||
685 | cio_skip(cio, len - 2); | ||
686 | } | ||
687 | |||
688 | static void j2k_write_cox(opj_j2k_t *j2k, int compno) { | ||
689 | int i; | ||
690 | |||
691 | opj_cp_t *cp = j2k->cp; | ||
692 | opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; | ||
693 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
694 | opj_cio_t *cio = j2k->cio; | ||
695 | |||
696 | cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */ | ||
697 | cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */ | ||
698 | cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */ | ||
699 | cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */ | ||
700 | cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */ | ||
701 | |||
702 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
703 | for (i = 0; i < tccp->numresolutions; i++) { | ||
704 | cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */ | ||
705 | } | ||
706 | } | ||
707 | } | ||
708 | |||
709 | static void j2k_read_cox(opj_j2k_t *j2k, int compno) { | ||
710 | int i; | ||
711 | |||
712 | opj_cp_t *cp = j2k->cp; | ||
713 | opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; | ||
714 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
715 | opj_cio_t *cio = j2k->cio; | ||
716 | |||
717 | tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */ | ||
718 | |||
719 | /* check the reduce value */ | ||
720 | cp->reduce = int_min((tccp->numresolutions)-1, cp->reduce); | ||
721 | tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */ | ||
722 | tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */ | ||
723 | tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */ | ||
724 | tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */ | ||
725 | if (tccp->csty & J2K_CP_CSTY_PRT) { | ||
726 | for (i = 0; i < tccp->numresolutions; i++) { | ||
727 | int tmp = cio_read(cio, 1); /* SPcox (I_i) */ | ||
728 | tccp->prcw[i] = tmp & 0xf; | ||
729 | tccp->prch[i] = tmp >> 4; | ||
730 | } | ||
731 | } | ||
732 | } | ||
733 | |||
734 | static void j2k_write_cod(opj_j2k_t *j2k) { | ||
735 | opj_cp_t *cp = NULL; | ||
736 | opj_tcp_t *tcp = NULL; | ||
737 | int lenp, len; | ||
738 | |||
739 | opj_cio_t *cio = j2k->cio; | ||
740 | |||
741 | cio_write(cio, J2K_MS_COD, 2); /* COD */ | ||
742 | |||
743 | lenp = cio_tell(cio); | ||
744 | cio_skip(cio, 2); | ||
745 | |||
746 | cp = j2k->cp; | ||
747 | tcp = &cp->tcps[j2k->curtileno]; | ||
748 | |||
749 | cio_write(cio, tcp->csty, 1); /* Scod */ | ||
750 | cio_write(cio, tcp->prg, 1); /* SGcod (A) */ | ||
751 | cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */ | ||
752 | cio_write(cio, tcp->mct, 1); /* SGcod (C) */ | ||
753 | |||
754 | j2k_write_cox(j2k, 0); | ||
755 | len = cio_tell(cio) - lenp; | ||
756 | cio_seek(cio, lenp); | ||
757 | cio_write(cio, len, 2); /* Lcod */ | ||
758 | cio_seek(cio, lenp + len); | ||
759 | } | ||
760 | |||
761 | static void j2k_read_cod(opj_j2k_t *j2k) { | ||
762 | int len, i, pos; | ||
763 | |||
764 | opj_cio_t *cio = j2k->cio; | ||
765 | opj_cp_t *cp = j2k->cp; | ||
766 | opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; | ||
767 | opj_image_t *image = j2k->image; | ||
768 | |||
769 | len = cio_read(cio, 2); /* Lcod */ | ||
770 | tcp->csty = cio_read(cio, 1); /* Scod */ | ||
771 | tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */ | ||
772 | tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */ | ||
773 | tcp->mct = cio_read(cio, 1); /* SGcod (C) */ | ||
774 | |||
775 | pos = cio_tell(cio); | ||
776 | for (i = 0; i < image->numcomps; i++) { | ||
777 | tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT; | ||
778 | cio_seek(cio, pos); | ||
779 | j2k_read_cox(j2k, i); | ||
780 | } | ||
781 | } | ||
782 | |||
783 | static void j2k_write_coc(opj_j2k_t *j2k, int compno) { | ||
784 | int lenp, len; | ||
785 | |||
786 | opj_cp_t *cp = j2k->cp; | ||
787 | opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; | ||
788 | opj_image_t *image = j2k->image; | ||
789 | opj_cio_t *cio = j2k->cio; | ||
790 | |||
791 | cio_write(cio, J2K_MS_COC, 2); /* COC */ | ||
792 | lenp = cio_tell(cio); | ||
793 | cio_skip(cio, 2); | ||
794 | cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */ | ||
795 | cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */ | ||
796 | j2k_write_cox(j2k, compno); | ||
797 | len = cio_tell(cio) - lenp; | ||
798 | cio_seek(cio, lenp); | ||
799 | cio_write(cio, len, 2); /* Lcoc */ | ||
800 | cio_seek(cio, lenp + len); | ||
801 | } | ||
802 | |||
803 | static void j2k_read_coc(opj_j2k_t *j2k) { | ||
804 | int len, compno; | ||
805 | |||
806 | opj_cp_t *cp = j2k->cp; | ||
807 | opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; | ||
808 | opj_image_t *image = j2k->image; | ||
809 | opj_cio_t *cio = j2k->cio; | ||
810 | |||
811 | len = cio_read(cio, 2); /* Lcoc */ | ||
812 | compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */ | ||
813 | tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */ | ||
814 | j2k_read_cox(j2k, compno); | ||
815 | } | ||
816 | |||
817 | static void j2k_write_qcx(opj_j2k_t *j2k, int compno) { | ||
818 | int bandno, numbands; | ||
819 | int expn, mant; | ||
820 | |||
821 | opj_cp_t *cp = j2k->cp; | ||
822 | opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; | ||
823 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
824 | opj_cio_t *cio = j2k->cio; | ||
825 | |||
826 | cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */ | ||
827 | numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; | ||
828 | |||
829 | for (bandno = 0; bandno < numbands; bandno++) { | ||
830 | expn = tccp->stepsizes[bandno].expn; | ||
831 | mant = tccp->stepsizes[bandno].mant; | ||
832 | |||
833 | if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { | ||
834 | cio_write(cio, expn << 3, 1); /* SPqcx_i */ | ||
835 | } else { | ||
836 | cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */ | ||
837 | } | ||
838 | } | ||
839 | } | ||
840 | |||
841 | static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) { | ||
842 | int tmp; | ||
843 | int bandno, numbands; | ||
844 | |||
845 | opj_cp_t *cp = j2k->cp; | ||
846 | opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; | ||
847 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
848 | opj_cio_t *cio = j2k->cio; | ||
849 | |||
850 | tmp = cio_read(cio, 1); /* Sqcx */ | ||
851 | tccp->qntsty = tmp & 0x1f; | ||
852 | tccp->numgbits = tmp >> 5; | ||
853 | numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? | ||
854 | 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2); | ||
855 | |||
856 | #ifdef USE_JPWL | ||
857 | if (j2k->cp->correct) { | ||
858 | |||
859 | /* if JPWL is on, we check whether there are too many subbands */ | ||
860 | if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) { | ||
861 | opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, | ||
862 | "JPWL: bad number of subbands in Sqcx (%d)\n", | ||
863 | numbands); | ||
864 | if (!JPWL_ASSUME) { | ||
865 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
866 | return; | ||
867 | } | ||
868 | /* we try to correct */ | ||
869 | numbands = 1; | ||
870 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n" | ||
871 | "- setting number of bands to %d => HYPOTHESIS!!!\n", | ||
872 | numbands); | ||
873 | }; | ||
874 | |||
875 | }; | ||
876 | #endif /* USE_JPWL */ | ||
877 | |||
878 | for (bandno = 0; bandno < numbands; bandno++) { | ||
879 | int expn, mant; | ||
880 | if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { | ||
881 | expn = cio_read(cio, 1) >> 3; /* SPqcx_i */ | ||
882 | mant = 0; | ||
883 | } else { | ||
884 | tmp = cio_read(cio, 2); /* SPqcx_i */ | ||
885 | expn = tmp >> 11; | ||
886 | mant = tmp & 0x7ff; | ||
887 | } | ||
888 | tccp->stepsizes[bandno].expn = expn; | ||
889 | tccp->stepsizes[bandno].mant = mant; | ||
890 | } | ||
891 | |||
892 | /* Add Antonin : if scalar_derived -> compute other stepsizes */ | ||
893 | if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { | ||
894 | for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) { | ||
895 | tccp->stepsizes[bandno].expn = | ||
896 | ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? | ||
897 | (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0; | ||
898 | tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant; | ||
899 | } | ||
900 | } | ||
901 | /* ddA */ | ||
902 | } | ||
903 | |||
904 | static void j2k_write_qcd(opj_j2k_t *j2k) { | ||
905 | int lenp, len; | ||
906 | |||
907 | opj_cio_t *cio = j2k->cio; | ||
908 | |||
909 | cio_write(cio, J2K_MS_QCD, 2); /* QCD */ | ||
910 | lenp = cio_tell(cio); | ||
911 | cio_skip(cio, 2); | ||
912 | j2k_write_qcx(j2k, 0); | ||
913 | len = cio_tell(cio) - lenp; | ||
914 | cio_seek(cio, lenp); | ||
915 | cio_write(cio, len, 2); /* Lqcd */ | ||
916 | cio_seek(cio, lenp + len); | ||
917 | } | ||
918 | |||
919 | static void j2k_read_qcd(opj_j2k_t *j2k) { | ||
920 | int len, i, pos; | ||
921 | |||
922 | opj_cio_t *cio = j2k->cio; | ||
923 | opj_image_t *image = j2k->image; | ||
924 | |||
925 | len = cio_read(cio, 2); /* Lqcd */ | ||
926 | pos = cio_tell(cio); | ||
927 | for (i = 0; i < image->numcomps; i++) { | ||
928 | cio_seek(cio, pos); | ||
929 | j2k_read_qcx(j2k, i, len - 2); | ||
930 | } | ||
931 | } | ||
932 | |||
933 | static void j2k_write_qcc(opj_j2k_t *j2k, int compno) { | ||
934 | int lenp, len; | ||
935 | |||
936 | opj_cio_t *cio = j2k->cio; | ||
937 | |||
938 | cio_write(cio, J2K_MS_QCC, 2); /* QCC */ | ||
939 | lenp = cio_tell(cio); | ||
940 | cio_skip(cio, 2); | ||
941 | cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */ | ||
942 | j2k_write_qcx(j2k, compno); | ||
943 | len = cio_tell(cio) - lenp; | ||
944 | cio_seek(cio, lenp); | ||
945 | cio_write(cio, len, 2); /* Lqcc */ | ||
946 | cio_seek(cio, lenp + len); | ||
947 | } | ||
948 | |||
949 | static void j2k_read_qcc(opj_j2k_t *j2k) { | ||
950 | int len, compno; | ||
951 | int numcomp = j2k->image->numcomps; | ||
952 | opj_cio_t *cio = j2k->cio; | ||
953 | |||
954 | len = cio_read(cio, 2); /* Lqcc */ | ||
955 | compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */ | ||
956 | |||
957 | #ifdef USE_JPWL | ||
958 | if (j2k->cp->correct) { | ||
959 | |||
960 | static int backup_compno = 0; | ||
961 | |||
962 | /* compno is negative or larger than the number of components!!! */ | ||
963 | if ((compno < 0) || (compno >= numcomp)) { | ||
964 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
965 | "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", | ||
966 | compno, numcomp); | ||
967 | if (!JPWL_ASSUME) { | ||
968 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
969 | return; | ||
970 | } | ||
971 | /* we try to correct */ | ||
972 | compno = backup_compno % numcomp; | ||
973 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" | ||
974 | "- setting component number to %d\n", | ||
975 | compno); | ||
976 | } | ||
977 | |||
978 | /* keep your private count of tiles */ | ||
979 | backup_compno++; | ||
980 | }; | ||
981 | #endif /* USE_JPWL */ | ||
982 | |||
983 | j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2)); | ||
984 | } | ||
985 | |||
986 | static void j2k_write_poc(opj_j2k_t *j2k) { | ||
987 | int len, numpchgs, i; | ||
988 | |||
989 | int numcomps = j2k->image->numcomps; | ||
990 | |||
991 | opj_cp_t *cp = j2k->cp; | ||
992 | opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; | ||
993 | opj_tccp_t *tccp = &tcp->tccps[0]; | ||
994 | opj_cio_t *cio = j2k->cio; | ||
995 | |||
996 | numpchgs = 1 + tcp->numpocs; | ||
997 | cio_write(cio, J2K_MS_POC, 2); /* POC */ | ||
998 | len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs; | ||
999 | cio_write(cio, len, 2); /* Lpoc */ | ||
1000 | for (i = 0; i < numpchgs; i++) { | ||
1001 | opj_poc_t *poc = &tcp->pocs[i]; | ||
1002 | cio_write(cio, poc->resno0, 1); /* RSpoc_i */ | ||
1003 | cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ | ||
1004 | cio_write(cio, poc->layno1, 2); /* LYEpoc_i */ | ||
1005 | poc->layno1 = int_min(poc->layno1, tcp->numlayers); | ||
1006 | cio_write(cio, poc->resno1, 1); /* REpoc_i */ | ||
1007 | poc->resno1 = int_min(poc->resno1, tccp->numresolutions); | ||
1008 | cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ | ||
1009 | poc->compno1 = int_min(poc->compno1, numcomps); | ||
1010 | cio_write(cio, poc->prg, 1); /* Ppoc_i */ | ||
1011 | } | ||
1012 | } | ||
1013 | |||
1014 | static void j2k_read_poc(opj_j2k_t *j2k) { | ||
1015 | int len, numpchgs, i, old_poc; | ||
1016 | |||
1017 | int numcomps = j2k->image->numcomps; | ||
1018 | |||
1019 | opj_cp_t *cp = j2k->cp; | ||
1020 | opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; | ||
1021 | opj_tccp_t *tccp = &tcp->tccps[0]; | ||
1022 | opj_cio_t *cio = j2k->cio; | ||
1023 | |||
1024 | old_poc = tcp->POC ? tcp->numpocs + 1 : 0; | ||
1025 | tcp->POC = 1; | ||
1026 | len = cio_read(cio, 2); /* Lpoc */ | ||
1027 | numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2)); | ||
1028 | |||
1029 | for (i = old_poc; i < numpchgs + old_poc; i++) { | ||
1030 | opj_poc_t *poc; | ||
1031 | poc = &tcp->pocs[i]; | ||
1032 | poc->resno0 = cio_read(cio, 1); /* RSpoc_i */ | ||
1033 | poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */ | ||
1034 | poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */ | ||
1035 | poc->resno1 = cio_read(cio, 1); /* REpoc_i */ | ||
1036 | poc->compno1 = int_min( | ||
1037 | cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */ | ||
1038 | poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */ | ||
1039 | } | ||
1040 | |||
1041 | tcp->numpocs = numpchgs + old_poc - 1; | ||
1042 | } | ||
1043 | |||
1044 | static void j2k_read_crg(opj_j2k_t *j2k) { | ||
1045 | int len, i, Xcrg_i, Ycrg_i; | ||
1046 | |||
1047 | opj_cio_t *cio = j2k->cio; | ||
1048 | int numcomps = j2k->image->numcomps; | ||
1049 | |||
1050 | len = cio_read(cio, 2); /* Lcrg */ | ||
1051 | for (i = 0; i < numcomps; i++) { | ||
1052 | Xcrg_i = cio_read(cio, 2); /* Xcrg_i */ | ||
1053 | Ycrg_i = cio_read(cio, 2); /* Ycrg_i */ | ||
1054 | } | ||
1055 | } | ||
1056 | |||
1057 | static void j2k_read_tlm(opj_j2k_t *j2k) { | ||
1058 | int len, Ztlm, Stlm, ST, SP, tile_tlm, i; | ||
1059 | long int Ttlm_i, Ptlm_i; | ||
1060 | |||
1061 | opj_cio_t *cio = j2k->cio; | ||
1062 | |||
1063 | len = cio_read(cio, 2); /* Ltlm */ | ||
1064 | Ztlm = cio_read(cio, 1); /* Ztlm */ | ||
1065 | Stlm = cio_read(cio, 1); /* Stlm */ | ||
1066 | ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); | ||
1067 | SP = (Stlm >> 6) & 0x01; | ||
1068 | tile_tlm = (len - 4) / ((SP + 1) * 2 + ST); | ||
1069 | for (i = 0; i < tile_tlm; i++) { | ||
1070 | Ttlm_i = cio_read(cio, ST); /* Ttlm_i */ | ||
1071 | Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */ | ||
1072 | } | ||
1073 | } | ||
1074 | |||
1075 | static void j2k_read_plm(opj_j2k_t *j2k) { | ||
1076 | int len, i, Zplm, Nplm, add, packet_len = 0; | ||
1077 | |||
1078 | opj_cio_t *cio = j2k->cio; | ||
1079 | |||
1080 | len = cio_read(cio, 2); /* Lplm */ | ||
1081 | Zplm = cio_read(cio, 1); /* Zplm */ | ||
1082 | len -= 3; | ||
1083 | while (len > 0) { | ||
1084 | Nplm = cio_read(cio, 4); /* Nplm */ | ||
1085 | len -= 4; | ||
1086 | for (i = Nplm; i > 0; i--) { | ||
1087 | add = cio_read(cio, 1); | ||
1088 | len--; | ||
1089 | packet_len = (packet_len << 7) + add; /* Iplm_ij */ | ||
1090 | if ((add & 0x80) == 0) { | ||
1091 | /* New packet */ | ||
1092 | packet_len = 0; | ||
1093 | } | ||
1094 | if (len <= 0) | ||
1095 | break; | ||
1096 | } | ||
1097 | } | ||
1098 | } | ||
1099 | |||
1100 | static void j2k_read_plt(opj_j2k_t *j2k) { | ||
1101 | int len, i, Zplt, packet_len = 0, add; | ||
1102 | |||
1103 | opj_cio_t *cio = j2k->cio; | ||
1104 | |||
1105 | len = cio_read(cio, 2); /* Lplt */ | ||
1106 | Zplt = cio_read(cio, 1); /* Zplt */ | ||
1107 | for (i = len - 3; i > 0; i--) { | ||
1108 | add = cio_read(cio, 1); | ||
1109 | packet_len = (packet_len << 7) + add; /* Iplt_i */ | ||
1110 | if ((add & 0x80) == 0) { | ||
1111 | /* New packet */ | ||
1112 | packet_len = 0; | ||
1113 | } | ||
1114 | } | ||
1115 | } | ||
1116 | |||
1117 | static void j2k_read_ppm(opj_j2k_t *j2k) { | ||
1118 | int len, Z_ppm, i, j; | ||
1119 | int N_ppm; | ||
1120 | |||
1121 | opj_cp_t *cp = j2k->cp; | ||
1122 | opj_cio_t *cio = j2k->cio; | ||
1123 | |||
1124 | len = cio_read(cio, 2); | ||
1125 | cp->ppm = 1; | ||
1126 | |||
1127 | Z_ppm = cio_read(cio, 1); /* Z_ppm */ | ||
1128 | len -= 3; | ||
1129 | while (len > 0) { | ||
1130 | if (cp->ppm_previous == 0) { | ||
1131 | N_ppm = cio_read(cio, 4); /* N_ppm */ | ||
1132 | len -= 4; | ||
1133 | } else { | ||
1134 | N_ppm = cp->ppm_previous; | ||
1135 | } | ||
1136 | j = cp->ppm_store; | ||
1137 | if (Z_ppm == 0) { /* First PPM marker */ | ||
1138 | cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char)); | ||
1139 | cp->ppm_data_first = cp->ppm_data; | ||
1140 | cp->ppm_len = N_ppm; | ||
1141 | } else { /* NON-first PPM marker */ | ||
1142 | cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char)); | ||
1143 | |||
1144 | #ifdef USE_JPWL | ||
1145 | /* this memory allocation check could be done even in non-JPWL cases */ | ||
1146 | if (cp->correct) { | ||
1147 | if (!cp->ppm_data) { | ||
1148 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1149 | "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n", | ||
1150 | cio_tell(cio)); | ||
1151 | if (!JPWL_ASSUME || JPWL_ASSUME) { | ||
1152 | free(cp->ppm_data); | ||
1153 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
1154 | return; | ||
1155 | } | ||
1156 | } | ||
1157 | } | ||
1158 | #endif | ||
1159 | |||
1160 | cp->ppm_data_first = cp->ppm_data; | ||
1161 | cp->ppm_len = N_ppm + cp->ppm_store; | ||
1162 | } | ||
1163 | for (i = N_ppm; i > 0; i--) { /* Read packet header */ | ||
1164 | cp->ppm_data[j] = cio_read(cio, 1); | ||
1165 | j++; | ||
1166 | len--; | ||
1167 | if (len == 0) | ||
1168 | break; /* Case of non-finished packet header in present marker but finished in next one */ | ||
1169 | } | ||
1170 | cp->ppm_previous = i - 1; | ||
1171 | cp->ppm_store = j; | ||
1172 | } | ||
1173 | } | ||
1174 | |||
1175 | static void j2k_read_ppt(opj_j2k_t *j2k) { | ||
1176 | int len, Z_ppt, i, j = 0; | ||
1177 | |||
1178 | opj_cp_t *cp = j2k->cp; | ||
1179 | opj_tcp_t *tcp = cp->tcps + j2k->curtileno; | ||
1180 | opj_cio_t *cio = j2k->cio; | ||
1181 | |||
1182 | len = cio_read(cio, 2); | ||
1183 | Z_ppt = cio_read(cio, 1); | ||
1184 | tcp->ppt = 1; | ||
1185 | if (Z_ppt == 0) { /* First PPT marker */ | ||
1186 | tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char)); | ||
1187 | tcp->ppt_data_first = tcp->ppt_data; | ||
1188 | tcp->ppt_store = 0; | ||
1189 | tcp->ppt_len = len - 3; | ||
1190 | } else { /* NON-first PPT marker */ | ||
1191 | tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char)); | ||
1192 | tcp->ppt_data_first = tcp->ppt_data; | ||
1193 | tcp->ppt_len = len - 3 + tcp->ppt_store; | ||
1194 | } | ||
1195 | j = tcp->ppt_store; | ||
1196 | for (i = len - 3; i > 0; i--) { | ||
1197 | tcp->ppt_data[j] = cio_read(cio, 1); | ||
1198 | j++; | ||
1199 | } | ||
1200 | tcp->ppt_store = j; | ||
1201 | } | ||
1202 | |||
1203 | static void j2k_write_tlm(opj_j2k_t *j2k){ | ||
1204 | int lenp; | ||
1205 | opj_cio_t *cio = j2k->cio; | ||
1206 | j2k->tlm_start = cio_tell(cio); | ||
1207 | cio_write(cio, J2K_MS_TLM, 2);/* TLM */ | ||
1208 | lenp = 4 + (5*j2k->totnum_tp); | ||
1209 | cio_write(cio,lenp,2); /* Ltlm */ | ||
1210 | cio_write(cio, 0,1); /* Ztlm=0*/ | ||
1211 | cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ | ||
1212 | cio_skip(cio,5*j2k->totnum_tp); | ||
1213 | } | ||
1214 | |||
1215 | static void j2k_write_sot(opj_j2k_t *j2k) { | ||
1216 | int lenp, len; | ||
1217 | |||
1218 | opj_cio_t *cio = j2k->cio; | ||
1219 | |||
1220 | j2k->sot_start = cio_tell(cio); | ||
1221 | cio_write(cio, J2K_MS_SOT, 2); /* SOT */ | ||
1222 | lenp = cio_tell(cio); | ||
1223 | cio_skip(cio, 2); /* Lsot (further) */ | ||
1224 | cio_write(cio, j2k->curtileno, 2); /* Isot */ | ||
1225 | cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */ | ||
1226 | cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */ | ||
1227 | cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */ | ||
1228 | len = cio_tell(cio) - lenp; | ||
1229 | cio_seek(cio, lenp); | ||
1230 | cio_write(cio, len, 2); /* Lsot */ | ||
1231 | cio_seek(cio, lenp + len); | ||
1232 | } | ||
1233 | |||
1234 | static void j2k_read_sot(opj_j2k_t *j2k) { | ||
1235 | int len, tileno, totlen, partno, numparts, i; | ||
1236 | opj_tcp_t *tcp = NULL; | ||
1237 | char status = 0; | ||
1238 | |||
1239 | opj_cp_t *cp = j2k->cp; | ||
1240 | opj_cio_t *cio = j2k->cio; | ||
1241 | |||
1242 | len = cio_read(cio, 2); | ||
1243 | tileno = cio_read(cio, 2); | ||
1244 | |||
1245 | #ifdef USE_JPWL | ||
1246 | if (j2k->cp->correct) { | ||
1247 | |||
1248 | static int backup_tileno = 0; | ||
1249 | |||
1250 | /* tileno is negative or larger than the number of tiles!!! */ | ||
1251 | if ((tileno < 0) || (tileno > (cp->tw * cp->th))) { | ||
1252 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1253 | "JPWL: bad tile number (%d out of a maximum of %d)\n", | ||
1254 | tileno, (cp->tw * cp->th)); | ||
1255 | if (!JPWL_ASSUME) { | ||
1256 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
1257 | return; | ||
1258 | } | ||
1259 | /* we try to correct */ | ||
1260 | tileno = backup_tileno; | ||
1261 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" | ||
1262 | "- setting tile number to %d\n", | ||
1263 | tileno); | ||
1264 | } | ||
1265 | |||
1266 | /* keep your private count of tiles */ | ||
1267 | backup_tileno++; | ||
1268 | }; | ||
1269 | #endif /* USE_JPWL */ | ||
1270 | |||
1271 | |||
1272 | if (cp->tileno_size == 0) { | ||
1273 | cp->tileno[cp->tileno_size] = tileno; | ||
1274 | cp->tileno_size++; | ||
1275 | } else { | ||
1276 | i = 0; | ||
1277 | while (i < cp->tileno_size && status == 0) { | ||
1278 | status = cp->tileno[i] == tileno ? 1 : 0; | ||
1279 | i++; | ||
1280 | } | ||
1281 | if (status == 0) { | ||
1282 | cp->tileno[cp->tileno_size] = tileno; | ||
1283 | cp->tileno_size++; | ||
1284 | } | ||
1285 | } | ||
1286 | |||
1287 | totlen = cio_read(cio, 4); | ||
1288 | |||
1289 | #ifdef USE_JPWL | ||
1290 | if (j2k->cp->correct) { | ||
1291 | |||
1292 | /* totlen is negative or larger than the bytes left!!! */ | ||
1293 | if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) { | ||
1294 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1295 | "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", | ||
1296 | totlen, cio_numbytesleft(cio) + 8); | ||
1297 | if (!JPWL_ASSUME) { | ||
1298 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
1299 | return; | ||
1300 | } | ||
1301 | /* we try to correct */ | ||
1302 | totlen = 0; | ||
1303 | opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" | ||
1304 | "- setting Psot to %d => assuming it is the last tile\n", | ||
1305 | totlen); | ||
1306 | } | ||
1307 | |||
1308 | }; | ||
1309 | #endif /* USE_JPWL */ | ||
1310 | |||
1311 | if (!totlen) | ||
1312 | totlen = cio_numbytesleft(cio) + 8; | ||
1313 | |||
1314 | partno = cio_read(cio, 1); | ||
1315 | numparts = cio_read(cio, 1); | ||
1316 | |||
1317 | j2k->curtileno = tileno; | ||
1318 | j2k->eot = cio_getbp(cio) - 12 + totlen; | ||
1319 | j2k->state = J2K_STATE_TPH; | ||
1320 | tcp = &cp->tcps[j2k->curtileno]; | ||
1321 | |||
1322 | if (tcp->first == 1) { | ||
1323 | |||
1324 | /* Initialization PPT */ | ||
1325 | opj_tccp_t *tmp = tcp->tccps; | ||
1326 | memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t)); | ||
1327 | tcp->ppt = 0; | ||
1328 | tcp->ppt_data = NULL; | ||
1329 | tcp->ppt_data_first = NULL; | ||
1330 | tcp->tccps = tmp; | ||
1331 | |||
1332 | for (i = 0; i < j2k->image->numcomps; i++) { | ||
1333 | tcp->tccps[i] = j2k->default_tcp->tccps[i]; | ||
1334 | } | ||
1335 | cp->tcps[j2k->curtileno].first = 0; | ||
1336 | } | ||
1337 | } | ||
1338 | |||
1339 | static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) { | ||
1340 | int l, layno; | ||
1341 | int totlen; | ||
1342 | opj_tcp_t *tcp = NULL; | ||
1343 | opj_image_info_t *image_info = NULL; | ||
1344 | |||
1345 | opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */ | ||
1346 | opj_cp_t *cp = j2k->cp; | ||
1347 | opj_cio_t *cio = j2k->cio; | ||
1348 | |||
1349 | tcd->tp_num = j2k->tp_num ; | ||
1350 | tcd->cur_tp_num = j2k->cur_tp_num; | ||
1351 | tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno]; | ||
1352 | |||
1353 | cio_write(cio, J2K_MS_SOD, 2); | ||
1354 | if (j2k->curtileno == 0) { | ||
1355 | j2k->sod_start = cio_tell(cio) + j2k->pos_correction; | ||
1356 | } | ||
1357 | |||
1358 | /* INDEX >> */ | ||
1359 | image_info = j2k->image_info; | ||
1360 | if (image_info && image_info->index_on) { | ||
1361 | image_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1; | ||
1362 | } | ||
1363 | /* << INDEX */ | ||
1364 | |||
1365 | tcp = &cp->tcps[j2k->curtileno]; | ||
1366 | for (layno = 0; layno < tcp->numlayers; layno++) { | ||
1367 | tcp->rates[layno] -= tcp->rates[layno] ? (j2k->sod_start / (cp->th * cp->tw)) : 0; | ||
1368 | } | ||
1369 | if(image_info) { | ||
1370 | image_info->num = 0; | ||
1371 | } | ||
1372 | |||
1373 | l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, image_info); | ||
1374 | |||
1375 | /* Writing Psot in SOT marker */ | ||
1376 | totlen = cio_tell(cio) + l - j2k->sot_start; | ||
1377 | cio_seek(cio, j2k->sot_start + 6); | ||
1378 | cio_write(cio, totlen, 4); | ||
1379 | cio_seek(cio, j2k->sot_start + totlen); | ||
1380 | /* Writing Ttlm and Ptlm in TLM marker */ | ||
1381 | if(cp->cinema){ | ||
1382 | cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num)); | ||
1383 | cio_write(cio, j2k->curtileno, 1); | ||
1384 | cio_write(cio, totlen, 4); | ||
1385 | } | ||
1386 | cio_seek(cio, j2k->sot_start + totlen); | ||
1387 | } | ||
1388 | |||
1389 | static void j2k_read_sod(opj_j2k_t *j2k) { | ||
1390 | int len, truncate = 0, i; | ||
1391 | unsigned char *data = NULL, *data_ptr = NULL; | ||
1392 | |||
1393 | opj_cio_t *cio = j2k->cio; | ||
1394 | int curtileno = j2k->curtileno; | ||
1395 | |||
1396 | len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1); | ||
1397 | |||
1398 | if (len == cio_numbytesleft(cio) + 1) { | ||
1399 | truncate = 1; /* Case of a truncate codestream */ | ||
1400 | } | ||
1401 | |||
1402 | data = (unsigned char *) opj_malloc((j2k->tile_len[curtileno] + len) * sizeof(unsigned char)); | ||
1403 | |||
1404 | for (i = 0; i < j2k->tile_len[curtileno]; i++) { | ||
1405 | data[i] = j2k->tile_data[curtileno][i]; | ||
1406 | } | ||
1407 | |||
1408 | data_ptr = data + j2k->tile_len[curtileno]; | ||
1409 | for (i = 0; i < len; i++) { | ||
1410 | data_ptr[i] = cio_read(cio, 1); | ||
1411 | } | ||
1412 | |||
1413 | j2k->tile_len[curtileno] += len; | ||
1414 | opj_free(j2k->tile_data[curtileno]); | ||
1415 | j2k->tile_data[curtileno] = data; | ||
1416 | |||
1417 | if (!truncate) { | ||
1418 | j2k->state = J2K_STATE_TPHSOT; | ||
1419 | } else { | ||
1420 | j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */ | ||
1421 | } | ||
1422 | } | ||
1423 | |||
1424 | static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) { | ||
1425 | |||
1426 | opj_cp_t *cp = j2k->cp; | ||
1427 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
1428 | opj_cio_t *cio = j2k->cio; | ||
1429 | int numcomps = j2k->image->numcomps; | ||
1430 | |||
1431 | cio_write(cio, J2K_MS_RGN, 2); /* RGN */ | ||
1432 | cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */ | ||
1433 | cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */ | ||
1434 | cio_write(cio, 0, 1); /* Srgn */ | ||
1435 | cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */ | ||
1436 | } | ||
1437 | |||
1438 | static void j2k_read_rgn(opj_j2k_t *j2k) { | ||
1439 | int len, compno, roisty; | ||
1440 | |||
1441 | opj_cp_t *cp = j2k->cp; | ||
1442 | opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; | ||
1443 | opj_cio_t *cio = j2k->cio; | ||
1444 | int numcomps = j2k->image->numcomps; | ||
1445 | |||
1446 | len = cio_read(cio, 2); /* Lrgn */ | ||
1447 | compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */ | ||
1448 | roisty = cio_read(cio, 1); /* Srgn */ | ||
1449 | |||
1450 | #ifdef USE_JPWL | ||
1451 | if (j2k->cp->correct) { | ||
1452 | /* totlen is negative or larger than the bytes left!!! */ | ||
1453 | if (compno >= numcomps) { | ||
1454 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1455 | "JPWL: bad component number in RGN (%d when there are only %d)\n", | ||
1456 | compno, numcomps); | ||
1457 | if (!JPWL_ASSUME || JPWL_ASSUME) { | ||
1458 | opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
1459 | return; | ||
1460 | } | ||
1461 | } | ||
1462 | }; | ||
1463 | #endif /* USE_JPWL */ | ||
1464 | |||
1465 | tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */ | ||
1466 | } | ||
1467 | |||
1468 | static void j2k_write_eoc(opj_j2k_t *j2k) { | ||
1469 | opj_cio_t *cio = j2k->cio; | ||
1470 | /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */ | ||
1471 | cio_write(cio, J2K_MS_EOC, 2); | ||
1472 | } | ||
1473 | |||
1474 | static void j2k_read_eoc(opj_j2k_t *j2k) { | ||
1475 | int i, tileno; | ||
1476 | |||
1477 | /* if packets should be decoded */ | ||
1478 | if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) { | ||
1479 | opj_tcd_t *tcd = tcd_create(j2k->cinfo); | ||
1480 | tcd_malloc_decode(tcd, j2k->image, j2k->cp); | ||
1481 | for (i = 0; i < j2k->cp->tileno_size; i++) { | ||
1482 | tileno = j2k->cp->tileno[i]; | ||
1483 | tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno); | ||
1484 | opj_free(j2k->tile_data[tileno]); | ||
1485 | j2k->tile_data[tileno] = NULL; | ||
1486 | } | ||
1487 | tcd_free_decode(tcd); | ||
1488 | tcd_destroy(tcd); | ||
1489 | } | ||
1490 | /* if packets should not be decoded */ | ||
1491 | else { | ||
1492 | for (i = 0; i < j2k->cp->tileno_size; i++) { | ||
1493 | tileno = j2k->cp->tileno[i]; | ||
1494 | opj_free(j2k->tile_data[tileno]); | ||
1495 | j2k->tile_data[tileno] = NULL; | ||
1496 | } | ||
1497 | } | ||
1498 | |||
1499 | j2k->state = J2K_STATE_MT; | ||
1500 | } | ||
1501 | |||
1502 | typedef struct opj_dec_mstabent { | ||
1503 | /** marker value */ | ||
1504 | int id; | ||
1505 | /** value of the state when the marker can appear */ | ||
1506 | int states; | ||
1507 | /** action linked to the marker */ | ||
1508 | void (*handler) (opj_j2k_t *j2k); | ||
1509 | } opj_dec_mstabent_t; | ||
1510 | |||
1511 | opj_dec_mstabent_t j2k_dec_mstab[] = { | ||
1512 | {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc}, | ||
1513 | {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot}, | ||
1514 | {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod}, | ||
1515 | {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc}, | ||
1516 | {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz}, | ||
1517 | {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod}, | ||
1518 | {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc}, | ||
1519 | {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn}, | ||
1520 | {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd}, | ||
1521 | {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc}, | ||
1522 | {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc}, | ||
1523 | {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm}, | ||
1524 | {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm}, | ||
1525 | {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt}, | ||
1526 | {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm}, | ||
1527 | {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt}, | ||
1528 | {J2K_MS_SOP, 0, 0}, | ||
1529 | {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg}, | ||
1530 | {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com}, | ||
1531 | |||
1532 | #ifdef USE_JPWL | ||
1533 | {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, | ||
1534 | {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, | ||
1535 | {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, | ||
1536 | {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, | ||
1537 | #endif /* USE_JPWL */ | ||
1538 | |||
1539 | {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk} | ||
1540 | }; | ||
1541 | |||
1542 | static void j2k_read_unk(opj_j2k_t *j2k) { | ||
1543 | opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n"); | ||
1544 | |||
1545 | #ifdef USE_JPWL | ||
1546 | if (j2k->cp->correct) { | ||
1547 | int m = 0, id, i; | ||
1548 | int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id; | ||
1549 | cio_seek(j2k->cio, cio_tell(j2k->cio) - 2); | ||
1550 | id = cio_read(j2k->cio, 2); | ||
1551 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1552 | "JPWL: really don't know this marker %x\n", | ||
1553 | id); | ||
1554 | if (!JPWL_ASSUME) { | ||
1555 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1556 | "- possible synch loss due to uncorrectable codestream errors => giving up\n"); | ||
1557 | return; | ||
1558 | } | ||
1559 | /* OK, activate this at your own risk!!! */ | ||
1560 | /* we look for the marker at the minimum hamming distance from this */ | ||
1561 | while (j2k_dec_mstab[m].id) { | ||
1562 | |||
1563 | /* 1's where they differ */ | ||
1564 | tmp_id = j2k_dec_mstab[m].id ^ id; | ||
1565 | |||
1566 | /* compute the hamming distance between our id and the current */ | ||
1567 | cur_dist = 0; | ||
1568 | for (i = 0; i < 16; i++) { | ||
1569 | if ((tmp_id >> i) & 0x0001) { | ||
1570 | cur_dist++; | ||
1571 | } | ||
1572 | } | ||
1573 | |||
1574 | /* if current distance is smaller, set the minimum */ | ||
1575 | if (cur_dist < min_dist) { | ||
1576 | min_dist = cur_dist; | ||
1577 | min_id = j2k_dec_mstab[m].id; | ||
1578 | } | ||
1579 | |||
1580 | /* jump to the next marker */ | ||
1581 | m++; | ||
1582 | } | ||
1583 | |||
1584 | /* do we substitute the marker? */ | ||
1585 | if (min_dist < JPWL_MAXIMUM_HAMMING) { | ||
1586 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1587 | "- marker %x is at distance %d from the read %x\n", | ||
1588 | min_id, min_dist, id); | ||
1589 | opj_event_msg(j2k->cinfo, EVT_ERROR, | ||
1590 | "- trying to substitute in place and crossing fingers!\n"); | ||
1591 | cio_seek(j2k->cio, cio_tell(j2k->cio) - 2); | ||
1592 | cio_write(j2k->cio, min_id, 2); | ||
1593 | |||
1594 | /* rewind */ | ||
1595 | cio_seek(j2k->cio, cio_tell(j2k->cio) - 2); | ||
1596 | |||
1597 | } | ||
1598 | |||
1599 | }; | ||
1600 | #endif /* USE_JPWL */ | ||
1601 | |||
1602 | } | ||
1603 | |||
1604 | /** | ||
1605 | Read the lookup table containing all the marker, status and action | ||
1606 | @param id Marker value | ||
1607 | */ | ||
1608 | static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) { | ||
1609 | opj_dec_mstabent_t *e; | ||
1610 | for (e = j2k_dec_mstab; e->id != 0; e++) { | ||
1611 | if (e->id == id) { | ||
1612 | break; | ||
1613 | } | ||
1614 | } | ||
1615 | return e; | ||
1616 | } | ||
1617 | |||
1618 | /* ----------------------------------------------------------------------- */ | ||
1619 | /* J2K / JPT decoder interface */ | ||
1620 | /* ----------------------------------------------------------------------- */ | ||
1621 | |||
1622 | opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) { | ||
1623 | opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t)); | ||
1624 | if(j2k) { | ||
1625 | j2k->cinfo = cinfo; | ||
1626 | j2k->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t)); | ||
1627 | if(!j2k->default_tcp) { | ||
1628 | opj_free(j2k); | ||
1629 | return NULL; | ||
1630 | } | ||
1631 | } | ||
1632 | return j2k; | ||
1633 | } | ||
1634 | |||
1635 | void j2k_destroy_decompress(opj_j2k_t *j2k) { | ||
1636 | int i = 0; | ||
1637 | |||
1638 | if(j2k->tile_len != NULL) { | ||
1639 | opj_free(j2k->tile_len); | ||
1640 | } | ||
1641 | if(j2k->tile_data != NULL) { | ||
1642 | opj_free(j2k->tile_data); | ||
1643 | } | ||
1644 | if(j2k->default_tcp != NULL) { | ||
1645 | opj_tcp_t *default_tcp = j2k->default_tcp; | ||
1646 | if(default_tcp->ppt_data_first != NULL) { | ||
1647 | opj_free(default_tcp->ppt_data_first); | ||
1648 | } | ||
1649 | if(j2k->default_tcp->tccps != NULL) { | ||
1650 | opj_free(j2k->default_tcp->tccps); | ||
1651 | } | ||
1652 | opj_free(j2k->default_tcp); | ||
1653 | } | ||
1654 | if(j2k->cp != NULL) { | ||
1655 | opj_cp_t *cp = j2k->cp; | ||
1656 | if(cp->tcps != NULL) { | ||
1657 | for(i = 0; i < cp->tw * cp->th; i++) { | ||
1658 | if(cp->tcps[i].ppt_data_first != NULL) { | ||
1659 | opj_free(cp->tcps[i].ppt_data_first); | ||
1660 | } | ||
1661 | if(cp->tcps[i].tccps != NULL) { | ||
1662 | opj_free(cp->tcps[i].tccps); | ||
1663 | } | ||
1664 | } | ||
1665 | opj_free(cp->tcps); | ||
1666 | } | ||
1667 | if(cp->ppm_data_first != NULL) { | ||
1668 | opj_free(cp->ppm_data_first); | ||
1669 | } | ||
1670 | if(cp->tileno != NULL) { | ||
1671 | opj_free(cp->tileno); | ||
1672 | } | ||
1673 | if(cp->comment != NULL) { | ||
1674 | opj_free(cp->comment); | ||
1675 | } | ||
1676 | |||
1677 | opj_free(cp); | ||
1678 | } | ||
1679 | |||
1680 | opj_free(j2k); | ||
1681 | } | ||
1682 | |||
1683 | void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) { | ||
1684 | if(j2k && parameters) { | ||
1685 | /* create and initialize the coding parameters structure */ | ||
1686 | opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t)); | ||
1687 | cp->reduce = parameters->cp_reduce; | ||
1688 | cp->layer = parameters->cp_layer; | ||
1689 | cp->limit_decoding = parameters->cp_limit_decoding; | ||
1690 | |||
1691 | #ifdef USE_JPWL | ||
1692 | cp->correct = parameters->jpwl_correct; | ||
1693 | cp->exp_comps = parameters->jpwl_exp_comps; | ||
1694 | cp->max_tiles = parameters->jpwl_max_tiles; | ||
1695 | #endif /* USE_JPWL */ | ||
1696 | |||
1697 | |||
1698 | /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */ | ||
1699 | j2k->cp = cp; | ||
1700 | } | ||
1701 | } | ||
1702 | |||
1703 | opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio) { | ||
1704 | opj_image_t *image = NULL; | ||
1705 | |||
1706 | opj_common_ptr cinfo = j2k->cinfo; | ||
1707 | |||
1708 | j2k->cio = cio; | ||
1709 | |||
1710 | /* create an empty image */ | ||
1711 | image = opj_image_create0(); | ||
1712 | j2k->image = image; | ||
1713 | |||
1714 | j2k->state = J2K_STATE_MHSOC; | ||
1715 | |||
1716 | for (;;) { | ||
1717 | opj_dec_mstabent_t *e; | ||
1718 | int id = cio_read(cio, 2); | ||
1719 | |||
1720 | |||
1721 | #ifdef USE_JPWL | ||
1722 | /* we try to honor JPWL correction power */ | ||
1723 | if (j2k->cp->correct) { | ||
1724 | |||
1725 | int orig_pos = cio_tell(cio); | ||
1726 | bool status; | ||
1727 | |||
1728 | /* call the corrector */ | ||
1729 | status = jpwl_correct(j2k); | ||
1730 | |||
1731 | /* go back to where you were */ | ||
1732 | cio_seek(cio, orig_pos - 2); | ||
1733 | |||
1734 | /* re-read the marker */ | ||
1735 | id = cio_read(cio, 2); | ||
1736 | |||
1737 | /* check whether it begins with ff */ | ||
1738 | if (id >> 8 != 0xff) { | ||
1739 | opj_event_msg(cinfo, EVT_ERROR, | ||
1740 | "JPWL: possible bad marker %x at %d\n", | ||
1741 | id, cio_tell(cio) - 2); | ||
1742 | if (!JPWL_ASSUME) { | ||
1743 | opj_image_destroy(image); | ||
1744 | opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
1745 | return 0; | ||
1746 | } | ||
1747 | /* we try to correct */ | ||
1748 | id = id | 0xff00; | ||
1749 | cio_seek(cio, cio_tell(cio) - 2); | ||
1750 | cio_write(cio, id, 2); | ||
1751 | opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n" | ||
1752 | "- setting marker to %x\n", | ||
1753 | id); | ||
1754 | } | ||
1755 | |||
1756 | } | ||
1757 | #endif /* USE_JPWL */ | ||
1758 | |||
1759 | if (id >> 8 != 0xff) { | ||
1760 | opj_image_destroy(image); | ||
1761 | opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id); | ||
1762 | return 0; | ||
1763 | } | ||
1764 | e = j2k_dec_mstab_lookup(id); | ||
1765 | // Check if the marker is known | ||
1766 | if (!(j2k->state & e->states)) { | ||
1767 | opj_image_destroy(image); | ||
1768 | opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id); | ||
1769 | return 0; | ||
1770 | } | ||
1771 | // Check if the decoding is limited to the main header | ||
1772 | if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) { | ||
1773 | opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n"); | ||
1774 | return image; | ||
1775 | } | ||
1776 | |||
1777 | if (e->handler) { | ||
1778 | (*e->handler)(j2k); | ||
1779 | } | ||
1780 | if (j2k->state == J2K_STATE_MT) { | ||
1781 | break; | ||
1782 | } | ||
1783 | if (j2k->state == J2K_STATE_NEOC) { | ||
1784 | break; | ||
1785 | } | ||
1786 | } | ||
1787 | if (j2k->state == J2K_STATE_NEOC) { | ||
1788 | j2k_read_eoc(j2k); | ||
1789 | } | ||
1790 | |||
1791 | if (j2k->state != J2K_STATE_MT) { | ||
1792 | opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n"); | ||
1793 | } | ||
1794 | |||
1795 | return image; | ||
1796 | } | ||
1797 | |||
1798 | /* | ||
1799 | * Read a JPT-stream and decode file | ||
1800 | * | ||
1801 | */ | ||
1802 | opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio) { | ||
1803 | opj_image_t *image = NULL; | ||
1804 | opj_jpt_msg_header_t header; | ||
1805 | int position; | ||
1806 | |||
1807 | opj_common_ptr cinfo = j2k->cinfo; | ||
1808 | |||
1809 | j2k->cio = cio; | ||
1810 | |||
1811 | /* create an empty image */ | ||
1812 | image = opj_image_create0(); | ||
1813 | |||
1814 | j2k->state = J2K_STATE_MHSOC; | ||
1815 | |||
1816 | /* Initialize the header */ | ||
1817 | jpt_init_msg_header(&header); | ||
1818 | /* Read the first header of the message */ | ||
1819 | jpt_read_msg_header(cinfo, cio, &header); | ||
1820 | |||
1821 | position = cio_tell(cio); | ||
1822 | if (header.Class_Id != 6) { /* 6 : Main header data-bin message */ | ||
1823 | opj_image_destroy(image); | ||
1824 | opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id); | ||
1825 | return 0; | ||
1826 | } | ||
1827 | |||
1828 | for (;;) { | ||
1829 | opj_dec_mstabent_t *e = NULL; | ||
1830 | int id; | ||
1831 | |||
1832 | if (!cio_numbytesleft(cio)) { | ||
1833 | j2k_read_eoc(j2k); | ||
1834 | return image; | ||
1835 | } | ||
1836 | /* data-bin read -> need to read a new header */ | ||
1837 | if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) { | ||
1838 | jpt_read_msg_header(cinfo, cio, &header); | ||
1839 | position = cio_tell(cio); | ||
1840 | if (header.Class_Id != 4) { /* 4 : Tile data-bin message */ | ||
1841 | opj_image_destroy(image); | ||
1842 | opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n"); | ||
1843 | return 0; | ||
1844 | } | ||
1845 | } | ||
1846 | |||
1847 | id = cio_read(cio, 2); | ||
1848 | if (id >> 8 != 0xff) { | ||
1849 | opj_image_destroy(image); | ||
1850 | opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id); | ||
1851 | return 0; | ||
1852 | } | ||
1853 | e = j2k_dec_mstab_lookup(id); | ||
1854 | if (!(j2k->state & e->states)) { | ||
1855 | opj_image_destroy(image); | ||
1856 | opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id); | ||
1857 | return 0; | ||
1858 | } | ||
1859 | if (e->handler) { | ||
1860 | (*e->handler)(j2k); | ||
1861 | } | ||
1862 | if (j2k->state == J2K_STATE_MT) { | ||
1863 | break; | ||
1864 | } | ||
1865 | if (j2k->state == J2K_STATE_NEOC) { | ||
1866 | break; | ||
1867 | } | ||
1868 | } | ||
1869 | if (j2k->state == J2K_STATE_NEOC) { | ||
1870 | j2k_read_eoc(j2k); | ||
1871 | } | ||
1872 | |||
1873 | if (j2k->state != J2K_STATE_MT) { | ||
1874 | opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n"); | ||
1875 | } | ||
1876 | |||
1877 | return image; | ||
1878 | } | ||
1879 | |||
1880 | /* ----------------------------------------------------------------------- */ | ||
1881 | /* J2K encoder interface */ | ||
1882 | /* ----------------------------------------------------------------------- */ | ||
1883 | |||
1884 | opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) { | ||
1885 | opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t)); | ||
1886 | if(j2k) { | ||
1887 | j2k->cinfo = cinfo; | ||
1888 | } | ||
1889 | return j2k; | ||
1890 | } | ||
1891 | |||
1892 | void j2k_destroy_compress(opj_j2k_t *j2k) { | ||
1893 | int tileno; | ||
1894 | |||
1895 | if(!j2k) return; | ||
1896 | |||
1897 | if(j2k->image_info != NULL) { | ||
1898 | opj_image_info_t *image_info = j2k->image_info; | ||
1899 | if (image_info->index_on && j2k->cp) { | ||
1900 | opj_cp_t *cp = j2k->cp; | ||
1901 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | ||
1902 | opj_tile_info_t *tile_info = &image_info->tile[tileno]; | ||
1903 | opj_free(tile_info->thresh); | ||
1904 | opj_free(tile_info->packet); | ||
1905 | } | ||
1906 | opj_free(image_info->tile); | ||
1907 | } | ||
1908 | opj_free(image_info); | ||
1909 | } | ||
1910 | if(j2k->cp != NULL) { | ||
1911 | opj_cp_t *cp = j2k->cp; | ||
1912 | |||
1913 | if(cp->comment) { | ||
1914 | opj_free(cp->comment); | ||
1915 | } | ||
1916 | if(cp->matrice) { | ||
1917 | opj_free(cp->matrice); | ||
1918 | } | ||
1919 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | ||
1920 | opj_free(cp->tcps[tileno].tccps); | ||
1921 | } | ||
1922 | opj_free(cp->tcps); | ||
1923 | opj_free(cp); | ||
1924 | } | ||
1925 | |||
1926 | opj_free(j2k); | ||
1927 | } | ||
1928 | |||
1929 | void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) { | ||
1930 | int i, j, tileno, numpocs_tile; | ||
1931 | opj_cp_t *cp = NULL; | ||
1932 | |||
1933 | if(!j2k || !parameters || ! image) { | ||
1934 | return; | ||
1935 | } | ||
1936 | |||
1937 | /* create and initialize the coding parameters structure */ | ||
1938 | cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t)); | ||
1939 | |||
1940 | /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ | ||
1941 | j2k->cp = cp; | ||
1942 | |||
1943 | /* set default values for cp */ | ||
1944 | cp->tw = 1; | ||
1945 | cp->th = 1; | ||
1946 | |||
1947 | /* | ||
1948 | copy user encoding parameters | ||
1949 | */ | ||
1950 | cp->cinema = parameters->cp_cinema; | ||
1951 | cp->max_comp_size = parameters->max_comp_size; | ||
1952 | cp->rsiz = parameters->cp_rsiz; | ||
1953 | cp->disto_alloc = parameters->cp_disto_alloc; | ||
1954 | cp->fixed_alloc = parameters->cp_fixed_alloc; | ||
1955 | cp->fixed_quality = parameters->cp_fixed_quality; | ||
1956 | |||
1957 | /* mod fixed_quality */ | ||
1958 | if(parameters->cp_matrice) { | ||
1959 | size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int); | ||
1960 | cp->matrice = (int *) opj_malloc(array_size); | ||
1961 | memcpy(cp->matrice, parameters->cp_matrice, array_size); | ||
1962 | } | ||
1963 | |||
1964 | /* creation of an index file ? */ | ||
1965 | cp->index_on = parameters->index_on; | ||
1966 | if(cp->index_on) { | ||
1967 | j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t)); | ||
1968 | } | ||
1969 | |||
1970 | /* tiles */ | ||
1971 | cp->tdx = parameters->cp_tdx; | ||
1972 | cp->tdy = parameters->cp_tdy; | ||
1973 | |||
1974 | /* tile offset */ | ||
1975 | cp->tx0 = parameters->cp_tx0; | ||
1976 | cp->ty0 = parameters->cp_ty0; | ||
1977 | |||
1978 | /* comment string */ | ||
1979 | if(parameters->cp_comment) { | ||
1980 | cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1); | ||
1981 | if(cp->comment) { | ||
1982 | strcpy(cp->comment, parameters->cp_comment); | ||
1983 | } | ||
1984 | } | ||
1985 | |||
1986 | /* | ||
1987 | calculate other encoding parameters | ||
1988 | */ | ||
1989 | |||
1990 | if (parameters->tile_size_on) { | ||
1991 | cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx); | ||
1992 | cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy); | ||
1993 | } else { | ||
1994 | cp->tdx = image->x1 - cp->tx0; | ||
1995 | cp->tdy = image->y1 - cp->ty0; | ||
1996 | } | ||
1997 | |||
1998 | if(parameters->tp_on){ | ||
1999 | cp->tp_flag = parameters->tp_flag; | ||
2000 | cp->tp_on = 1; | ||
2001 | } | ||
2002 | |||
2003 | cp->img_size = 0; | ||
2004 | for(i=0;i<image->numcomps ;i++){ | ||
2005 | cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec); | ||
2006 | } | ||
2007 | |||
2008 | |||
2009 | #ifdef USE_JPWL | ||
2010 | /* | ||
2011 | calculate JPWL encoding parameters | ||
2012 | */ | ||
2013 | |||
2014 | if (parameters->jpwl_epc_on) { | ||
2015 | int i; | ||
2016 | |||
2017 | /* set JPWL on */ | ||
2018 | cp->epc_on = true; | ||
2019 | cp->info_on = false; /* no informative technique */ | ||
2020 | |||
2021 | /* set EPB on */ | ||
2022 | if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) { | ||
2023 | cp->epb_on = true; | ||
2024 | |||
2025 | cp->hprot_MH = parameters->jpwl_hprot_MH; | ||
2026 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { | ||
2027 | cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i]; | ||
2028 | cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i]; | ||
2029 | } | ||
2030 | /* if tile specs are not specified, copy MH specs */ | ||
2031 | if (cp->hprot_TPH[0] == -1) { | ||
2032 | cp->hprot_TPH_tileno[0] = 0; | ||
2033 | cp->hprot_TPH[0] = parameters->jpwl_hprot_MH; | ||
2034 | } | ||
2035 | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { | ||
2036 | cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i]; | ||
2037 | cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i]; | ||
2038 | cp->pprot[i] = parameters->jpwl_pprot[i]; | ||
2039 | } | ||
2040 | } | ||
2041 | |||
2042 | /* set ESD writing */ | ||
2043 | if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) { | ||
2044 | cp->esd_on = true; | ||
2045 | |||
2046 | cp->sens_size = parameters->jpwl_sens_size; | ||
2047 | cp->sens_addr = parameters->jpwl_sens_addr; | ||
2048 | cp->sens_range = parameters->jpwl_sens_range; | ||
2049 | |||
2050 | cp->sens_MH = parameters->jpwl_sens_MH; | ||
2051 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { | ||
2052 | cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i]; | ||
2053 | cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i]; | ||
2054 | } | ||
2055 | } | ||
2056 | |||
2057 | /* always set RED writing to false: we are at the encoder */ | ||
2058 | cp->red_on = false; | ||
2059 | |||
2060 | } else { | ||
2061 | cp->epc_on = false; | ||
2062 | } | ||
2063 | #endif /* USE_JPWL */ | ||
2064 | |||
2065 | |||
2066 | /* initialize the mutiple tiles */ | ||
2067 | /* ---------------------------- */ | ||
2068 | cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t)); | ||
2069 | |||
2070 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | ||
2071 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
2072 | tcp->numlayers = parameters->tcp_numlayers; | ||
2073 | for (j = 0; j < tcp->numlayers; j++) { | ||
2074 | if(cp->cinema){ | ||
2075 | if (cp->fixed_quality) { | ||
2076 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; | ||
2077 | } | ||
2078 | tcp->rates[j] = parameters->tcp_rates[j]; | ||
2079 | }else{ | ||
2080 | if (cp->fixed_quality) { /* add fixed_quality */ | ||
2081 | tcp->distoratio[j] = parameters->tcp_distoratio[j]; | ||
2082 | } else { | ||
2083 | tcp->rates[j] = parameters->tcp_rates[j]; | ||
2084 | } | ||
2085 | } | ||
2086 | } | ||
2087 | tcp->csty = parameters->csty; | ||
2088 | tcp->prg = parameters->prog_order; | ||
2089 | tcp->mct = parameters->tcp_mct; | ||
2090 | |||
2091 | numpocs_tile = 0; | ||
2092 | tcp->POC = 0; | ||
2093 | if (parameters->numpocs) { | ||
2094 | /* initialisation of POC */ | ||
2095 | tcp->POC = 1; | ||
2096 | j2k_check_poc_val(parameters, image->numcomps, tcp->numlayers); | ||
2097 | for (i = 0; i < parameters->numpocs; i++) { | ||
2098 | if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) { | ||
2099 | opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; | ||
2100 | tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; | ||
2101 | tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; | ||
2102 | tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; | ||
2103 | tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; | ||
2104 | tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1; | ||
2105 | tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1; | ||
2106 | tcp_poc->tile = parameters->POC[numpocs_tile].tile; | ||
2107 | numpocs_tile++; | ||
2108 | } | ||
2109 | } | ||
2110 | tcp->numpocs = numpocs_tile -1 ; | ||
2111 | }else{ | ||
2112 | tcp->numpocs = 0; | ||
2113 | } | ||
2114 | |||
2115 | tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t)); | ||
2116 | |||
2117 | for (i = 0; i < image->numcomps; i++) { | ||
2118 | opj_tccp_t *tccp = &tcp->tccps[i]; | ||
2119 | tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */ | ||
2120 | tccp->numresolutions = parameters->numresolution; | ||
2121 | tccp->cblkw = int_floorlog2(parameters->cblockw_init); | ||
2122 | tccp->cblkh = int_floorlog2(parameters->cblockh_init); | ||
2123 | tccp->cblksty = parameters->mode; | ||
2124 | tccp->qmfbid = parameters->irreversible ? 0 : 1; | ||
2125 | tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT; | ||
2126 | tccp->numgbits = 2; | ||
2127 | if (i == parameters->roi_compno) { | ||
2128 | tccp->roishift = parameters->roi_shift; | ||
2129 | } else { | ||
2130 | tccp->roishift = 0; | ||
2131 | } | ||
2132 | |||
2133 | if(parameters->cp_cinema) | ||
2134 | { | ||
2135 | //Precinct size for lowest frequency subband=128 | ||
2136 | tccp->prcw[0] = 7; | ||
2137 | tccp->prch[0] = 7; | ||
2138 | //Precinct size at all other resolutions = 256 | ||
2139 | for (j = 1; j < tccp->numresolutions; j++) { | ||
2140 | tccp->prcw[j] = 8; | ||
2141 | tccp->prch[j] = 8; | ||
2142 | } | ||
2143 | }else{ | ||
2144 | if (parameters->csty & J2K_CCP_CSTY_PRT) { | ||
2145 | int p = 0; | ||
2146 | for (j = tccp->numresolutions - 1; j >= 0; j--) { | ||
2147 | if (p < parameters->res_spec) { | ||
2148 | |||
2149 | if (parameters->prcw_init[p] < 1) { | ||
2150 | tccp->prcw[j] = 1; | ||
2151 | } else { | ||
2152 | tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]); | ||
2153 | } | ||
2154 | |||
2155 | if (parameters->prch_init[p] < 1) { | ||
2156 | tccp->prch[j] = 1; | ||
2157 | }else { | ||
2158 | tccp->prch[j] = int_floorlog2(parameters->prch_init[p]); | ||
2159 | } | ||
2160 | |||
2161 | } else { | ||
2162 | int res_spec = parameters->res_spec; | ||
2163 | int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1)); | ||
2164 | int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1)); | ||
2165 | |||
2166 | if (size_prcw < 1) { | ||
2167 | tccp->prcw[j] = 1; | ||
2168 | } else { | ||
2169 | tccp->prcw[j] = int_floorlog2(size_prcw); | ||
2170 | } | ||
2171 | |||
2172 | if (size_prch < 1) { | ||
2173 | tccp->prch[j] = 1; | ||
2174 | } else { | ||
2175 | tccp->prch[j] = int_floorlog2(size_prch); | ||
2176 | } | ||
2177 | } | ||
2178 | p++; | ||
2179 | /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */ | ||
2180 | } //end for | ||
2181 | } else { | ||
2182 | for (j = 0; j < tccp->numresolutions; j++) { | ||
2183 | tccp->prcw[j] = 15; | ||
2184 | tccp->prch[j] = 15; | ||
2185 | } | ||
2186 | } | ||
2187 | } | ||
2188 | |||
2189 | dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); | ||
2190 | } | ||
2191 | } | ||
2192 | } | ||
2193 | |||
2194 | /** | ||
2195 | Create an index file | ||
2196 | @param j2k | ||
2197 | @param cio | ||
2198 | @param image_info | ||
2199 | @param index Index filename | ||
2200 | @return Returns 1 if successful, returns 0 otherwise | ||
2201 | */ | ||
2202 | static int j2k_create_index(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_info_t *image_info, char *index) { | ||
2203 | int tileno, compno, layno, resno, precno, pack_nb, x, y; | ||
2204 | FILE *stream = NULL; | ||
2205 | double total_disto = 0; | ||
2206 | |||
2207 | image_info->codestream_size = cio_tell(cio) + j2k->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */ | ||
2208 | |||
2209 | |||
2210 | #ifdef USE_JPWL | ||
2211 | /* if JPWL is enabled and the name coincides with our own set | ||
2212 | then discard the creation of the file: this was just done to | ||
2213 | enable indexing, we do not want an index file | ||
2214 | */ | ||
2215 | if (j2k->cp->epc_on && !strcmp(index, JPWL_PRIVATEINDEX_NAME)) | ||
2216 | return 1; | ||
2217 | #endif /* USE_JPWL */ | ||
2218 | |||
2219 | |||
2220 | stream = fopen(index, "w"); | ||
2221 | if (!stream) { | ||
2222 | opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index); | ||
2223 | return 0; | ||
2224 | } | ||
2225 | |||
2226 | fprintf(stream, "%d %d\n", image_info->image_w, image_info->image_h); | ||
2227 | fprintf(stream, "%d\n", image_info->prog); | ||
2228 | fprintf(stream, "%d %d\n", image_info->tile_x, image_info->tile_y); | ||
2229 | fprintf(stream, "%d %d\n", image_info->tw, image_info->th); | ||
2230 | fprintf(stream, "%d\n", image_info->comp); | ||
2231 | fprintf(stream, "%d\n", image_info->layer); | ||
2232 | fprintf(stream, "%d\n", image_info->decomposition); | ||
2233 | |||
2234 | for (resno = image_info->decomposition; resno >= 0; resno--) { | ||
2235 | fprintf(stream, "[%d,%d] ", | ||
2236 | (1 << image_info->tile[0].pdx[resno]), (1 << image_info->tile[0].pdx[resno])); /* based on tile 0 */ | ||
2237 | } | ||
2238 | fprintf(stream, "\n"); | ||
2239 | fprintf(stream, "%d\n", image_info->main_head_end); | ||
2240 | fprintf(stream, "%d\n", image_info->codestream_size); | ||
2241 | |||
2242 | for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) { | ||
2243 | fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n", | ||
2244 | image_info->tile[tileno].num_tile, | ||
2245 | image_info->tile[tileno].start_pos, | ||
2246 | image_info->tile[tileno].end_header, | ||
2247 | image_info->tile[tileno].end_pos, | ||
2248 | image_info->tile[tileno].distotile, image_info->tile[tileno].nbpix, | ||
2249 | image_info->tile[tileno].distotile / image_info->tile[tileno].nbpix); | ||
2250 | } | ||
2251 | |||
2252 | for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) { | ||
2253 | int start_pos, end_pos; | ||
2254 | double disto = 0; | ||
2255 | pack_nb = 0; | ||
2256 | |||
2257 | /* | ||
2258 | fprintf(stream, "pkno tileno layerno resno compno precno start_pos end_pos deltaSE \n"); | ||
2259 | */ | ||
2260 | |||
2261 | if (image_info->prog == LRCP) { /* LRCP */ | ||
2262 | /* | ||
2263 | fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto"); | ||
2264 | */ | ||
2265 | for (layno = 0; layno < image_info->layer; layno++) { | ||
2266 | for (resno = 0; resno < image_info->decomposition + 1; resno++) { | ||
2267 | for (compno = 0; compno < image_info->comp; compno++) { | ||
2268 | int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno]; | ||
2269 | for (precno = 0; precno < prec_max; precno++) { | ||
2270 | start_pos = image_info->tile[tileno].packet[pack_nb].start_pos; | ||
2271 | end_pos = image_info->tile[tileno].packet[pack_nb].end_pos; | ||
2272 | disto = image_info->tile[tileno].packet[pack_nb].disto; | ||
2273 | fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n", | ||
2274 | pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto); | ||
2275 | total_disto += disto; | ||
2276 | pack_nb++; | ||
2277 | } | ||
2278 | } | ||
2279 | } | ||
2280 | } | ||
2281 | } /* LRCP */ | ||
2282 | else if (image_info->prog == RLCP) { /* RLCP */ | ||
2283 | /* | ||
2284 | fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto"); | ||
2285 | */ | ||
2286 | for (resno = 0; resno < image_info->decomposition + 1; resno++) { | ||
2287 | for (layno = 0; layno < image_info->layer; layno++) { | ||
2288 | for (compno = 0; compno < image_info->comp; compno++) { | ||
2289 | int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno]; | ||
2290 | for (precno = 0; precno < prec_max; precno++) { | ||
2291 | start_pos = image_info->tile[tileno].packet[pack_nb].start_pos; | ||
2292 | end_pos = image_info->tile[tileno].packet[pack_nb].end_pos; | ||
2293 | disto = image_info->tile[tileno].packet[pack_nb].disto; | ||
2294 | fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n", | ||
2295 | pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto); | ||
2296 | total_disto += disto; | ||
2297 | pack_nb++; | ||
2298 | } | ||
2299 | } | ||
2300 | } | ||
2301 | } | ||
2302 | } /* RLCP */ | ||
2303 | else if (image_info->prog == RPCL) { /* RPCL */ | ||
2304 | /* | ||
2305 | fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n"); | ||
2306 | */ | ||
2307 | for (resno = 0; resno < image_info->decomposition + 1; resno++) { | ||
2308 | /* I suppose components have same XRsiz, YRsiz */ | ||
2309 | int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x; | ||
2310 | int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y; | ||
2311 | int x1 = x0 + image_info->tile_x; | ||
2312 | int y1 = y0 + image_info->tile_y; | ||
2313 | for(y = y0; y < y1; y++) { | ||
2314 | for(x = x0; x < x1; x++) { | ||
2315 | for (compno = 0; compno < image_info->comp; compno++) { | ||
2316 | int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno]; | ||
2317 | for (precno = 0; precno < prec_max; precno++) { | ||
2318 | int pcnx = image_info->tile[tileno].pw[resno]; | ||
2319 | int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno ); | ||
2320 | int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno ); | ||
2321 | int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; | ||
2322 | int precno_y = (int) floor( (float)precno/(float)pcnx ); | ||
2323 | if (precno_y*pcy == y ) { | ||
2324 | if (precno_x*pcx == x ) { | ||
2325 | for (layno = 0; layno < image_info->layer; layno++) { | ||
2326 | start_pos = image_info->tile[tileno].packet[pack_nb].start_pos; | ||
2327 | end_pos = image_info->tile[tileno].packet[pack_nb].end_pos; | ||
2328 | disto = image_info->tile[tileno].packet[pack_nb].disto; | ||
2329 | fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n", | ||
2330 | pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); | ||
2331 | total_disto += disto; | ||
2332 | pack_nb++; | ||
2333 | } | ||
2334 | } | ||
2335 | } | ||
2336 | } /* precno */ | ||
2337 | } /* compno */ | ||
2338 | } /* x = x0..x1 */ | ||
2339 | } /* y = y0..y1 */ | ||
2340 | } /* resno */ | ||
2341 | } /* RPCL */ | ||
2342 | else if (image_info->prog == PCRL) { /* PCRL */ | ||
2343 | /* I suppose components have same XRsiz, YRsiz */ | ||
2344 | int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x; | ||
2345 | int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y; | ||
2346 | int x1 = x0 + image_info->tile_x; | ||
2347 | int y1 = y0 + image_info->tile_y; | ||
2348 | /* | ||
2349 | fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n"); | ||
2350 | */ | ||
2351 | for(y = y0; y < y1; y++) { | ||
2352 | for(x = x0; x < x1; x++) { | ||
2353 | for (compno = 0; compno < image_info->comp; compno++) { | ||
2354 | for (resno = 0; resno < image_info->decomposition + 1; resno++) { | ||
2355 | int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno]; | ||
2356 | for (precno = 0; precno < prec_max; precno++) { | ||
2357 | int pcnx = image_info->tile[tileno].pw[resno]; | ||
2358 | int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno ); | ||
2359 | int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno ); | ||
2360 | int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; | ||
2361 | int precno_y = (int) floor( (float)precno/(float)pcnx ); | ||
2362 | if (precno_y*pcy == y ) { | ||
2363 | if (precno_x*pcx == x ) { | ||
2364 | for (layno = 0; layno < image_info->layer; layno++) { | ||
2365 | start_pos = image_info->tile[tileno].packet[pack_nb].start_pos; | ||
2366 | end_pos = image_info->tile[tileno].packet[pack_nb].end_pos; | ||
2367 | disto = image_info->tile[tileno].packet[pack_nb].disto; | ||
2368 | fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n", | ||
2369 | pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); | ||
2370 | total_disto += disto; | ||
2371 | pack_nb++; | ||
2372 | } | ||
2373 | } | ||
2374 | } | ||
2375 | } /* precno */ | ||
2376 | } /* resno */ | ||
2377 | } /* compno */ | ||
2378 | } /* x = x0..x1 */ | ||
2379 | } /* y = y0..y1 */ | ||
2380 | } /* PCRL */ | ||
2381 | else { /* CPRL */ | ||
2382 | /* | ||
2383 | fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n"); | ||
2384 | */ | ||
2385 | for (compno = 0; compno < image_info->comp; compno++) { | ||
2386 | /* I suppose components have same XRsiz, YRsiz */ | ||
2387 | int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x; | ||
2388 | int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y; | ||
2389 | int x1 = x0 + image_info->tile_x; | ||
2390 | int y1 = y0 + image_info->tile_y; | ||
2391 | for(y = y0; y < y1; y++) { | ||
2392 | for(x = x0; x < x1; x++) { | ||
2393 | for (resno = 0; resno < image_info->decomposition + 1; resno++) { | ||
2394 | int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno]; | ||
2395 | for (precno = 0; precno < prec_max; precno++) { | ||
2396 | int pcnx = image_info->tile[tileno].pw[resno]; | ||
2397 | int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno ); | ||
2398 | int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno ); | ||
2399 | int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; | ||
2400 | int precno_y = (int) floor( (float)precno/(float)pcnx ); | ||
2401 | if (precno_y*pcy == y ) { | ||
2402 | if (precno_x*pcx == x ) { | ||
2403 | for (layno = 0; layno < image_info->layer; layno++) { | ||
2404 | start_pos = image_info->tile[tileno].packet[pack_nb].start_pos; | ||
2405 | end_pos = image_info->tile[tileno].packet[pack_nb].end_pos; | ||
2406 | disto = image_info->tile[tileno].packet[pack_nb].disto; | ||
2407 | fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n", | ||
2408 | pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); | ||
2409 | total_disto += disto; | ||
2410 | pack_nb++; | ||
2411 | } | ||
2412 | } | ||
2413 | } | ||
2414 | } /* precno */ | ||
2415 | } /* resno */ | ||
2416 | } /* x = x0..x1 */ | ||
2417 | } /* y = y0..y1 */ | ||
2418 | } /* comno */ | ||
2419 | } /* CPRL */ | ||
2420 | } /* tileno */ | ||
2421 | |||
2422 | fprintf(stream, "%8e\n", image_info->D_max); /* SE max */ | ||
2423 | fprintf(stream, "%.8e\n", total_disto); /* SE totale */ | ||
2424 | fclose(stream); | ||
2425 | |||
2426 | return 1; | ||
2427 | } | ||
2428 | |||
2429 | bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) { | ||
2430 | int tileno, compno; | ||
2431 | opj_image_info_t *image_info = NULL; | ||
2432 | opj_cp_t *cp = NULL; | ||
2433 | |||
2434 | opj_tcd_t *tcd = NULL; /* TCD component */ | ||
2435 | |||
2436 | j2k->cio = cio; | ||
2437 | j2k->image = image; | ||
2438 | |||
2439 | cp = j2k->cp; | ||
2440 | |||
2441 | /* j2k_dump_cp(stdout, image, cp); */ | ||
2442 | |||
2443 | /* INDEX >> */ | ||
2444 | image_info = j2k->image_info; | ||
2445 | if (image_info && cp->index_on) { | ||
2446 | image_info->index_on = cp->index_on; | ||
2447 | image_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t)); | ||
2448 | image_info->image_w = image->x1 - image->x0; | ||
2449 | image_info->image_h = image->y1 - image->y0; | ||
2450 | image_info->prog = (&cp->tcps[0])->prg; | ||
2451 | image_info->tw = cp->tw; | ||
2452 | image_info->th = cp->th; | ||
2453 | image_info->tile_x = cp->tdx; /* new version parser */ | ||
2454 | image_info->tile_y = cp->tdy; /* new version parser */ | ||
2455 | image_info->tile_Ox = cp->tx0; /* new version parser */ | ||
2456 | image_info->tile_Oy = cp->ty0; /* new version parser */ | ||
2457 | image_info->comp = image->numcomps; | ||
2458 | image_info->layer = (&cp->tcps[0])->numlayers; | ||
2459 | image_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1; | ||
2460 | image_info->D_max = 0; /* ADD Marcela */ | ||
2461 | } | ||
2462 | /* << INDEX */ | ||
2463 | |||
2464 | j2k_write_soc(j2k); | ||
2465 | j2k_write_siz(j2k); | ||
2466 | j2k_write_cod(j2k); | ||
2467 | j2k_write_qcd(j2k); | ||
2468 | |||
2469 | if(cp->cinema){ | ||
2470 | for (compno = 1; compno < image->numcomps; compno++) { | ||
2471 | j2k_write_coc(j2k, compno); | ||
2472 | j2k_write_qcc(j2k, compno); | ||
2473 | } | ||
2474 | } | ||
2475 | |||
2476 | for (compno = 0; compno < image->numcomps; compno++) { | ||
2477 | opj_tcp_t *tcp = &cp->tcps[0]; | ||
2478 | if (tcp->tccps[compno].roishift) | ||
2479 | j2k_write_rgn(j2k, compno, 0); | ||
2480 | } | ||
2481 | if (cp->comment != NULL) { | ||
2482 | j2k_write_com(j2k); | ||
2483 | } | ||
2484 | /* INDEX >> */ | ||
2485 | if(image_info && image_info->index_on) { | ||
2486 | image_info->main_head_end = cio_tell(cio) - 1; | ||
2487 | } | ||
2488 | /* << INDEX */ | ||
2489 | |||
2490 | j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k); | ||
2491 | /* TLM Marker*/ | ||
2492 | if(cp->cinema){ | ||
2493 | j2k_write_tlm(j2k); | ||
2494 | if (cp->cinema == CINEMA4K_24) { | ||
2495 | j2k_write_poc(j2k); | ||
2496 | } | ||
2497 | } | ||
2498 | /**** Main Header ENDS here ***/ | ||
2499 | |||
2500 | /* create the tile encoder */ | ||
2501 | tcd = tcd_create(j2k->cinfo); | ||
2502 | |||
2503 | /* encode each tile */ | ||
2504 | |||
2505 | for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { | ||
2506 | int pino; | ||
2507 | int tilepartno=0; | ||
2508 | |||
2509 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
2510 | opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th); | ||
2511 | |||
2512 | j2k->curtileno = tileno; | ||
2513 | j2k->cur_tp_num = 0; | ||
2514 | |||
2515 | /* initialisation before tile encoding */ | ||
2516 | if (tileno == 0) { | ||
2517 | tcd_malloc_encode(tcd, image, cp, j2k->curtileno); | ||
2518 | } else { | ||
2519 | tcd_init_encode(tcd, image, cp, j2k->curtileno); | ||
2520 | } | ||
2521 | |||
2522 | /* INDEX >> */ | ||
2523 | if(image_info && image_info->index_on) { | ||
2524 | image_info->tile[j2k->curtileno].num_tile = j2k->curtileno; | ||
2525 | image_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction; | ||
2526 | } | ||
2527 | /* << INDEX */ | ||
2528 | |||
2529 | for(pino = 0; pino <= tcp->numpocs; pino++) { | ||
2530 | int tot_num_tp; | ||
2531 | tcd->cur_pino=pino; | ||
2532 | |||
2533 | /*Get number of tile parts*/ | ||
2534 | tot_num_tp = j2k_get_num_tp(cp,pino,tileno); | ||
2535 | tcd->tp_pos = cp->tp_pos; | ||
2536 | |||
2537 | for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){ | ||
2538 | j2k->tp_num = tilepartno; | ||
2539 | j2k_write_sot(j2k); | ||
2540 | |||
2541 | if(j2k->cur_tp_num == 0 && cp->cinema == 0){ | ||
2542 | for (compno = 1; compno < image->numcomps; compno++) { | ||
2543 | j2k_write_coc(j2k, compno); | ||
2544 | j2k_write_qcc(j2k, compno); | ||
2545 | } | ||
2546 | if (cp->tcps[tileno].numpocs) { | ||
2547 | j2k_write_poc(j2k); | ||
2548 | } | ||
2549 | } | ||
2550 | |||
2551 | j2k_write_sod(j2k, tcd); | ||
2552 | j2k->cur_tp_num ++; | ||
2553 | } | ||
2554 | |||
2555 | } | ||
2556 | /* INDEX >> */ | ||
2557 | if(image_info && image_info->index_on) { | ||
2558 | image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1; | ||
2559 | } | ||
2560 | /* << INDEX */ | ||
2561 | |||
2562 | |||
2563 | /* | ||
2564 | if (tile->PPT) { // BAD PPT !!! | ||
2565 | FILE *PPT_file; | ||
2566 | int i; | ||
2567 | PPT_file=fopen("PPT","rb"); | ||
2568 | fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256); | ||
2569 | for (i=0;i<tile->len_ppt;i++) { | ||
2570 | unsigned char elmt; | ||
2571 | fread(&elmt, 1, 1, PPT_file); | ||
2572 | fwrite(&elmt,1,1,f); | ||
2573 | } | ||
2574 | fclose(PPT_file); | ||
2575 | unlink("PPT"); | ||
2576 | } | ||
2577 | */ | ||
2578 | |||
2579 | } | ||
2580 | |||
2581 | /* destroy the tile encoder */ | ||
2582 | tcd_free_encode(tcd); | ||
2583 | tcd_destroy(tcd); | ||
2584 | |||
2585 | j2k_write_eoc(j2k); | ||
2586 | |||
2587 | /* Creation of the index file */ | ||
2588 | if(image_info && image_info->index_on) { | ||
2589 | if(!j2k_create_index(j2k, cio, image_info, index)) { | ||
2590 | opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to create index file %s\n", index); | ||
2591 | return false; | ||
2592 | } | ||
2593 | } | ||
2594 | |||
2595 | |||
2596 | #ifdef USE_JPWL | ||
2597 | /* | ||
2598 | preparation of JPWL marker segments: can be finalized only when the whole | ||
2599 | codestream is known | ||
2600 | */ | ||
2601 | if(image_info && image_info->index_on && cp->epc_on) { | ||
2602 | |||
2603 | /* let's begin creating a marker list, according to user wishes */ | ||
2604 | jpwl_prepare_marks(j2k, cio, image); | ||
2605 | |||
2606 | /* now we dump the JPWL markers on the codestream */ | ||
2607 | jpwl_dump_marks(j2k, cio, image); | ||
2608 | |||
2609 | /* do not know exactly what is this for, | ||
2610 | but it gets called during index creation */ | ||
2611 | j2k->pos_correction = 0; | ||
2612 | |||
2613 | /* Re-creation of the index file, with updated info */ | ||
2614 | if(image_info && image_info->index_on) { | ||
2615 | if(!j2k_create_index(j2k, cio, image_info, index)) { | ||
2616 | opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to re-create index file %s\n", index); | ||
2617 | return false; | ||
2618 | } | ||
2619 | } | ||
2620 | |||
2621 | /* now we finalize the marker contents */ | ||
2622 | /*jpwl_finalize_marks(j2k, cio, image);*/ | ||
2623 | |||
2624 | } | ||
2625 | #endif /* USE_JPWL */ | ||
2626 | |||
2627 | |||
2628 | return true; | ||
2629 | } | ||
2630 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/j2k.h b/libraries/openjpeg-libsl/libopenjpeg/j2k.h new file mode 100644 index 0000000..c18f4cd --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/j2k.h | |||
@@ -0,0 +1,525 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2006-2007, Parvatha Elangovan | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | #ifndef __J2K_H | ||
33 | #define __J2K_H | ||
34 | /** | ||
35 | @file j2k.h | ||
36 | @brief The JPEG-2000 Codestream Reader/Writer (J2K) | ||
37 | |||
38 | The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data. | ||
39 | */ | ||
40 | |||
41 | /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ | ||
42 | /*@{*/ | ||
43 | |||
44 | #define J2K_CP_CSTY_PRT 0x01 | ||
45 | #define J2K_CP_CSTY_SOP 0x02 | ||
46 | #define J2K_CP_CSTY_EPH 0x04 | ||
47 | #define J2K_CCP_CSTY_PRT 0x01 | ||
48 | #define J2K_CCP_CBLKSTY_LAZY 0x01 | ||
49 | #define J2K_CCP_CBLKSTY_RESET 0x02 | ||
50 | #define J2K_CCP_CBLKSTY_TERMALL 0x04 | ||
51 | #define J2K_CCP_CBLKSTY_VSC 0x08 | ||
52 | #define J2K_CCP_CBLKSTY_PTERM 0x10 | ||
53 | #define J2K_CCP_CBLKSTY_SEGSYM 0x20 | ||
54 | #define J2K_CCP_QNTSTY_NOQNT 0 | ||
55 | #define J2K_CCP_QNTSTY_SIQNT 1 | ||
56 | #define J2K_CCP_QNTSTY_SEQNT 2 | ||
57 | |||
58 | /* ----------------------------------------------------------------------- */ | ||
59 | |||
60 | #define J2K_MS_SOC 0xff4f /**< SOC marker value */ | ||
61 | #define J2K_MS_SOT 0xff90 /**< SOT marker value */ | ||
62 | #define J2K_MS_SOD 0xff93 /**< SOD marker value */ | ||
63 | #define J2K_MS_EOC 0xffd9 /**< EOC marker value */ | ||
64 | #define J2K_MS_SIZ 0xff51 /**< SIZ marker value */ | ||
65 | #define J2K_MS_COD 0xff52 /**< COD marker value */ | ||
66 | #define J2K_MS_COC 0xff53 /**< COC marker value */ | ||
67 | #define J2K_MS_RGN 0xff5e /**< RGN marker value */ | ||
68 | #define J2K_MS_QCD 0xff5c /**< QCD marker value */ | ||
69 | #define J2K_MS_QCC 0xff5d /**< QCC marker value */ | ||
70 | #define J2K_MS_POC 0xff5f /**< POC marker value */ | ||
71 | #define J2K_MS_TLM 0xff55 /**< TLM marker value */ | ||
72 | #define J2K_MS_PLM 0xff57 /**< PLM marker value */ | ||
73 | #define J2K_MS_PLT 0xff58 /**< PLT marker value */ | ||
74 | #define J2K_MS_PPM 0xff60 /**< PPM marker value */ | ||
75 | #define J2K_MS_PPT 0xff61 /**< PPT marker value */ | ||
76 | #define J2K_MS_SOP 0xff91 /**< SOP marker value */ | ||
77 | #define J2K_MS_EPH 0xff92 /**< EPH marker value */ | ||
78 | #define J2K_MS_CRG 0xff63 /**< CRG marker value */ | ||
79 | #define J2K_MS_COM 0xff64 /**< COM marker value */ | ||
80 | /* UniPG>> */ | ||
81 | #ifdef USE_JPWL | ||
82 | #define J2K_MS_EPC 0xff68 /**< EPC marker value (Part11) */ | ||
83 | #define J2K_MS_EPB 0xff66 /**< EPB marker value (Part11) */ | ||
84 | #define J2K_MS_ESD 0xff67 /**< ESD marker value (Part11) */ | ||
85 | #define J2K_MS_RED 0xff69 /**< RED marker value (Part11) */ | ||
86 | #endif /* USE_JPWL */ | ||
87 | /* <<UniPG */ | ||
88 | |||
89 | /* ----------------------------------------------------------------------- */ | ||
90 | |||
91 | /** | ||
92 | Values that specify the status of the decoding process when decoding the main header. | ||
93 | These values may be combined with a | operator. | ||
94 | */ | ||
95 | typedef enum J2K_STATUS { | ||
96 | J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */ | ||
97 | J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */ | ||
98 | J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */ | ||
99 | J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */ | ||
100 | J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */ | ||
101 | J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */ | ||
102 | J2K_STATE_NEOC = 0x0040 /**< the decoding process must not expect a EOC marker because the codestream is truncated */ | ||
103 | } J2K_STATUS; | ||
104 | |||
105 | /* ----------------------------------------------------------------------- */ | ||
106 | |||
107 | /** | ||
108 | T2 encoding mode | ||
109 | */ | ||
110 | typedef enum T2_MODE { | ||
111 | THRESH_CALC = 0, /** Function called in Rate allocation process*/ | ||
112 | FINAL_PASS = 1 /** Function called in Tier 2 process*/ | ||
113 | }J2K_T2_MODE; | ||
114 | |||
115 | /** | ||
116 | Quantization stepsize | ||
117 | */ | ||
118 | typedef struct opj_stepsize { | ||
119 | /** exponent */ | ||
120 | int expn; | ||
121 | /** mantissa */ | ||
122 | int mant; | ||
123 | } opj_stepsize_t; | ||
124 | |||
125 | /** | ||
126 | Tile-component coding parameters | ||
127 | */ | ||
128 | typedef struct opj_tccp { | ||
129 | /** coding style */ | ||
130 | int csty; | ||
131 | /** number of resolutions */ | ||
132 | int numresolutions; | ||
133 | /** code-blocks width */ | ||
134 | int cblkw; | ||
135 | /** code-blocks height */ | ||
136 | int cblkh; | ||
137 | /** code-block coding style */ | ||
138 | int cblksty; | ||
139 | /** discrete wavelet transform identifier */ | ||
140 | int qmfbid; | ||
141 | /** quantisation style */ | ||
142 | int qntsty; | ||
143 | /** stepsizes used for quantization */ | ||
144 | opj_stepsize_t stepsizes[J2K_MAXBANDS]; | ||
145 | /** number of guard bits */ | ||
146 | int numgbits; | ||
147 | /** Region Of Interest shift */ | ||
148 | int roishift; | ||
149 | /** precinct width */ | ||
150 | int prcw[J2K_MAXRLVLS]; | ||
151 | /** precinct height */ | ||
152 | int prch[J2K_MAXRLVLS]; | ||
153 | } opj_tccp_t; | ||
154 | |||
155 | /** | ||
156 | Tile coding parameters : | ||
157 | this structure is used to store coding/decoding parameters common to all | ||
158 | tiles (information like COD, COC in main header) | ||
159 | */ | ||
160 | typedef struct opj_tcp { | ||
161 | /** 1 : first part-tile of a tile */ | ||
162 | int first; | ||
163 | /** coding style */ | ||
164 | int csty; | ||
165 | /** progression order */ | ||
166 | OPJ_PROG_ORDER prg; | ||
167 | /** number of layers */ | ||
168 | int numlayers; | ||
169 | /** multi-component transform identifier */ | ||
170 | int mct; | ||
171 | /** rates of layers */ | ||
172 | float rates[100]; | ||
173 | /** number of progression order changes */ | ||
174 | int numpocs; | ||
175 | /** indicates if a POC marker has been used O:NO, 1:YES */ | ||
176 | int POC; | ||
177 | /** progression order changes */ | ||
178 | opj_poc_t pocs[32]; | ||
179 | /** packet header store there for futur use in t2_decode_packet */ | ||
180 | unsigned char *ppt_data; | ||
181 | /** pointer remaining on the first byte of the first header if ppt is used */ | ||
182 | unsigned char *ppt_data_first; | ||
183 | /** If ppt == 1 --> there was a PPT marker for the present tile */ | ||
184 | int ppt; | ||
185 | /** used in case of multiple marker PPT (number of info already stored) */ | ||
186 | int ppt_store; | ||
187 | /** ppmbug1 */ | ||
188 | int ppt_len; | ||
189 | /** add fixed_quality */ | ||
190 | float distoratio[100]; | ||
191 | /** tile-component coding parameters */ | ||
192 | opj_tccp_t *tccps; | ||
193 | } opj_tcp_t; | ||
194 | |||
195 | /** | ||
196 | Coding parameters | ||
197 | */ | ||
198 | typedef struct opj_cp { | ||
199 | /** Digital cinema profile*/ | ||
200 | OPJ_CINEMA_MODE cinema; | ||
201 | /** Maximum rate for each component. If == 0, component size limitation is not considered */ | ||
202 | int max_comp_size; | ||
203 | /** Size of the image in bits*/ | ||
204 | int img_size; | ||
205 | /** Rsiz*/ | ||
206 | OPJ_RSIZ_CAPABILITIES rsiz; | ||
207 | /** Enabling Tile part generation*/ | ||
208 | char tp_on; | ||
209 | /** Flag determining tile part generation*/ | ||
210 | char tp_flag; | ||
211 | /** Position of tile part flag in progression order*/ | ||
212 | int tp_pos; | ||
213 | /** allocation by rate/distortion */ | ||
214 | int disto_alloc; | ||
215 | /** allocation by fixed layer */ | ||
216 | int fixed_alloc; | ||
217 | /** add fixed_quality */ | ||
218 | int fixed_quality; | ||
219 | /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */ | ||
220 | int reduce; | ||
221 | /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */ | ||
222 | int layer; | ||
223 | /** if == NO_LIMITATION, decode entire codestream; if == LIMIT_TO_MAIN_HEADER then only decode the main header */ | ||
224 | OPJ_LIMIT_DECODING limit_decoding; | ||
225 | /** 0 = no index || 1 = index */ | ||
226 | int index_on; | ||
227 | /** XTOsiz */ | ||
228 | int tx0; | ||
229 | /** YTOsiz */ | ||
230 | int ty0; | ||
231 | /** XTsiz */ | ||
232 | int tdx; | ||
233 | /** YTsiz */ | ||
234 | int tdy; | ||
235 | /** comment for coding */ | ||
236 | char *comment; | ||
237 | /** number of tiles in width */ | ||
238 | int tw; | ||
239 | /** number of tiles in heigth */ | ||
240 | int th; | ||
241 | /** ID number of the tiles present in the codestream */ | ||
242 | int *tileno; | ||
243 | /** size of the vector tileno */ | ||
244 | int tileno_size; | ||
245 | /** packet header store there for futur use in t2_decode_packet */ | ||
246 | unsigned char *ppm_data; | ||
247 | /** pointer remaining on the first byte of the first header if ppm is used */ | ||
248 | unsigned char *ppm_data_first; | ||
249 | /** if ppm == 1 --> there was a PPM marker for the present tile */ | ||
250 | int ppm; | ||
251 | /** use in case of multiple marker PPM (number of info already store) */ | ||
252 | int ppm_store; | ||
253 | /** use in case of multiple marker PPM (case on non-finished previous info) */ | ||
254 | int ppm_previous; | ||
255 | /** ppmbug1 */ | ||
256 | int ppm_len; | ||
257 | /** tile coding parameters */ | ||
258 | opj_tcp_t *tcps; | ||
259 | /** fixed layer */ | ||
260 | int *matrice; | ||
261 | /* UniPG>> */ | ||
262 | #ifdef USE_JPWL | ||
263 | /** enables writing of EPC in MH, thus activating JPWL */ | ||
264 | bool epc_on; | ||
265 | /** enables writing of EPB, in case of activated JPWL */ | ||
266 | bool epb_on; | ||
267 | /** enables writing of ESD, in case of activated JPWL */ | ||
268 | bool esd_on; | ||
269 | /** enables writing of informative techniques of ESD, in case of activated JPWL */ | ||
270 | bool info_on; | ||
271 | /** enables writing of RED, in case of activated JPWL */ | ||
272 | bool red_on; | ||
273 | /** error protection method for MH (0,1,16,32,37-128) */ | ||
274 | int hprot_MH; | ||
275 | /** tile number of header protection specification (>=0) */ | ||
276 | int hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS]; | ||
277 | /** error protection methods for TPHs (0,1,16,32,37-128) */ | ||
278 | int hprot_TPH[JPWL_MAX_NO_TILESPECS]; | ||
279 | /** tile number of packet protection specification (>=0) */ | ||
280 | int pprot_tileno[JPWL_MAX_NO_PACKSPECS]; | ||
281 | /** packet number of packet protection specification (>=0) */ | ||
282 | int pprot_packno[JPWL_MAX_NO_PACKSPECS]; | ||
283 | /** error protection methods for packets (0,1,16,32,37-128) */ | ||
284 | int pprot[JPWL_MAX_NO_PACKSPECS]; | ||
285 | /** enables writing of ESD, (0/2/4 bytes) */ | ||
286 | int sens_size; | ||
287 | /** sensitivity addressing size (0=auto/2/4 bytes) */ | ||
288 | int sens_addr; | ||
289 | /** sensitivity range (0-3) */ | ||
290 | int sens_range; | ||
291 | /** sensitivity method for MH (-1,0-7) */ | ||
292 | int sens_MH; | ||
293 | /** tile number of sensitivity specification (>=0) */ | ||
294 | int sens_TPH_tileno[JPWL_MAX_NO_TILESPECS]; | ||
295 | /** sensitivity methods for TPHs (-1,0-7) */ | ||
296 | int sens_TPH[JPWL_MAX_NO_TILESPECS]; | ||
297 | /** enables JPWL correction at the decoder */ | ||
298 | bool correct; | ||
299 | /** expected number of components at the decoder */ | ||
300 | int exp_comps; | ||
301 | /** maximum number of tiles at the decoder */ | ||
302 | int max_tiles; | ||
303 | #endif /* USE_JPWL */ | ||
304 | /* <<UniPG */ | ||
305 | } opj_cp_t; | ||
306 | |||
307 | /** | ||
308 | Information concerning a packet inside tile | ||
309 | */ | ||
310 | typedef struct opj_packet_info { | ||
311 | /** start position */ | ||
312 | int start_pos; | ||
313 | /** end position */ | ||
314 | int end_pos; | ||
315 | /** ADD for Marcela */ | ||
316 | double disto; | ||
317 | } opj_packet_info_t; | ||
318 | |||
319 | /** | ||
320 | Index structure : information regarding tiles inside image | ||
321 | */ | ||
322 | typedef struct opj_tile_info { | ||
323 | /** value of thresh for each layer by tile cfr. Marcela */ | ||
324 | double *thresh; | ||
325 | /** number of tile */ | ||
326 | int num_tile; | ||
327 | /** start position */ | ||
328 | int start_pos; | ||
329 | /** end position of the header */ | ||
330 | int end_header; | ||
331 | /** end position */ | ||
332 | int end_pos; | ||
333 | /** precinct number for each resolution level (width) */ | ||
334 | int pw[33]; | ||
335 | /** precinct number for each resolution level (height) */ | ||
336 | int ph[33]; | ||
337 | /** precinct size (in power of 2), in X for each resolution level */ | ||
338 | int pdx[33]; | ||
339 | /** precinct size (in power of 2), in Y for each resolution level */ | ||
340 | int pdy[33]; | ||
341 | /** information concerning packets inside tile */ | ||
342 | opj_packet_info_t *packet; | ||
343 | /** add fixed_quality */ | ||
344 | int nbpix; | ||
345 | /** add fixed_quality */ | ||
346 | double distotile; | ||
347 | } opj_tile_info_t; | ||
348 | |||
349 | /** | ||
350 | Index structure | ||
351 | */ | ||
352 | typedef struct opj_image_info { | ||
353 | /** 0 = no index || 1 = index */ | ||
354 | int index_on; | ||
355 | /** maximum distortion reduction on the whole image (add for Marcela) */ | ||
356 | double D_max; | ||
357 | /** packet number */ | ||
358 | int num; | ||
359 | /** writing the packet in the index with t2_encode_packets */ | ||
360 | int index_write; | ||
361 | /** image width */ | ||
362 | int image_w; | ||
363 | /** image height */ | ||
364 | int image_h; | ||
365 | /** progression order */ | ||
366 | OPJ_PROG_ORDER prog; | ||
367 | /** tile size in x */ | ||
368 | int tile_x; | ||
369 | /** tile size in y */ | ||
370 | int tile_y; | ||
371 | /** */ | ||
372 | int tile_Ox; | ||
373 | /** */ | ||
374 | int tile_Oy; | ||
375 | /** number of tiles in X */ | ||
376 | int tw; | ||
377 | /** number of tiles in Y */ | ||
378 | int th; | ||
379 | /** component numbers */ | ||
380 | int comp; | ||
381 | /** number of layer */ | ||
382 | int layer; | ||
383 | /** number of decomposition */ | ||
384 | int decomposition; | ||
385 | /** main header position */ | ||
386 | int main_head_end; | ||
387 | /** codestream's size */ | ||
388 | int codestream_size; | ||
389 | /** information regarding tiles inside image */ | ||
390 | opj_tile_info_t *tile; | ||
391 | } opj_image_info_t; | ||
392 | |||
393 | /** | ||
394 | JPEG-2000 codestream reader/writer | ||
395 | */ | ||
396 | typedef struct opj_j2k { | ||
397 | /** codec context */ | ||
398 | opj_common_ptr cinfo; | ||
399 | |||
400 | /** locate in which part of the codestream the decoder is (main header, tile header, end) */ | ||
401 | int state; | ||
402 | /** number of the tile curently concern by coding/decoding */ | ||
403 | int curtileno; | ||
404 | /** Tile part number*/ | ||
405 | int tp_num; | ||
406 | /** Tilepart number currently coding*/ | ||
407 | int cur_tp_num; | ||
408 | /** Total number of tileparts of the current tile*/ | ||
409 | int *cur_totnum_tp; | ||
410 | /** | ||
411 | locate the start position of the TLM marker | ||
412 | after encoding the tilepart, a jump (in j2k_write_sod) is done to the TLM marker to store the value of its length. | ||
413 | */ | ||
414 | int tlm_start; | ||
415 | /** Total num of tile parts in whole image = num tiles* num tileparts in each tile*/ | ||
416 | /** used in TLMmarker*/ | ||
417 | int totnum_tp; | ||
418 | /** | ||
419 | locate the position of the end of the tile in the codestream, | ||
420 | used to detect a truncated codestream (in j2k_read_sod) | ||
421 | */ | ||
422 | unsigned char *eot; | ||
423 | /** | ||
424 | locate the start position of the SOT marker of the current coded tile: | ||
425 | after encoding the tile, a jump (in j2k_write_sod) is done to the SOT marker to store the value of its length. | ||
426 | */ | ||
427 | int sot_start; | ||
428 | int sod_start; | ||
429 | /** | ||
430 | as the J2K-file is written in several parts during encoding, | ||
431 | it enables to make the right correction in position return by cio_tell | ||
432 | */ | ||
433 | int pos_correction; | ||
434 | /** array used to store the data of each tile */ | ||
435 | unsigned char **tile_data; | ||
436 | /** array used to store the length of each tile */ | ||
437 | int *tile_len; | ||
438 | /** | ||
439 | decompression only : | ||
440 | store decoding parameters common to all tiles (information like COD, COC in main header) | ||
441 | */ | ||
442 | opj_tcp_t *default_tcp; | ||
443 | /** pointer to the encoded / decoded image */ | ||
444 | opj_image_t *image; | ||
445 | /** pointer to the coding parameters */ | ||
446 | opj_cp_t *cp; | ||
447 | /** helper used to write the index file */ | ||
448 | opj_image_info_t *image_info; | ||
449 | /** pointer to the byte i/o stream */ | ||
450 | opj_cio_t *cio; | ||
451 | } opj_j2k_t; | ||
452 | |||
453 | /** @name Exported functions */ | ||
454 | /*@{*/ | ||
455 | /* ----------------------------------------------------------------------- */ | ||
456 | /** | ||
457 | Creates a J2K decompression structure | ||
458 | @param cinfo Codec context info | ||
459 | @return Returns a handle to a J2K decompressor if successful, returns NULL otherwise | ||
460 | */ | ||
461 | opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo); | ||
462 | /** | ||
463 | Destroy a J2K decompressor handle | ||
464 | @param j2k J2K decompressor handle to destroy | ||
465 | */ | ||
466 | void j2k_destroy_decompress(opj_j2k_t *j2k); | ||
467 | /** | ||
468 | Setup the decoder decoding parameters using user parameters. | ||
469 | Decoding parameters are returned in j2k->cp. | ||
470 | @param j2k J2K decompressor handle | ||
471 | @param parameters decompression parameters | ||
472 | */ | ||
473 | void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters); | ||
474 | /** | ||
475 | Decode an image from a JPEG-2000 codestream | ||
476 | @param j2k J2K decompressor handle | ||
477 | @param cio Input buffer stream | ||
478 | @return Returns a decoded image if successful, returns NULL otherwise | ||
479 | */ | ||
480 | opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio); | ||
481 | /** | ||
482 | Decode an image form a JPT-stream (JPEG 2000, JPIP) | ||
483 | @param j2k J2K decompressor handle | ||
484 | @param cio Input buffer stream | ||
485 | @return Returns a decoded image if successful, returns NULL otherwise | ||
486 | */ | ||
487 | opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio); | ||
488 | /** | ||
489 | Creates a J2K compression structure | ||
490 | @param cinfo Codec context info | ||
491 | @return Returns a handle to a J2K compressor if successful, returns NULL otherwise | ||
492 | */ | ||
493 | opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo); | ||
494 | /** | ||
495 | Destroy a J2K compressor handle | ||
496 | @param j2k J2K compressor handle to destroy | ||
497 | */ | ||
498 | void j2k_destroy_compress(opj_j2k_t *j2k); | ||
499 | /** | ||
500 | Setup the encoder parameters using the current image and using user parameters. | ||
501 | Coding parameters are returned in j2k->cp. | ||
502 | @param j2k J2K compressor handle | ||
503 | @param parameters compression parameters | ||
504 | @param image input filled image | ||
505 | */ | ||
506 | void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image); | ||
507 | /** | ||
508 | Converts an enum type progression order to string type | ||
509 | */ | ||
510 | char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order); | ||
511 | /** | ||
512 | Encode an image into a JPEG-2000 codestream | ||
513 | @param j2k J2K compressor handle | ||
514 | @param cio Output buffer stream | ||
515 | @param image Image to encode | ||
516 | @param index Name of the index file if required, NULL otherwise | ||
517 | @return Returns true if successful, returns false otherwise | ||
518 | */ | ||
519 | bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index); | ||
520 | /* ----------------------------------------------------------------------- */ | ||
521 | /*@}*/ | ||
522 | |||
523 | /*@}*/ | ||
524 | |||
525 | #endif /* __J2K_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/j2k_lib.c b/libraries/openjpeg-libsl/libopenjpeg/j2k_lib.c new file mode 100644 index 0000000..69444a7 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/j2k_lib.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #ifdef WIN32 | ||
28 | #include <windows.h> | ||
29 | #else | ||
30 | #include <sys/time.h> | ||
31 | #include <sys/resource.h> | ||
32 | #include <sys/times.h> | ||
33 | #endif /* WIN32 */ | ||
34 | #include "opj_includes.h" | ||
35 | |||
36 | double opj_clock() { | ||
37 | #ifdef WIN32 | ||
38 | /* WIN32: use QueryPerformance (very accurate) */ | ||
39 | LARGE_INTEGER freq , t ; | ||
40 | /* freq is the clock speed of the CPU */ | ||
41 | QueryPerformanceFrequency(&freq) ; | ||
42 | /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ | ||
43 | /* t is the high resolution performance counter (see MSDN) */ | ||
44 | QueryPerformanceCounter ( & t ) ; | ||
45 | return ( t.QuadPart /(double) freq.QuadPart ) ; | ||
46 | #else | ||
47 | /* Unix or Linux: use resource usage */ | ||
48 | struct rusage t; | ||
49 | double procTime; | ||
50 | /* (1) Get the rusage data structure at this moment (man getrusage) */ | ||
51 | getrusage(0,&t); | ||
52 | /* (2) What is the elapsed time ? - CPU time = User time + System time */ | ||
53 | /* (2a) Get the seconds */ | ||
54 | procTime = t.ru_utime.tv_sec + t.ru_stime.tv_sec; | ||
55 | /* (2b) More precisely! Get the microseconds part ! */ | ||
56 | return ( procTime + (t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ; | ||
57 | #endif | ||
58 | } | ||
59 | |||
60 | void* opj_malloc( size_t size ) { | ||
61 | void *memblock = malloc(size); | ||
62 | if(memblock) { | ||
63 | memset(memblock, 0, size); | ||
64 | } | ||
65 | return memblock; | ||
66 | } | ||
67 | |||
68 | void* opj_realloc( void *memblock, size_t size ) { | ||
69 | return realloc(memblock, size); | ||
70 | } | ||
71 | |||
72 | void opj_free( void *memblock ) { | ||
73 | free(memblock); | ||
74 | } | ||
75 | |||
76 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/j2k_lib.h b/libraries/openjpeg-libsl/libopenjpeg/j2k_lib.h new file mode 100644 index 0000000..3d1b53c --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/j2k_lib.h | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | #ifndef __J2K_LIB_H | ||
27 | #define __J2K_LIB_H | ||
28 | /** | ||
29 | @file j2k_lib.h | ||
30 | @brief Internal functions | ||
31 | |||
32 | The functions in J2K_LIB.C are internal utilities mainly used for memory management. | ||
33 | */ | ||
34 | |||
35 | /** @defgroup MISC MISC - Miscellaneous internal functions */ | ||
36 | /*@{*/ | ||
37 | |||
38 | /** @name Exported functions */ | ||
39 | /*@{*/ | ||
40 | /* ----------------------------------------------------------------------- */ | ||
41 | |||
42 | /** | ||
43 | Difference in successive opj_clock() calls tells you the elapsed time | ||
44 | @return Returns time in seconds | ||
45 | */ | ||
46 | double opj_clock(); | ||
47 | |||
48 | /** | ||
49 | Allocate a memory block with elements initialized to 0 | ||
50 | @param size Bytes to allocate | ||
51 | @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available | ||
52 | */ | ||
53 | void* opj_malloc( size_t size ); | ||
54 | |||
55 | /** | ||
56 | Reallocate memory blocks. | ||
57 | @param memblock Pointer to previously allocated memory block | ||
58 | @param size New size in bytes | ||
59 | @return Returns a void pointer to the reallocated (and possibly moved) memory block | ||
60 | */ | ||
61 | void* opj_realloc( void *memblock, size_t size ); | ||
62 | |||
63 | /** | ||
64 | Deallocates or frees a memory block. | ||
65 | @param memblock Previously allocated memory block to be freed | ||
66 | */ | ||
67 | void opj_free( void *memblock ); | ||
68 | |||
69 | /* ----------------------------------------------------------------------- */ | ||
70 | /*@}*/ | ||
71 | |||
72 | /*@}*/ | ||
73 | |||
74 | #endif /* __J2K_LIB_H */ | ||
75 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/jp2.c b/libraries/openjpeg-libsl/libopenjpeg/jp2.c new file mode 100644 index 0000000..d102349 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/jp2.c | |||
@@ -0,0 +1,700 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */ | ||
35 | /*@{*/ | ||
36 | |||
37 | /** @name Local static functions */ | ||
38 | /*@{*/ | ||
39 | |||
40 | /** | ||
41 | Read box headers | ||
42 | @param cinfo Codec context info | ||
43 | @param cio Input stream | ||
44 | @param box | ||
45 | @return Returns true if successful, returns false otherwise | ||
46 | */ | ||
47 | static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box); | ||
48 | /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/ | ||
49 | /** | ||
50 | Read the IHDR box - Image Header box | ||
51 | @param jp2 JP2 handle | ||
52 | @param cio Input buffer stream | ||
53 | @return Returns true if successful, returns false otherwise | ||
54 | */ | ||
55 | static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio); | ||
56 | static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio); | ||
57 | static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio); | ||
58 | static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio); | ||
59 | static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio); | ||
60 | static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio); | ||
61 | /** | ||
62 | Write the FTYP box - File type box | ||
63 | @param jp2 JP2 handle | ||
64 | @param cio Output buffer stream | ||
65 | */ | ||
66 | static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio); | ||
67 | /** | ||
68 | Read the FTYP box - File type box | ||
69 | @param jp2 JP2 handle | ||
70 | @param cio Input buffer stream | ||
71 | @return Returns true if successful, returns false otherwise | ||
72 | */ | ||
73 | static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio); | ||
74 | static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index); | ||
75 | static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset); | ||
76 | static void jp2_write_jp(opj_cio_t *cio); | ||
77 | /** | ||
78 | Read the JP box - JPEG 2000 signature | ||
79 | @param jp2 JP2 handle | ||
80 | @param cio Input buffer stream | ||
81 | @return Returns true if successful, returns false otherwise | ||
82 | */ | ||
83 | static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio); | ||
84 | /** | ||
85 | Decode the structure of a JP2 file | ||
86 | @param jp2 JP2 handle | ||
87 | @param cio Input buffer stream | ||
88 | @return Returns true if successful, returns false otherwise | ||
89 | */ | ||
90 | static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio); | ||
91 | |||
92 | /*@}*/ | ||
93 | |||
94 | /*@}*/ | ||
95 | |||
96 | /* ----------------------------------------------------------------------- */ | ||
97 | |||
98 | static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) { | ||
99 | box->init_pos = cio_tell(cio); | ||
100 | box->length = cio_read(cio, 4); | ||
101 | box->type = cio_read(cio, 4); | ||
102 | if (box->length == 1) { | ||
103 | if (cio_read(cio, 4) != 0) { | ||
104 | opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n"); | ||
105 | return false; | ||
106 | } | ||
107 | box->length = cio_read(cio, 4); | ||
108 | if (box->length == 0) | ||
109 | box->length = cio_numbytesleft(cio) + 12; | ||
110 | } | ||
111 | else if (box->length == 0) { | ||
112 | box->length = cio_numbytesleft(cio) + 8; | ||
113 | } | ||
114 | |||
115 | return true; | ||
116 | } | ||
117 | |||
118 | #if 0 | ||
119 | static void jp2_write_url(opj_cio_t *cio, char *Idx_file) { | ||
120 | unsigned int i; | ||
121 | opj_jp2_box_t box; | ||
122 | |||
123 | box.init_pos = cio_tell(cio); | ||
124 | cio_skip(cio, 4); | ||
125 | cio_write(cio, JP2_URL, 4); /* DBTL */ | ||
126 | cio_write(cio, 0, 1); /* VERS */ | ||
127 | cio_write(cio, 0, 3); /* FLAG */ | ||
128 | |||
129 | if(Idx_file) { | ||
130 | for (i = 0; i < strlen(Idx_file); i++) { | ||
131 | cio_write(cio, Idx_file[i], 1); | ||
132 | } | ||
133 | } | ||
134 | |||
135 | box.length = cio_tell(cio) - box.init_pos; | ||
136 | cio_seek(cio, box.init_pos); | ||
137 | cio_write(cio, box.length, 4); /* L */ | ||
138 | cio_seek(cio, box.init_pos + box.length); | ||
139 | } | ||
140 | #endif | ||
141 | |||
142 | static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
143 | opj_jp2_box_t box; | ||
144 | |||
145 | opj_common_ptr cinfo = jp2->cinfo; | ||
146 | |||
147 | jp2_read_boxhdr(cinfo, cio, &box); | ||
148 | if (JP2_IHDR != box.type) { | ||
149 | opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n"); | ||
150 | return false; | ||
151 | } | ||
152 | |||
153 | jp2->h = cio_read(cio, 4); /* HEIGHT */ | ||
154 | jp2->w = cio_read(cio, 4); /* WIDTH */ | ||
155 | jp2->numcomps = cio_read(cio, 2); /* NC */ | ||
156 | jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t)); | ||
157 | |||
158 | jp2->bpc = cio_read(cio, 1); /* BPC */ | ||
159 | |||
160 | jp2->C = cio_read(cio, 1); /* C */ | ||
161 | jp2->UnkC = cio_read(cio, 1); /* UnkC */ | ||
162 | jp2->IPR = cio_read(cio, 1); /* IPR */ | ||
163 | |||
164 | if (cio_tell(cio) - box.init_pos != box.length) { | ||
165 | opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n"); | ||
166 | return false; | ||
167 | } | ||
168 | |||
169 | return true; | ||
170 | } | ||
171 | |||
172 | static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
173 | opj_jp2_box_t box; | ||
174 | |||
175 | box.init_pos = cio_tell(cio); | ||
176 | cio_skip(cio, 4); | ||
177 | cio_write(cio, JP2_IHDR, 4); /* IHDR */ | ||
178 | |||
179 | cio_write(cio, jp2->h, 4); /* HEIGHT */ | ||
180 | cio_write(cio, jp2->w, 4); /* WIDTH */ | ||
181 | cio_write(cio, jp2->numcomps, 2); /* NC */ | ||
182 | |||
183 | cio_write(cio, jp2->bpc, 1); /* BPC */ | ||
184 | |||
185 | cio_write(cio, jp2->C, 1); /* C : Always 7 */ | ||
186 | cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */ | ||
187 | cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */ | ||
188 | |||
189 | box.length = cio_tell(cio) - box.init_pos; | ||
190 | cio_seek(cio, box.init_pos); | ||
191 | cio_write(cio, box.length, 4); /* L */ | ||
192 | cio_seek(cio, box.init_pos + box.length); | ||
193 | } | ||
194 | |||
195 | static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
196 | unsigned int i; | ||
197 | opj_jp2_box_t box; | ||
198 | |||
199 | box.init_pos = cio_tell(cio); | ||
200 | cio_skip(cio, 4); | ||
201 | cio_write(cio, JP2_BPCC, 4); /* BPCC */ | ||
202 | |||
203 | for (i = 0; i < jp2->numcomps; i++) { | ||
204 | cio_write(cio, jp2->comps[i].bpcc, 1); | ||
205 | } | ||
206 | |||
207 | box.length = cio_tell(cio) - box.init_pos; | ||
208 | cio_seek(cio, box.init_pos); | ||
209 | cio_write(cio, box.length, 4); /* L */ | ||
210 | cio_seek(cio, box.init_pos + box.length); | ||
211 | } | ||
212 | |||
213 | |||
214 | static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
215 | unsigned int i; | ||
216 | opj_jp2_box_t box; | ||
217 | |||
218 | opj_common_ptr cinfo = jp2->cinfo; | ||
219 | |||
220 | jp2_read_boxhdr(cinfo, cio, &box); | ||
221 | if (JP2_BPCC != box.type) { | ||
222 | opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n"); | ||
223 | return false; | ||
224 | } | ||
225 | |||
226 | for (i = 0; i < jp2->numcomps; i++) { | ||
227 | jp2->comps[i].bpcc = cio_read(cio, 1); | ||
228 | } | ||
229 | |||
230 | if (cio_tell(cio) - box.init_pos != box.length) { | ||
231 | opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n"); | ||
232 | return false; | ||
233 | } | ||
234 | |||
235 | return true; | ||
236 | } | ||
237 | |||
238 | static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
239 | opj_jp2_box_t box; | ||
240 | |||
241 | box.init_pos = cio_tell(cio); | ||
242 | cio_skip(cio, 4); | ||
243 | cio_write(cio, JP2_COLR, 4); /* COLR */ | ||
244 | |||
245 | cio_write(cio, jp2->meth, 1); /* METH */ | ||
246 | cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */ | ||
247 | cio_write(cio, jp2->approx, 1); /* APPROX */ | ||
248 | |||
249 | if (jp2->meth == 1) { | ||
250 | cio_write(cio, jp2->enumcs, 4); /* EnumCS */ | ||
251 | } else { | ||
252 | cio_write(cio, 0, 1); /* PROFILE (??) */ | ||
253 | } | ||
254 | |||
255 | box.length = cio_tell(cio) - box.init_pos; | ||
256 | cio_seek(cio, box.init_pos); | ||
257 | cio_write(cio, box.length, 4); /* L */ | ||
258 | cio_seek(cio, box.init_pos + box.length); | ||
259 | } | ||
260 | |||
261 | static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
262 | opj_jp2_box_t box; | ||
263 | int skip_len; | ||
264 | |||
265 | opj_common_ptr cinfo = jp2->cinfo; | ||
266 | |||
267 | jp2_read_boxhdr(cinfo, cio, &box); | ||
268 | do { | ||
269 | if (JP2_COLR != box.type) { | ||
270 | cio_skip(cio, box.length - 8); | ||
271 | jp2_read_boxhdr(cinfo, cio, &box); | ||
272 | } | ||
273 | } while(JP2_COLR != box.type); | ||
274 | |||
275 | jp2->meth = cio_read(cio, 1); /* METH */ | ||
276 | jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */ | ||
277 | jp2->approx = cio_read(cio, 1); /* APPROX */ | ||
278 | |||
279 | if (jp2->meth == 1) { | ||
280 | jp2->enumcs = cio_read(cio, 4); /* EnumCS */ | ||
281 | } else { | ||
282 | /* skip PROFILE */ | ||
283 | skip_len = box.init_pos + box.length - cio_tell(cio); | ||
284 | if (skip_len < 0) { | ||
285 | opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n"); | ||
286 | return false; | ||
287 | } | ||
288 | cio_skip(cio, box.init_pos + box.length - cio_tell(cio)); | ||
289 | } | ||
290 | |||
291 | if (cio_tell(cio) - box.init_pos != box.length) { | ||
292 | opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n"); | ||
293 | return false; | ||
294 | } | ||
295 | return true; | ||
296 | } | ||
297 | |||
298 | void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
299 | opj_jp2_box_t box; | ||
300 | |||
301 | box.init_pos = cio_tell(cio); | ||
302 | cio_skip(cio, 4); | ||
303 | cio_write(cio, JP2_JP2H, 4); /* JP2H */ | ||
304 | |||
305 | jp2_write_ihdr(jp2, cio); | ||
306 | |||
307 | if (jp2->bpc == 255) { | ||
308 | jp2_write_bpcc(jp2, cio); | ||
309 | } | ||
310 | jp2_write_colr(jp2, cio); | ||
311 | |||
312 | box.length = cio_tell(cio) - box.init_pos; | ||
313 | cio_seek(cio, box.init_pos); | ||
314 | cio_write(cio, box.length, 4); /* L */ | ||
315 | cio_seek(cio, box.init_pos + box.length); | ||
316 | } | ||
317 | |||
318 | bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
319 | opj_jp2_box_t box; | ||
320 | int skip_len; | ||
321 | |||
322 | opj_common_ptr cinfo = jp2->cinfo; | ||
323 | |||
324 | jp2_read_boxhdr(cinfo, cio, &box); | ||
325 | do { | ||
326 | if (JP2_JP2H != box.type) { | ||
327 | if (box.type == JP2_JP2C) { | ||
328 | opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n"); | ||
329 | return false; | ||
330 | } | ||
331 | cio_skip(cio, box.length - 8); | ||
332 | jp2_read_boxhdr(cinfo, cio, &box); | ||
333 | } | ||
334 | } while(JP2_JP2H != box.type); | ||
335 | |||
336 | if (!jp2_read_ihdr(jp2, cio)) | ||
337 | return false; | ||
338 | |||
339 | if (jp2->bpc == 255) { | ||
340 | if (!jp2_read_bpcc(jp2, cio)) | ||
341 | return false; | ||
342 | } | ||
343 | if (!jp2_read_colr(jp2, cio)) | ||
344 | return false; | ||
345 | |||
346 | skip_len = box.init_pos + box.length - cio_tell(cio); | ||
347 | if (skip_len < 0) { | ||
348 | opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n"); | ||
349 | return false; | ||
350 | } | ||
351 | cio_skip(cio, box.init_pos + box.length - cio_tell(cio)); | ||
352 | |||
353 | return true; | ||
354 | } | ||
355 | |||
356 | static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
357 | unsigned int i; | ||
358 | opj_jp2_box_t box; | ||
359 | |||
360 | box.init_pos = cio_tell(cio); | ||
361 | cio_skip(cio, 4); | ||
362 | cio_write(cio, JP2_FTYP, 4); /* FTYP */ | ||
363 | |||
364 | cio_write(cio, jp2->brand, 4); /* BR */ | ||
365 | cio_write(cio, jp2->minversion, 4); /* MinV */ | ||
366 | |||
367 | for (i = 0; i < jp2->numcl; i++) { | ||
368 | cio_write(cio, jp2->cl[i], 4); /* CL */ | ||
369 | } | ||
370 | |||
371 | box.length = cio_tell(cio) - box.init_pos; | ||
372 | cio_seek(cio, box.init_pos); | ||
373 | cio_write(cio, box.length, 4); /* L */ | ||
374 | cio_seek(cio, box.init_pos + box.length); | ||
375 | } | ||
376 | |||
377 | static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
378 | int i; | ||
379 | opj_jp2_box_t box; | ||
380 | |||
381 | opj_common_ptr cinfo = jp2->cinfo; | ||
382 | |||
383 | jp2_read_boxhdr(cinfo, cio, &box); | ||
384 | |||
385 | if (JP2_FTYP != box.type) { | ||
386 | opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n"); | ||
387 | return false; | ||
388 | } | ||
389 | |||
390 | jp2->brand = cio_read(cio, 4); /* BR */ | ||
391 | jp2->minversion = cio_read(cio, 4); /* MinV */ | ||
392 | jp2->numcl = (box.length - 16) / 4; | ||
393 | jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int)); | ||
394 | |||
395 | for (i = 0; i < (int)jp2->numcl; i++) { | ||
396 | jp2->cl[i] = cio_read(cio, 4); /* CLi */ | ||
397 | } | ||
398 | |||
399 | if (cio_tell(cio) - box.init_pos != box.length) { | ||
400 | opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n"); | ||
401 | return false; | ||
402 | } | ||
403 | |||
404 | return true; | ||
405 | } | ||
406 | |||
407 | static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) { | ||
408 | unsigned int j2k_codestream_offset, j2k_codestream_length; | ||
409 | opj_jp2_box_t box; | ||
410 | |||
411 | opj_j2k_t *j2k = jp2->j2k; | ||
412 | |||
413 | box.init_pos = cio_tell(cio); | ||
414 | cio_skip(cio, 4); | ||
415 | cio_write(cio, JP2_JP2C, 4); /* JP2C */ | ||
416 | |||
417 | /* J2K encoding */ | ||
418 | j2k_codestream_offset = cio_tell(cio); | ||
419 | if(!j2k_encode(j2k, cio, image, index)) { | ||
420 | opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n"); | ||
421 | return 0; | ||
422 | } | ||
423 | j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset; | ||
424 | |||
425 | jp2->j2k_codestream_offset = j2k_codestream_offset; | ||
426 | jp2->j2k_codestream_length = j2k_codestream_length; | ||
427 | |||
428 | box.length = 8 + jp2->j2k_codestream_length; | ||
429 | cio_seek(cio, box.init_pos); | ||
430 | cio_write(cio, box.length, 4); /* L */ | ||
431 | cio_seek(cio, box.init_pos + box.length); | ||
432 | |||
433 | return box.length; | ||
434 | } | ||
435 | |||
436 | static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) { | ||
437 | opj_jp2_box_t box; | ||
438 | |||
439 | opj_common_ptr cinfo = jp2->cinfo; | ||
440 | |||
441 | jp2_read_boxhdr(cinfo, cio, &box); | ||
442 | do { | ||
443 | if(JP2_JP2C != box.type) { | ||
444 | cio_skip(cio, box.length - 8); | ||
445 | jp2_read_boxhdr(cinfo, cio, &box); | ||
446 | } | ||
447 | } while(JP2_JP2C != box.type); | ||
448 | |||
449 | *j2k_codestream_offset = cio_tell(cio); | ||
450 | *j2k_codestream_length = box.length - 8; | ||
451 | |||
452 | return true; | ||
453 | } | ||
454 | |||
455 | static void jp2_write_jp(opj_cio_t *cio) { | ||
456 | opj_jp2_box_t box; | ||
457 | |||
458 | box.init_pos = cio_tell(cio); | ||
459 | cio_skip(cio, 4); | ||
460 | cio_write(cio, JP2_JP, 4); /* JP2 signature */ | ||
461 | cio_write(cio, 0x0d0a870a, 4); | ||
462 | |||
463 | box.length = cio_tell(cio) - box.init_pos; | ||
464 | cio_seek(cio, box.init_pos); | ||
465 | cio_write(cio, box.length, 4); /* L */ | ||
466 | cio_seek(cio, box.init_pos + box.length); | ||
467 | } | ||
468 | |||
469 | static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
470 | opj_jp2_box_t box; | ||
471 | |||
472 | opj_common_ptr cinfo = jp2->cinfo; | ||
473 | |||
474 | jp2_read_boxhdr(cinfo, cio, &box); | ||
475 | if (JP2_JP != box.type) { | ||
476 | opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n"); | ||
477 | return false; | ||
478 | } | ||
479 | if (0x0d0a870a != cio_read(cio, 4)) { | ||
480 | opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n"); | ||
481 | return false; | ||
482 | } | ||
483 | if (cio_tell(cio) - box.init_pos != box.length) { | ||
484 | opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n"); | ||
485 | return false; | ||
486 | } | ||
487 | |||
488 | return true; | ||
489 | } | ||
490 | |||
491 | |||
492 | static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
493 | if (!jp2_read_jp(jp2, cio)) | ||
494 | return false; | ||
495 | if (!jp2_read_ftyp(jp2, cio)) | ||
496 | return false; | ||
497 | if (!jp2_read_jp2h(jp2, cio)) | ||
498 | return false; | ||
499 | if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset)) | ||
500 | return false; | ||
501 | |||
502 | return true; | ||
503 | } | ||
504 | |||
505 | /* ----------------------------------------------------------------------- */ | ||
506 | /* JP2 decoder interface */ | ||
507 | /* ----------------------------------------------------------------------- */ | ||
508 | |||
509 | opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) { | ||
510 | opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t)); | ||
511 | if(jp2) { | ||
512 | jp2->cinfo = cinfo; | ||
513 | /* create the J2K codec */ | ||
514 | jp2->j2k = j2k_create_decompress(cinfo); | ||
515 | if(jp2->j2k == NULL) { | ||
516 | jp2_destroy_decompress(jp2); | ||
517 | return NULL; | ||
518 | } | ||
519 | } | ||
520 | return jp2; | ||
521 | } | ||
522 | |||
523 | void jp2_destroy_decompress(opj_jp2_t *jp2) { | ||
524 | if(jp2) { | ||
525 | /* destroy the J2K codec */ | ||
526 | j2k_destroy_decompress(jp2->j2k); | ||
527 | |||
528 | if(jp2->comps) { | ||
529 | opj_free(jp2->comps); | ||
530 | } | ||
531 | if(jp2->cl) { | ||
532 | opj_free(jp2->cl); | ||
533 | } | ||
534 | opj_free(jp2); | ||
535 | } | ||
536 | } | ||
537 | |||
538 | void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) { | ||
539 | /* setup the J2K codec */ | ||
540 | j2k_setup_decoder(jp2->j2k, parameters); | ||
541 | /* further JP2 initializations go here */ | ||
542 | } | ||
543 | |||
544 | opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) { | ||
545 | opj_common_ptr cinfo; | ||
546 | opj_image_t *image = NULL; | ||
547 | |||
548 | if(!jp2 || !cio) { | ||
549 | return NULL; | ||
550 | } | ||
551 | |||
552 | cinfo = jp2->cinfo; | ||
553 | |||
554 | /* JP2 decoding */ | ||
555 | if(!jp2_read_struct(jp2, cio)) { | ||
556 | opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n"); | ||
557 | return NULL; | ||
558 | } | ||
559 | |||
560 | /* J2K decoding */ | ||
561 | image = j2k_decode(jp2->j2k, cio); | ||
562 | if(!image) { | ||
563 | opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n"); | ||
564 | } | ||
565 | |||
566 | return image; | ||
567 | } | ||
568 | |||
569 | /* ----------------------------------------------------------------------- */ | ||
570 | /* JP2 encoder interface */ | ||
571 | /* ----------------------------------------------------------------------- */ | ||
572 | |||
573 | opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) { | ||
574 | opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t)); | ||
575 | if(jp2) { | ||
576 | jp2->cinfo = cinfo; | ||
577 | /* create the J2K codec */ | ||
578 | jp2->j2k = j2k_create_compress(cinfo); | ||
579 | if(jp2->j2k == NULL) { | ||
580 | jp2_destroy_compress(jp2); | ||
581 | return NULL; | ||
582 | } | ||
583 | } | ||
584 | return jp2; | ||
585 | } | ||
586 | |||
587 | void jp2_destroy_compress(opj_jp2_t *jp2) { | ||
588 | if(jp2) { | ||
589 | /* destroy the J2K codec */ | ||
590 | j2k_destroy_compress(jp2->j2k); | ||
591 | |||
592 | if(jp2->comps) { | ||
593 | opj_free(jp2->comps); | ||
594 | } | ||
595 | if(jp2->cl) { | ||
596 | opj_free(jp2->cl); | ||
597 | } | ||
598 | opj_free(jp2); | ||
599 | } | ||
600 | } | ||
601 | |||
602 | void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) { | ||
603 | int i; | ||
604 | int depth_0, sign; | ||
605 | |||
606 | if(!jp2 || !parameters || !image) | ||
607 | return; | ||
608 | |||
609 | /* setup the J2K codec */ | ||
610 | /* ------------------- */ | ||
611 | |||
612 | /* Check if number of components respects standard */ | ||
613 | if (image->numcomps < 1 || image->numcomps > 16384) { | ||
614 | opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n"); | ||
615 | return; | ||
616 | } | ||
617 | |||
618 | j2k_setup_encoder(jp2->j2k, parameters, image); | ||
619 | |||
620 | /* setup the JP2 codec */ | ||
621 | /* ------------------- */ | ||
622 | |||
623 | /* Profile box */ | ||
624 | |||
625 | jp2->brand = JP2_JP2; /* BR */ | ||
626 | jp2->minversion = 0; /* MinV */ | ||
627 | jp2->numcl = 1; | ||
628 | jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int)); | ||
629 | jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */ | ||
630 | |||
631 | /* Image Header box */ | ||
632 | |||
633 | jp2->numcomps = image->numcomps; /* NC */ | ||
634 | jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t)); | ||
635 | jp2->h = image->y1 - image->y0; /* HEIGHT */ | ||
636 | jp2->w = image->x1 - image->x0; /* WIDTH */ | ||
637 | /* BPC */ | ||
638 | depth_0 = image->comps[0].prec - 1; | ||
639 | sign = image->comps[0].sgnd; | ||
640 | jp2->bpc = depth_0 + (sign << 7); | ||
641 | for (i = 1; i < image->numcomps; i++) { | ||
642 | int depth = image->comps[i].prec - 1; | ||
643 | sign = image->comps[i].sgnd; | ||
644 | if (depth_0 != depth) | ||
645 | jp2->bpc = 255; | ||
646 | } | ||
647 | jp2->C = 7; /* C : Always 7 */ | ||
648 | jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */ | ||
649 | jp2->IPR = 0; /* IPR, no intellectual property */ | ||
650 | |||
651 | /* BitsPerComponent box */ | ||
652 | |||
653 | for (i = 0; i < image->numcomps; i++) { | ||
654 | jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7); | ||
655 | } | ||
656 | |||
657 | /* Colour Specification box */ | ||
658 | |||
659 | if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) { | ||
660 | jp2->meth = 1; /* METH: Enumerated colourspace */ | ||
661 | } else { | ||
662 | jp2->meth = 2; /* METH: Restricted ICC profile */ | ||
663 | } | ||
664 | if (jp2->meth == 1) { | ||
665 | if (image->color_space == 1) | ||
666 | jp2->enumcs = 16; /* sRGB as defined by IEC 61966–2–1 */ | ||
667 | else if (image->color_space == 2) | ||
668 | jp2->enumcs = 17; /* greyscale */ | ||
669 | else if (image->color_space == 3) | ||
670 | jp2->enumcs = 18; /* YUV */ | ||
671 | } else { | ||
672 | jp2->enumcs = 0; /* PROFILE (??) */ | ||
673 | } | ||
674 | jp2->precedence = 0; /* PRECEDENCE */ | ||
675 | jp2->approx = 0; /* APPROX */ | ||
676 | |||
677 | } | ||
678 | |||
679 | bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) { | ||
680 | |||
681 | /* JP2 encoding */ | ||
682 | |||
683 | /* JPEG 2000 Signature box */ | ||
684 | jp2_write_jp(cio); | ||
685 | /* File Type box */ | ||
686 | jp2_write_ftyp(jp2, cio); | ||
687 | /* JP2 Header box */ | ||
688 | jp2_write_jp2h(jp2, cio); | ||
689 | |||
690 | /* J2K encoding */ | ||
691 | |||
692 | if(!jp2_write_jp2c(jp2, cio, image, index)) { | ||
693 | opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n"); | ||
694 | return false; | ||
695 | } | ||
696 | |||
697 | return true; | ||
698 | } | ||
699 | |||
700 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/jp2.h b/libraries/openjpeg-libsl/libopenjpeg/jp2.h new file mode 100644 index 0000000..61fc1e4 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/jp2.h | |||
@@ -0,0 +1,176 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
5 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | #ifndef __JP2_H | ||
30 | #define __JP2_H | ||
31 | /** | ||
32 | @file jp2.h | ||
33 | @brief The JPEG-2000 file format Reader/Writer (JP2) | ||
34 | |||
35 | */ | ||
36 | |||
37 | /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */ | ||
38 | /*@{*/ | ||
39 | |||
40 | #define JPIP_JPIP 0x6a706970 | ||
41 | |||
42 | #define JP2_JP 0x6a502020 /**< JPEG 2000 signature box */ | ||
43 | #define JP2_FTYP 0x66747970 /**< File type box */ | ||
44 | #define JP2_JP2H 0x6a703268 /**< JP2 header box */ | ||
45 | #define JP2_IHDR 0x69686472 /**< Image header box */ | ||
46 | #define JP2_COLR 0x636f6c72 /**< Colour specification box */ | ||
47 | #define JP2_JP2C 0x6a703263 /**< Contiguous codestream box */ | ||
48 | #define JP2_URL 0x75726c20 /**< URL box */ | ||
49 | #define JP2_DBTL 0x6474626c /**< ??? */ | ||
50 | #define JP2_BPCC 0x62706363 /**< Bits per component box */ | ||
51 | #define JP2_JP2 0x6a703220 /**< File type fields */ | ||
52 | |||
53 | /* ----------------------------------------------------------------------- */ | ||
54 | |||
55 | /** | ||
56 | JP2 component | ||
57 | */ | ||
58 | typedef struct opj_jp2_comps { | ||
59 | int depth; | ||
60 | int sgnd; | ||
61 | int bpcc; | ||
62 | } opj_jp2_comps_t; | ||
63 | |||
64 | /** | ||
65 | JPEG-2000 file format reader/writer | ||
66 | */ | ||
67 | typedef struct opj_jp2 { | ||
68 | /** codec context */ | ||
69 | opj_common_ptr cinfo; | ||
70 | /** handle to the J2K codec */ | ||
71 | opj_j2k_t *j2k; | ||
72 | unsigned int w; | ||
73 | unsigned int h; | ||
74 | unsigned int numcomps; | ||
75 | unsigned int bpc; | ||
76 | unsigned int C; | ||
77 | unsigned int UnkC; | ||
78 | unsigned int IPR; | ||
79 | unsigned int meth; | ||
80 | unsigned int approx; | ||
81 | unsigned int enumcs; | ||
82 | unsigned int precedence; | ||
83 | unsigned int brand; | ||
84 | unsigned int minversion; | ||
85 | unsigned int numcl; | ||
86 | unsigned int *cl; | ||
87 | opj_jp2_comps_t *comps; | ||
88 | unsigned int j2k_codestream_offset; | ||
89 | unsigned int j2k_codestream_length; | ||
90 | } opj_jp2_t; | ||
91 | |||
92 | /** | ||
93 | JP2 Box | ||
94 | */ | ||
95 | typedef struct opj_jp2_box { | ||
96 | int length; | ||
97 | int type; | ||
98 | int init_pos; | ||
99 | } opj_jp2_box_t; | ||
100 | |||
101 | /** @name Exported functions */ | ||
102 | /*@{*/ | ||
103 | /* ----------------------------------------------------------------------- */ | ||
104 | /** | ||
105 | Write the JP2H box - JP2 Header box (used in MJ2) | ||
106 | @param jp2 JP2 handle | ||
107 | @param cio Output buffer stream | ||
108 | */ | ||
109 | void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio); | ||
110 | /** | ||
111 | Read the JP2H box - JP2 Header box (used in MJ2) | ||
112 | @param jp2 JP2 handle | ||
113 | @param cio Input buffer stream | ||
114 | @return Returns true if successful, returns false otherwise | ||
115 | */ | ||
116 | bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio); | ||
117 | /** | ||
118 | Creates a JP2 decompression structure | ||
119 | @param cinfo Codec context info | ||
120 | @return Returns a handle to a JP2 decompressor if successful, returns NULL otherwise | ||
121 | */ | ||
122 | opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo); | ||
123 | /** | ||
124 | Destroy a JP2 decompressor handle | ||
125 | @param jp2 JP2 decompressor handle to destroy | ||
126 | */ | ||
127 | void jp2_destroy_decompress(opj_jp2_t *jp2); | ||
128 | /** | ||
129 | Setup the decoder decoding parameters using user parameters. | ||
130 | Decoding parameters are returned in jp2->j2k->cp. | ||
131 | @param jp2 JP2 decompressor handle | ||
132 | @param parameters decompression parameters | ||
133 | */ | ||
134 | void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters); | ||
135 | /** | ||
136 | Decode an image from a JPEG-2000 file stream | ||
137 | @param jp2 JP2 decompressor handle | ||
138 | @param cio Input buffer stream | ||
139 | @return Returns a decoded image if successful, returns NULL otherwise | ||
140 | */ | ||
141 | opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio); | ||
142 | /** | ||
143 | Creates a JP2 compression structure | ||
144 | @param cinfo Codec context info | ||
145 | @return Returns a handle to a JP2 compressor if successful, returns NULL otherwise | ||
146 | */ | ||
147 | opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo); | ||
148 | /** | ||
149 | Destroy a JP2 compressor handle | ||
150 | @param jp2 JP2 compressor handle to destroy | ||
151 | */ | ||
152 | void jp2_destroy_compress(opj_jp2_t *jp2); | ||
153 | /** | ||
154 | Setup the encoder parameters using the current image and using user parameters. | ||
155 | Coding parameters are returned in jp2->j2k->cp. | ||
156 | @param jp2 JP2 compressor handle | ||
157 | @param parameters compression parameters | ||
158 | @param image input filled image | ||
159 | */ | ||
160 | void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image); | ||
161 | /** | ||
162 | Encode an image into a JPEG-2000 file stream | ||
163 | @param jp2 JP2 compressor handle | ||
164 | @param cio Output buffer stream | ||
165 | @param image Image to encode | ||
166 | @param index Name of the index file if required, NULL otherwise | ||
167 | @return Returns true if successful, returns false otherwise | ||
168 | */ | ||
169 | bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index); | ||
170 | /* ----------------------------------------------------------------------- */ | ||
171 | /*@}*/ | ||
172 | |||
173 | /*@}*/ | ||
174 | |||
175 | #endif /* __JP2_H */ | ||
176 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/jpt.c b/libraries/openjpeg-libsl/libopenjpeg/jpt.c new file mode 100644 index 0000000..a2566ea --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/jpt.c | |||
@@ -0,0 +1,155 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
5 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #include "opj_includes.h" | ||
31 | |||
32 | /* | ||
33 | * Read the information contains in VBAS [JPP/JPT stream message header] | ||
34 | * Store information (7 bits) in value | ||
35 | * | ||
36 | */ | ||
37 | unsigned int jpt_read_VBAS_info(opj_cio_t *cio, unsigned int value) { | ||
38 | unsigned char elmt; | ||
39 | |||
40 | elmt = cio_read(cio, 1); | ||
41 | while ((elmt >> 7) == 1) { | ||
42 | value = (value << 7); | ||
43 | value |= (elmt & 0x7f); | ||
44 | elmt = cio_read(cio, 1); | ||
45 | } | ||
46 | value = (value << 7); | ||
47 | value |= (elmt & 0x7f); | ||
48 | |||
49 | return value; | ||
50 | } | ||
51 | |||
52 | /* | ||
53 | * Initialize the value of the message header structure | ||
54 | * | ||
55 | */ | ||
56 | void jpt_init_msg_header(opj_jpt_msg_header_t * header) { | ||
57 | header->Id = 0; /* In-class Identifier */ | ||
58 | header->last_byte = 0; /* Last byte information */ | ||
59 | header->Class_Id = 0; /* Class Identifier */ | ||
60 | header->CSn_Id = 0; /* CSn : index identifier */ | ||
61 | header->Msg_offset = 0; /* Message offset */ | ||
62 | header->Msg_length = 0; /* Message length */ | ||
63 | header->Layer_nb = 0; /* Auxiliary for JPP case */ | ||
64 | } | ||
65 | |||
66 | /* | ||
67 | * Re-initialize the value of the message header structure | ||
68 | * | ||
69 | * Only parameters always present in message header | ||
70 | * | ||
71 | */ | ||
72 | void jpt_reinit_msg_header(opj_jpt_msg_header_t * header) { | ||
73 | header->Id = 0; /* In-class Identifier */ | ||
74 | header->last_byte = 0; /* Last byte information */ | ||
75 | header->Msg_offset = 0; /* Message offset */ | ||
76 | header->Msg_length = 0; /* Message length */ | ||
77 | } | ||
78 | |||
79 | /* | ||
80 | * Read the message header for a JPP/JPT - stream | ||
81 | * | ||
82 | */ | ||
83 | void jpt_read_msg_header(opj_common_ptr cinfo, opj_cio_t *cio, opj_jpt_msg_header_t *header) { | ||
84 | unsigned char elmt, Class = 0, CSn = 0; | ||
85 | jpt_reinit_msg_header(header); | ||
86 | |||
87 | /* ------------- */ | ||
88 | /* VBAS : Bin-ID */ | ||
89 | /* ------------- */ | ||
90 | elmt = cio_read(cio, 1); | ||
91 | |||
92 | /* See for Class and CSn */ | ||
93 | switch ((elmt >> 5) & 0x03) { | ||
94 | case 0: | ||
95 | opj_event_msg(cinfo, EVT_ERROR, "Forbidden value encounter in message header !!\n"); | ||
96 | break; | ||
97 | case 1: | ||
98 | Class = 0; | ||
99 | CSn = 0; | ||
100 | break; | ||
101 | case 2: | ||
102 | Class = 1; | ||
103 | CSn = 0; | ||
104 | break; | ||
105 | case 3: | ||
106 | Class = 1; | ||
107 | CSn = 1; | ||
108 | break; | ||
109 | default: | ||
110 | break; | ||
111 | } | ||
112 | |||
113 | /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */ | ||
114 | if (((elmt >> 4) & 0x01) == 1) | ||
115 | header->last_byte = 1; | ||
116 | |||
117 | /* In-class identifier */ | ||
118 | header->Id |= (elmt & 0x0f); | ||
119 | if ((elmt >> 7) == 1) | ||
120 | header->Id = jpt_read_VBAS_info(cio, header->Id); | ||
121 | |||
122 | /* ------------ */ | ||
123 | /* VBAS : Class */ | ||
124 | /* ------------ */ | ||
125 | if (Class == 1) { | ||
126 | header->Class_Id = 0; | ||
127 | header->Class_Id = jpt_read_VBAS_info(cio, header->Class_Id); | ||
128 | } | ||
129 | |||
130 | /* ---------- */ | ||
131 | /* VBAS : CSn */ | ||
132 | /* ---------- */ | ||
133 | if (CSn == 1) { | ||
134 | header->CSn_Id = 0; | ||
135 | header->CSn_Id = jpt_read_VBAS_info(cio, header->CSn_Id); | ||
136 | } | ||
137 | |||
138 | /* ----------------- */ | ||
139 | /* VBAS : Msg_offset */ | ||
140 | /* ----------------- */ | ||
141 | header->Msg_offset = jpt_read_VBAS_info(cio, header->Msg_offset); | ||
142 | |||
143 | /* ----------------- */ | ||
144 | /* VBAS : Msg_length */ | ||
145 | /* ----------------- */ | ||
146 | header->Msg_length = jpt_read_VBAS_info(cio, header->Msg_length); | ||
147 | |||
148 | /* ---------- */ | ||
149 | /* VBAS : Aux */ | ||
150 | /* ---------- */ | ||
151 | if ((header->Class_Id & 0x01) == 1) { | ||
152 | header->Layer_nb = 0; | ||
153 | header->Layer_nb = jpt_read_VBAS_info(cio, header->Layer_nb); | ||
154 | } | ||
155 | } | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/jpt.h b/libraries/openjpeg-libsl/libopenjpeg/jpt.h new file mode 100644 index 0000000..eb01f98 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/jpt.h | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
5 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #ifndef __JPT_H | ||
31 | #define __JPT_H | ||
32 | /** | ||
33 | @file jpt.h | ||
34 | @brief JPT-stream reader (JPEG 2000, JPIP) | ||
35 | |||
36 | JPT-stream functions are implemented in J2K.C. | ||
37 | */ | ||
38 | |||
39 | /** | ||
40 | Message Header JPT stream structure | ||
41 | */ | ||
42 | typedef struct opj_jpt_msg_header { | ||
43 | /** In-class Identifier */ | ||
44 | unsigned int Id; | ||
45 | /** Last byte information */ | ||
46 | unsigned int last_byte; | ||
47 | /** Class Identifier */ | ||
48 | unsigned int Class_Id; | ||
49 | /** CSn : index identifier */ | ||
50 | unsigned int CSn_Id; | ||
51 | /** Message offset */ | ||
52 | unsigned int Msg_offset; | ||
53 | /** Message length */ | ||
54 | unsigned int Msg_length; | ||
55 | /** Auxiliary for JPP case */ | ||
56 | unsigned int Layer_nb; | ||
57 | } opj_jpt_msg_header_t; | ||
58 | |||
59 | /* ----------------------------------------------------------------------- */ | ||
60 | |||
61 | /** | ||
62 | Initialize the value of the message header structure | ||
63 | @param header Message header structure | ||
64 | */ | ||
65 | void jpt_init_msg_header(opj_jpt_msg_header_t * header); | ||
66 | |||
67 | /** | ||
68 | Read the message header for a JPP/JPT - stream | ||
69 | @param cinfo Codec context info | ||
70 | @param cio CIO handle | ||
71 | @param header Message header structure | ||
72 | */ | ||
73 | void jpt_read_msg_header(opj_common_ptr cinfo, opj_cio_t *cio, opj_jpt_msg_header_t *header); | ||
74 | |||
75 | #endif | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/mct.c b/libraries/openjpeg-libsl/libopenjpeg/mct.c new file mode 100644 index 0000000..fad9355 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/mct.c | |||
@@ -0,0 +1,132 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /* <summary> */ | ||
35 | /* This table contains the norms of the basis function of the reversible MCT. */ | ||
36 | /* </summary> */ | ||
37 | static const double mct_norms[3] = { 1.732, .8292, .8292 }; | ||
38 | |||
39 | /* <summary> */ | ||
40 | /* This table contains the norms of the basis function of the irreversible MCT. */ | ||
41 | /* </summary> */ | ||
42 | static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 }; | ||
43 | |||
44 | /* <summary> */ | ||
45 | /* Foward reversible MCT. */ | ||
46 | /* </summary> */ | ||
47 | void mct_encode(int *c0, int *c1, int *c2, int n) { | ||
48 | int i; | ||
49 | for (i = 0; i < n; i++) { | ||
50 | int r, g, b, y, u, v; | ||
51 | r = c0[i]; | ||
52 | g = c1[i]; | ||
53 | b = c2[i]; | ||
54 | y = (r + (g << 1) + b) >> 2; | ||
55 | u = b - g; | ||
56 | v = r - g; | ||
57 | c0[i] = y; | ||
58 | c1[i] = u; | ||
59 | c2[i] = v; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | /* <summary> */ | ||
64 | /* Inverse reversible MCT. */ | ||
65 | /* </summary> */ | ||
66 | void mct_decode(int *c0, int *c1, int *c2, int n) { | ||
67 | int i; | ||
68 | for (i = 0; i < n; i++) { | ||
69 | int y, u, v, r, g, b; | ||
70 | y = c0[i]; | ||
71 | u = c1[i]; | ||
72 | v = c2[i]; | ||
73 | g = y - ((u + v) >> 2); | ||
74 | r = v + g; | ||
75 | b = u + g; | ||
76 | c0[i] = r; | ||
77 | c1[i] = g; | ||
78 | c2[i] = b; | ||
79 | } | ||
80 | } | ||
81 | |||
82 | /* <summary> */ | ||
83 | /* Get norm of basis function of reversible MCT. */ | ||
84 | /* </summary> */ | ||
85 | double mct_getnorm(int compno) { | ||
86 | return mct_norms[compno]; | ||
87 | } | ||
88 | |||
89 | /* <summary> */ | ||
90 | /* Foward irreversible MCT. */ | ||
91 | /* </summary> */ | ||
92 | void mct_encode_real(int *c0, int *c1, int *c2, int n) { | ||
93 | int i; | ||
94 | for (i = 0; i < n; i++) { | ||
95 | int r, g, b, y, u, v; | ||
96 | r = c0[i]; | ||
97 | g = c1[i]; | ||
98 | b = c2[i]; | ||
99 | y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934); | ||
100 | u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096); | ||
101 | v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666); | ||
102 | c0[i] = y; | ||
103 | c1[i] = u; | ||
104 | c2[i] = v; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | /* <summary> */ | ||
109 | /* Inverse irreversible MCT. */ | ||
110 | /* </summary> */ | ||
111 | void mct_decode_real(int *c0, int *c1, int *c2, int n) { | ||
112 | int i; | ||
113 | for (i = 0; i < n; i++) { | ||
114 | int y, u, v, r, g, b; | ||
115 | y = c0[i]; | ||
116 | u = c1[i]; | ||
117 | v = c2[i]; | ||
118 | r = y + fix_mul(v, 11485); | ||
119 | g = y - fix_mul(u, 2819) - fix_mul(v, 5850); | ||
120 | b = y + fix_mul(u, 14516); | ||
121 | c0[i] = r; | ||
122 | c1[i] = g; | ||
123 | c2[i] = b; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | /* <summary> */ | ||
128 | /* Get norm of basis function of irreversible MCT. */ | ||
129 | /* </summary> */ | ||
130 | double mct_getnorm_real(int compno) { | ||
131 | return mct_norms_real[compno]; | ||
132 | } | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/mct.h b/libraries/openjpeg-libsl/libopenjpeg/mct.h new file mode 100644 index 0000000..7596187 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/mct.h | |||
@@ -0,0 +1,98 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __MCT_H | ||
33 | #define __MCT_H | ||
34 | /** | ||
35 | @file mct.h | ||
36 | @brief Implementation of a multi-component transforms (MCT) | ||
37 | |||
38 | The functions in MCT.C have for goal to realize reversible and irreversible multicomponent | ||
39 | transform. The functions in MCT.C are used by some function in TCD.C. | ||
40 | */ | ||
41 | |||
42 | /** @defgroup MCT MCT - Implementation of a multi-component transform */ | ||
43 | /*@{*/ | ||
44 | |||
45 | /** @name Exported functions */ | ||
46 | /*@{*/ | ||
47 | /* ----------------------------------------------------------------------- */ | ||
48 | /** | ||
49 | Apply a reversible multi-component transform to an image | ||
50 | @param c0 Samples for red component | ||
51 | @param c1 Samples for green component | ||
52 | @param c2 Samples blue component | ||
53 | @param n Number of samples for each component | ||
54 | */ | ||
55 | void mct_encode(int *c0, int *c1, int *c2, int n); | ||
56 | /** | ||
57 | Apply a reversible multi-component inverse transform to an image | ||
58 | @param c0 Samples for luminance component | ||
59 | @param c1 Samples for red chrominance component | ||
60 | @param c2 Samples for blue chrominance component | ||
61 | @param n Number of samples for each component | ||
62 | */ | ||
63 | void mct_decode(int *c0, int *c1, int *c2, int n); | ||
64 | /** | ||
65 | Get norm of the basis function used for the reversible multi-component transform | ||
66 | @param compno Number of the component (0->Y, 1->U, 2->V) | ||
67 | @return | ||
68 | */ | ||
69 | double mct_getnorm(int compno); | ||
70 | |||
71 | /** | ||
72 | Apply an irreversible multi-component transform to an image | ||
73 | @param c0 Samples for red component | ||
74 | @param c1 Samples for green component | ||
75 | @param c2 Samples blue component | ||
76 | @param n Number of samples for each component | ||
77 | */ | ||
78 | void mct_encode_real(int *c0, int *c1, int *c2, int n); | ||
79 | /** | ||
80 | Apply an irreversible multi-component inverse transform to an image | ||
81 | @param c0 Samples for luminance component | ||
82 | @param c1 Samples for red chrominance component | ||
83 | @param c2 Samples for blue chrominance component | ||
84 | @param n Number of samples for each component | ||
85 | */ | ||
86 | void mct_decode_real(int *c0, int *c1, int *c2, int n); | ||
87 | /** | ||
88 | Get norm of the basis function used for the irreversible multi-component transform | ||
89 | @param compno Number of the component (0->Y, 1->U, 2->V) | ||
90 | @return | ||
91 | */ | ||
92 | double mct_getnorm_real(int compno); | ||
93 | /* ----------------------------------------------------------------------- */ | ||
94 | /*@}*/ | ||
95 | |||
96 | /*@}*/ | ||
97 | |||
98 | #endif /* __MCT_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/mqc.c b/libraries/openjpeg-libsl/libopenjpeg/mqc.c new file mode 100644 index 0000000..e6dd007 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/mqc.c | |||
@@ -0,0 +1,542 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /** @defgroup MQC MQC - Implementation of an MQ-Coder */ | ||
35 | /*@{*/ | ||
36 | |||
37 | /** @name Local static functions */ | ||
38 | /*@{*/ | ||
39 | |||
40 | /** | ||
41 | Output a byte, doing bit-stuffing if necessary. | ||
42 | After a 0xff byte, the next byte must be smaller than 0x90. | ||
43 | @param mqc MQC handle | ||
44 | */ | ||
45 | static void mqc_byteout(opj_mqc_t *mqc); | ||
46 | /** | ||
47 | Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000 | ||
48 | @param mqc MQC handle | ||
49 | */ | ||
50 | static void mqc_renorme(opj_mqc_t *mqc); | ||
51 | /** | ||
52 | Encode the most probable symbol | ||
53 | @param mqc MQC handle | ||
54 | */ | ||
55 | static void mqc_codemps(opj_mqc_t *mqc); | ||
56 | /** | ||
57 | Encode the most least symbol | ||
58 | @param mqc MQC handle | ||
59 | */ | ||
60 | static void mqc_codelps(opj_mqc_t *mqc); | ||
61 | /** | ||
62 | Fill mqc->c with 1's for flushing | ||
63 | @param mqc MQC handle | ||
64 | */ | ||
65 | static void mqc_setbits(opj_mqc_t *mqc); | ||
66 | /** | ||
67 | FIXME: documentation ??? | ||
68 | @param mqc MQC handle | ||
69 | @return | ||
70 | */ | ||
71 | static int mqc_mpsexchange(opj_mqc_t *mqc); | ||
72 | /** | ||
73 | FIXME: documentation ??? | ||
74 | @param mqc MQC handle | ||
75 | @return | ||
76 | */ | ||
77 | static int mqc_lpsexchange(opj_mqc_t *mqc); | ||
78 | /** | ||
79 | Input a byte | ||
80 | @param mqc MQC handle | ||
81 | */ | ||
82 | static void mqc_bytein(opj_mqc_t *mqc); | ||
83 | /** | ||
84 | Renormalize mqc->a and mqc->c while decoding | ||
85 | @param mqc MQC handle | ||
86 | */ | ||
87 | static void mqc_renormd(opj_mqc_t *mqc); | ||
88 | |||
89 | /*@}*/ | ||
90 | |||
91 | /*@}*/ | ||
92 | |||
93 | /* <summary> */ | ||
94 | /* This array defines all the possible states for a context. */ | ||
95 | /* </summary> */ | ||
96 | static opj_mqc_state_t mqc_states[47 * 2] = { | ||
97 | {0x5601, 0, &mqc_states[2], &mqc_states[3]}, | ||
98 | {0x5601, 1, &mqc_states[3], &mqc_states[2]}, | ||
99 | {0x3401, 0, &mqc_states[4], &mqc_states[12]}, | ||
100 | {0x3401, 1, &mqc_states[5], &mqc_states[13]}, | ||
101 | {0x1801, 0, &mqc_states[6], &mqc_states[18]}, | ||
102 | {0x1801, 1, &mqc_states[7], &mqc_states[19]}, | ||
103 | {0x0ac1, 0, &mqc_states[8], &mqc_states[24]}, | ||
104 | {0x0ac1, 1, &mqc_states[9], &mqc_states[25]}, | ||
105 | {0x0521, 0, &mqc_states[10], &mqc_states[58]}, | ||
106 | {0x0521, 1, &mqc_states[11], &mqc_states[59]}, | ||
107 | {0x0221, 0, &mqc_states[76], &mqc_states[66]}, | ||
108 | {0x0221, 1, &mqc_states[77], &mqc_states[67]}, | ||
109 | {0x5601, 0, &mqc_states[14], &mqc_states[13]}, | ||
110 | {0x5601, 1, &mqc_states[15], &mqc_states[12]}, | ||
111 | {0x5401, 0, &mqc_states[16], &mqc_states[28]}, | ||
112 | {0x5401, 1, &mqc_states[17], &mqc_states[29]}, | ||
113 | {0x4801, 0, &mqc_states[18], &mqc_states[28]}, | ||
114 | {0x4801, 1, &mqc_states[19], &mqc_states[29]}, | ||
115 | {0x3801, 0, &mqc_states[20], &mqc_states[28]}, | ||
116 | {0x3801, 1, &mqc_states[21], &mqc_states[29]}, | ||
117 | {0x3001, 0, &mqc_states[22], &mqc_states[34]}, | ||
118 | {0x3001, 1, &mqc_states[23], &mqc_states[35]}, | ||
119 | {0x2401, 0, &mqc_states[24], &mqc_states[36]}, | ||
120 | {0x2401, 1, &mqc_states[25], &mqc_states[37]}, | ||
121 | {0x1c01, 0, &mqc_states[26], &mqc_states[40]}, | ||
122 | {0x1c01, 1, &mqc_states[27], &mqc_states[41]}, | ||
123 | {0x1601, 0, &mqc_states[58], &mqc_states[42]}, | ||
124 | {0x1601, 1, &mqc_states[59], &mqc_states[43]}, | ||
125 | {0x5601, 0, &mqc_states[30], &mqc_states[29]}, | ||
126 | {0x5601, 1, &mqc_states[31], &mqc_states[28]}, | ||
127 | {0x5401, 0, &mqc_states[32], &mqc_states[28]}, | ||
128 | {0x5401, 1, &mqc_states[33], &mqc_states[29]}, | ||
129 | {0x5101, 0, &mqc_states[34], &mqc_states[30]}, | ||
130 | {0x5101, 1, &mqc_states[35], &mqc_states[31]}, | ||
131 | {0x4801, 0, &mqc_states[36], &mqc_states[32]}, | ||
132 | {0x4801, 1, &mqc_states[37], &mqc_states[33]}, | ||
133 | {0x3801, 0, &mqc_states[38], &mqc_states[34]}, | ||
134 | {0x3801, 1, &mqc_states[39], &mqc_states[35]}, | ||
135 | {0x3401, 0, &mqc_states[40], &mqc_states[36]}, | ||
136 | {0x3401, 1, &mqc_states[41], &mqc_states[37]}, | ||
137 | {0x3001, 0, &mqc_states[42], &mqc_states[38]}, | ||
138 | {0x3001, 1, &mqc_states[43], &mqc_states[39]}, | ||
139 | {0x2801, 0, &mqc_states[44], &mqc_states[38]}, | ||
140 | {0x2801, 1, &mqc_states[45], &mqc_states[39]}, | ||
141 | {0x2401, 0, &mqc_states[46], &mqc_states[40]}, | ||
142 | {0x2401, 1, &mqc_states[47], &mqc_states[41]}, | ||
143 | {0x2201, 0, &mqc_states[48], &mqc_states[42]}, | ||
144 | {0x2201, 1, &mqc_states[49], &mqc_states[43]}, | ||
145 | {0x1c01, 0, &mqc_states[50], &mqc_states[44]}, | ||
146 | {0x1c01, 1, &mqc_states[51], &mqc_states[45]}, | ||
147 | {0x1801, 0, &mqc_states[52], &mqc_states[46]}, | ||
148 | {0x1801, 1, &mqc_states[53], &mqc_states[47]}, | ||
149 | {0x1601, 0, &mqc_states[54], &mqc_states[48]}, | ||
150 | {0x1601, 1, &mqc_states[55], &mqc_states[49]}, | ||
151 | {0x1401, 0, &mqc_states[56], &mqc_states[50]}, | ||
152 | {0x1401, 1, &mqc_states[57], &mqc_states[51]}, | ||
153 | {0x1201, 0, &mqc_states[58], &mqc_states[52]}, | ||
154 | {0x1201, 1, &mqc_states[59], &mqc_states[53]}, | ||
155 | {0x1101, 0, &mqc_states[60], &mqc_states[54]}, | ||
156 | {0x1101, 1, &mqc_states[61], &mqc_states[55]}, | ||
157 | {0x0ac1, 0, &mqc_states[62], &mqc_states[56]}, | ||
158 | {0x0ac1, 1, &mqc_states[63], &mqc_states[57]}, | ||
159 | {0x09c1, 0, &mqc_states[64], &mqc_states[58]}, | ||
160 | {0x09c1, 1, &mqc_states[65], &mqc_states[59]}, | ||
161 | {0x08a1, 0, &mqc_states[66], &mqc_states[60]}, | ||
162 | {0x08a1, 1, &mqc_states[67], &mqc_states[61]}, | ||
163 | {0x0521, 0, &mqc_states[68], &mqc_states[62]}, | ||
164 | {0x0521, 1, &mqc_states[69], &mqc_states[63]}, | ||
165 | {0x0441, 0, &mqc_states[70], &mqc_states[64]}, | ||
166 | {0x0441, 1, &mqc_states[71], &mqc_states[65]}, | ||
167 | {0x02a1, 0, &mqc_states[72], &mqc_states[66]}, | ||
168 | {0x02a1, 1, &mqc_states[73], &mqc_states[67]}, | ||
169 | {0x0221, 0, &mqc_states[74], &mqc_states[68]}, | ||
170 | {0x0221, 1, &mqc_states[75], &mqc_states[69]}, | ||
171 | {0x0141, 0, &mqc_states[76], &mqc_states[70]}, | ||
172 | {0x0141, 1, &mqc_states[77], &mqc_states[71]}, | ||
173 | {0x0111, 0, &mqc_states[78], &mqc_states[72]}, | ||
174 | {0x0111, 1, &mqc_states[79], &mqc_states[73]}, | ||
175 | {0x0085, 0, &mqc_states[80], &mqc_states[74]}, | ||
176 | {0x0085, 1, &mqc_states[81], &mqc_states[75]}, | ||
177 | {0x0049, 0, &mqc_states[82], &mqc_states[76]}, | ||
178 | {0x0049, 1, &mqc_states[83], &mqc_states[77]}, | ||
179 | {0x0025, 0, &mqc_states[84], &mqc_states[78]}, | ||
180 | {0x0025, 1, &mqc_states[85], &mqc_states[79]}, | ||
181 | {0x0015, 0, &mqc_states[86], &mqc_states[80]}, | ||
182 | {0x0015, 1, &mqc_states[87], &mqc_states[81]}, | ||
183 | {0x0009, 0, &mqc_states[88], &mqc_states[82]}, | ||
184 | {0x0009, 1, &mqc_states[89], &mqc_states[83]}, | ||
185 | {0x0005, 0, &mqc_states[90], &mqc_states[84]}, | ||
186 | {0x0005, 1, &mqc_states[91], &mqc_states[85]}, | ||
187 | {0x0001, 0, &mqc_states[90], &mqc_states[86]}, | ||
188 | {0x0001, 1, &mqc_states[91], &mqc_states[87]}, | ||
189 | {0x5601, 0, &mqc_states[92], &mqc_states[92]}, | ||
190 | {0x5601, 1, &mqc_states[93], &mqc_states[93]}, | ||
191 | }; | ||
192 | |||
193 | /* | ||
194 | ========================================================== | ||
195 | local functions | ||
196 | ========================================================== | ||
197 | */ | ||
198 | |||
199 | static void mqc_byteout(opj_mqc_t *mqc) { | ||
200 | if (*mqc->bp == 0xff) { | ||
201 | mqc->bp++; | ||
202 | *mqc->bp = mqc->c >> 20; | ||
203 | mqc->c &= 0xfffff; | ||
204 | mqc->ct = 7; | ||
205 | } else { | ||
206 | if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */ | ||
207 | mqc->bp++; | ||
208 | *mqc->bp = mqc->c >> 19; | ||
209 | mqc->c &= 0x7ffff; | ||
210 | mqc->ct = 8; | ||
211 | } else { | ||
212 | (*mqc->bp)++; | ||
213 | if (*mqc->bp == 0xff) { | ||
214 | mqc->c &= 0x7ffffff; | ||
215 | mqc->bp++; | ||
216 | *mqc->bp = mqc->c >> 20; | ||
217 | mqc->c &= 0xfffff; | ||
218 | mqc->ct = 7; | ||
219 | } else { | ||
220 | mqc->bp++; | ||
221 | *mqc->bp = mqc->c >> 19; | ||
222 | mqc->c &= 0x7ffff; | ||
223 | mqc->ct = 8; | ||
224 | } | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | |||
229 | static void mqc_renorme(opj_mqc_t *mqc) { | ||
230 | do { | ||
231 | mqc->a <<= 1; | ||
232 | mqc->c <<= 1; | ||
233 | mqc->ct--; | ||
234 | if (mqc->ct == 0) { | ||
235 | mqc_byteout(mqc); | ||
236 | } | ||
237 | } while ((mqc->a & 0x8000) == 0); | ||
238 | } | ||
239 | |||
240 | static void mqc_codemps(opj_mqc_t *mqc) { | ||
241 | mqc->a -= (*mqc->curctx)->qeval; | ||
242 | if ((mqc->a & 0x8000) == 0) { | ||
243 | if (mqc->a < (*mqc->curctx)->qeval) { | ||
244 | mqc->a = (*mqc->curctx)->qeval; | ||
245 | } else { | ||
246 | mqc->c += (*mqc->curctx)->qeval; | ||
247 | } | ||
248 | *mqc->curctx = (*mqc->curctx)->nmps; | ||
249 | mqc_renorme(mqc); | ||
250 | } else { | ||
251 | mqc->c += (*mqc->curctx)->qeval; | ||
252 | } | ||
253 | } | ||
254 | |||
255 | static void mqc_codelps(opj_mqc_t *mqc) { | ||
256 | mqc->a -= (*mqc->curctx)->qeval; | ||
257 | if (mqc->a < (*mqc->curctx)->qeval) { | ||
258 | mqc->c += (*mqc->curctx)->qeval; | ||
259 | } else { | ||
260 | mqc->a = (*mqc->curctx)->qeval; | ||
261 | } | ||
262 | *mqc->curctx = (*mqc->curctx)->nlps; | ||
263 | mqc_renorme(mqc); | ||
264 | } | ||
265 | |||
266 | static void mqc_setbits(opj_mqc_t *mqc) { | ||
267 | unsigned int tempc = mqc->c + mqc->a; | ||
268 | mqc->c |= 0xffff; | ||
269 | if (mqc->c >= tempc) { | ||
270 | mqc->c -= 0x8000; | ||
271 | } | ||
272 | } | ||
273 | |||
274 | static int mqc_mpsexchange(opj_mqc_t *mqc) { | ||
275 | int d; | ||
276 | if (mqc->a < (*mqc->curctx)->qeval) { | ||
277 | d = 1 - (*mqc->curctx)->mps; | ||
278 | *mqc->curctx = (*mqc->curctx)->nlps; | ||
279 | } else { | ||
280 | d = (*mqc->curctx)->mps; | ||
281 | *mqc->curctx = (*mqc->curctx)->nmps; | ||
282 | } | ||
283 | |||
284 | return d; | ||
285 | } | ||
286 | |||
287 | static int mqc_lpsexchange(opj_mqc_t *mqc) { | ||
288 | int d; | ||
289 | if (mqc->a < (*mqc->curctx)->qeval) { | ||
290 | mqc->a = (*mqc->curctx)->qeval; | ||
291 | d = (*mqc->curctx)->mps; | ||
292 | *mqc->curctx = (*mqc->curctx)->nmps; | ||
293 | } else { | ||
294 | mqc->a = (*mqc->curctx)->qeval; | ||
295 | d = 1 - (*mqc->curctx)->mps; | ||
296 | *mqc->curctx = (*mqc->curctx)->nlps; | ||
297 | } | ||
298 | |||
299 | return d; | ||
300 | } | ||
301 | |||
302 | static void mqc_bytein(opj_mqc_t *mqc) { | ||
303 | if (mqc->bp != mqc->end) { | ||
304 | unsigned int c; | ||
305 | if (mqc->bp + 1 != mqc->end) { | ||
306 | c = *(mqc->bp + 1); | ||
307 | } else { | ||
308 | c = 0xff; | ||
309 | } | ||
310 | if (*mqc->bp == 0xff) { | ||
311 | if (c > 0x8f) { | ||
312 | mqc->c += 0xff00; | ||
313 | mqc->ct = 8; | ||
314 | } else { | ||
315 | mqc->bp++; | ||
316 | mqc->c += c << 9; | ||
317 | mqc->ct = 7; | ||
318 | } | ||
319 | } else { | ||
320 | mqc->bp++; | ||
321 | mqc->c += c << 8; | ||
322 | mqc->ct = 8; | ||
323 | } | ||
324 | } else { | ||
325 | mqc->c += 0xff00; | ||
326 | mqc->ct = 8; | ||
327 | } | ||
328 | } | ||
329 | |||
330 | static void mqc_renormd(opj_mqc_t *mqc) { | ||
331 | do { | ||
332 | if (mqc->ct == 0) { | ||
333 | mqc_bytein(mqc); | ||
334 | } | ||
335 | mqc->a <<= 1; | ||
336 | mqc->c <<= 1; | ||
337 | mqc->ct--; | ||
338 | } while (mqc->a < 0x8000); | ||
339 | } | ||
340 | |||
341 | /* | ||
342 | ========================================================== | ||
343 | MQ-Coder interface | ||
344 | ========================================================== | ||
345 | */ | ||
346 | |||
347 | opj_mqc_t* mqc_create() { | ||
348 | opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t)); | ||
349 | return mqc; | ||
350 | } | ||
351 | |||
352 | void mqc_destroy(opj_mqc_t *mqc) { | ||
353 | if(mqc) { | ||
354 | opj_free(mqc); | ||
355 | } | ||
356 | } | ||
357 | |||
358 | int mqc_numbytes(opj_mqc_t *mqc) { | ||
359 | return mqc->bp - mqc->start; | ||
360 | } | ||
361 | |||
362 | void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) { | ||
363 | mqc_setcurctx(mqc, 0); | ||
364 | mqc->a = 0x8000; | ||
365 | mqc->c = 0; | ||
366 | mqc->bp = bp - 1; | ||
367 | mqc->ct = 12; | ||
368 | if (*mqc->bp == 0xff) { | ||
369 | mqc->ct = 13; | ||
370 | } | ||
371 | mqc->start = bp; | ||
372 | } | ||
373 | |||
374 | void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) { | ||
375 | mqc->curctx = &mqc->ctxs[ctxno]; | ||
376 | } | ||
377 | |||
378 | void mqc_encode(opj_mqc_t *mqc, int d) { | ||
379 | if ((*mqc->curctx)->mps == d) { | ||
380 | mqc_codemps(mqc); | ||
381 | } else { | ||
382 | mqc_codelps(mqc); | ||
383 | } | ||
384 | } | ||
385 | |||
386 | void mqc_flush(opj_mqc_t *mqc) { | ||
387 | mqc_setbits(mqc); | ||
388 | mqc->c <<= mqc->ct; | ||
389 | mqc_byteout(mqc); | ||
390 | mqc->c <<= mqc->ct; | ||
391 | mqc_byteout(mqc); | ||
392 | |||
393 | if (*mqc->bp != 0xff) { | ||
394 | mqc->bp++; | ||
395 | } | ||
396 | } | ||
397 | |||
398 | void mqc_bypass_init_enc(opj_mqc_t *mqc) { | ||
399 | mqc->c = 0; | ||
400 | mqc->ct = 8; | ||
401 | /*if (*mqc->bp == 0xff) { | ||
402 | mqc->ct = 7; | ||
403 | } */ | ||
404 | } | ||
405 | |||
406 | void mqc_bypass_enc(opj_mqc_t *mqc, int d) { | ||
407 | mqc->ct--; | ||
408 | mqc->c = mqc->c + (d << mqc->ct); | ||
409 | if (mqc->ct == 0) { | ||
410 | mqc->bp++; | ||
411 | *mqc->bp = mqc->c; | ||
412 | mqc->ct = 8; | ||
413 | if (*mqc->bp == 0xff) { | ||
414 | mqc->ct = 7; | ||
415 | } | ||
416 | mqc->c = 0; | ||
417 | } | ||
418 | } | ||
419 | |||
420 | int mqc_bypass_flush_enc(opj_mqc_t *mqc) { | ||
421 | unsigned char bit_padding; | ||
422 | |||
423 | bit_padding = 0; | ||
424 | |||
425 | if (mqc->ct != 0) { | ||
426 | while (mqc->ct > 0) { | ||
427 | mqc->ct--; | ||
428 | mqc->c += bit_padding << mqc->ct; | ||
429 | bit_padding = (bit_padding + 1) & 0x01; | ||
430 | } | ||
431 | mqc->bp++; | ||
432 | *mqc->bp = mqc->c; | ||
433 | mqc->ct = 8; | ||
434 | mqc->c = 0; | ||
435 | } | ||
436 | |||
437 | return 1; | ||
438 | } | ||
439 | |||
440 | void mqc_reset_enc(opj_mqc_t *mqc) { | ||
441 | mqc_resetstates(mqc); | ||
442 | mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); | ||
443 | mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); | ||
444 | mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); | ||
445 | } | ||
446 | |||
447 | int mqc_restart_enc(opj_mqc_t *mqc) { | ||
448 | int correction = 1; | ||
449 | |||
450 | /* <flush part> */ | ||
451 | int n = 27 - 15 - mqc->ct; | ||
452 | mqc->c <<= mqc->ct; | ||
453 | while (n > 0) { | ||
454 | mqc_byteout(mqc); | ||
455 | n -= mqc->ct; | ||
456 | mqc->c <<= mqc->ct; | ||
457 | } | ||
458 | mqc_byteout(mqc); | ||
459 | |||
460 | return correction; | ||
461 | } | ||
462 | |||
463 | void mqc_restart_init_enc(opj_mqc_t *mqc) { | ||
464 | /* <Re-init part> */ | ||
465 | mqc_setcurctx(mqc, 0); | ||
466 | mqc->a = 0x8000; | ||
467 | mqc->c = 0; | ||
468 | mqc->ct = 12; | ||
469 | mqc->bp--; | ||
470 | if (*mqc->bp == 0xff) { | ||
471 | mqc->ct = 13; | ||
472 | } | ||
473 | } | ||
474 | |||
475 | void mqc_erterm_enc(opj_mqc_t *mqc) { | ||
476 | int k = 11 - mqc->ct + 1; | ||
477 | |||
478 | while (k > 0) { | ||
479 | mqc->c <<= mqc->ct; | ||
480 | mqc->ct = 0; | ||
481 | mqc_byteout(mqc); | ||
482 | k -= mqc->ct; | ||
483 | } | ||
484 | |||
485 | if (*mqc->bp != 0xff) { | ||
486 | mqc_byteout(mqc); | ||
487 | } | ||
488 | } | ||
489 | |||
490 | void mqc_segmark_enc(opj_mqc_t *mqc) { | ||
491 | int i; | ||
492 | mqc_setcurctx(mqc, 18); | ||
493 | |||
494 | for (i = 1; i < 5; i++) { | ||
495 | mqc_encode(mqc, i % 2); | ||
496 | } | ||
497 | } | ||
498 | |||
499 | void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { | ||
500 | mqc_setcurctx(mqc, 0); | ||
501 | mqc->start = bp; | ||
502 | mqc->end = bp + len; | ||
503 | mqc->bp = bp; | ||
504 | if (len==0) mqc->c = 0xff << 16; | ||
505 | else mqc->c = *mqc->bp << 16; | ||
506 | mqc_bytein(mqc); | ||
507 | mqc->c <<= 7; | ||
508 | mqc->ct -= 7; | ||
509 | mqc->a = 0x8000; | ||
510 | } | ||
511 | |||
512 | int mqc_decode(opj_mqc_t *mqc) { | ||
513 | int d; | ||
514 | mqc->a -= (*mqc->curctx)->qeval; | ||
515 | if ((mqc->c >> 16) < (*mqc->curctx)->qeval) { | ||
516 | d = mqc_lpsexchange(mqc); | ||
517 | mqc_renormd(mqc); | ||
518 | } else { | ||
519 | mqc->c -= (*mqc->curctx)->qeval << 16; | ||
520 | if ((mqc->a & 0x8000) == 0) { | ||
521 | d = mqc_mpsexchange(mqc); | ||
522 | mqc_renormd(mqc); | ||
523 | } else { | ||
524 | d = (*mqc->curctx)->mps; | ||
525 | } | ||
526 | } | ||
527 | |||
528 | return d; | ||
529 | } | ||
530 | |||
531 | void mqc_resetstates(opj_mqc_t *mqc) { | ||
532 | int i; | ||
533 | for (i = 0; i < MQC_NUMCTXS; i++) { | ||
534 | mqc->ctxs[i] = mqc_states; | ||
535 | } | ||
536 | } | ||
537 | |||
538 | void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) { | ||
539 | mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)]; | ||
540 | } | ||
541 | |||
542 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/mqc.h b/libraries/openjpeg-libsl/libopenjpeg/mqc.h new file mode 100644 index 0000000..67f38c1 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/mqc.h | |||
@@ -0,0 +1,197 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __MQC_H | ||
33 | #define __MQC_H | ||
34 | /** | ||
35 | @file mqc.h | ||
36 | @brief Implementation of an MQ-Coder (MQC) | ||
37 | |||
38 | The functions in MQC.C have for goal to realize the MQ-coder operations. The functions | ||
39 | in MQC.C are used by some function in T1.C. | ||
40 | */ | ||
41 | |||
42 | /** @defgroup MQC MQC - Implementation of an MQ-Coder */ | ||
43 | /*@{*/ | ||
44 | |||
45 | /** | ||
46 | This struct defines the state of a context. | ||
47 | */ | ||
48 | typedef struct opj_mqc_state { | ||
49 | /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */ | ||
50 | unsigned int qeval; | ||
51 | /** the Most Probable Symbol (0 or 1) */ | ||
52 | int mps; | ||
53 | /** next state if the next encoded symbol is the MPS */ | ||
54 | struct opj_mqc_state *nmps; | ||
55 | /** next state if the next encoded symbol is the LPS */ | ||
56 | struct opj_mqc_state *nlps; | ||
57 | } opj_mqc_state_t; | ||
58 | |||
59 | #define MQC_NUMCTXS 32 | ||
60 | |||
61 | /** | ||
62 | MQ coder | ||
63 | */ | ||
64 | typedef struct opj_mqc { | ||
65 | unsigned int c; | ||
66 | unsigned int a; | ||
67 | unsigned int ct; | ||
68 | unsigned char *bp; | ||
69 | unsigned char *start; | ||
70 | unsigned char *end; | ||
71 | opj_mqc_state_t *ctxs[MQC_NUMCTXS]; | ||
72 | opj_mqc_state_t **curctx; | ||
73 | } opj_mqc_t; | ||
74 | |||
75 | /** @name Exported functions */ | ||
76 | /*@{*/ | ||
77 | /* ----------------------------------------------------------------------- */ | ||
78 | /** | ||
79 | Create a new MQC handle | ||
80 | @return Returns a new MQC handle if successful, returns NULL otherwise | ||
81 | */ | ||
82 | opj_mqc_t* mqc_create(); | ||
83 | /** | ||
84 | Destroy a previously created MQC handle | ||
85 | @param mqc MQC handle to destroy | ||
86 | */ | ||
87 | void mqc_destroy(opj_mqc_t *mqc); | ||
88 | /** | ||
89 | Return the number of bytes written/read since initialisation | ||
90 | @param mqc MQC handle | ||
91 | @return Returns the number of bytes already encoded | ||
92 | */ | ||
93 | int mqc_numbytes(opj_mqc_t *mqc); | ||
94 | /** | ||
95 | Reset the states of all the context of the coder/decoder | ||
96 | (each context is set to a state where 0 and 1 are more or less equiprobable) | ||
97 | @param mqc MQC handle | ||
98 | */ | ||
99 | void mqc_resetstates(opj_mqc_t *mqc); | ||
100 | /** | ||
101 | Set the state of a particular context | ||
102 | @param mqc MQC handle | ||
103 | @param ctxno Number that identifies the context | ||
104 | @param msb The MSB of the new state of the context | ||
105 | @param prob Number that identifies the probability of the symbols for the new state of the context | ||
106 | */ | ||
107 | void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob); | ||
108 | /** | ||
109 | Initialize the encoder | ||
110 | @param mqc MQC handle | ||
111 | @param bp Pointer to the start of the buffer where the bytes will be written | ||
112 | */ | ||
113 | void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp); | ||
114 | /** | ||
115 | Set the current context used for coding/decoding | ||
116 | @param mqc MQC handle | ||
117 | @param ctxno Number that identifies the context | ||
118 | */ | ||
119 | void mqc_setcurctx(opj_mqc_t *mqc, int ctxno); | ||
120 | /** | ||
121 | Encode a symbol using the MQ-coder | ||
122 | @param mqc MQC handle | ||
123 | @param d The symbol to be encoded (0 or 1) | ||
124 | */ | ||
125 | void mqc_encode(opj_mqc_t *mqc, int d); | ||
126 | /** | ||
127 | Flush the encoder, so that all remaining data is written | ||
128 | @param mqc MQC handle | ||
129 | */ | ||
130 | void mqc_flush(opj_mqc_t *mqc); | ||
131 | /** | ||
132 | BYPASS mode switch, initialization operation. | ||
133 | JPEG 2000 p 505. | ||
134 | <h2>Not fully implemented and tested !!</h2> | ||
135 | @param mqc MQC handle | ||
136 | */ | ||
137 | void mqc_bypass_init_enc(opj_mqc_t *mqc); | ||
138 | /** | ||
139 | BYPASS mode switch, coding operation. | ||
140 | JPEG 2000 p 505. | ||
141 | <h2>Not fully implemented and tested !!</h2> | ||
142 | @param mqc MQC handle | ||
143 | @param d The symbol to be encoded (0 or 1) | ||
144 | */ | ||
145 | void mqc_bypass_enc(opj_mqc_t *mqc, int d); | ||
146 | /** | ||
147 | BYPASS mode switch, flush operation | ||
148 | <h2>Not fully implemented and tested !!</h2> | ||
149 | @param mqc MQC handle | ||
150 | @return Returns 1 (always) | ||
151 | */ | ||
152 | int mqc_bypass_flush_enc(opj_mqc_t *mqc); | ||
153 | /** | ||
154 | RESET mode switch | ||
155 | @param mqc MQC handle | ||
156 | */ | ||
157 | void mqc_reset_enc(opj_mqc_t *mqc); | ||
158 | /** | ||
159 | RESTART mode switch (TERMALL) | ||
160 | @param mqc MQC handle | ||
161 | @return Returns 1 (always) | ||
162 | */ | ||
163 | int mqc_restart_enc(opj_mqc_t *mqc); | ||
164 | /** | ||
165 | RESTART mode switch (TERMALL) reinitialisation | ||
166 | @param mqc MQC handle | ||
167 | */ | ||
168 | void mqc_restart_init_enc(opj_mqc_t *mqc); | ||
169 | /** | ||
170 | ERTERM mode switch (PTERM) | ||
171 | @param mqc MQC handle | ||
172 | */ | ||
173 | void mqc_erterm_enc(opj_mqc_t *mqc); | ||
174 | /** | ||
175 | SEGMARK mode switch (SEGSYM) | ||
176 | @param mqc MQC handle | ||
177 | */ | ||
178 | void mqc_segmark_enc(opj_mqc_t *mqc); | ||
179 | /** | ||
180 | Initialize the decoder | ||
181 | @param mqc MQC handle | ||
182 | @param bp Pointer to the start of the buffer from which the bytes will be read | ||
183 | @param len Length of the input buffer | ||
184 | */ | ||
185 | void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len); | ||
186 | /** | ||
187 | Decode a symbol | ||
188 | @param mqc MQC handle | ||
189 | @return Returns the decoded symbol (0 or 1) | ||
190 | */ | ||
191 | int mqc_decode(opj_mqc_t *mqc); | ||
192 | /* ----------------------------------------------------------------------- */ | ||
193 | /*@}*/ | ||
194 | |||
195 | /*@}*/ | ||
196 | |||
197 | #endif /* __MQC_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/openjpeg.c b/libraries/openjpeg-libsl/libopenjpeg/openjpeg.c new file mode 100644 index 0000000..6f12ff0 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/openjpeg.c | |||
@@ -0,0 +1,307 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #ifdef WIN32 | ||
28 | #include <windows.h> | ||
29 | #endif /* WIN32 */ | ||
30 | |||
31 | #include "opj_includes.h" | ||
32 | |||
33 | /* ---------------------------------------------------------------------- */ | ||
34 | #ifdef WIN32 | ||
35 | #ifndef OPJ_STATIC | ||
36 | BOOL APIENTRY | ||
37 | DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { | ||
38 | switch (ul_reason_for_call) { | ||
39 | case DLL_PROCESS_ATTACH : | ||
40 | break; | ||
41 | case DLL_PROCESS_DETACH : | ||
42 | break; | ||
43 | case DLL_THREAD_ATTACH : | ||
44 | case DLL_THREAD_DETACH : | ||
45 | break; | ||
46 | } | ||
47 | |||
48 | return TRUE; | ||
49 | } | ||
50 | #endif /* OPJ_STATIC */ | ||
51 | #endif /* WIN32 */ | ||
52 | |||
53 | /* ---------------------------------------------------------------------- */ | ||
54 | |||
55 | |||
56 | const char* OPJ_CALLCONV opj_version() { | ||
57 | return OPENJPEG_VERSION; | ||
58 | } | ||
59 | |||
60 | opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) { | ||
61 | opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t)); | ||
62 | if(!dinfo) return NULL; | ||
63 | dinfo->is_decompressor = true; | ||
64 | switch(format) { | ||
65 | case CODEC_J2K: | ||
66 | case CODEC_JPT: | ||
67 | /* get a J2K decoder handle */ | ||
68 | dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo); | ||
69 | if(!dinfo->j2k_handle) { | ||
70 | opj_free(dinfo); | ||
71 | return NULL; | ||
72 | } | ||
73 | break; | ||
74 | case CODEC_JP2: | ||
75 | /* get a JP2 decoder handle */ | ||
76 | dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo); | ||
77 | if(!dinfo->jp2_handle) { | ||
78 | opj_free(dinfo); | ||
79 | return NULL; | ||
80 | } | ||
81 | break; | ||
82 | case CODEC_UNKNOWN: | ||
83 | default: | ||
84 | opj_free(dinfo); | ||
85 | return NULL; | ||
86 | } | ||
87 | |||
88 | dinfo->codec_format = format; | ||
89 | |||
90 | return dinfo; | ||
91 | } | ||
92 | |||
93 | void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) { | ||
94 | if(dinfo) { | ||
95 | /* destroy the codec */ | ||
96 | switch(dinfo->codec_format) { | ||
97 | case CODEC_J2K: | ||
98 | case CODEC_JPT: | ||
99 | j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle); | ||
100 | break; | ||
101 | case CODEC_JP2: | ||
102 | jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle); | ||
103 | break; | ||
104 | case CODEC_UNKNOWN: | ||
105 | default: | ||
106 | break; | ||
107 | } | ||
108 | /* destroy the decompressor */ | ||
109 | opj_free(dinfo); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) { | ||
114 | if(parameters) { | ||
115 | memset(parameters, 0, sizeof(opj_dparameters_t)); | ||
116 | /* default decoding parameters */ | ||
117 | parameters->cp_layer = 0; | ||
118 | parameters->cp_reduce = 0; | ||
119 | parameters->cp_limit_decoding = NO_LIMITATION; | ||
120 | |||
121 | parameters->decod_format = -1; | ||
122 | parameters->cod_format = -1; | ||
123 | /* UniPG>> */ | ||
124 | #ifdef USE_JPWL | ||
125 | parameters->jpwl_correct = false; | ||
126 | parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS; | ||
127 | parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES; | ||
128 | #endif /* USE_JPWL */ | ||
129 | /* <<UniPG */ | ||
130 | } | ||
131 | } | ||
132 | |||
133 | void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) { | ||
134 | if(dinfo && parameters) { | ||
135 | switch(dinfo->codec_format) { | ||
136 | case CODEC_J2K: | ||
137 | case CODEC_JPT: | ||
138 | j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters); | ||
139 | break; | ||
140 | case CODEC_JP2: | ||
141 | jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters); | ||
142 | break; | ||
143 | case CODEC_UNKNOWN: | ||
144 | default: | ||
145 | break; | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | |||
150 | opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) { | ||
151 | if(dinfo && cio) { | ||
152 | switch(dinfo->codec_format) { | ||
153 | case CODEC_J2K: | ||
154 | return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio); | ||
155 | case CODEC_JPT: | ||
156 | return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio); | ||
157 | case CODEC_JP2: | ||
158 | return jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio); | ||
159 | case CODEC_UNKNOWN: | ||
160 | default: | ||
161 | break; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | return NULL; | ||
166 | } | ||
167 | |||
168 | opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) { | ||
169 | opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t)); | ||
170 | if(!cinfo) return NULL; | ||
171 | cinfo->is_decompressor = false; | ||
172 | switch(format) { | ||
173 | case CODEC_J2K: | ||
174 | /* get a J2K coder handle */ | ||
175 | cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo); | ||
176 | if(!cinfo->j2k_handle) { | ||
177 | opj_free(cinfo); | ||
178 | return NULL; | ||
179 | } | ||
180 | break; | ||
181 | case CODEC_JP2: | ||
182 | /* get a JP2 coder handle */ | ||
183 | cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo); | ||
184 | if(!cinfo->jp2_handle) { | ||
185 | opj_free(cinfo); | ||
186 | return NULL; | ||
187 | } | ||
188 | break; | ||
189 | case CODEC_JPT: | ||
190 | case CODEC_UNKNOWN: | ||
191 | default: | ||
192 | opj_free(cinfo); | ||
193 | return NULL; | ||
194 | } | ||
195 | |||
196 | cinfo->codec_format = format; | ||
197 | |||
198 | return cinfo; | ||
199 | } | ||
200 | |||
201 | void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) { | ||
202 | if(cinfo) { | ||
203 | /* destroy the codec */ | ||
204 | switch(cinfo->codec_format) { | ||
205 | case CODEC_J2K: | ||
206 | j2k_destroy_decompress((opj_j2k_t*)cinfo->j2k_handle); | ||
207 | break; | ||
208 | case CODEC_JP2: | ||
209 | jp2_destroy_decompress((opj_jp2_t*)cinfo->jp2_handle); | ||
210 | break; | ||
211 | case CODEC_JPT: | ||
212 | case CODEC_UNKNOWN: | ||
213 | default: | ||
214 | break; | ||
215 | } | ||
216 | /* destroy the decompressor */ | ||
217 | opj_free(cinfo); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) { | ||
222 | if(parameters) { | ||
223 | memset(parameters, 0, sizeof(opj_cparameters_t)); | ||
224 | /* default coding parameters */ | ||
225 | parameters->cp_cinema = OFF; | ||
226 | parameters->max_comp_size = 0; | ||
227 | parameters->numresolution = 6; | ||
228 | parameters->cp_rsiz = STD_RSIZ; | ||
229 | parameters->cblockw_init = 64; | ||
230 | parameters->cblockh_init = 64; | ||
231 | parameters->prog_order = LRCP; | ||
232 | parameters->roi_compno = -1; /* no ROI */ | ||
233 | parameters->subsampling_dx = 1; | ||
234 | parameters->subsampling_dy = 1; | ||
235 | parameters->tp_on = 0; | ||
236 | parameters->decod_format = -1; | ||
237 | parameters->cod_format = -1; | ||
238 | /* UniPG>> */ | ||
239 | #ifdef USE_JPWL | ||
240 | parameters->jpwl_epc_on = false; | ||
241 | parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */ | ||
242 | { | ||
243 | int i; | ||
244 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { | ||
245 | parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */ | ||
246 | parameters->jpwl_hprot_TPH[i] = 0; /* absent */ | ||
247 | } | ||
248 | }; | ||
249 | { | ||
250 | int i; | ||
251 | for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) { | ||
252 | parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */ | ||
253 | parameters->jpwl_pprot_packno[i] = -1; /* unassigned */ | ||
254 | parameters->jpwl_pprot[i] = 0; /* absent */ | ||
255 | } | ||
256 | }; | ||
257 | parameters->jpwl_sens_size = 0; /* 0 means no ESD */ | ||
258 | parameters->jpwl_sens_addr = 0; /* 0 means auto */ | ||
259 | parameters->jpwl_sens_range = 0; /* 0 means packet */ | ||
260 | parameters->jpwl_sens_MH = -1; /* -1 means unassigned */ | ||
261 | { | ||
262 | int i; | ||
263 | for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) { | ||
264 | parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */ | ||
265 | parameters->jpwl_sens_TPH[i] = -1; /* absent */ | ||
266 | } | ||
267 | }; | ||
268 | #endif /* USE_JPWL */ | ||
269 | /* <<UniPG */ | ||
270 | } | ||
271 | } | ||
272 | |||
273 | void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) { | ||
274 | if(cinfo && parameters && image) { | ||
275 | switch(cinfo->codec_format) { | ||
276 | case CODEC_J2K: | ||
277 | j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image); | ||
278 | break; | ||
279 | case CODEC_JP2: | ||
280 | jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image); | ||
281 | break; | ||
282 | case CODEC_JPT: | ||
283 | case CODEC_UNKNOWN: | ||
284 | default: | ||
285 | break; | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | |||
290 | bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) { | ||
291 | if(cinfo && cio && image) { | ||
292 | switch(cinfo->codec_format) { | ||
293 | case CODEC_J2K: | ||
294 | return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, index); | ||
295 | case CODEC_JP2: | ||
296 | return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, index); | ||
297 | case CODEC_JPT: | ||
298 | case CODEC_UNKNOWN: | ||
299 | default: | ||
300 | break; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | return false; | ||
305 | } | ||
306 | |||
307 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/openjpeg.h b/libraries/openjpeg-libsl/libopenjpeg/openjpeg.h new file mode 100644 index 0000000..e74c90e --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/openjpeg.h | |||
@@ -0,0 +1,751 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2006-2007, Parvatha Elangovan | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | #ifndef OPENJPEG_H | ||
33 | #define OPENJPEG_H | ||
34 | |||
35 | #define OPENJPEG_VERSION "1.2.0" | ||
36 | |||
37 | /* | ||
38 | ========================================================== | ||
39 | Compiler directives | ||
40 | ========================================================== | ||
41 | */ | ||
42 | |||
43 | #if defined(OPJ_STATIC) || !(defined(WIN32) || defined(__WIN32__)) | ||
44 | #define OPJ_API | ||
45 | #define OPJ_CALLCONV | ||
46 | #else | ||
47 | #define OPJ_CALLCONV __stdcall | ||
48 | /* | ||
49 | The following ifdef block is the standard way of creating macros which make exporting | ||
50 | from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS | ||
51 | symbol defined on the command line. this symbol should not be defined on any project | ||
52 | that uses this DLL. This way any other project whose source files include this file see | ||
53 | OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols | ||
54 | defined with this macro as being exported. | ||
55 | */ | ||
56 | #ifdef OPJ_EXPORTS | ||
57 | #define OPJ_API __declspec(dllexport) | ||
58 | #else | ||
59 | #define OPJ_API __declspec(dllimport) | ||
60 | #endif /* OPJ_EXPORTS */ | ||
61 | #endif /* !OPJ_STATIC || !WIN32 */ | ||
62 | |||
63 | #ifndef __cplusplus | ||
64 | #if defined(HAVE_STDBOOL_H) | ||
65 | /* | ||
66 | The C language implementation does correctly provide the standard header | ||
67 | file "stdbool.h". | ||
68 | */ | ||
69 | #include <stdbool.h> | ||
70 | #else | ||
71 | /* | ||
72 | The C language implementation does not provide the standard header file | ||
73 | "stdbool.h" as required by ISO/IEC 9899:1999. Try to compensate for this | ||
74 | braindamage below. | ||
75 | */ | ||
76 | #if !defined(bool) | ||
77 | #define bool int | ||
78 | #endif | ||
79 | #if !defined(true) | ||
80 | #define true 1 | ||
81 | #endif | ||
82 | #if !defined(false) | ||
83 | #define false 0 | ||
84 | #endif | ||
85 | #endif | ||
86 | #endif /* __cplusplus */ | ||
87 | |||
88 | /* | ||
89 | ========================================================== | ||
90 | Useful constant definitions | ||
91 | ========================================================== | ||
92 | */ | ||
93 | |||
94 | #define OPJ_PATH_LEN 4096 /**< Maximum allowed size for filenames */ | ||
95 | |||
96 | #define J2K_MAXRLVLS 33 /**< Number of maximum resolution level authorized */ | ||
97 | #define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /**< Number of maximum sub-band linked to number of resolution level */ | ||
98 | |||
99 | /* UniPG>> */ | ||
100 | #define JPWL_MAX_NO_TILESPECS 16 /**< Maximum number of tile parts expected by JPWL: increase at your will */ | ||
101 | #define JPWL_MAX_NO_PACKSPECS 16 /**< Maximum number of packet parts expected by JPWL: increase at your will */ | ||
102 | #define JPWL_MAX_NO_MARKERS 512 /**< Maximum number of JPWL markers: increase at your will */ | ||
103 | #define JPWL_PRIVATEINDEX_NAME "jpwl_index_privatefilename" /**< index file name used when JPWL is on */ | ||
104 | #define JPWL_EXPECTED_COMPONENTS 3 /**< Expect this number of components, so you'll find better the first EPB */ | ||
105 | #define JPWL_MAXIMUM_TILES 8192 /**< Expect this maximum number of tiles, to avoid some crashes */ | ||
106 | #define JPWL_MAXIMUM_HAMMING 2 /**< Expect this maximum number of bit errors in marker id's */ | ||
107 | /* <<UniPG */ | ||
108 | |||
109 | /* | ||
110 | ========================================================== | ||
111 | enum definitions | ||
112 | ========================================================== | ||
113 | */ | ||
114 | /** | ||
115 | Rsiz Capabilities | ||
116 | */ | ||
117 | typedef enum RSIZ_CAPABILITIES { | ||
118 | STD_RSIZ = 0, /** Standard JPEG2000 profile*/ | ||
119 | CINEMA2K = 3, /** Profile name for a 2K image*/ | ||
120 | CINEMA4K = 4 /** Profile name for a 4K image*/ | ||
121 | } OPJ_RSIZ_CAPABILITIES; | ||
122 | |||
123 | /** | ||
124 | Digital cinema operation mode | ||
125 | */ | ||
126 | typedef enum CINEMA_MODE { | ||
127 | OFF = 0, /** Not Digital Cinema*/ | ||
128 | CINEMA2K_24 = 1, /** 2K Digital Cinema at 24 fps*/ | ||
129 | CINEMA2K_48 = 2, /** 2K Digital Cinema at 48 fps*/ | ||
130 | CINEMA4K_24 = 3 /** 4K Digital Cinema at 24 fps*/ | ||
131 | }OPJ_CINEMA_MODE; | ||
132 | |||
133 | /** | ||
134 | Progression order | ||
135 | */ | ||
136 | typedef enum PROG_ORDER { | ||
137 | PROG_UNKNOWN = -1, /**< place-holder */ | ||
138 | LRCP = 0, /**< layer-resolution-component-precinct order */ | ||
139 | RLCP = 1, /**< resolution-layer-component-precinct order */ | ||
140 | RPCL = 2, /**< resolution-precinct-component-layer order */ | ||
141 | PCRL = 3, /**< precinct-component-resolution-layer order */ | ||
142 | CPRL = 4 /**< component-precinct-resolution-layer order */ | ||
143 | } OPJ_PROG_ORDER; | ||
144 | |||
145 | /** | ||
146 | Supported image color spaces | ||
147 | */ | ||
148 | typedef enum COLOR_SPACE { | ||
149 | CLRSPC_UNKNOWN = -1, /**< place-holder */ | ||
150 | CLRSPC_SRGB = 1, /**< sRGB */ | ||
151 | CLRSPC_GRAY = 2, /**< grayscale */ | ||
152 | CLRSPC_SYCC = 3 /**< YUV */ | ||
153 | } OPJ_COLOR_SPACE; | ||
154 | |||
155 | /** | ||
156 | Supported codec | ||
157 | */ | ||
158 | typedef enum CODEC_FORMAT { | ||
159 | CODEC_UNKNOWN = -1, /**< place-holder */ | ||
160 | CODEC_J2K = 0, /**< JPEG-2000 codestream : read/write */ | ||
161 | CODEC_JPT = 1, /**< JPT-stream (JPEG 2000, JPIP) : read only */ | ||
162 | CODEC_JP2 = 2 /**< JPEG-2000 file format : read/write */ | ||
163 | } OPJ_CODEC_FORMAT; | ||
164 | |||
165 | /** | ||
166 | Limit decoding to certain portions of the codestream. | ||
167 | */ | ||
168 | typedef enum LIMIT_DECODING { | ||
169 | NO_LIMITATION = 0, /**< No limitation for the decoding. The entire codestream will de decoded */ | ||
170 | LIMIT_TO_MAIN_HEADER = 1, /**< The decoding is limited to the Main Header */ | ||
171 | DECODE_ALL_BUT_PACKETS = 2 /**< Decode everything except the JPEG 2000 packets */ | ||
172 | } OPJ_LIMIT_DECODING; | ||
173 | |||
174 | /* | ||
175 | ========================================================== | ||
176 | event manager typedef definitions | ||
177 | ========================================================== | ||
178 | */ | ||
179 | |||
180 | /** | ||
181 | Callback function prototype for events | ||
182 | @param msg Event message | ||
183 | @param client_data | ||
184 | */ | ||
185 | typedef void (*opj_msg_callback) (const char *msg, void *client_data); | ||
186 | |||
187 | /** | ||
188 | Message handler object | ||
189 | used for | ||
190 | <ul> | ||
191 | <li>Error messages | ||
192 | <li>Warning messages | ||
193 | <li>Debugging messages | ||
194 | </ul> | ||
195 | */ | ||
196 | typedef struct opj_event_mgr { | ||
197 | /** Error message callback if available, NULL otherwise */ | ||
198 | opj_msg_callback error_handler; | ||
199 | /** Warning message callback if available, NULL otherwise */ | ||
200 | opj_msg_callback warning_handler; | ||
201 | /** Debug message callback if available, NULL otherwise */ | ||
202 | opj_msg_callback info_handler; | ||
203 | } opj_event_mgr_t; | ||
204 | |||
205 | |||
206 | /* | ||
207 | ========================================================== | ||
208 | codec typedef definitions | ||
209 | ========================================================== | ||
210 | */ | ||
211 | |||
212 | /** | ||
213 | Progression order changes | ||
214 | */ | ||
215 | typedef struct opj_poc { | ||
216 | /** Resolution num start, Component num start, given by POC */ | ||
217 | int resno0, compno0; | ||
218 | /** Layer num end,Resolution num end, Component num end, given by POC */ | ||
219 | int layno1, resno1, compno1; | ||
220 | /** Layer num start,Precinct num start, Precinct num end */ | ||
221 | int layno0, precno0, precno1; | ||
222 | /** Progression order enum*/ | ||
223 | OPJ_PROG_ORDER prg1,prg; | ||
224 | /** Progression order string*/ | ||
225 | char progorder[5]; | ||
226 | /** Tile number */ | ||
227 | int tile; | ||
228 | /** Start and end values for Tile width and height*/ | ||
229 | int tx0,tx1,ty0,ty1; | ||
230 | /** Start value, initialised in pi_initialise_encode*/ | ||
231 | int layS, resS, compS, prcS; | ||
232 | /** End value, initialised in pi_initialise_encode */ | ||
233 | int layE, resE, compE, prcE; | ||
234 | /** Start and end values of Tile width and height, initialised in pi_initialise_encode*/ | ||
235 | int txS,txE,tyS,tyE,dx,dy; | ||
236 | /** Temporary values for Tile parts, initialised in pi_create_encode */ | ||
237 | int lay_t, res_t, comp_t, prc_t,tx0_t,ty0_t; | ||
238 | } opj_poc_t; | ||
239 | |||
240 | /** | ||
241 | Compression parameters | ||
242 | */ | ||
243 | typedef struct opj_cparameters { | ||
244 | /** size of tile: tile_size_on = false (not in argument) or = true (in argument) */ | ||
245 | bool tile_size_on; | ||
246 | /** XTOsiz */ | ||
247 | int cp_tx0; | ||
248 | /** YTOsiz */ | ||
249 | int cp_ty0; | ||
250 | /** XTsiz */ | ||
251 | int cp_tdx; | ||
252 | /** YTsiz */ | ||
253 | int cp_tdy; | ||
254 | /** allocation by rate/distortion */ | ||
255 | int cp_disto_alloc; | ||
256 | /** allocation by fixed layer */ | ||
257 | int cp_fixed_alloc; | ||
258 | /** add fixed_quality */ | ||
259 | int cp_fixed_quality; | ||
260 | /** fixed layer */ | ||
261 | int *cp_matrice; | ||
262 | /** comment for coding */ | ||
263 | char *cp_comment; | ||
264 | /** csty : coding style */ | ||
265 | int csty; | ||
266 | /** progression order (default LRCP) */ | ||
267 | OPJ_PROG_ORDER prog_order; | ||
268 | /** progression order changes */ | ||
269 | opj_poc_t POC[32]; | ||
270 | /** number of progression order changes (POC), default to 0 */ | ||
271 | int numpocs; | ||
272 | /** number of layers */ | ||
273 | int tcp_numlayers; | ||
274 | /** rates of layers */ | ||
275 | float tcp_rates[100]; | ||
276 | /** different psnr for successive layers */ | ||
277 | float tcp_distoratio[100]; | ||
278 | /** number of resolutions */ | ||
279 | int numresolution; | ||
280 | /** initial code block width, default to 64 */ | ||
281 | int cblockw_init; | ||
282 | /** initial code block height, default to 64 */ | ||
283 | int cblockh_init; | ||
284 | /** mode switch (cblk_style) */ | ||
285 | int mode; | ||
286 | /** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */ | ||
287 | int irreversible; | ||
288 | /** region of interest: affected component in [0..3], -1 means no ROI */ | ||
289 | int roi_compno; | ||
290 | /** region of interest: upshift value */ | ||
291 | int roi_shift; | ||
292 | /* number of precinct size specifications */ | ||
293 | int res_spec; | ||
294 | /** initial precinct width */ | ||
295 | int prcw_init[J2K_MAXRLVLS]; | ||
296 | /** initial precinct height */ | ||
297 | int prch_init[J2K_MAXRLVLS]; | ||
298 | |||
299 | /**@name command line encoder parameters (not used inside the library) */ | ||
300 | /*@{*/ | ||
301 | /** input file name */ | ||
302 | char infile[OPJ_PATH_LEN]; | ||
303 | /** output file name */ | ||
304 | char outfile[OPJ_PATH_LEN]; | ||
305 | /** creation of an index file, default to 0 (false) */ | ||
306 | int index_on; | ||
307 | /** index file name */ | ||
308 | char index[OPJ_PATH_LEN]; | ||
309 | /** subimage encoding: origin image offset in x direction */ | ||
310 | int image_offset_x0; | ||
311 | /** subimage encoding: origin image offset in y direction */ | ||
312 | int image_offset_y0; | ||
313 | /** subsampling value for dx */ | ||
314 | int subsampling_dx; | ||
315 | /** subsampling value for dy */ | ||
316 | int subsampling_dy; | ||
317 | /** input file format 0: PGX, 1: PxM, 2: BMP 3:TIF*/ | ||
318 | int decod_format; | ||
319 | /** output file format 0: J2K, 1: JP2, 2: JPT */ | ||
320 | int cod_format; | ||
321 | /*@}*/ | ||
322 | |||
323 | /* UniPG>> */ | ||
324 | /**@name JPWL encoding parameters */ | ||
325 | /*@{*/ | ||
326 | /** enables writing of EPC in MH, thus activating JPWL */ | ||
327 | bool jpwl_epc_on; | ||
328 | /** error protection method for MH (0,1,16,32,37-128) */ | ||
329 | int jpwl_hprot_MH; | ||
330 | /** tile number of header protection specification (>=0) */ | ||
331 | int jpwl_hprot_TPH_tileno[JPWL_MAX_NO_TILESPECS]; | ||
332 | /** error protection methods for TPHs (0,1,16,32,37-128) */ | ||
333 | int jpwl_hprot_TPH[JPWL_MAX_NO_TILESPECS]; | ||
334 | /** tile number of packet protection specification (>=0) */ | ||
335 | int jpwl_pprot_tileno[JPWL_MAX_NO_PACKSPECS]; | ||
336 | /** packet number of packet protection specification (>=0) */ | ||
337 | int jpwl_pprot_packno[JPWL_MAX_NO_PACKSPECS]; | ||
338 | /** error protection methods for packets (0,1,16,32,37-128) */ | ||
339 | int jpwl_pprot[JPWL_MAX_NO_PACKSPECS]; | ||
340 | /** enables writing of ESD, (0=no/1/2 bytes) */ | ||
341 | int jpwl_sens_size; | ||
342 | /** sensitivity addressing size (0=auto/2/4 bytes) */ | ||
343 | int jpwl_sens_addr; | ||
344 | /** sensitivity range (0-3) */ | ||
345 | int jpwl_sens_range; | ||
346 | /** sensitivity method for MH (-1=no,0-7) */ | ||
347 | int jpwl_sens_MH; | ||
348 | /** tile number of sensitivity specification (>=0) */ | ||
349 | int jpwl_sens_TPH_tileno[JPWL_MAX_NO_TILESPECS]; | ||
350 | /** sensitivity methods for TPHs (-1=no,0-7) */ | ||
351 | int jpwl_sens_TPH[JPWL_MAX_NO_TILESPECS]; | ||
352 | /*@}*/ | ||
353 | /* <<UniPG */ | ||
354 | |||
355 | /** Digital Cinema compliance 0-not compliant, 1-compliant*/ | ||
356 | OPJ_CINEMA_MODE cp_cinema; | ||
357 | /** Maximum rate for each component. If == 0, component size limitation is not considered */ | ||
358 | int max_comp_size; | ||
359 | /** Profile name*/ | ||
360 | OPJ_RSIZ_CAPABILITIES cp_rsiz; | ||
361 | /** Tile part generation*/ | ||
362 | char tp_on; | ||
363 | /** Flag for Tile part generation*/ | ||
364 | char tp_flag; | ||
365 | /** MCT (multiple component transform) */ | ||
366 | char tcp_mct; | ||
367 | } opj_cparameters_t; | ||
368 | |||
369 | /** | ||
370 | Decompression parameters | ||
371 | */ | ||
372 | typedef struct opj_dparameters { | ||
373 | /** | ||
374 | Set the number of highest resolution levels to be discarded. | ||
375 | The image resolution is effectively divided by 2 to the power of the number of discarded levels. | ||
376 | The reduce factor is limited by the smallest total number of decomposition levels among tiles. | ||
377 | if != 0, then original dimension divided by 2^(reduce); | ||
378 | if == 0 or not used, image is decoded to the full resolution | ||
379 | */ | ||
380 | int cp_reduce; | ||
381 | /** | ||
382 | Set the maximum number of quality layers to decode. | ||
383 | If there are less quality layers than the specified number, all the quality layers are decoded. | ||
384 | if != 0, then only the first "layer" layers are decoded; | ||
385 | if == 0 or not used, all the quality layers are decoded | ||
386 | */ | ||
387 | int cp_layer; | ||
388 | |||
389 | /**@name command line encoder parameters (not used inside the library) */ | ||
390 | /*@{*/ | ||
391 | /** input file name */ | ||
392 | char infile[OPJ_PATH_LEN]; | ||
393 | /** output file name */ | ||
394 | char outfile[OPJ_PATH_LEN]; | ||
395 | /** input file format 0: J2K, 1: JP2, 2: JPT */ | ||
396 | int decod_format; | ||
397 | /** output file format 0: PGX, 1: PxM, 2: BMP */ | ||
398 | int cod_format; | ||
399 | /*@}*/ | ||
400 | |||
401 | /* UniPG>> */ | ||
402 | /**@name JPWL decoding parameters */ | ||
403 | /*@{*/ | ||
404 | /** activates the JPWL correction capabilities */ | ||
405 | bool jpwl_correct; | ||
406 | /** expected number of components */ | ||
407 | int jpwl_exp_comps; | ||
408 | /** maximum number of tiles */ | ||
409 | int jpwl_max_tiles; | ||
410 | /*@}*/ | ||
411 | /* <<UniPG */ | ||
412 | |||
413 | /** | ||
414 | Specify whether the decoding should be done on the entire codestream, or be limited to the main header | ||
415 | Limiting the decoding to the main header makes it possible to extract the characteristics of the codestream | ||
416 | if == NO_LIMITATION, the entire codestream is decoded; | ||
417 | if == LIMIT_TO_MAIN_HEADER, only the main header is decoded; | ||
418 | */ | ||
419 | OPJ_LIMIT_DECODING cp_limit_decoding; | ||
420 | |||
421 | } opj_dparameters_t; | ||
422 | |||
423 | /** Common fields between JPEG-2000 compression and decompression master structs. */ | ||
424 | |||
425 | #define opj_common_fields \ | ||
426 | opj_event_mgr_t *event_mgr; /**< pointer to the event manager */\ | ||
427 | void * client_data; /**< Available for use by application */\ | ||
428 | bool is_decompressor; /**< So common code can tell which is which */\ | ||
429 | OPJ_CODEC_FORMAT codec_format; /**< selected codec */\ | ||
430 | void *j2k_handle; /**< pointer to the J2K codec */\ | ||
431 | void *jp2_handle; /**< pointer to the JP2 codec */\ | ||
432 | void *mj2_handle /**< pointer to the MJ2 codec */ | ||
433 | |||
434 | /* Routines that are to be used by both halves of the library are declared | ||
435 | * to receive a pointer to this structure. There are no actual instances of | ||
436 | * opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t. | ||
437 | */ | ||
438 | typedef struct opj_common_struct { | ||
439 | opj_common_fields; /* Fields common to both master struct types */ | ||
440 | /* Additional fields follow in an actual opj_cinfo_t or | ||
441 | * opj_dinfo_t. All three structs must agree on these | ||
442 | * initial fields! (This would be a lot cleaner in C++.) | ||
443 | */ | ||
444 | } opj_common_struct_t; | ||
445 | |||
446 | typedef opj_common_struct_t * opj_common_ptr; | ||
447 | |||
448 | /** | ||
449 | Compression context info | ||
450 | */ | ||
451 | typedef struct opj_cinfo { | ||
452 | /** Fields shared with opj_dinfo_t */ | ||
453 | opj_common_fields; | ||
454 | /* other specific fields go here */ | ||
455 | } opj_cinfo_t; | ||
456 | |||
457 | /** | ||
458 | Decompression context info | ||
459 | */ | ||
460 | typedef struct opj_dinfo { | ||
461 | /** Fields shared with opj_cinfo_t */ | ||
462 | opj_common_fields; | ||
463 | /* other specific fields go here */ | ||
464 | } opj_dinfo_t; | ||
465 | |||
466 | /* | ||
467 | ========================================================== | ||
468 | I/O stream typedef definitions | ||
469 | ========================================================== | ||
470 | */ | ||
471 | |||
472 | /* | ||
473 | * Stream open flags. | ||
474 | */ | ||
475 | /** The stream was opened for reading. */ | ||
476 | #define OPJ_STREAM_READ 0x0001 | ||
477 | /** The stream was opened for writing. */ | ||
478 | #define OPJ_STREAM_WRITE 0x0002 | ||
479 | |||
480 | /** | ||
481 | Byte input-output stream (CIO) | ||
482 | */ | ||
483 | typedef struct opj_cio { | ||
484 | /** codec context */ | ||
485 | opj_common_ptr cinfo; | ||
486 | |||
487 | /** open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE */ | ||
488 | int openmode; | ||
489 | /** pointer to the start of the buffer */ | ||
490 | unsigned char *buffer; | ||
491 | /** buffer size in bytes */ | ||
492 | int length; | ||
493 | |||
494 | /** pointer to the start of the stream */ | ||
495 | unsigned char *start; | ||
496 | /** pointer to the end of the stream */ | ||
497 | unsigned char *end; | ||
498 | /** pointer to the current position */ | ||
499 | unsigned char *bp; | ||
500 | } opj_cio_t; | ||
501 | |||
502 | /* | ||
503 | ========================================================== | ||
504 | image typedef definitions | ||
505 | ========================================================== | ||
506 | */ | ||
507 | |||
508 | /** | ||
509 | Defines a single image component | ||
510 | */ | ||
511 | typedef struct opj_image_comp { | ||
512 | /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */ | ||
513 | int dx; | ||
514 | /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */ | ||
515 | int dy; | ||
516 | /** data width */ | ||
517 | int w; | ||
518 | /** data height */ | ||
519 | int h; | ||
520 | /** x component offset compared to the whole image */ | ||
521 | int x0; | ||
522 | /** y component offset compared to the whole image */ | ||
523 | int y0; | ||
524 | /** precision */ | ||
525 | int prec; | ||
526 | /** image depth in bits */ | ||
527 | int bpp; | ||
528 | /** signed (1) / unsigned (0) */ | ||
529 | int sgnd; | ||
530 | /** number of decoded resolution */ | ||
531 | int resno_decoded; | ||
532 | /** number of division by 2 of the out image compared to the original size of image */ | ||
533 | int factor; | ||
534 | /** image component data */ | ||
535 | int *data; | ||
536 | } opj_image_comp_t; | ||
537 | |||
538 | /** | ||
539 | Defines image data and characteristics | ||
540 | */ | ||
541 | typedef struct opj_image { | ||
542 | /** XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area */ | ||
543 | int x0; | ||
544 | /** YOsiz: vertical offset from the origin of the reference grid to the top side of the image area */ | ||
545 | int y0; | ||
546 | /** Xsiz: width of the reference grid */ | ||
547 | int x1; | ||
548 | /** Ysiz: height of the reference grid */ | ||
549 | int y1; | ||
550 | /** number of components in the image */ | ||
551 | int numcomps; | ||
552 | /** color space: sRGB, Greyscale or YUV */ | ||
553 | OPJ_COLOR_SPACE color_space; | ||
554 | /** image components */ | ||
555 | opj_image_comp_t *comps; | ||
556 | } opj_image_t; | ||
557 | |||
558 | /** | ||
559 | Component parameters structure used by the opj_image_create function | ||
560 | */ | ||
561 | typedef struct opj_image_comptparm { | ||
562 | /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */ | ||
563 | int dx; | ||
564 | /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */ | ||
565 | int dy; | ||
566 | /** data width */ | ||
567 | int w; | ||
568 | /** data height */ | ||
569 | int h; | ||
570 | /** x component offset compared to the whole image */ | ||
571 | int x0; | ||
572 | /** y component offset compared to the whole image */ | ||
573 | int y0; | ||
574 | /** precision */ | ||
575 | int prec; | ||
576 | /** image depth in bits */ | ||
577 | int bpp; | ||
578 | /** signed (1) / unsigned (0) */ | ||
579 | int sgnd; | ||
580 | } opj_image_cmptparm_t; | ||
581 | |||
582 | #ifdef __cplusplus | ||
583 | extern "C" { | ||
584 | #endif | ||
585 | |||
586 | |||
587 | /* | ||
588 | ========================================================== | ||
589 | openjpeg version | ||
590 | ========================================================== | ||
591 | */ | ||
592 | |||
593 | OPJ_API const char * OPJ_CALLCONV opj_version(); | ||
594 | |||
595 | /* | ||
596 | ========================================================== | ||
597 | image functions definitions | ||
598 | ========================================================== | ||
599 | */ | ||
600 | |||
601 | /** | ||
602 | Create an image | ||
603 | @param numcmpts number of components | ||
604 | @param cmptparms components parameters | ||
605 | @param clrspc image color space | ||
606 | @return returns a new image structure if successful, returns NULL otherwise | ||
607 | */ | ||
608 | OPJ_API opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); | ||
609 | |||
610 | /** | ||
611 | Deallocate any resources associated with an image | ||
612 | @param image image to be destroyed | ||
613 | */ | ||
614 | OPJ_API void OPJ_CALLCONV opj_image_destroy(opj_image_t *image); | ||
615 | |||
616 | /* | ||
617 | ========================================================== | ||
618 | stream functions definitions | ||
619 | ========================================================== | ||
620 | */ | ||
621 | |||
622 | /** | ||
623 | Open and allocate a memory stream for read / write. | ||
624 | On reading, the user must provide a buffer containing encoded data. The buffer will be | ||
625 | wrapped by the returned CIO handle. | ||
626 | On writing, buffer parameters must be set to 0: a buffer will be allocated by the library | ||
627 | to contain encoded data. | ||
628 | @param cinfo Codec context info | ||
629 | @param buffer Reading: buffer address. Writing: NULL | ||
630 | @param length Reading: buffer length. Writing: 0 | ||
631 | @return Returns a CIO handle if successful, returns NULL otherwise | ||
632 | */ | ||
633 | OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length); | ||
634 | |||
635 | /** | ||
636 | Close and free a CIO handle | ||
637 | @param cio CIO handle to free | ||
638 | */ | ||
639 | OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio); | ||
640 | |||
641 | /** | ||
642 | Get position in byte stream | ||
643 | @param cio CIO handle | ||
644 | @return Returns the position in bytes | ||
645 | */ | ||
646 | OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio); | ||
647 | /** | ||
648 | Set position in byte stream | ||
649 | @param cio CIO handle | ||
650 | @param pos Position, in number of bytes, from the beginning of the stream | ||
651 | */ | ||
652 | OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos); | ||
653 | |||
654 | /* | ||
655 | ========================================================== | ||
656 | event manager functions definitions | ||
657 | ========================================================== | ||
658 | */ | ||
659 | |||
660 | OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context); | ||
661 | |||
662 | /* | ||
663 | ========================================================== | ||
664 | codec functions definitions | ||
665 | ========================================================== | ||
666 | */ | ||
667 | /** | ||
668 | Creates a J2K/JPT/JP2 decompression structure | ||
669 | @param format Decoder to select | ||
670 | @return Returns a handle to a decompressor if successful, returns NULL otherwise | ||
671 | */ | ||
672 | OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format); | ||
673 | /** | ||
674 | Destroy a decompressor handle | ||
675 | @param dinfo decompressor handle to destroy | ||
676 | */ | ||
677 | OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo); | ||
678 | /** | ||
679 | Set decoding parameters to default values | ||
680 | @param parameters Decompression parameters | ||
681 | */ | ||
682 | OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters); | ||
683 | /** | ||
684 | Setup the decoder decoding parameters using user parameters. | ||
685 | Decoding parameters are returned in j2k->cp. | ||
686 | @param dinfo decompressor handle | ||
687 | @param parameters decompression parameters | ||
688 | */ | ||
689 | OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters); | ||
690 | /** | ||
691 | Decode an image from a JPEG-2000 codestream | ||
692 | @param dinfo decompressor handle | ||
693 | @param cio Input buffer stream | ||
694 | @return Returns a decoded image if successful, returns NULL otherwise | ||
695 | */ | ||
696 | OPJ_API opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio); | ||
697 | /** | ||
698 | Creates a J2K/JP2 compression structure | ||
699 | @param format Coder to select | ||
700 | @return Returns a handle to a compressor if successful, returns NULL otherwise | ||
701 | */ | ||
702 | OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format); | ||
703 | /** | ||
704 | Destroy a compressor handle | ||
705 | @param cinfo compressor handle to destroy | ||
706 | */ | ||
707 | OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo); | ||
708 | /** | ||
709 | Set encoding parameters to default values, that means : | ||
710 | <ul> | ||
711 | <li>Lossless | ||
712 | <li>1 tile | ||
713 | <li>Size of precinct : 2^15 x 2^15 (means 1 precinct) | ||
714 | <li>Size of code-block : 64 x 64 | ||
715 | <li>Number of resolutions: 6 | ||
716 | <li>No SOP marker in the codestream | ||
717 | <li>No EPH marker in the codestream | ||
718 | <li>No sub-sampling in x or y direction | ||
719 | <li>No mode switch activated | ||
720 | <li>Progression order: LRCP | ||
721 | <li>No index file | ||
722 | <li>No ROI upshifted | ||
723 | <li>No offset of the origin of the image | ||
724 | <li>No offset of the origin of the tiles | ||
725 | <li>Reversible DWT 5-3 | ||
726 | </ul> | ||
727 | @param parameters Compression parameters | ||
728 | */ | ||
729 | OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters); | ||
730 | /** | ||
731 | Setup the encoder parameters using the current image and using user parameters. | ||
732 | @param cinfo compressor handle | ||
733 | @param parameters compression parameters | ||
734 | @param image input filled image | ||
735 | */ | ||
736 | OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image); | ||
737 | /** | ||
738 | Encode an image into a JPEG-2000 codestream | ||
739 | @param cinfo compressor handle | ||
740 | @param cio Output buffer stream | ||
741 | @param image Image to encode | ||
742 | @param index Name of the index file if required, NULL otherwise | ||
743 | @return Returns true if successful, returns false otherwise | ||
744 | */ | ||
745 | OPJ_API bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index); | ||
746 | |||
747 | #ifdef __cplusplus | ||
748 | } | ||
749 | #endif | ||
750 | |||
751 | #endif /* OPENJPEG_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/opj_includes.h b/libraries/openjpeg-libsl/libopenjpeg/opj_includes.h new file mode 100644 index 0000000..72152a5 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/opj_includes.h | |||
@@ -0,0 +1,102 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2005, Hervé Drolon, FreeImage Team | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
24 | * POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | #ifndef OPJ_INCLUDES_H | ||
27 | #define OPJ_INCLUDES_H | ||
28 | |||
29 | /* | ||
30 | ========================================================== | ||
31 | Standard includes used by the library | ||
32 | ========================================================== | ||
33 | */ | ||
34 | #include <memory.h> | ||
35 | #include <stdlib.h> | ||
36 | #include <string.h> | ||
37 | #include <math.h> | ||
38 | #include <float.h> | ||
39 | #include <time.h> | ||
40 | #include <stdio.h> | ||
41 | #include <stdarg.h> | ||
42 | #include <ctype.h> | ||
43 | |||
44 | /* | ||
45 | ========================================================== | ||
46 | OpenJPEG interface | ||
47 | ========================================================== | ||
48 | */ | ||
49 | #include "openjpeg.h" | ||
50 | |||
51 | /* | ||
52 | ========================================================== | ||
53 | OpenJPEG modules | ||
54 | ========================================================== | ||
55 | */ | ||
56 | |||
57 | /* | ||
58 | The inline keyword is supported by C99 but not by C90. | ||
59 | Most compilers implement their own version of this keyword ... | ||
60 | */ | ||
61 | #ifndef INLINE | ||
62 | #if defined(_MSC_VER) | ||
63 | #define INLINE __inline | ||
64 | #elif defined(__GNUC__) | ||
65 | #define INLINE __inline__ | ||
66 | #elif defined(__MWERKS__) | ||
67 | #define INLINE inline | ||
68 | #else | ||
69 | /* add other compilers here ... */ | ||
70 | #define INLINE | ||
71 | #endif /* defined(<Compiler>) */ | ||
72 | #endif /* INLINE */ | ||
73 | |||
74 | #include "j2k_lib.h" | ||
75 | #include "event.h" | ||
76 | #include "cio.h" | ||
77 | |||
78 | #include "image.h" | ||
79 | #include "j2k.h" | ||
80 | #include "jp2.h" | ||
81 | #include "jpt.h" | ||
82 | |||
83 | #include "mqc.h" | ||
84 | #include "raw.h" | ||
85 | #include "bio.h" | ||
86 | #include "tgt.h" | ||
87 | #include "pi.h" | ||
88 | #include "tcd.h" | ||
89 | #include "t1.h" | ||
90 | #include "dwt.h" | ||
91 | #include "t2.h" | ||
92 | #include "mct.h" | ||
93 | #include "int.h" | ||
94 | #include "fix.h" | ||
95 | |||
96 | /* JPWL>> */ | ||
97 | #ifdef USE_JPWL | ||
98 | #include "../jpwl/jpwl.h" | ||
99 | #endif /* USE_JPWL */ | ||
100 | /* <<JPWL */ | ||
101 | |||
102 | #endif /* OPJ_INCLUDES_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/pi.c b/libraries/openjpeg-libsl/libopenjpeg/pi.c new file mode 100644 index 0000000..a012f51 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/pi.c | |||
@@ -0,0 +1,1078 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2006-2007, Parvatha Elangovan | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "opj_includes.h" | ||
34 | |||
35 | /** @defgroup PI PI - Implementation of a packet iterator */ | ||
36 | /*@{*/ | ||
37 | |||
38 | /** @name Local static functions */ | ||
39 | /*@{*/ | ||
40 | |||
41 | /** | ||
42 | Get next packet in layer-resolution-component-precinct order. | ||
43 | @param pi packet iterator to modify | ||
44 | @return returns false if pi pointed to the last packet or else returns true | ||
45 | */ | ||
46 | static bool pi_next_lrcp(opj_pi_iterator_t * pi); | ||
47 | /** | ||
48 | Get next packet in resolution-layer-component-precinct order. | ||
49 | @param pi packet iterator to modify | ||
50 | @return returns false if pi pointed to the last packet or else returns true | ||
51 | */ | ||
52 | static bool pi_next_rlcp(opj_pi_iterator_t * pi); | ||
53 | /** | ||
54 | Get next packet in resolution-precinct-component-layer order. | ||
55 | @param pi packet iterator to modify | ||
56 | @return returns false if pi pointed to the last packet or else returns true | ||
57 | */ | ||
58 | static bool pi_next_rpcl(opj_pi_iterator_t * pi); | ||
59 | /** | ||
60 | Get next packet in precinct-component-resolution-layer order. | ||
61 | @param pi packet iterator to modify | ||
62 | @return returns false if pi pointed to the last packet or else returns true | ||
63 | */ | ||
64 | static bool pi_next_pcrl(opj_pi_iterator_t * pi); | ||
65 | /** | ||
66 | Get next packet in component-precinct-resolution-layer order. | ||
67 | @param pi packet iterator to modify | ||
68 | @return returns false if pi pointed to the last packet or else returns true | ||
69 | */ | ||
70 | static bool pi_next_cprl(opj_pi_iterator_t * pi); | ||
71 | |||
72 | /*@}*/ | ||
73 | |||
74 | /*@}*/ | ||
75 | |||
76 | /* | ||
77 | ========================================================== | ||
78 | local functions | ||
79 | ========================================================== | ||
80 | */ | ||
81 | |||
82 | static bool pi_next_lrcp(opj_pi_iterator_t * pi) { | ||
83 | opj_pi_comp_t *comp = NULL; | ||
84 | opj_pi_resolution_t *res = NULL; | ||
85 | long index = 0; | ||
86 | |||
87 | if (!pi->first) { | ||
88 | comp = &pi->comps[pi->compno]; | ||
89 | res = &comp->resolutions[pi->resno]; | ||
90 | goto LABEL_SKIP; | ||
91 | } else { | ||
92 | pi->first = 0; | ||
93 | } | ||
94 | |||
95 | for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { | ||
96 | for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; | ||
97 | pi->resno++) { | ||
98 | for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { | ||
99 | comp = &pi->comps[pi->compno]; | ||
100 | if (pi->resno >= comp->numresolutions) { | ||
101 | continue; | ||
102 | } | ||
103 | res = &comp->resolutions[pi->resno]; | ||
104 | if (!pi->tp_on){ | ||
105 | pi->poc.precno1 = res->pw * res->ph; | ||
106 | } | ||
107 | for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) { | ||
108 | index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; | ||
109 | if (!pi->include[index]) { | ||
110 | pi->include[index] = 1; | ||
111 | return true; | ||
112 | } | ||
113 | LABEL_SKIP:; | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return false; | ||
120 | } | ||
121 | |||
122 | static bool pi_next_rlcp(opj_pi_iterator_t * pi) { | ||
123 | opj_pi_comp_t *comp = NULL; | ||
124 | opj_pi_resolution_t *res = NULL; | ||
125 | long index = 0; | ||
126 | |||
127 | if (!pi->first) { | ||
128 | comp = &pi->comps[pi->compno]; | ||
129 | res = &comp->resolutions[pi->resno]; | ||
130 | goto LABEL_SKIP; | ||
131 | } else { | ||
132 | pi->first = 0; | ||
133 | } | ||
134 | |||
135 | for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { | ||
136 | for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { | ||
137 | for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { | ||
138 | comp = &pi->comps[pi->compno]; | ||
139 | if (pi->resno >= comp->numresolutions) { | ||
140 | continue; | ||
141 | } | ||
142 | res = &comp->resolutions[pi->resno]; | ||
143 | if(!pi->tp_on){ | ||
144 | pi->poc.precno1 = res->pw * res->ph; | ||
145 | } | ||
146 | for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) { | ||
147 | index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; | ||
148 | if (!pi->include[index]) { | ||
149 | pi->include[index] = 1; | ||
150 | return true; | ||
151 | } | ||
152 | LABEL_SKIP:; | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | return false; | ||
159 | } | ||
160 | |||
161 | static bool pi_next_rpcl(opj_pi_iterator_t * pi) { | ||
162 | opj_pi_comp_t *comp = NULL; | ||
163 | opj_pi_resolution_t *res = NULL; | ||
164 | long index = 0; | ||
165 | |||
166 | if (!pi->first) { | ||
167 | goto LABEL_SKIP; | ||
168 | } else { | ||
169 | int compno, resno; | ||
170 | pi->first = 0; | ||
171 | pi->dx = 0; | ||
172 | pi->dy = 0; | ||
173 | for (compno = 0; compno < pi->numcomps; compno++) { | ||
174 | comp = &pi->comps[compno]; | ||
175 | for (resno = 0; resno < comp->numresolutions; resno++) { | ||
176 | int dx, dy; | ||
177 | res = &comp->resolutions[resno]; | ||
178 | dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); | ||
179 | dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); | ||
180 | pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); | ||
181 | pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | if (!pi->tp_on){ | ||
186 | pi->poc.ty0 = pi->ty0; | ||
187 | pi->poc.tx0 = pi->tx0; | ||
188 | pi->poc.ty1 = pi->ty1; | ||
189 | pi->poc.tx1 = pi->tx1; | ||
190 | } | ||
191 | for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { | ||
192 | for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) { | ||
193 | for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { | ||
194 | for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { | ||
195 | int levelno; | ||
196 | int trx0, try0; | ||
197 | int trx1, try1; | ||
198 | int rpx, rpy; | ||
199 | int prci, prcj; | ||
200 | comp = &pi->comps[pi->compno]; | ||
201 | if (pi->resno >= comp->numresolutions) { | ||
202 | continue; | ||
203 | } | ||
204 | res = &comp->resolutions[pi->resno]; | ||
205 | levelno = comp->numresolutions - 1 - pi->resno; | ||
206 | trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); | ||
207 | try0 = int_ceildiv(pi->ty0, comp->dy << levelno); | ||
208 | trx1 = int_ceildiv(pi->tx1, comp->dx << levelno); | ||
209 | try1 = int_ceildiv(pi->ty1, comp->dy << levelno); | ||
210 | rpx = res->pdx + levelno; | ||
211 | rpy = res->pdy + levelno; | ||
212 | if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ | ||
213 | continue; | ||
214 | } | ||
215 | if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ | ||
216 | continue; | ||
217 | } | ||
218 | |||
219 | if ((res->pw==0)||(res->pw==0)) continue; | ||
220 | |||
221 | if ((trx0==trx1)||(try0==try1)) continue; | ||
222 | |||
223 | prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) | ||
224 | - int_floordivpow2(trx0, res->pdx); | ||
225 | prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) | ||
226 | - int_floordivpow2(try0, res->pdy); | ||
227 | pi->precno = prci + prcj * res->pw; | ||
228 | for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { | ||
229 | index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; | ||
230 | if (!pi->include[index]) { | ||
231 | pi->include[index] = 1; | ||
232 | return true; | ||
233 | } | ||
234 | LABEL_SKIP:; | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | |||
241 | return false; | ||
242 | } | ||
243 | |||
244 | static bool pi_next_pcrl(opj_pi_iterator_t * pi) { | ||
245 | opj_pi_comp_t *comp = NULL; | ||
246 | opj_pi_resolution_t *res = NULL; | ||
247 | long index = 0; | ||
248 | |||
249 | if (!pi->first) { | ||
250 | comp = &pi->comps[pi->compno]; | ||
251 | goto LABEL_SKIP; | ||
252 | } else { | ||
253 | int compno, resno; | ||
254 | pi->first = 0; | ||
255 | pi->dx = 0; | ||
256 | pi->dy = 0; | ||
257 | for (compno = 0; compno < pi->numcomps; compno++) { | ||
258 | comp = &pi->comps[compno]; | ||
259 | for (resno = 0; resno < comp->numresolutions; resno++) { | ||
260 | int dx, dy; | ||
261 | res = &comp->resolutions[resno]; | ||
262 | dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); | ||
263 | dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); | ||
264 | pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); | ||
265 | pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); | ||
266 | } | ||
267 | } | ||
268 | } | ||
269 | if (!pi->tp_on){ | ||
270 | pi->poc.ty0 = pi->ty0; | ||
271 | pi->poc.tx0 = pi->tx0; | ||
272 | pi->poc.ty1 = pi->ty1; | ||
273 | pi->poc.tx1 = pi->tx1; | ||
274 | } | ||
275 | for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) { | ||
276 | for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { | ||
277 | for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { | ||
278 | comp = &pi->comps[pi->compno]; | ||
279 | for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { | ||
280 | int levelno; | ||
281 | int trx0, try0; | ||
282 | int trx1, try1; | ||
283 | int rpx, rpy; | ||
284 | int prci, prcj; | ||
285 | res = &comp->resolutions[pi->resno]; | ||
286 | levelno = comp->numresolutions - 1 - pi->resno; | ||
287 | trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); | ||
288 | try0 = int_ceildiv(pi->ty0, comp->dy << levelno); | ||
289 | trx1 = int_ceildiv(pi->tx1, comp->dx << levelno); | ||
290 | try1 = int_ceildiv(pi->ty1, comp->dy << levelno); | ||
291 | rpx = res->pdx + levelno; | ||
292 | rpy = res->pdy + levelno; | ||
293 | if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ | ||
294 | continue; | ||
295 | } | ||
296 | if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ | ||
297 | continue; | ||
298 | } | ||
299 | |||
300 | if ((res->pw==0)||(res->pw==0)) continue; | ||
301 | |||
302 | if ((trx0==trx1)||(try0==try1)) continue; | ||
303 | |||
304 | prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) | ||
305 | - int_floordivpow2(trx0, res->pdx); | ||
306 | prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) | ||
307 | - int_floordivpow2(try0, res->pdy); | ||
308 | pi->precno = prci + prcj * res->pw; | ||
309 | for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { | ||
310 | index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; | ||
311 | if (!pi->include[index]) { | ||
312 | pi->include[index] = 1; | ||
313 | return true; | ||
314 | } | ||
315 | LABEL_SKIP:; | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | } | ||
320 | } | ||
321 | |||
322 | return false; | ||
323 | } | ||
324 | |||
325 | static bool pi_next_cprl(opj_pi_iterator_t * pi) { | ||
326 | opj_pi_comp_t *comp = NULL; | ||
327 | opj_pi_resolution_t *res = NULL; | ||
328 | long index = 0; | ||
329 | |||
330 | if (!pi->first) { | ||
331 | comp = &pi->comps[pi->compno]; | ||
332 | goto LABEL_SKIP; | ||
333 | } else { | ||
334 | pi->first = 0; | ||
335 | } | ||
336 | |||
337 | for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { | ||
338 | int resno; | ||
339 | comp = &pi->comps[pi->compno]; | ||
340 | pi->dx = 0; | ||
341 | pi->dy = 0; | ||
342 | for (resno = 0; resno < comp->numresolutions; resno++) { | ||
343 | int dx, dy; | ||
344 | res = &comp->resolutions[resno]; | ||
345 | dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); | ||
346 | dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); | ||
347 | pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); | ||
348 | pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); | ||
349 | } | ||
350 | if (!pi->tp_on){ | ||
351 | pi->poc.ty0 = pi->ty0; | ||
352 | pi->poc.tx0 = pi->tx0; | ||
353 | pi->poc.ty1 = pi->ty1; | ||
354 | pi->poc.tx1 = pi->tx1; | ||
355 | } | ||
356 | for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) { | ||
357 | for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { | ||
358 | for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { | ||
359 | int levelno; | ||
360 | int trx0, try0; | ||
361 | int trx1, try1; | ||
362 | int rpx, rpy; | ||
363 | int prci, prcj; | ||
364 | res = &comp->resolutions[pi->resno]; | ||
365 | levelno = comp->numresolutions - 1 - pi->resno; | ||
366 | trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); | ||
367 | try0 = int_ceildiv(pi->ty0, comp->dy << levelno); | ||
368 | trx1 = int_ceildiv(pi->tx1, comp->dx << levelno); | ||
369 | try1 = int_ceildiv(pi->ty1, comp->dy << levelno); | ||
370 | rpx = res->pdx + levelno; | ||
371 | rpy = res->pdy + levelno; | ||
372 | if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpx))))){ | ||
373 | continue; | ||
374 | } | ||
375 | if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){ | ||
376 | continue; | ||
377 | } | ||
378 | |||
379 | if ((res->pw==0)||(res->pw==0)) continue; | ||
380 | |||
381 | if ((trx0==trx1)||(try0==try1)) continue; | ||
382 | |||
383 | prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) | ||
384 | - int_floordivpow2(trx0, res->pdx); | ||
385 | prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) | ||
386 | - int_floordivpow2(try0, res->pdy); | ||
387 | pi->precno = prci + prcj * res->pw; | ||
388 | for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { | ||
389 | index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; | ||
390 | if (!pi->include[index]) { | ||
391 | pi->include[index] = 1; | ||
392 | return true; | ||
393 | } | ||
394 | LABEL_SKIP:; | ||
395 | } | ||
396 | } | ||
397 | } | ||
398 | } | ||
399 | } | ||
400 | |||
401 | return false; | ||
402 | } | ||
403 | |||
404 | /* | ||
405 | ========================================================== | ||
406 | Packet iterator interface | ||
407 | ========================================================== | ||
408 | */ | ||
409 | |||
410 | opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) { | ||
411 | int p, q; | ||
412 | int compno, resno, pino; | ||
413 | opj_pi_iterator_t *pi = NULL; | ||
414 | opj_tcp_t *tcp = NULL; | ||
415 | opj_tccp_t *tccp = NULL; | ||
416 | size_t array_size; | ||
417 | |||
418 | tcp = &cp->tcps[tileno]; | ||
419 | |||
420 | array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t); | ||
421 | pi = (opj_pi_iterator_t *) opj_malloc(array_size); | ||
422 | if(!pi) { | ||
423 | /* TODO: throw an error */ | ||
424 | return NULL; | ||
425 | } | ||
426 | |||
427 | for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ | ||
428 | int maxres = 0; | ||
429 | int maxprec = 0; | ||
430 | p = tileno % cp->tw; | ||
431 | q = tileno / cp->tw; | ||
432 | |||
433 | pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0); | ||
434 | pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0); | ||
435 | pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); | ||
436 | pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); | ||
437 | pi[pino].numcomps = image->numcomps; | ||
438 | |||
439 | array_size = image->numcomps * sizeof(opj_pi_comp_t); | ||
440 | pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size); | ||
441 | if(!pi[pino].comps) { | ||
442 | /* TODO: throw an error */ | ||
443 | pi_destroy(pi, cp, tileno); | ||
444 | return NULL; | ||
445 | } | ||
446 | memset(pi[pino].comps, 0, array_size); | ||
447 | |||
448 | for (compno = 0; compno < pi->numcomps; compno++) { | ||
449 | int tcx0, tcy0, tcx1, tcy1; | ||
450 | opj_pi_comp_t *comp = &pi[pino].comps[compno]; | ||
451 | tccp = &tcp->tccps[compno]; | ||
452 | comp->dx = image->comps[compno].dx; | ||
453 | comp->dy = image->comps[compno].dy; | ||
454 | comp->numresolutions = tccp->numresolutions; | ||
455 | |||
456 | array_size = comp->numresolutions * sizeof(opj_pi_resolution_t); | ||
457 | comp->resolutions = (opj_pi_resolution_t *) opj_malloc(array_size); | ||
458 | if(!comp->resolutions) { | ||
459 | /* TODO: throw an error */ | ||
460 | pi_destroy(pi, cp, tileno); | ||
461 | return NULL; | ||
462 | } | ||
463 | |||
464 | tcx0 = int_ceildiv(pi->tx0, comp->dx); | ||
465 | tcy0 = int_ceildiv(pi->ty0, comp->dy); | ||
466 | tcx1 = int_ceildiv(pi->tx1, comp->dx); | ||
467 | tcy1 = int_ceildiv(pi->ty1, comp->dy); | ||
468 | if (comp->numresolutions > maxres) { | ||
469 | maxres = comp->numresolutions; | ||
470 | } | ||
471 | |||
472 | for (resno = 0; resno < comp->numresolutions; resno++) { | ||
473 | int levelno; | ||
474 | int rx0, ry0, rx1, ry1; | ||
475 | int px0, py0, px1, py1; | ||
476 | opj_pi_resolution_t *res = &comp->resolutions[resno]; | ||
477 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
478 | res->pdx = tccp->prcw[resno]; | ||
479 | res->pdy = tccp->prch[resno]; | ||
480 | } else { | ||
481 | res->pdx = 15; | ||
482 | res->pdy = 15; | ||
483 | } | ||
484 | levelno = comp->numresolutions - 1 - resno; | ||
485 | rx0 = int_ceildivpow2(tcx0, levelno); | ||
486 | ry0 = int_ceildivpow2(tcy0, levelno); | ||
487 | rx1 = int_ceildivpow2(tcx1, levelno); | ||
488 | ry1 = int_ceildivpow2(tcy1, levelno); | ||
489 | px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; | ||
490 | py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; | ||
491 | px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; | ||
492 | py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; | ||
493 | res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); | ||
494 | res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); | ||
495 | |||
496 | if (res->pw*res->ph > maxprec) { | ||
497 | maxprec = res->pw*res->ph; | ||
498 | } | ||
499 | |||
500 | } | ||
501 | } | ||
502 | |||
503 | tccp = &tcp->tccps[0]; | ||
504 | pi[pino].step_p = 1; | ||
505 | pi[pino].step_c = maxprec * pi[pino].step_p; | ||
506 | pi[pino].step_r = image->numcomps * pi[pino].step_c; | ||
507 | pi[pino].step_l = maxres * pi[pino].step_r; | ||
508 | |||
509 | if (pino == 0) { | ||
510 | array_size = image->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int); | ||
511 | pi[pino].include = (short int *) opj_malloc(array_size); | ||
512 | if(!pi[pino].include) { | ||
513 | /* TODO: throw an error */ | ||
514 | pi_destroy(pi, cp, tileno); | ||
515 | return NULL; | ||
516 | } | ||
517 | } | ||
518 | else { | ||
519 | pi[pino].include = pi[pino - 1].include; | ||
520 | } | ||
521 | |||
522 | if (tcp->POC == 0) { | ||
523 | pi[pino].first = 1; | ||
524 | pi[pino].poc.resno0 = 0; | ||
525 | pi[pino].poc.compno0 = 0; | ||
526 | pi[pino].poc.layno1 = tcp->numlayers; | ||
527 | pi[pino].poc.resno1 = maxres; | ||
528 | pi[pino].poc.compno1 = image->numcomps; | ||
529 | pi[pino].poc.prg = tcp->prg; | ||
530 | } else { | ||
531 | pi[pino].first = 1; | ||
532 | pi[pino].poc.resno0 = tcp->pocs[pino].resno0; | ||
533 | pi[pino].poc.compno0 = tcp->pocs[pino].compno0; | ||
534 | pi[pino].poc.layno1 = tcp->pocs[pino].layno1; | ||
535 | pi[pino].poc.resno1 = tcp->pocs[pino].resno1; | ||
536 | pi[pino].poc.compno1 = tcp->pocs[pino].compno1; | ||
537 | pi[pino].poc.prg = tcp->pocs[pino].prg; | ||
538 | } | ||
539 | pi[pino].poc.layno0 = 0; | ||
540 | pi[pino].poc.precno0 = 0; | ||
541 | pi[pino].poc.precno1 = maxprec; | ||
542 | |||
543 | } | ||
544 | |||
545 | return pi; | ||
546 | } | ||
547 | |||
548 | |||
549 | opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ | ||
550 | int p, q, pino; | ||
551 | int compno, resno; | ||
552 | int maxres = 0; | ||
553 | int maxprec = 0; | ||
554 | opj_pi_iterator_t *pi = NULL; | ||
555 | opj_tcp_t *tcp = NULL; | ||
556 | opj_tccp_t *tccp = NULL; | ||
557 | size_t array_size; | ||
558 | |||
559 | tcp = &cp->tcps[tileno]; | ||
560 | |||
561 | array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t); | ||
562 | pi = (opj_pi_iterator_t *) opj_malloc(array_size); | ||
563 | if(!pi) { return NULL;} | ||
564 | pi->tp_on = cp->tp_on; | ||
565 | |||
566 | for(pino = 0;pino < tcp->numpocs+1 ; pino ++){ | ||
567 | p = tileno % cp->tw; | ||
568 | q = tileno / cp->tw; | ||
569 | |||
570 | pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0); | ||
571 | pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0); | ||
572 | pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); | ||
573 | pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); | ||
574 | pi[pino].numcomps = image->numcomps; | ||
575 | |||
576 | array_size = image->numcomps * sizeof(opj_pi_comp_t); | ||
577 | pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size); | ||
578 | if(!pi[pino].comps) { | ||
579 | pi_destroy(pi, cp, tileno); | ||
580 | return NULL; | ||
581 | } | ||
582 | memset(pi[pino].comps, 0, array_size); | ||
583 | |||
584 | for (compno = 0; compno < pi[pino].numcomps; compno++) { | ||
585 | int tcx0, tcy0, tcx1, tcy1; | ||
586 | opj_pi_comp_t *comp = &pi[pino].comps[compno]; | ||
587 | tccp = &tcp->tccps[compno]; | ||
588 | comp->dx = image->comps[compno].dx; | ||
589 | comp->dy = image->comps[compno].dy; | ||
590 | comp->numresolutions = tccp->numresolutions; | ||
591 | |||
592 | array_size = comp->numresolutions * sizeof(opj_pi_resolution_t); | ||
593 | comp->resolutions = (opj_pi_resolution_t *) opj_malloc(array_size); | ||
594 | if(!comp->resolutions) { | ||
595 | pi_destroy(pi, cp, tileno); | ||
596 | return NULL; | ||
597 | } | ||
598 | |||
599 | tcx0 = int_ceildiv(pi[pino].tx0, comp->dx); | ||
600 | tcy0 = int_ceildiv(pi[pino].ty0, comp->dy); | ||
601 | tcx1 = int_ceildiv(pi[pino].tx1, comp->dx); | ||
602 | tcy1 = int_ceildiv(pi[pino].ty1, comp->dy); | ||
603 | if (comp->numresolutions > maxres) { | ||
604 | maxres = comp->numresolutions; | ||
605 | } | ||
606 | |||
607 | for (resno = 0; resno < comp->numresolutions; resno++) { | ||
608 | int levelno; | ||
609 | int rx0, ry0, rx1, ry1; | ||
610 | int px0, py0, px1, py1; | ||
611 | opj_pi_resolution_t *res = &comp->resolutions[resno]; | ||
612 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
613 | res->pdx = tccp->prcw[resno]; | ||
614 | res->pdy = tccp->prch[resno]; | ||
615 | } else { | ||
616 | res->pdx = 15; | ||
617 | res->pdy = 15; | ||
618 | } | ||
619 | levelno = comp->numresolutions - 1 - resno; | ||
620 | rx0 = int_ceildivpow2(tcx0, levelno); | ||
621 | ry0 = int_ceildivpow2(tcy0, levelno); | ||
622 | rx1 = int_ceildivpow2(tcx1, levelno); | ||
623 | ry1 = int_ceildivpow2(tcy1, levelno); | ||
624 | px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; | ||
625 | py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; | ||
626 | px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; | ||
627 | py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; | ||
628 | res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); | ||
629 | res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); | ||
630 | |||
631 | if (res->pw*res->ph > maxprec) { | ||
632 | maxprec = res->pw * res->ph; | ||
633 | } | ||
634 | } | ||
635 | } | ||
636 | |||
637 | tccp = &tcp->tccps[0]; | ||
638 | pi[pino].step_p = 1; | ||
639 | pi[pino].step_c = maxprec * pi[pino].step_p; | ||
640 | pi[pino].step_r = image->numcomps * pi[pino].step_c; | ||
641 | pi[pino].step_l = maxres * pi[pino].step_r; | ||
642 | |||
643 | for (compno = 0; compno < pi->numcomps; compno++) { | ||
644 | opj_pi_comp_t *comp = &pi->comps[compno]; | ||
645 | for (resno = 0; resno < comp->numresolutions; resno++) { | ||
646 | int dx, dy; | ||
647 | opj_pi_resolution_t *res = &comp->resolutions[resno]; | ||
648 | dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); | ||
649 | dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); | ||
650 | pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx); | ||
651 | pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy); | ||
652 | } | ||
653 | } | ||
654 | |||
655 | if (pino == 0) { | ||
656 | array_size = tcp->numlayers * pi[pino].step_l * sizeof(short int); | ||
657 | pi[pino].include = (short int *) opj_malloc(array_size); | ||
658 | if(!pi[pino].include) { | ||
659 | pi_destroy(pi, cp, tileno); | ||
660 | return NULL; | ||
661 | } | ||
662 | } | ||
663 | else { | ||
664 | pi[pino].include = pi[pino - 1].include; | ||
665 | } | ||
666 | |||
667 | /* Generation of boundaries for each prog flag*/ | ||
668 | if(tcp->POC & (t2_mode == FINAL_PASS)){ | ||
669 | tcp->pocs[pino].compS= tcp->pocs[pino].compno0; | ||
670 | tcp->pocs[pino].compE= tcp->pocs[pino].compno1; | ||
671 | tcp->pocs[pino].resS = tcp->pocs[pino].resno0; | ||
672 | tcp->pocs[pino].resE = tcp->pocs[pino].resno1; | ||
673 | tcp->pocs[pino].layE = tcp->pocs[pino].layno1; | ||
674 | tcp->pocs[pino].prg = tcp->pocs[pino].prg1; | ||
675 | if (pino > 0) | ||
676 | tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0; | ||
677 | }else { | ||
678 | tcp->pocs[pino].compS= 0; | ||
679 | tcp->pocs[pino].compE= image->numcomps; | ||
680 | tcp->pocs[pino].resS = 0; | ||
681 | tcp->pocs[pino].resE = maxres; | ||
682 | tcp->pocs[pino].layS = 0; | ||
683 | tcp->pocs[pino].layE = tcp->numlayers; | ||
684 | tcp->pocs[pino].prg = tcp->prg; | ||
685 | } | ||
686 | tcp->pocs[pino].prcS = 0; | ||
687 | tcp->pocs[pino].prcE = maxprec;; | ||
688 | tcp->pocs[pino].txS = pi[pino].tx0; | ||
689 | tcp->pocs[pino].txE = pi[pino].tx1; | ||
690 | tcp->pocs[pino].tyS = pi[pino].ty0; | ||
691 | tcp->pocs[pino].tyE = pi[pino].ty1; | ||
692 | tcp->pocs[pino].dx = pi[pino].dx; | ||
693 | tcp->pocs[pino].dy = pi[pino].dy; | ||
694 | } | ||
695 | return pi; | ||
696 | } | ||
697 | |||
698 | |||
699 | |||
700 | void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) { | ||
701 | int compno, pino; | ||
702 | opj_tcp_t *tcp = &cp->tcps[tileno]; | ||
703 | if(pi) { | ||
704 | for (pino = 0; pino < tcp->numpocs + 1; pino++) { | ||
705 | if(pi[pino].comps) { | ||
706 | for (compno = 0; compno < pi->numcomps; compno++) { | ||
707 | opj_pi_comp_t *comp = &pi[pino].comps[compno]; | ||
708 | if(comp->resolutions) { | ||
709 | opj_free(comp->resolutions); | ||
710 | } | ||
711 | } | ||
712 | opj_free(pi[pino].comps); | ||
713 | } | ||
714 | } | ||
715 | if(pi->include) { | ||
716 | opj_free(pi->include); | ||
717 | } | ||
718 | opj_free(pi); | ||
719 | } | ||
720 | } | ||
721 | |||
722 | bool pi_next(opj_pi_iterator_t * pi) { | ||
723 | switch (pi->poc.prg) { | ||
724 | case LRCP: | ||
725 | return pi_next_lrcp(pi); | ||
726 | case RLCP: | ||
727 | return pi_next_rlcp(pi); | ||
728 | case RPCL: | ||
729 | return pi_next_rpcl(pi); | ||
730 | case PCRL: | ||
731 | return pi_next_pcrl(pi); | ||
732 | case CPRL: | ||
733 | return pi_next_cprl(pi); | ||
734 | case PROG_UNKNOWN: | ||
735 | return false; | ||
736 | } | ||
737 | |||
738 | return false; | ||
739 | } | ||
740 | |||
741 | int pi_check_next_level(int pos,opj_cp_t *cp,int tileno, int pino, char *prog){ | ||
742 | int i,l; | ||
743 | opj_tcp_t *tcps =&cp->tcps[tileno]; | ||
744 | opj_poc_t *tcp = &tcps->pocs[pino]; | ||
745 | if(pos>=0){ | ||
746 | for(i=pos;pos>=0;i--){ | ||
747 | switch(prog[i]){ | ||
748 | case 'R': | ||
749 | if(tcp->res_t==tcp->resE){ | ||
750 | l=pi_check_next_level(pos-1,cp,tileno,pino,prog); | ||
751 | if(l==1){ | ||
752 | return 1; | ||
753 | }else{ | ||
754 | return 0; | ||
755 | } | ||
756 | }else{ | ||
757 | return 1; | ||
758 | } | ||
759 | break; | ||
760 | case 'C': | ||
761 | if(tcp->comp_t==tcp->compE){ | ||
762 | l=pi_check_next_level(pos-1,cp,tileno,pino,prog); | ||
763 | if(l==1){ | ||
764 | return 1; | ||
765 | }else{ | ||
766 | return 0; | ||
767 | } | ||
768 | }else{ | ||
769 | return 1; | ||
770 | } | ||
771 | break; | ||
772 | case 'L': | ||
773 | if(tcp->lay_t==tcp->layE){ | ||
774 | l=pi_check_next_level(pos-1,cp,tileno,pino,prog); | ||
775 | if(l==1){ | ||
776 | return 1; | ||
777 | }else{ | ||
778 | return 0; | ||
779 | } | ||
780 | }else{ | ||
781 | return 1; | ||
782 | } | ||
783 | break; | ||
784 | case 'P': | ||
785 | switch(tcp->prg){ | ||
786 | case LRCP||RLCP: | ||
787 | if(tcp->prc_t == tcp->prcE){ | ||
788 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
789 | if(l==1){ | ||
790 | return 1; | ||
791 | }else{ | ||
792 | return 0; | ||
793 | } | ||
794 | }else{ | ||
795 | return 1; | ||
796 | } | ||
797 | break; | ||
798 | default: | ||
799 | if(tcp->tx0_t == tcp->txE){ | ||
800 | //TY | ||
801 | if(tcp->ty0_t == tcp->tyE){ | ||
802 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
803 | if(l==1){ | ||
804 | return 1; | ||
805 | }else{ | ||
806 | return 0; | ||
807 | } | ||
808 | }else{ | ||
809 | return 1; | ||
810 | }//TY | ||
811 | }else{ | ||
812 | return 1; | ||
813 | } | ||
814 | break; | ||
815 | }//end case P | ||
816 | }//end switch | ||
817 | }//end for | ||
818 | }//end if | ||
819 | return 0; | ||
820 | } | ||
821 | |||
822 | |||
823 | void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos){ | ||
824 | char *prog; | ||
825 | int i,l; | ||
826 | int incr_top=1,resetX=0; | ||
827 | opj_tcp_t *tcps =&cp->tcps[tileno]; | ||
828 | opj_poc_t *tcp= &tcps->pocs[pino]; | ||
829 | prog = j2k_convert_progression_order(tcp->prg); | ||
830 | |||
831 | pi[pino].first = 1; | ||
832 | pi[pino].poc.prg = tcp->prg; | ||
833 | |||
834 | if(!(cp->tp_on)){ | ||
835 | pi[pino].poc.resno0 = tcp->resS; | ||
836 | pi[pino].poc.resno1 = tcp->resE; | ||
837 | pi[pino].poc.compno0 = tcp->compS; | ||
838 | pi[pino].poc.compno1 = tcp->compE; | ||
839 | pi[pino].poc.layno0 = tcp->layS; | ||
840 | pi[pino].poc.layno1 = tcp->layE; | ||
841 | pi[pino].poc.precno0 = tcp->prcS; | ||
842 | pi[pino].poc.precno1 = tcp->prcE; | ||
843 | pi[pino].poc.tx0 = tcp->txS; | ||
844 | pi[pino].poc.ty0 = tcp->tyS; | ||
845 | pi[pino].poc.tx1 = tcp->txE; | ||
846 | pi[pino].poc.ty1 = tcp->tyE; | ||
847 | }else { | ||
848 | for(i=tppos+1;i<4;i++){ | ||
849 | switch(prog[i]){ | ||
850 | case 'R': | ||
851 | pi[pino].poc.resno0 = tcp->resS; | ||
852 | pi[pino].poc.resno1 = tcp->resE; | ||
853 | break; | ||
854 | case 'C': | ||
855 | pi[pino].poc.compno0 = tcp->compS; | ||
856 | pi[pino].poc.compno1 = tcp->compE; | ||
857 | break; | ||
858 | case 'L': | ||
859 | pi[pino].poc.layno0 = tcp->layS; | ||
860 | pi[pino].poc.layno1 = tcp->layE; | ||
861 | break; | ||
862 | case 'P': | ||
863 | switch(tcp->prg){ | ||
864 | case LRCP: | ||
865 | case RLCP: | ||
866 | pi[pino].poc.precno0 = tcp->prcS; | ||
867 | pi[pino].poc.precno1 = tcp->prcE; | ||
868 | break; | ||
869 | default: | ||
870 | pi[pino].poc.tx0 = tcp->txS; | ||
871 | pi[pino].poc.ty0 = tcp->tyS; | ||
872 | pi[pino].poc.tx1 = tcp->txE; | ||
873 | pi[pino].poc.ty1 = tcp->tyE; | ||
874 | break; | ||
875 | } | ||
876 | break; | ||
877 | } | ||
878 | } | ||
879 | |||
880 | if(tpnum==0){ | ||
881 | for(i=tppos;i>=0;i--){ | ||
882 | switch(prog[i]){ | ||
883 | case 'C': | ||
884 | tcp->comp_t = tcp->compS; | ||
885 | pi[pino].poc.compno0 = tcp->comp_t; | ||
886 | pi[pino].poc.compno1 = tcp->comp_t+1; | ||
887 | tcp->comp_t+=1; | ||
888 | break; | ||
889 | case 'R': | ||
890 | tcp->res_t = tcp->resS; | ||
891 | pi[pino].poc.resno0 = tcp->res_t; | ||
892 | pi[pino].poc.resno1 = tcp->res_t+1; | ||
893 | tcp->res_t+=1; | ||
894 | break; | ||
895 | case 'L': | ||
896 | tcp->lay_t = tcp->layS; | ||
897 | pi[pino].poc.layno0 = tcp->lay_t; | ||
898 | pi[pino].poc.layno1 = tcp->lay_t+1; | ||
899 | tcp->lay_t+=1; | ||
900 | break; | ||
901 | case 'P': | ||
902 | switch(tcp->prg){ | ||
903 | case LRCP: | ||
904 | case RLCP: | ||
905 | tcp->prc_t = tcp->prcS; | ||
906 | pi[pino].poc.precno0 = tcp->prc_t; | ||
907 | pi[pino].poc.precno1 = tcp->prc_t+1; | ||
908 | tcp->prc_t+=1; | ||
909 | break; | ||
910 | default: | ||
911 | tcp->tx0_t = tcp->txS; | ||
912 | tcp->ty0_t = tcp->tyS; | ||
913 | pi[pino].poc.tx0 = tcp->tx0_t; | ||
914 | pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); | ||
915 | pi[pino].poc.ty0 = tcp->ty0_t; | ||
916 | pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); | ||
917 | tcp->tx0_t = pi[pino].poc.tx1; | ||
918 | tcp->ty0_t = pi[pino].poc.ty1; | ||
919 | break; | ||
920 | } | ||
921 | break; | ||
922 | } | ||
923 | } | ||
924 | incr_top=1; | ||
925 | }else{ | ||
926 | for(i=tppos;i>=0;i--){ | ||
927 | switch(prog[i]){ | ||
928 | case 'C': | ||
929 | pi[pino].poc.compno0 = tcp->comp_t-1; | ||
930 | pi[pino].poc.compno1 = tcp->comp_t; | ||
931 | break; | ||
932 | case 'R': | ||
933 | pi[pino].poc.resno0 = tcp->res_t-1; | ||
934 | pi[pino].poc.resno1 = tcp->res_t; | ||
935 | break; | ||
936 | case 'L': | ||
937 | pi[pino].poc.layno0 = tcp->lay_t-1; | ||
938 | pi[pino].poc.layno1 = tcp->lay_t; | ||
939 | break; | ||
940 | case 'P': | ||
941 | switch(tcp->prg){ | ||
942 | case LRCP: | ||
943 | case RLCP: | ||
944 | pi[pino].poc.precno0 = tcp->prc_t-1; | ||
945 | pi[pino].poc.precno1 = tcp->prc_t; | ||
946 | break; | ||
947 | default: | ||
948 | pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx); | ||
949 | pi[pino].poc.tx1 = tcp->tx0_t ; | ||
950 | pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); | ||
951 | pi[pino].poc.ty1 = tcp->ty0_t ; | ||
952 | break; | ||
953 | } | ||
954 | break; | ||
955 | } | ||
956 | if(incr_top==1){ | ||
957 | switch(prog[i]){ | ||
958 | case 'R': | ||
959 | if(tcp->res_t==tcp->resE){ | ||
960 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
961 | if(l==1){ | ||
962 | tcp->res_t = tcp->resS; | ||
963 | pi[pino].poc.resno0 = tcp->res_t; | ||
964 | pi[pino].poc.resno1 = tcp->res_t+1; | ||
965 | tcp->res_t+=1; | ||
966 | incr_top=1; | ||
967 | }else{ | ||
968 | incr_top=0; | ||
969 | } | ||
970 | }else{ | ||
971 | pi[pino].poc.resno0 = tcp->res_t; | ||
972 | pi[pino].poc.resno1 = tcp->res_t+1; | ||
973 | tcp->res_t+=1; | ||
974 | incr_top=0; | ||
975 | } | ||
976 | break; | ||
977 | case 'C': | ||
978 | if(tcp->comp_t ==tcp->compE){ | ||
979 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
980 | if(l==1){ | ||
981 | tcp->comp_t = tcp->compS; | ||
982 | pi[pino].poc.compno0 = tcp->comp_t; | ||
983 | pi[pino].poc.compno1 = tcp->comp_t+1; | ||
984 | tcp->comp_t+=1; | ||
985 | incr_top=1; | ||
986 | }else{ | ||
987 | incr_top=0; | ||
988 | } | ||
989 | }else{ | ||
990 | pi[pino].poc.compno0 = tcp->comp_t; | ||
991 | pi[pino].poc.compno1 = tcp->comp_t+1; | ||
992 | tcp->comp_t+=1; | ||
993 | incr_top=0; | ||
994 | } | ||
995 | break; | ||
996 | case 'L': | ||
997 | if(tcp->lay_t == tcp->layE){ | ||
998 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
999 | if(l==1){ | ||
1000 | tcp->lay_t = tcp->layS; | ||
1001 | pi[pino].poc.layno0 = tcp->lay_t; | ||
1002 | pi[pino].poc.layno1 = tcp->lay_t+1; | ||
1003 | tcp->lay_t+=1; | ||
1004 | incr_top=1; | ||
1005 | }else{ | ||
1006 | incr_top=0; | ||
1007 | } | ||
1008 | }else{ | ||
1009 | pi[pino].poc.layno0 = tcp->lay_t; | ||
1010 | pi[pino].poc.layno1 = tcp->lay_t+1; | ||
1011 | tcp->lay_t+=1; | ||
1012 | incr_top=0; | ||
1013 | } | ||
1014 | break; | ||
1015 | case 'P': | ||
1016 | switch(tcp->prg){ | ||
1017 | case LRCP: | ||
1018 | case RLCP: | ||
1019 | if(tcp->prc_t == tcp->prcE){ | ||
1020 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
1021 | if(l==1){ | ||
1022 | tcp->prc_t = tcp->prcS; | ||
1023 | pi[pino].poc.precno0 = tcp->prc_t; | ||
1024 | pi[pino].poc.precno1 = tcp->prc_t+1; | ||
1025 | tcp->prc_t+=1; | ||
1026 | incr_top=1; | ||
1027 | }else{ | ||
1028 | incr_top=0; | ||
1029 | } | ||
1030 | }else{ | ||
1031 | pi[pino].poc.precno0 = tcp->prc_t; | ||
1032 | pi[pino].poc.precno1 = tcp->prc_t+1; | ||
1033 | tcp->prc_t+=1; | ||
1034 | incr_top=0; | ||
1035 | } | ||
1036 | break; | ||
1037 | default: | ||
1038 | if(tcp->tx0_t >= tcp->txE){ | ||
1039 | if(tcp->ty0_t >= tcp->tyE){ | ||
1040 | l=pi_check_next_level(i-1,cp,tileno,pino,prog); | ||
1041 | if(l==1){ | ||
1042 | tcp->ty0_t = tcp->tyS; | ||
1043 | pi[pino].poc.ty0 = tcp->ty0_t; | ||
1044 | pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); | ||
1045 | tcp->ty0_t = pi[pino].poc.ty1; | ||
1046 | incr_top=1;resetX=1; | ||
1047 | }else{ | ||
1048 | incr_top=0;resetX=0; | ||
1049 | } | ||
1050 | }else{ | ||
1051 | pi[pino].poc.ty0 = tcp->ty0_t; | ||
1052 | pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); | ||
1053 | tcp->ty0_t = pi[pino].poc.ty1; | ||
1054 | incr_top=0;resetX=1; | ||
1055 | } | ||
1056 | if(resetX==1){ | ||
1057 | tcp->tx0_t = tcp->txS; | ||
1058 | pi[pino].poc.tx0 = tcp->tx0_t; | ||
1059 | pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx); | ||
1060 | tcp->tx0_t = pi[pino].poc.tx1; | ||
1061 | } | ||
1062 | }else{ | ||
1063 | pi[pino].poc.tx0 = tcp->tx0_t; | ||
1064 | pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx); | ||
1065 | tcp->tx0_t = pi[pino].poc.tx1; | ||
1066 | incr_top=0; | ||
1067 | } | ||
1068 | break; | ||
1069 | } | ||
1070 | break; | ||
1071 | } | ||
1072 | } | ||
1073 | } | ||
1074 | } | ||
1075 | } | ||
1076 | } | ||
1077 | |||
1078 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/pi.h b/libraries/openjpeg-libsl/libopenjpeg/pi.h new file mode 100644 index 0000000..fc99329 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/pi.h | |||
@@ -0,0 +1,152 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __PI_H | ||
33 | #define __PI_H | ||
34 | /** | ||
35 | @file pi.h | ||
36 | @brief Implementation of a packet iterator (PI) | ||
37 | |||
38 | The functions in PI.C have for goal to realize a packet iterator that permits to get the next | ||
39 | packet following the progression order and change of it. The functions in PI.C are used | ||
40 | by some function in T2.C. | ||
41 | */ | ||
42 | |||
43 | /** @defgroup PI PI - Implementation of a packet iterator */ | ||
44 | /*@{*/ | ||
45 | |||
46 | /** | ||
47 | FIXME: documentation | ||
48 | */ | ||
49 | typedef struct opj_pi_resolution { | ||
50 | int pdx, pdy; | ||
51 | int pw, ph; | ||
52 | } opj_pi_resolution_t; | ||
53 | |||
54 | /** | ||
55 | FIXME: documentation | ||
56 | */ | ||
57 | typedef struct opj_pi_comp { | ||
58 | int dx, dy; | ||
59 | /** number of resolution levels */ | ||
60 | int numresolutions; | ||
61 | opj_pi_resolution_t *resolutions; | ||
62 | } opj_pi_comp_t; | ||
63 | |||
64 | /** | ||
65 | Packet iterator | ||
66 | */ | ||
67 | typedef struct opj_pi_iterator { | ||
68 | /** Enabling Tile part generation*/ | ||
69 | char tp_on; | ||
70 | /** precise if the packet has been already used (usefull for progression order change) */ | ||
71 | short int *include; | ||
72 | /** layer step used to localize the packet in the include vector */ | ||
73 | int step_l; | ||
74 | /** resolution step used to localize the packet in the include vector */ | ||
75 | int step_r; | ||
76 | /** component step used to localize the packet in the include vector */ | ||
77 | int step_c; | ||
78 | /** precinct step used to localize the packet in the include vector */ | ||
79 | int step_p; | ||
80 | /** component that identify the packet */ | ||
81 | int compno; | ||
82 | /** resolution that identify the packet */ | ||
83 | int resno; | ||
84 | /** precinct that identify the packet */ | ||
85 | int precno; | ||
86 | /** layer that identify the packet */ | ||
87 | int layno; | ||
88 | /** 0 if the first packet */ | ||
89 | int first; | ||
90 | /** progression order change information */ | ||
91 | opj_poc_t poc; | ||
92 | /** number of components in the image */ | ||
93 | int numcomps; | ||
94 | /** Components*/ | ||
95 | opj_pi_comp_t *comps; | ||
96 | int tx0, ty0, tx1, ty1; | ||
97 | int x, y, dx, dy; | ||
98 | } opj_pi_iterator_t; | ||
99 | |||
100 | /** @name Exported functions */ | ||
101 | /*@{*/ | ||
102 | /* ----------------------------------------------------------------------- */ | ||
103 | /** | ||
104 | Create a packet iterator for Encoder | ||
105 | @param image Raw image for which the packets will be listed | ||
106 | @param cp Coding parameters | ||
107 | @param tileno Number that identifies the tile for which to list the packets | ||
108 | @param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass | ||
109 | @return Returns a packet iterator that points to the first packet of the tile | ||
110 | @see pi_destroy | ||
111 | */ | ||
112 | opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno,J2K_T2_MODE t2_mode); | ||
113 | /** | ||
114 | Modify the packet iterator for enabling tile part generation | ||
115 | @param pi Handle to the packet iterator generated in pi_initialise_encode | ||
116 | @param cp Coding parameters | ||
117 | @param tileno Number that identifies the tile for which to list the packets | ||
118 | @param tpnum Tile part number of the current tile | ||
119 | @param tppos The position of the tile part flag in the progression order | ||
120 | */ | ||
121 | void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos); | ||
122 | /** | ||
123 | Create a packet iterator for Decoder | ||
124 | @param image Raw image for which the packets will be listed | ||
125 | @param cp Coding parameters | ||
126 | @param tileno Number that identifies the tile for which to list the packets | ||
127 | @return Returns a packet iterator that points to the first packet of the tile | ||
128 | @see pi_destroy | ||
129 | */ | ||
130 | opj_pi_iterator_t *pi_create_decode(opj_image_t * image, opj_cp_t * cp, int tileno); | ||
131 | |||
132 | /** | ||
133 | Destroy a packet iterator | ||
134 | @param pi Previously created packet iterator | ||
135 | @param cp Coding parameters | ||
136 | @param tileno Number that identifies the tile for which the packets were listed | ||
137 | @see pi_create | ||
138 | */ | ||
139 | void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno); | ||
140 | |||
141 | /** | ||
142 | Modify the packet iterator to point to the next packet | ||
143 | @param pi Packet iterator to modify | ||
144 | @return Returns false if pi pointed to the last packet or else returns true | ||
145 | */ | ||
146 | bool pi_next(opj_pi_iterator_t * pi); | ||
147 | /* ----------------------------------------------------------------------- */ | ||
148 | /*@}*/ | ||
149 | |||
150 | /*@}*/ | ||
151 | |||
152 | #endif /* __PI_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/raw.c b/libraries/openjpeg-libsl/libopenjpeg/raw.c new file mode 100644 index 0000000..808a468 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/raw.c | |||
@@ -0,0 +1,87 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
5 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #include "opj_includes.h" | ||
31 | |||
32 | /* | ||
33 | ========================================================== | ||
34 | local functions | ||
35 | ========================================================== | ||
36 | */ | ||
37 | |||
38 | |||
39 | /* | ||
40 | ========================================================== | ||
41 | RAW encoding interface | ||
42 | ========================================================== | ||
43 | */ | ||
44 | |||
45 | opj_raw_t* raw_create() { | ||
46 | opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t)); | ||
47 | return raw; | ||
48 | } | ||
49 | |||
50 | void raw_destroy(opj_raw_t *raw) { | ||
51 | if(raw) { | ||
52 | opj_free(raw); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | int raw_numbytes(opj_raw_t *raw) { | ||
57 | return raw->bp - raw->start; | ||
58 | } | ||
59 | |||
60 | void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len) { | ||
61 | raw->start = bp; | ||
62 | raw->lenmax = len; | ||
63 | raw->len = 0; | ||
64 | raw->c = 0; | ||
65 | raw->ct = 0; | ||
66 | } | ||
67 | |||
68 | int raw_decode(opj_raw_t *raw) { | ||
69 | int d; | ||
70 | if (raw->ct == 0) { | ||
71 | raw->ct = 8; | ||
72 | if (raw->len == raw->lenmax) { | ||
73 | raw->c = 0xff; | ||
74 | } else { | ||
75 | if (raw->c == 0xff) { | ||
76 | raw->ct = 7; | ||
77 | } | ||
78 | raw->c = *(raw->start + raw->len); | ||
79 | raw->len++; | ||
80 | } | ||
81 | } | ||
82 | raw->ct--; | ||
83 | d = (raw->c >> raw->ct) & 0x01; | ||
84 | |||
85 | return d; | ||
86 | } | ||
87 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/raw.h b/libraries/openjpeg-libsl/libopenjpeg/raw.h new file mode 100644 index 0000000..c4ff9b3 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/raw.h | |||
@@ -0,0 +1,100 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
5 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #ifndef __RAW_H | ||
31 | #define __RAW_H | ||
32 | /** | ||
33 | @file raw.h | ||
34 | @brief Implementation of operations for raw encoding (RAW) | ||
35 | |||
36 | The functions in RAW.C have for goal to realize the operation of raw encoding linked | ||
37 | with the corresponding mode switch. | ||
38 | */ | ||
39 | |||
40 | /** @defgroup RAW RAW - Implementation of operations for raw encoding */ | ||
41 | /*@{*/ | ||
42 | |||
43 | /** | ||
44 | RAW encoding operations | ||
45 | */ | ||
46 | typedef struct opj_raw { | ||
47 | /** temporary buffer where bits are coded or decoded */ | ||
48 | unsigned char c; | ||
49 | /** number of bits already read or free to write */ | ||
50 | unsigned int ct; | ||
51 | /** maximum length to decode */ | ||
52 | unsigned int lenmax; | ||
53 | /** length decoded */ | ||
54 | unsigned int len; | ||
55 | /** pointer to the current position in the buffer */ | ||
56 | unsigned char *bp; | ||
57 | /** pointer to the start of the buffer */ | ||
58 | unsigned char *start; | ||
59 | /** pointer to the end of the buffer */ | ||
60 | unsigned char *end; | ||
61 | } opj_raw_t; | ||
62 | |||
63 | /** @name Exported functions */ | ||
64 | /*@{*/ | ||
65 | /* ----------------------------------------------------------------------- */ | ||
66 | /** | ||
67 | Create a new RAW handle | ||
68 | @return Returns a new RAW handle if successful, returns NULL otherwise | ||
69 | */ | ||
70 | opj_raw_t* raw_create(); | ||
71 | /** | ||
72 | Destroy a previously created RAW handle | ||
73 | @param raw RAW handle to destroy | ||
74 | */ | ||
75 | void raw_destroy(opj_raw_t *raw); | ||
76 | /** | ||
77 | Return the number of bytes written/read since initialisation | ||
78 | @param raw RAW handle to destroy | ||
79 | @return Returns the number of bytes already encoded | ||
80 | */ | ||
81 | int raw_numbytes(opj_raw_t *raw); | ||
82 | /** | ||
83 | Initialize the decoder | ||
84 | @param raw RAW handle | ||
85 | @param bp Pointer to the start of the buffer from which the bytes will be read | ||
86 | @param len Length of the input buffer | ||
87 | */ | ||
88 | void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len); | ||
89 | /** | ||
90 | Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN | ||
91 | @param raw RAW handle | ||
92 | @return Returns the decoded symbol (0 or 1) | ||
93 | */ | ||
94 | int raw_decode(opj_raw_t *raw); | ||
95 | /* ----------------------------------------------------------------------- */ | ||
96 | /*@}*/ | ||
97 | |||
98 | /*@}*/ | ||
99 | |||
100 | #endif /* __RAW_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/t1.c b/libraries/openjpeg-libsl/libopenjpeg/t1.c new file mode 100644 index 0000000..b5d9599 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/t1.c | |||
@@ -0,0 +1,1210 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com> | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "opj_includes.h" | ||
34 | #include "t1_luts.h" | ||
35 | |||
36 | /** @defgroup T1 T1 - Implementation of the tier-1 coding */ | ||
37 | /*@{*/ | ||
38 | |||
39 | /** @name Local static functions */ | ||
40 | /*@{*/ | ||
41 | |||
42 | static char t1_getctxno_zc(int f, int orient); | ||
43 | static char t1_getctxno_sc(int f); | ||
44 | static char t1_getctxno_mag(int f); | ||
45 | static char t1_getspb(int f); | ||
46 | static short t1_getnmsedec_sig(int x, int bitpos); | ||
47 | static short t1_getnmsedec_ref(int x, int bitpos); | ||
48 | static void t1_updateflags(flag_t *flagsp, int s, int stride); | ||
49 | /** | ||
50 | Encode significant pass | ||
51 | */ | ||
52 | static void t1_enc_sigpass_step( | ||
53 | opj_t1_t *t1, | ||
54 | flag_t *flagsp, | ||
55 | int *datap, | ||
56 | int orient, | ||
57 | int bpno, | ||
58 | int one, | ||
59 | int *nmsedec, | ||
60 | char type, | ||
61 | int vsc); | ||
62 | /** | ||
63 | Decode significant pass | ||
64 | */ | ||
65 | static void t1_dec_sigpass_step( | ||
66 | opj_t1_t *t1, | ||
67 | flag_t *flagsp, | ||
68 | int *datap, | ||
69 | int orient, | ||
70 | int oneplushalf, | ||
71 | char type, | ||
72 | int vsc); | ||
73 | /** | ||
74 | Encode significant pass | ||
75 | */ | ||
76 | static void t1_enc_sigpass( | ||
77 | opj_t1_t *t1, | ||
78 | int bpno, | ||
79 | int orient, | ||
80 | int *nmsedec, | ||
81 | char type, | ||
82 | int cblksty); | ||
83 | /** | ||
84 | Decode significant pass | ||
85 | */ | ||
86 | static void t1_dec_sigpass( | ||
87 | opj_t1_t *t1, | ||
88 | int bpno, | ||
89 | int orient, | ||
90 | char type, | ||
91 | int cblksty); | ||
92 | /** | ||
93 | Encode refinement pass | ||
94 | */ | ||
95 | static void t1_enc_refpass_step( | ||
96 | opj_t1_t *t1, | ||
97 | flag_t *flagsp, | ||
98 | int *datap, | ||
99 | int bpno, | ||
100 | int one, | ||
101 | int *nmsedec, | ||
102 | char type, | ||
103 | int vsc); | ||
104 | /** | ||
105 | Decode refinement pass | ||
106 | */ | ||
107 | static void t1_dec_refpass_step( | ||
108 | opj_t1_t *t1, | ||
109 | flag_t *flagsp, | ||
110 | int *datap, | ||
111 | int poshalf, | ||
112 | int neghalf, | ||
113 | char type, | ||
114 | int vsc); | ||
115 | /** | ||
116 | Encode refinement pass | ||
117 | */ | ||
118 | static void t1_enc_refpass( | ||
119 | opj_t1_t *t1, | ||
120 | int bpno, | ||
121 | int *nmsedec, | ||
122 | char type, | ||
123 | int cblksty); | ||
124 | /** | ||
125 | Decode refinement pass | ||
126 | */ | ||
127 | static void t1_dec_refpass( | ||
128 | opj_t1_t *t1, | ||
129 | int bpno, | ||
130 | char type, | ||
131 | int cblksty); | ||
132 | /** | ||
133 | Encode clean-up pass | ||
134 | */ | ||
135 | static void t1_enc_clnpass_step( | ||
136 | opj_t1_t *t1, | ||
137 | flag_t *flagsp, | ||
138 | int *datap, | ||
139 | int orient, | ||
140 | int bpno, | ||
141 | int one, | ||
142 | int *nmsedec, | ||
143 | int partial, | ||
144 | int vsc); | ||
145 | /** | ||
146 | Decode clean-up pass | ||
147 | */ | ||
148 | static void t1_dec_clnpass_step( | ||
149 | opj_t1_t *t1, | ||
150 | flag_t *flagsp, | ||
151 | int *datap, | ||
152 | int orient, | ||
153 | int oneplushalf, | ||
154 | int partial, | ||
155 | int vsc); | ||
156 | /** | ||
157 | Encode clean-up pass | ||
158 | */ | ||
159 | static void t1_enc_clnpass( | ||
160 | opj_t1_t *t1, | ||
161 | int bpno, | ||
162 | int orient, | ||
163 | int *nmsedec, | ||
164 | int cblksty); | ||
165 | /** | ||
166 | Decode clean-up pass | ||
167 | */ | ||
168 | static void t1_dec_clnpass( | ||
169 | opj_t1_t *t1, | ||
170 | int bpno, | ||
171 | int orient, | ||
172 | int cblksty); | ||
173 | static double t1_getwmsedec( | ||
174 | int nmsedec, | ||
175 | int compno, | ||
176 | int level, | ||
177 | int orient, | ||
178 | int bpno, | ||
179 | int qmfbid, | ||
180 | double stepsize, | ||
181 | int numcomps); | ||
182 | /** | ||
183 | Encode 1 code-block | ||
184 | @param t1 T1 handle | ||
185 | @param cblk Code-block coding parameters | ||
186 | @param orient | ||
187 | @param compno Component number | ||
188 | @param level | ||
189 | @param qmfbid | ||
190 | @param stepsize | ||
191 | @param cblksty Code-block style | ||
192 | @param numcomps | ||
193 | @param tile | ||
194 | */ | ||
195 | static void t1_encode_cblk( | ||
196 | opj_t1_t *t1, | ||
197 | opj_tcd_cblk_t * cblk, | ||
198 | int orient, | ||
199 | int compno, | ||
200 | int level, | ||
201 | int qmfbid, | ||
202 | double stepsize, | ||
203 | int cblksty, | ||
204 | int numcomps, | ||
205 | opj_tcd_tile_t * tile); | ||
206 | /** | ||
207 | Decode 1 code-block | ||
208 | @param t1 T1 handle | ||
209 | @param cblk Code-block coding parameters | ||
210 | @param orient | ||
211 | @param roishift Region of interest shifting value | ||
212 | @param cblksty Code-block style | ||
213 | */ | ||
214 | static void t1_decode_cblk( | ||
215 | opj_t1_t *t1, | ||
216 | opj_tcd_cblk_t * cblk, | ||
217 | int orient, | ||
218 | int roishift, | ||
219 | int cblksty); | ||
220 | |||
221 | /*@}*/ | ||
222 | |||
223 | /*@}*/ | ||
224 | |||
225 | /* ----------------------------------------------------------------------- */ | ||
226 | |||
227 | static char t1_getctxno_zc(int f, int orient) { | ||
228 | return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)]; | ||
229 | } | ||
230 | |||
231 | static char t1_getctxno_sc(int f) { | ||
232 | return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; | ||
233 | } | ||
234 | |||
235 | static char t1_getctxno_mag(int f) { | ||
236 | return lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)]; | ||
237 | } | ||
238 | |||
239 | static char t1_getspb(int f) { | ||
240 | return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; | ||
241 | } | ||
242 | |||
243 | static short t1_getnmsedec_sig(int x, int bitpos) { | ||
244 | if (bitpos > T1_NMSEDEC_FRACBITS) { | ||
245 | return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; | ||
246 | } | ||
247 | |||
248 | return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; | ||
249 | } | ||
250 | |||
251 | static short t1_getnmsedec_ref(int x, int bitpos) { | ||
252 | if (bitpos > T1_NMSEDEC_FRACBITS) { | ||
253 | return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; | ||
254 | } | ||
255 | |||
256 | return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; | ||
257 | } | ||
258 | |||
259 | static void t1_updateflags(flag_t *flagsp, int s, int stride) { | ||
260 | flag_t *np = flagsp - stride; | ||
261 | flag_t *sp = flagsp + stride; | ||
262 | |||
263 | static const flag_t mod[] = { | ||
264 | T1_SIG_S, T1_SIG_N, T1_SIG_E, T1_SIG_W, | ||
265 | T1_SIG_S | T1_SGN_S, T1_SIG_N | T1_SGN_N, T1_SIG_E | T1_SGN_E, T1_SIG_W | T1_SGN_W | ||
266 | }; | ||
267 | |||
268 | s <<= 2; | ||
269 | |||
270 | np[-1] |= T1_SIG_SE; | ||
271 | np[0] |= mod[s]; | ||
272 | np[1] |= T1_SIG_SW; | ||
273 | |||
274 | flagsp[-1] |= mod[s+2]; | ||
275 | flagsp[1] |= mod[s+3]; | ||
276 | |||
277 | sp[-1] |= T1_SIG_NE; | ||
278 | sp[0] |= mod[s+1]; | ||
279 | sp[1] |= T1_SIG_NW; | ||
280 | } | ||
281 | |||
282 | static void t1_enc_sigpass_step( | ||
283 | opj_t1_t *t1, | ||
284 | flag_t *flagsp, | ||
285 | int *datap, | ||
286 | int orient, | ||
287 | int bpno, | ||
288 | int one, | ||
289 | int *nmsedec, | ||
290 | char type, | ||
291 | int vsc) | ||
292 | { | ||
293 | int v, flag; | ||
294 | |||
295 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
296 | |||
297 | flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp); | ||
298 | if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) { | ||
299 | v = int_abs(*datap) & one ? 1 : 0; | ||
300 | if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ | ||
301 | mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */ | ||
302 | mqc_bypass_enc(mqc, v); | ||
303 | } else { | ||
304 | mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); | ||
305 | mqc_encode(mqc, v); | ||
306 | } | ||
307 | if (v) { | ||
308 | v = *datap < 0 ? 1 : 0; | ||
309 | *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS); | ||
310 | if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ | ||
311 | mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */ | ||
312 | mqc_bypass_enc(mqc, v); | ||
313 | } else { | ||
314 | mqc_setcurctx(mqc, t1_getctxno_sc(flag)); | ||
315 | mqc_encode(mqc, v ^ t1_getspb(flag)); | ||
316 | } | ||
317 | t1_updateflags(flagsp, v, t1->flags_stride); | ||
318 | *flagsp |= T1_SIG; | ||
319 | } | ||
320 | *flagsp |= T1_VISIT; | ||
321 | } | ||
322 | } | ||
323 | |||
324 | static void t1_dec_sigpass_step( | ||
325 | opj_t1_t *t1, | ||
326 | flag_t *flagsp, | ||
327 | int *datap, | ||
328 | int orient, | ||
329 | int oneplushalf, | ||
330 | char type, | ||
331 | int vsc) | ||
332 | { | ||
333 | int v, flag; | ||
334 | |||
335 | opj_raw_t *raw = t1->raw; /* RAW component */ | ||
336 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
337 | |||
338 | flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp); | ||
339 | if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) { | ||
340 | if (type == T1_TYPE_RAW) { | ||
341 | if (raw_decode(raw)) { | ||
342 | v = raw_decode(raw); /* ESSAI */ | ||
343 | *datap = v ? -oneplushalf : oneplushalf; | ||
344 | t1_updateflags(flagsp, v, t1->flags_stride); | ||
345 | *flagsp |= T1_SIG; | ||
346 | } | ||
347 | } else { | ||
348 | mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); | ||
349 | if (mqc_decode(mqc)) { | ||
350 | mqc_setcurctx(mqc, t1_getctxno_sc(flag)); | ||
351 | v = mqc_decode(mqc) ^ t1_getspb(flag); | ||
352 | *datap = v ? -oneplushalf : oneplushalf; | ||
353 | t1_updateflags(flagsp, v, t1->flags_stride); | ||
354 | *flagsp |= T1_SIG; | ||
355 | } | ||
356 | } | ||
357 | *flagsp |= T1_VISIT; | ||
358 | } | ||
359 | } /* VSC and BYPASS by Antonin */ | ||
360 | |||
361 | static void t1_enc_sigpass( | ||
362 | opj_t1_t *t1, | ||
363 | int bpno, | ||
364 | int orient, | ||
365 | int *nmsedec, | ||
366 | char type, | ||
367 | int cblksty) | ||
368 | { | ||
369 | int i, j, k, one, vsc; | ||
370 | *nmsedec = 0; | ||
371 | one = 1 << (bpno + T1_NMSEDEC_FRACBITS); | ||
372 | for (k = 0; k < t1->h; k += 4) { | ||
373 | for (i = 0; i < t1->w; ++i) { | ||
374 | for (j = k; j < k + 4 && j < t1->h; ++j) { | ||
375 | vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0; | ||
376 | t1_enc_sigpass_step( | ||
377 | t1, | ||
378 | &t1->flags[((j+1) * t1->flags_stride) + i + 1], | ||
379 | &t1->data[(j * t1->w) + i], | ||
380 | orient, | ||
381 | bpno, | ||
382 | one, | ||
383 | nmsedec, | ||
384 | type, | ||
385 | vsc); | ||
386 | } | ||
387 | } | ||
388 | } | ||
389 | } | ||
390 | |||
391 | static void t1_dec_sigpass( | ||
392 | opj_t1_t *t1, | ||
393 | int bpno, | ||
394 | int orient, | ||
395 | char type, | ||
396 | int cblksty) | ||
397 | { | ||
398 | int i, j, k, one, half, oneplushalf, vsc; | ||
399 | one = 1 << bpno; | ||
400 | half = one >> 1; | ||
401 | oneplushalf = one | half; | ||
402 | for (k = 0; k < t1->h; k += 4) { | ||
403 | for (i = 0; i < t1->w; ++i) { | ||
404 | for (j = k; j < k + 4 && j < t1->h; ++j) { | ||
405 | vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0; | ||
406 | t1_dec_sigpass_step( | ||
407 | t1, | ||
408 | &t1->flags[((j+1) * t1->flags_stride) + i + 1], | ||
409 | &t1->data[(j * t1->w) + i], | ||
410 | orient, | ||
411 | oneplushalf, | ||
412 | type, | ||
413 | vsc); | ||
414 | } | ||
415 | } | ||
416 | } | ||
417 | } /* VSC and BYPASS by Antonin */ | ||
418 | |||
419 | static void t1_enc_refpass_step( | ||
420 | opj_t1_t *t1, | ||
421 | flag_t *flagsp, | ||
422 | int *datap, | ||
423 | int bpno, | ||
424 | int one, | ||
425 | int *nmsedec, | ||
426 | char type, | ||
427 | int vsc) | ||
428 | { | ||
429 | int v, flag; | ||
430 | |||
431 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
432 | |||
433 | flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp); | ||
434 | if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) { | ||
435 | *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS); | ||
436 | v = int_abs(*datap) & one ? 1 : 0; | ||
437 | if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ | ||
438 | mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */ | ||
439 | mqc_bypass_enc(mqc, v); | ||
440 | } else { | ||
441 | mqc_setcurctx(mqc, t1_getctxno_mag(flag)); | ||
442 | mqc_encode(mqc, v); | ||
443 | } | ||
444 | *flagsp |= T1_REFINE; | ||
445 | } | ||
446 | } | ||
447 | |||
448 | static void t1_dec_refpass_step( | ||
449 | opj_t1_t *t1, | ||
450 | flag_t *flagsp, | ||
451 | int *datap, | ||
452 | int poshalf, | ||
453 | int neghalf, | ||
454 | char type, | ||
455 | int vsc) | ||
456 | { | ||
457 | int v, t, flag; | ||
458 | |||
459 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
460 | opj_raw_t *raw = t1->raw; /* RAW component */ | ||
461 | |||
462 | flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp); | ||
463 | if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) { | ||
464 | if (type == T1_TYPE_RAW) { | ||
465 | mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */ | ||
466 | v = raw_decode(raw); | ||
467 | } else { | ||
468 | mqc_setcurctx(mqc, t1_getctxno_mag(flag)); | ||
469 | v = mqc_decode(mqc); | ||
470 | } | ||
471 | t = v ? poshalf : neghalf; | ||
472 | *datap += *datap < 0 ? -t : t; | ||
473 | *flagsp |= T1_REFINE; | ||
474 | } | ||
475 | } /* VSC and BYPASS by Antonin */ | ||
476 | |||
477 | static void t1_enc_refpass( | ||
478 | opj_t1_t *t1, | ||
479 | int bpno, | ||
480 | int *nmsedec, | ||
481 | char type, | ||
482 | int cblksty) | ||
483 | { | ||
484 | int i, j, k, one, vsc; | ||
485 | *nmsedec = 0; | ||
486 | one = 1 << (bpno + T1_NMSEDEC_FRACBITS); | ||
487 | for (k = 0; k < t1->h; k += 4) { | ||
488 | for (i = 0; i < t1->w; ++i) { | ||
489 | for (j = k; j < k + 4 && j < t1->h; ++j) { | ||
490 | vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0; | ||
491 | t1_enc_refpass_step( | ||
492 | t1, | ||
493 | &t1->flags[((j+1) * t1->flags_stride) + i + 1], | ||
494 | &t1->data[(j * t1->w) + i], | ||
495 | bpno, | ||
496 | one, | ||
497 | nmsedec, | ||
498 | type, | ||
499 | vsc); | ||
500 | } | ||
501 | } | ||
502 | } | ||
503 | } | ||
504 | |||
505 | static void t1_dec_refpass( | ||
506 | opj_t1_t *t1, | ||
507 | int bpno, | ||
508 | char type, | ||
509 | int cblksty) | ||
510 | { | ||
511 | int i, j, k, one, poshalf, neghalf; | ||
512 | int vsc; | ||
513 | one = 1 << bpno; | ||
514 | poshalf = one >> 1; | ||
515 | neghalf = bpno > 0 ? -poshalf : -1; | ||
516 | for (k = 0; k < t1->h; k += 4) { | ||
517 | for (i = 0; i < t1->w; ++i) { | ||
518 | for (j = k; j < k + 4 && j < t1->h; ++j) { | ||
519 | vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0; | ||
520 | t1_dec_refpass_step( | ||
521 | t1, | ||
522 | &t1->flags[((j+1) * t1->flags_stride) + i + 1], | ||
523 | &t1->data[(j * t1->w) + i], | ||
524 | poshalf, | ||
525 | neghalf, | ||
526 | type, | ||
527 | vsc); | ||
528 | } | ||
529 | } | ||
530 | } | ||
531 | } /* VSC and BYPASS by Antonin */ | ||
532 | |||
533 | static void t1_enc_clnpass_step( | ||
534 | opj_t1_t *t1, | ||
535 | flag_t *flagsp, | ||
536 | int *datap, | ||
537 | int orient, | ||
538 | int bpno, | ||
539 | int one, | ||
540 | int *nmsedec, | ||
541 | int partial, | ||
542 | int vsc) | ||
543 | { | ||
544 | int v, flag; | ||
545 | |||
546 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
547 | |||
548 | flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp); | ||
549 | if (partial) { | ||
550 | goto LABEL_PARTIAL; | ||
551 | } | ||
552 | if (!(*flagsp & (T1_SIG | T1_VISIT))) { | ||
553 | mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); | ||
554 | v = int_abs(*datap) & one ? 1 : 0; | ||
555 | mqc_encode(mqc, v); | ||
556 | if (v) { | ||
557 | LABEL_PARTIAL: | ||
558 | *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS); | ||
559 | mqc_setcurctx(mqc, t1_getctxno_sc(flag)); | ||
560 | v = *datap < 0 ? 1 : 0; | ||
561 | mqc_encode(mqc, v ^ t1_getspb(flag)); | ||
562 | t1_updateflags(flagsp, v, t1->flags_stride); | ||
563 | *flagsp |= T1_SIG; | ||
564 | } | ||
565 | } | ||
566 | *flagsp &= ~T1_VISIT; | ||
567 | } | ||
568 | |||
569 | static void t1_dec_clnpass_step( | ||
570 | opj_t1_t *t1, | ||
571 | flag_t *flagsp, | ||
572 | int *datap, | ||
573 | int orient, | ||
574 | int oneplushalf, | ||
575 | int partial, | ||
576 | int vsc) | ||
577 | { | ||
578 | int v, flag; | ||
579 | |||
580 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
581 | |||
582 | flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp); | ||
583 | if (partial) { | ||
584 | goto LABEL_PARTIAL; | ||
585 | } | ||
586 | if (!(flag & (T1_SIG | T1_VISIT))) { | ||
587 | mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); | ||
588 | if (mqc_decode(mqc)) { | ||
589 | LABEL_PARTIAL: | ||
590 | mqc_setcurctx(mqc, t1_getctxno_sc(flag)); | ||
591 | v = mqc_decode(mqc) ^ t1_getspb(flag); | ||
592 | *datap = v ? -oneplushalf : oneplushalf; | ||
593 | t1_updateflags(flagsp, v, t1->flags_stride); | ||
594 | *flagsp |= T1_SIG; | ||
595 | } | ||
596 | } | ||
597 | *flagsp &= ~T1_VISIT; | ||
598 | } /* VSC and BYPASS by Antonin */ | ||
599 | |||
600 | static void t1_enc_clnpass( | ||
601 | opj_t1_t *t1, | ||
602 | int bpno, | ||
603 | int orient, | ||
604 | int *nmsedec, | ||
605 | int cblksty) | ||
606 | { | ||
607 | int i, j, k, one, agg, runlen, vsc; | ||
608 | |||
609 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
610 | |||
611 | *nmsedec = 0; | ||
612 | one = 1 << (bpno + T1_NMSEDEC_FRACBITS); | ||
613 | for (k = 0; k < t1->h; k += 4) { | ||
614 | for (i = 0; i < t1->w; ++i) { | ||
615 | if (k + 3 < t1->h) { | ||
616 | if (cblksty & J2K_CCP_CBLKSTY_VSC) { | ||
617 | agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
618 | || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
619 | || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
620 | || (MACRO_t1_flags(1 + k + 3,1 + i) | ||
621 | & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); | ||
622 | } else { | ||
623 | agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
624 | || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
625 | || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
626 | || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); | ||
627 | } | ||
628 | } else { | ||
629 | agg = 0; | ||
630 | } | ||
631 | if (agg) { | ||
632 | for (runlen = 0; runlen < 4; ++runlen) { | ||
633 | if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one) | ||
634 | break; | ||
635 | } | ||
636 | mqc_setcurctx(mqc, T1_CTXNO_AGG); | ||
637 | mqc_encode(mqc, runlen != 4); | ||
638 | if (runlen == 4) { | ||
639 | continue; | ||
640 | } | ||
641 | mqc_setcurctx(mqc, T1_CTXNO_UNI); | ||
642 | mqc_encode(mqc, runlen >> 1); | ||
643 | mqc_encode(mqc, runlen & 1); | ||
644 | } else { | ||
645 | runlen = 0; | ||
646 | } | ||
647 | for (j = k + runlen; j < k + 4 && j < t1->h; ++j) { | ||
648 | vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0; | ||
649 | t1_enc_clnpass_step( | ||
650 | t1, | ||
651 | &t1->flags[((j+1) * t1->flags_stride) + i + 1], | ||
652 | &t1->data[(j * t1->w) + i], | ||
653 | orient, | ||
654 | bpno, | ||
655 | one, | ||
656 | nmsedec, | ||
657 | agg && (j == k + runlen), | ||
658 | vsc); | ||
659 | } | ||
660 | } | ||
661 | } | ||
662 | } | ||
663 | |||
664 | static void t1_dec_clnpass( | ||
665 | opj_t1_t *t1, | ||
666 | int bpno, | ||
667 | int orient, | ||
668 | int cblksty) | ||
669 | { | ||
670 | int i, j, k, one, half, oneplushalf, agg, runlen, vsc; | ||
671 | int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; | ||
672 | |||
673 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
674 | |||
675 | one = 1 << bpno; | ||
676 | half = one >> 1; | ||
677 | oneplushalf = one | half; | ||
678 | for (k = 0; k < t1->h; k += 4) { | ||
679 | for (i = 0; i < t1->w; ++i) { | ||
680 | if (k + 3 < t1->h) { | ||
681 | if (cblksty & J2K_CCP_CBLKSTY_VSC) { | ||
682 | agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
683 | || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
684 | || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
685 | || (MACRO_t1_flags(1 + k + 3,1 + i) | ||
686 | & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); | ||
687 | } else { | ||
688 | agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
689 | || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
690 | || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH) | ||
691 | || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); | ||
692 | } | ||
693 | } else { | ||
694 | agg = 0; | ||
695 | } | ||
696 | if (agg) { | ||
697 | mqc_setcurctx(mqc, T1_CTXNO_AGG); | ||
698 | if (!mqc_decode(mqc)) { | ||
699 | continue; | ||
700 | } | ||
701 | mqc_setcurctx(mqc, T1_CTXNO_UNI); | ||
702 | runlen = mqc_decode(mqc); | ||
703 | runlen = (runlen << 1) | mqc_decode(mqc); | ||
704 | } else { | ||
705 | runlen = 0; | ||
706 | } | ||
707 | for (j = k + runlen; j < k + 4 && j < t1->h; ++j) { | ||
708 | vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0; | ||
709 | t1_dec_clnpass_step( | ||
710 | t1, | ||
711 | &t1->flags[((j+1) * t1->flags_stride) + i + 1], | ||
712 | &t1->data[(j * t1->w) + i], | ||
713 | orient, | ||
714 | oneplushalf, | ||
715 | agg && (j == k + runlen), | ||
716 | vsc); | ||
717 | } | ||
718 | } | ||
719 | } | ||
720 | if (segsym) { | ||
721 | int v = 0; | ||
722 | mqc_setcurctx(mqc, T1_CTXNO_UNI); | ||
723 | v = mqc_decode(mqc); | ||
724 | v = (v << 1) | mqc_decode(mqc); | ||
725 | v = (v << 1) | mqc_decode(mqc); | ||
726 | v = (v << 1) | mqc_decode(mqc); | ||
727 | /* | ||
728 | if (v!=0xa) { | ||
729 | opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); | ||
730 | } | ||
731 | */ | ||
732 | } | ||
733 | } /* VSC and BYPASS by Antonin */ | ||
734 | |||
735 | |||
736 | /** mod fixed_quality */ | ||
737 | static double t1_getwmsedec( | ||
738 | int nmsedec, | ||
739 | int compno, | ||
740 | int level, | ||
741 | int orient, | ||
742 | int bpno, | ||
743 | int qmfbid, | ||
744 | double stepsize, | ||
745 | int numcomps) | ||
746 | { | ||
747 | double w1, w2, wmsedec; | ||
748 | if (qmfbid == 1) { | ||
749 | w1 = (numcomps > 1) ? mct_getnorm(compno) : 1.0; | ||
750 | w2 = dwt_getnorm(level, orient); | ||
751 | } else { /* if (qmfbid == 0) */ | ||
752 | w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1.0; | ||
753 | w2 = dwt_getnorm_real(level, orient); | ||
754 | } | ||
755 | wmsedec = w1 * w2 * stepsize * (1 << bpno); | ||
756 | wmsedec *= wmsedec * nmsedec / 8192.0; | ||
757 | |||
758 | return wmsedec; | ||
759 | } | ||
760 | |||
761 | static void allocate_buffers( | ||
762 | opj_t1_t *t1, | ||
763 | int w, | ||
764 | int h) | ||
765 | { | ||
766 | int datasize; | ||
767 | int flagssize; | ||
768 | |||
769 | datasize=w * h; | ||
770 | //fprintf(stderr,"w=%i h=%i datasize=%i flagssize=%i\n",w,h,datasize,flagssize); | ||
771 | |||
772 | if(datasize > t1->datasize){ | ||
773 | //fprintf(stderr,"Allocating t1->data: datasize=%i\n",datasize); | ||
774 | free(t1->data); | ||
775 | t1->data=malloc(datasize * sizeof(int)); | ||
776 | if(!t1->data){ | ||
777 | return; | ||
778 | } | ||
779 | t1->datasize=datasize; | ||
780 | } | ||
781 | //memset(t1->data,0xff,t1->datasize); | ||
782 | memset(t1->data,0,datasize * sizeof(int)); | ||
783 | |||
784 | t1->flags_stride=w+2; | ||
785 | flagssize=t1->flags_stride * (h+2); | ||
786 | |||
787 | if(flagssize > t1->flagssize){ | ||
788 | //fprintf(stderr,"Allocating t1->flags: flagssize=%i\n",flagssize); | ||
789 | free(t1->flags); | ||
790 | t1->flags=malloc(flagssize * sizeof(flag_t)); | ||
791 | if(!t1->flags){ | ||
792 | fprintf(stderr,"Allocating t1->flags FAILED!\n"); | ||
793 | return; | ||
794 | } | ||
795 | t1->flagssize=flagssize; | ||
796 | } | ||
797 | //memset(t1->flags,0xff,t1->flagssize); | ||
798 | memset(t1->flags,0,flagssize * sizeof(flag_t)); | ||
799 | |||
800 | t1->w=w; | ||
801 | t1->h=h; | ||
802 | } | ||
803 | |||
804 | /** mod fixed_quality */ | ||
805 | static void t1_encode_cblk( | ||
806 | opj_t1_t *t1, | ||
807 | opj_tcd_cblk_t * cblk, | ||
808 | int orient, | ||
809 | int compno, | ||
810 | int level, | ||
811 | int qmfbid, | ||
812 | double stepsize, | ||
813 | int cblksty, | ||
814 | int numcomps, | ||
815 | opj_tcd_tile_t * tile) | ||
816 | { | ||
817 | int i, j; | ||
818 | int passno; | ||
819 | int bpno, passtype; | ||
820 | int max; | ||
821 | int nmsedec = 0; | ||
822 | double cumwmsedec = 0.0; | ||
823 | char type = T1_TYPE_MQ; | ||
824 | |||
825 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
826 | |||
827 | max = 0; | ||
828 | for (j = 0; j < t1->h; ++j) { | ||
829 | for (i = 0; i < t1->w; ++i) { | ||
830 | max = int_max(max, int_abs(t1->data[(j * t1->w) + i])); | ||
831 | } | ||
832 | } | ||
833 | |||
834 | cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0; | ||
835 | |||
836 | bpno = cblk->numbps - 1; | ||
837 | passtype = 2; | ||
838 | |||
839 | mqc_resetstates(mqc); | ||
840 | mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); | ||
841 | mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); | ||
842 | mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); | ||
843 | mqc_init_enc(mqc, cblk->data); | ||
844 | |||
845 | for (passno = 0; bpno >= 0; ++passno) { | ||
846 | opj_tcd_pass_t *pass = &cblk->passes[passno]; | ||
847 | int correction = 3; | ||
848 | type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; | ||
849 | |||
850 | switch (passtype) { | ||
851 | case 0: | ||
852 | t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty); | ||
853 | break; | ||
854 | case 1: | ||
855 | t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty); | ||
856 | break; | ||
857 | case 2: | ||
858 | t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty); | ||
859 | /* code switch SEGMARK (i.e. SEGSYM) */ | ||
860 | if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) | ||
861 | mqc_segmark_enc(mqc); | ||
862 | break; | ||
863 | } | ||
864 | |||
865 | /* fixed_quality */ | ||
866 | cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); | ||
867 | tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); | ||
868 | |||
869 | /* Code switch "RESTART" (i.e. TERMALL) */ | ||
870 | if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) { | ||
871 | if (type == T1_TYPE_RAW) { | ||
872 | mqc_flush(mqc); | ||
873 | correction = 1; | ||
874 | /* correction = mqc_bypass_flush_enc(); */ | ||
875 | } else { /* correction = mqc_restart_enc(); */ | ||
876 | mqc_flush(mqc); | ||
877 | correction = 1; | ||
878 | } | ||
879 | pass->term = 1; | ||
880 | } else { | ||
881 | if (((bpno < (cblk->numbps - 4) && (passtype > 0)) | ||
882 | || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) { | ||
883 | if (type == T1_TYPE_RAW) { | ||
884 | mqc_flush(mqc); | ||
885 | correction = 1; | ||
886 | /* correction = mqc_bypass_flush_enc(); */ | ||
887 | } else { /* correction = mqc_restart_enc(); */ | ||
888 | mqc_flush(mqc); | ||
889 | correction = 1; | ||
890 | } | ||
891 | pass->term = 1; | ||
892 | } else { | ||
893 | pass->term = 0; | ||
894 | } | ||
895 | } | ||
896 | |||
897 | if (++passtype == 3) { | ||
898 | passtype = 0; | ||
899 | bpno--; | ||
900 | } | ||
901 | |||
902 | if (pass->term && bpno > 0) { | ||
903 | type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; | ||
904 | if (type == T1_TYPE_RAW) | ||
905 | mqc_bypass_init_enc(mqc); | ||
906 | else | ||
907 | mqc_restart_init_enc(mqc); | ||
908 | } | ||
909 | |||
910 | pass->distortiondec = cumwmsedec; | ||
911 | pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */ | ||
912 | |||
913 | /* Code-switch "RESET" */ | ||
914 | if (cblksty & J2K_CCP_CBLKSTY_RESET) | ||
915 | mqc_reset_enc(mqc); | ||
916 | } | ||
917 | |||
918 | /* Code switch "ERTERM" (i.e. PTERM) */ | ||
919 | if (cblksty & J2K_CCP_CBLKSTY_PTERM) | ||
920 | mqc_erterm_enc(mqc); | ||
921 | else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) | ||
922 | mqc_flush(mqc); | ||
923 | |||
924 | cblk->totalpasses = passno; | ||
925 | |||
926 | for (passno = 0; passno<cblk->totalpasses; passno++) { | ||
927 | opj_tcd_pass_t *pass = &cblk->passes[passno]; | ||
928 | if (pass->rate > mqc_numbytes(mqc)) | ||
929 | pass->rate = mqc_numbytes(mqc); | ||
930 | /*Preventing generation of FF as last data byte of a pass*/ | ||
931 | if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){ | ||
932 | pass->rate--; | ||
933 | } | ||
934 | pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate); | ||
935 | } | ||
936 | } | ||
937 | |||
938 | static void t1_decode_cblk( | ||
939 | opj_t1_t *t1, | ||
940 | opj_tcd_cblk_t * cblk, | ||
941 | int orient, | ||
942 | int roishift, | ||
943 | int cblksty) | ||
944 | { | ||
945 | int bpno, passtype; | ||
946 | int segno, passno; | ||
947 | char type = T1_TYPE_MQ; /* BYPASS mode */ | ||
948 | |||
949 | opj_raw_t *raw = t1->raw; /* RAW component */ | ||
950 | opj_mqc_t *mqc = t1->mqc; /* MQC component */ | ||
951 | |||
952 | allocate_buffers( | ||
953 | t1, | ||
954 | cblk->x1 - cblk->x0, | ||
955 | cblk->y1 - cblk->y0); | ||
956 | |||
957 | bpno = roishift + cblk->numbps - 1; | ||
958 | passtype = 2; | ||
959 | |||
960 | mqc_resetstates(mqc); | ||
961 | mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); | ||
962 | mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); | ||
963 | mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); | ||
964 | |||
965 | for (segno = 0; segno < cblk->numsegs; ++segno) { | ||
966 | opj_tcd_seg_t *seg = &cblk->segs[segno]; | ||
967 | |||
968 | /* BYPASS mode */ | ||
969 | type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; | ||
970 | if (type == T1_TYPE_RAW) { | ||
971 | raw_init_dec(raw, seg->data, seg->len); | ||
972 | } else { | ||
973 | mqc_init_dec(mqc, seg->data, seg->len); | ||
974 | } | ||
975 | |||
976 | for (passno = 0; passno < seg->numpasses; ++passno) { | ||
977 | switch (passtype) { | ||
978 | case 0: | ||
979 | t1_dec_sigpass(t1, bpno+1, orient, type, cblksty); | ||
980 | break; | ||
981 | case 1: | ||
982 | t1_dec_refpass(t1, bpno+1, type, cblksty); | ||
983 | break; | ||
984 | case 2: | ||
985 | t1_dec_clnpass(t1, bpno+1, orient, cblksty); | ||
986 | break; | ||
987 | } | ||
988 | |||
989 | if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) { | ||
990 | mqc_resetstates(mqc); | ||
991 | mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46); | ||
992 | mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); | ||
993 | mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); | ||
994 | } | ||
995 | if (++passtype == 3) { | ||
996 | passtype = 0; | ||
997 | bpno--; | ||
998 | } | ||
999 | } | ||
1000 | } | ||
1001 | } | ||
1002 | |||
1003 | /* ----------------------------------------------------------------------- */ | ||
1004 | |||
1005 | opj_t1_t* t1_create(opj_common_ptr cinfo) { | ||
1006 | opj_t1_t *t1 = (opj_t1_t*) malloc(sizeof(opj_t1_t)); | ||
1007 | if(!t1) | ||
1008 | return NULL; | ||
1009 | |||
1010 | t1->cinfo = cinfo; | ||
1011 | /* create MQC and RAW handles */ | ||
1012 | t1->mqc = mqc_create(); | ||
1013 | t1->raw = raw_create(); | ||
1014 | |||
1015 | t1->datasize=0; | ||
1016 | t1->data=NULL; | ||
1017 | t1->flagssize=0; | ||
1018 | t1->flags=NULL; | ||
1019 | |||
1020 | return t1; | ||
1021 | } | ||
1022 | |||
1023 | void t1_destroy(opj_t1_t *t1) { | ||
1024 | if(t1) { | ||
1025 | /* destroy MQC and RAW handles */ | ||
1026 | mqc_destroy(t1->mqc); | ||
1027 | raw_destroy(t1->raw); | ||
1028 | free(t1->data); | ||
1029 | free(t1->flags); | ||
1030 | free(t1); | ||
1031 | } | ||
1032 | } | ||
1033 | |||
1034 | void t1_encode_cblks( | ||
1035 | opj_t1_t *t1, | ||
1036 | opj_tcd_tile_t *tile, | ||
1037 | opj_tcp_t *tcp) | ||
1038 | { | ||
1039 | int compno, resno, bandno, precno, cblkno; | ||
1040 | |||
1041 | tile->distotile = 0; /* fixed_quality */ | ||
1042 | |||
1043 | for (compno = 0; compno < tile->numcomps; ++compno) { | ||
1044 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1045 | |||
1046 | for (resno = 0; resno < tilec->numresolutions; ++resno) { | ||
1047 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
1048 | |||
1049 | for (bandno = 0; bandno < res->numbands; ++bandno) { | ||
1050 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
1051 | |||
1052 | for (precno = 0; precno < res->pw * res->ph; ++precno) { | ||
1053 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
1054 | |||
1055 | for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) { | ||
1056 | int x, y, w, i, j, orient; | ||
1057 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
1058 | |||
1059 | x = cblk->x0 - band->x0; | ||
1060 | y = cblk->y0 - band->y0; | ||
1061 | if (band->bandno & 1) { | ||
1062 | opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; | ||
1063 | x += pres->x1 - pres->x0; | ||
1064 | } | ||
1065 | if (band->bandno & 2) { | ||
1066 | opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; | ||
1067 | y += pres->y1 - pres->y0; | ||
1068 | } | ||
1069 | |||
1070 | allocate_buffers( | ||
1071 | t1, | ||
1072 | cblk->x1 - cblk->x0, | ||
1073 | cblk->y1 - cblk->y0); | ||
1074 | |||
1075 | w = tilec->x1 - tilec->x0; | ||
1076 | if (tcp->tccps[compno].qmfbid == 1) { | ||
1077 | for (j = 0; j < t1->h; ++j) { | ||
1078 | for (i = 0; i < t1->w; ++i) { | ||
1079 | t1->data[(j * t1->w) + i] = | ||
1080 | tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS; | ||
1081 | } | ||
1082 | } | ||
1083 | } else { /* if (tcp->tccps[compno].qmfbid == 0) */ | ||
1084 | for (j = 0; j < t1->h; ++j) { | ||
1085 | for (i = 0; i < t1->w; ++i) { | ||
1086 | t1->data[(j * t1->w) + i] = | ||
1087 | fix_mul( | ||
1088 | tilec->data[x + i + (y + j) * w], | ||
1089 | 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS); | ||
1090 | } | ||
1091 | } | ||
1092 | } | ||
1093 | orient = band->bandno; /* FIXME */ | ||
1094 | if (orient == 2) { | ||
1095 | orient = 1; | ||
1096 | } else if (orient == 1) { | ||
1097 | orient = 2; | ||
1098 | } | ||
1099 | |||
1100 | t1_encode_cblk( | ||
1101 | t1, | ||
1102 | cblk, | ||
1103 | orient, | ||
1104 | compno, | ||
1105 | tilec->numresolutions - 1 - resno, | ||
1106 | tcp->tccps[compno].qmfbid, | ||
1107 | band->stepsize, | ||
1108 | tcp->tccps[compno].cblksty, | ||
1109 | tile->numcomps, | ||
1110 | tile); | ||
1111 | |||
1112 | } /* cblkno */ | ||
1113 | } /* precno */ | ||
1114 | } /* bandno */ | ||
1115 | } /* resno */ | ||
1116 | } /* compno */ | ||
1117 | } | ||
1118 | |||
1119 | void t1_decode_cblks( | ||
1120 | opj_t1_t *t1, | ||
1121 | opj_tcd_tile_t *tile, | ||
1122 | opj_tcp_t *tcp) | ||
1123 | { | ||
1124 | int compno, resno, bandno, precno, cblkno; | ||
1125 | |||
1126 | for (compno = 0; compno < tile->numcomps; ++compno) { | ||
1127 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1128 | |||
1129 | for (resno = 0; resno < tilec->numresolutions; ++resno) { | ||
1130 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
1131 | |||
1132 | for (bandno = 0; bandno < res->numbands; ++bandno) { | ||
1133 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
1134 | |||
1135 | for (precno = 0; precno < res->pw * res->ph; ++precno) { | ||
1136 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
1137 | |||
1138 | for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) { | ||
1139 | int x, y, w, i, j, orient, cblk_w, cblk_h; | ||
1140 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
1141 | |||
1142 | orient = band->bandno; /* FIXME */ | ||
1143 | if (orient == 2) { | ||
1144 | orient = 1; | ||
1145 | } else if (orient == 1) { | ||
1146 | orient = 2; | ||
1147 | } | ||
1148 | |||
1149 | t1_decode_cblk( | ||
1150 | t1, | ||
1151 | cblk, | ||
1152 | orient, | ||
1153 | tcp->tccps[compno].roishift, | ||
1154 | tcp->tccps[compno].cblksty); | ||
1155 | |||
1156 | x = cblk->x0 - band->x0; | ||
1157 | y = cblk->y0 - band->y0; | ||
1158 | if (band->bandno & 1) { | ||
1159 | opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; | ||
1160 | x += pres->x1 - pres->x0; | ||
1161 | } | ||
1162 | if (band->bandno & 2) { | ||
1163 | opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; | ||
1164 | y += pres->y1 - pres->y0; | ||
1165 | } | ||
1166 | |||
1167 | cblk_w = cblk->x1 - cblk->x0; | ||
1168 | cblk_h = cblk->y1 - cblk->y0; | ||
1169 | |||
1170 | if (tcp->tccps[compno].roishift) { | ||
1171 | int thresh = 1 << tcp->tccps[compno].roishift; | ||
1172 | for (j = 0; j < cblk_h; ++j) { | ||
1173 | for (i = 0; i < cblk_w; ++i) { | ||
1174 | int val = t1->data[(j * t1->w) + i]; | ||
1175 | int mag = int_abs(val); | ||
1176 | if (mag >= thresh) { | ||
1177 | mag >>= tcp->tccps[compno].roishift; | ||
1178 | t1->data[(j * t1->w) + i] = val < 0 ? -mag : mag; | ||
1179 | } | ||
1180 | } | ||
1181 | } | ||
1182 | } | ||
1183 | |||
1184 | w = tilec->x1 - tilec->x0; | ||
1185 | if (tcp->tccps[compno].qmfbid == 1) { | ||
1186 | for (j = 0; j < cblk_h; ++j) { | ||
1187 | for (i = 0; i < cblk_w; ++i) { | ||
1188 | tilec->data[x + i + (y + j) * w] = t1->data[(j * t1->w) + i]/2; | ||
1189 | } | ||
1190 | } | ||
1191 | } else { /* if (tcp->tccps[compno].qmfbid == 0) */ | ||
1192 | for (j = 0; j < cblk_h; ++j) { | ||
1193 | for (i = 0; i < cblk_w; ++i) { | ||
1194 | if (t1->data[(j * t1->w) + i] >> 1 == 0) { | ||
1195 | tilec->data[x + i + (y + j) * w] = 0; | ||
1196 | } else { | ||
1197 | double tmp = (double)(t1->data[(j * t1->w) + i] * band->stepsize * 4096.0); | ||
1198 | int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); | ||
1199 | tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2); | ||
1200 | } | ||
1201 | } | ||
1202 | } | ||
1203 | } | ||
1204 | } /* cblkno */ | ||
1205 | } /* precno */ | ||
1206 | } /* bandno */ | ||
1207 | } /* resno */ | ||
1208 | } /* compno */ | ||
1209 | } | ||
1210 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/t1.h b/libraries/openjpeg-libsl/libopenjpeg/t1.h new file mode 100644 index 0000000..1b6cdb7 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/t1.h | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | #ifndef __T1_H | ||
32 | #define __T1_H | ||
33 | /** | ||
34 | @file t1.h | ||
35 | @brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1) | ||
36 | |||
37 | The functions in T1.C have for goal to realize the tier-1 coding operation. The functions | ||
38 | in T1.C are used by some function in TCD.C. | ||
39 | */ | ||
40 | |||
41 | /** @defgroup T1 T1 - Implementation of the tier-1 coding */ | ||
42 | /*@{*/ | ||
43 | |||
44 | /* ----------------------------------------------------------------------- */ | ||
45 | #define T1_NMSEDEC_BITS 7 | ||
46 | |||
47 | #define T1_SIG_NE 0x0001 /**< Context orientation : North-East direction */ | ||
48 | #define T1_SIG_SE 0x0002 /**< Context orientation : South-East direction */ | ||
49 | #define T1_SIG_SW 0x0004 /**< Context orientation : South-West direction */ | ||
50 | #define T1_SIG_NW 0x0008 /**< Context orientation : North-West direction */ | ||
51 | #define T1_SIG_N 0x0010 /**< Context orientation : North direction */ | ||
52 | #define T1_SIG_E 0x0020 /**< Context orientation : East direction */ | ||
53 | #define T1_SIG_S 0x0040 /**< Context orientation : South direction */ | ||
54 | #define T1_SIG_W 0x0080 /**< Context orientation : West direction */ | ||
55 | #define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW) | ||
56 | #define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W) | ||
57 | |||
58 | #define T1_SGN_N 0x0100 | ||
59 | #define T1_SGN_E 0x0200 | ||
60 | #define T1_SGN_S 0x0400 | ||
61 | #define T1_SGN_W 0x0800 | ||
62 | #define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W) | ||
63 | |||
64 | #define T1_SIG 0x1000 | ||
65 | #define T1_REFINE 0x2000 | ||
66 | #define T1_VISIT 0x4000 | ||
67 | |||
68 | #define T1_NUMCTXS_ZC 9 | ||
69 | #define T1_NUMCTXS_SC 5 | ||
70 | #define T1_NUMCTXS_MAG 3 | ||
71 | #define T1_NUMCTXS_AGG 1 | ||
72 | #define T1_NUMCTXS_UNI 1 | ||
73 | |||
74 | #define T1_CTXNO_ZC 0 | ||
75 | #define T1_CTXNO_SC (T1_CTXNO_ZC+T1_NUMCTXS_ZC) | ||
76 | #define T1_CTXNO_MAG (T1_CTXNO_SC+T1_NUMCTXS_SC) | ||
77 | #define T1_CTXNO_AGG (T1_CTXNO_MAG+T1_NUMCTXS_MAG) | ||
78 | #define T1_CTXNO_UNI (T1_CTXNO_AGG+T1_NUMCTXS_AGG) | ||
79 | #define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI) | ||
80 | |||
81 | #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1) | ||
82 | |||
83 | #define T1_TYPE_MQ 0 /**< Normal coding using entropy coder */ | ||
84 | #define T1_TYPE_RAW 1 /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/ | ||
85 | |||
86 | /* ----------------------------------------------------------------------- */ | ||
87 | |||
88 | typedef short flag_t; | ||
89 | |||
90 | /** | ||
91 | Tier-1 coding (coding of code-block coefficients) | ||
92 | */ | ||
93 | typedef struct opj_t1 { | ||
94 | /** codec context */ | ||
95 | opj_common_ptr cinfo; | ||
96 | |||
97 | /** MQC component */ | ||
98 | opj_mqc_t *mqc; | ||
99 | /** RAW component */ | ||
100 | opj_raw_t *raw; | ||
101 | |||
102 | int *data; | ||
103 | flag_t *flags; | ||
104 | int w; | ||
105 | int h; | ||
106 | int datasize; | ||
107 | int flagssize; | ||
108 | int flags_stride; | ||
109 | } opj_t1_t; | ||
110 | |||
111 | #define MACRO_t1_flags(x,y) t1->flags[((x)*(t1->flags_stride))+(y)] | ||
112 | |||
113 | /** @name Exported functions */ | ||
114 | /*@{*/ | ||
115 | /* ----------------------------------------------------------------------- */ | ||
116 | /** | ||
117 | Create a new T1 handle | ||
118 | and initialize the look-up tables of the Tier-1 coder/decoder | ||
119 | @return Returns a new T1 handle if successful, returns NULL otherwise | ||
120 | @see t1_init_luts | ||
121 | */ | ||
122 | opj_t1_t* t1_create(opj_common_ptr cinfo); | ||
123 | /** | ||
124 | Destroy a previously created T1 handle | ||
125 | @param t1 T1 handle to destroy | ||
126 | */ | ||
127 | void t1_destroy(opj_t1_t *t1); | ||
128 | /** | ||
129 | Encode the code-blocks of a tile | ||
130 | @param t1 T1 handle | ||
131 | @param tile The tile to encode | ||
132 | @param tcp Tile coding parameters | ||
133 | */ | ||
134 | void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); | ||
135 | /** | ||
136 | Decode the code-blocks of a tile | ||
137 | @param t1 T1 handle | ||
138 | @param tile The tile to decode | ||
139 | @param tcp Tile coding parameters | ||
140 | */ | ||
141 | void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); | ||
142 | /* ----------------------------------------------------------------------- */ | ||
143 | /*@}*/ | ||
144 | |||
145 | /*@}*/ | ||
146 | |||
147 | #endif /* __T1_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/t1_generate_luts.c b/libraries/openjpeg-libsl/libopenjpeg/t1_generate_luts.c new file mode 100644 index 0000000..62cb158 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/t1_generate_luts.c | |||
@@ -0,0 +1,295 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com> | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "opj_includes.h" | ||
34 | #include <math.h> | ||
35 | |||
36 | static int t1_init_ctxno_zc(int f, int orient) { | ||
37 | int h, v, d, n, t, hv; | ||
38 | n = 0; | ||
39 | h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0); | ||
40 | v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0); | ||
41 | d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0); | ||
42 | |||
43 | switch (orient) { | ||
44 | case 2: | ||
45 | t = h; | ||
46 | h = v; | ||
47 | v = t; | ||
48 | case 0: | ||
49 | case 1: | ||
50 | if (!h) { | ||
51 | if (!v) { | ||
52 | if (!d) | ||
53 | n = 0; | ||
54 | else if (d == 1) | ||
55 | n = 1; | ||
56 | else | ||
57 | n = 2; | ||
58 | } else if (v == 1) { | ||
59 | n = 3; | ||
60 | } else { | ||
61 | n = 4; | ||
62 | } | ||
63 | } else if (h == 1) { | ||
64 | if (!v) { | ||
65 | if (!d) | ||
66 | n = 5; | ||
67 | else | ||
68 | n = 6; | ||
69 | } else { | ||
70 | n = 7; | ||
71 | } | ||
72 | } else | ||
73 | n = 8; | ||
74 | break; | ||
75 | case 3: | ||
76 | hv = h + v; | ||
77 | if (!d) { | ||
78 | if (!hv) { | ||
79 | n = 0; | ||
80 | } else if (hv == 1) { | ||
81 | n = 1; | ||
82 | } else { | ||
83 | n = 2; | ||
84 | } | ||
85 | } else if (d == 1) { | ||
86 | if (!hv) { | ||
87 | n = 3; | ||
88 | } else if (hv == 1) { | ||
89 | n = 4; | ||
90 | } else { | ||
91 | n = 5; | ||
92 | } | ||
93 | } else if (d == 2) { | ||
94 | if (!hv) { | ||
95 | n = 6; | ||
96 | } else { | ||
97 | n = 7; | ||
98 | } | ||
99 | } else { | ||
100 | n = 8; | ||
101 | } | ||
102 | break; | ||
103 | } | ||
104 | |||
105 | return (T1_CTXNO_ZC + n); | ||
106 | } | ||
107 | |||
108 | static int t1_init_ctxno_sc(int f) { | ||
109 | int hc, vc, n; | ||
110 | n = 0; | ||
111 | |||
112 | hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == | ||
113 | T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W), | ||
114 | 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == | ||
115 | (T1_SIG_E | T1_SGN_E)) + | ||
116 | ((f & (T1_SIG_W | T1_SGN_W)) == | ||
117 | (T1_SIG_W | T1_SGN_W)), 1); | ||
118 | |||
119 | vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == | ||
120 | T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S), | ||
121 | 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == | ||
122 | (T1_SIG_N | T1_SGN_N)) + | ||
123 | ((f & (T1_SIG_S | T1_SGN_S)) == | ||
124 | (T1_SIG_S | T1_SGN_S)), 1); | ||
125 | |||
126 | if (hc < 0) { | ||
127 | hc = -hc; | ||
128 | vc = -vc; | ||
129 | } | ||
130 | if (!hc) { | ||
131 | if (vc == -1) | ||
132 | n = 1; | ||
133 | else if (!vc) | ||
134 | n = 0; | ||
135 | else | ||
136 | n = 1; | ||
137 | } else if (hc == 1) { | ||
138 | if (vc == -1) | ||
139 | n = 2; | ||
140 | else if (!vc) | ||
141 | n = 3; | ||
142 | else | ||
143 | n = 4; | ||
144 | } | ||
145 | |||
146 | return (T1_CTXNO_SC + n); | ||
147 | } | ||
148 | |||
149 | static int t1_init_ctxno_mag(int f) { | ||
150 | int n; | ||
151 | if (!(f & T1_REFINE)) | ||
152 | n = (f & (T1_SIG_OTH)) ? 1 : 0; | ||
153 | else | ||
154 | n = 2; | ||
155 | |||
156 | return (T1_CTXNO_MAG + n); | ||
157 | } | ||
158 | |||
159 | static int t1_init_spb(int f) { | ||
160 | int hc, vc, n; | ||
161 | |||
162 | hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == | ||
163 | T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W), | ||
164 | 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == | ||
165 | (T1_SIG_E | T1_SGN_E)) + | ||
166 | ((f & (T1_SIG_W | T1_SGN_W)) == | ||
167 | (T1_SIG_W | T1_SGN_W)), 1); | ||
168 | |||
169 | vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == | ||
170 | T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S), | ||
171 | 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == | ||
172 | (T1_SIG_N | T1_SGN_N)) + | ||
173 | ((f & (T1_SIG_S | T1_SGN_S)) == | ||
174 | (T1_SIG_S | T1_SGN_S)), 1); | ||
175 | |||
176 | if (!hc && !vc) | ||
177 | n = 0; | ||
178 | else | ||
179 | n = (!(hc > 0 || (!hc && vc > 0))); | ||
180 | |||
181 | return n; | ||
182 | } | ||
183 | |||
184 | void dump_array16(int array[],int size){ | ||
185 | int i; | ||
186 | --size; | ||
187 | for (i = 0; i < size; ++i) { | ||
188 | printf("0x%04x, ", array[i]); | ||
189 | if(!((i+1)&0x7)) | ||
190 | printf("\n "); | ||
191 | } | ||
192 | printf("0x%04x\n};\n\n", array[size]); | ||
193 | } | ||
194 | |||
195 | int main(){ | ||
196 | int i, j; | ||
197 | double u, v, t; | ||
198 | |||
199 | int lut_ctxno_zc[1024]; | ||
200 | int lut_ctxno_mag[4096]; | ||
201 | int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS]; | ||
202 | int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS]; | ||
203 | int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS]; | ||
204 | int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS]; | ||
205 | |||
206 | printf("/* This file was automatically generated by t1_generate_luts.c */\n\n"); | ||
207 | |||
208 | // lut_ctxno_zc | ||
209 | for (j = 0; j < 4; ++j) { | ||
210 | for (i = 0; i < 256; ++i) { | ||
211 | lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j); | ||
212 | } | ||
213 | } | ||
214 | |||
215 | printf("static int8_t lut_ctxno_zc[1024] = {\n "); | ||
216 | for (i = 0; i < 1023; ++i) { | ||
217 | printf("%i, ", lut_ctxno_zc[i]); | ||
218 | if(!((i+1)&0x1f)) | ||
219 | printf("\n "); | ||
220 | } | ||
221 | printf("%i\n};\n\n", lut_ctxno_zc[1023]); | ||
222 | |||
223 | // lut_ctxno_sc | ||
224 | printf("static int8_t lut_ctxno_sc[256] = {\n "); | ||
225 | for (i = 0; i < 255; ++i) { | ||
226 | printf("0x%x, ", t1_init_ctxno_sc(i << 4)); | ||
227 | if(!((i+1)&0xf)) | ||
228 | printf("\n "); | ||
229 | } | ||
230 | printf("0x%x\n};\n\n", t1_init_ctxno_sc(255 << 4)); | ||
231 | |||
232 | // lut_ctxno_mag | ||
233 | for (j = 0; j < 2; ++j) { | ||
234 | for (i = 0; i < 2048; ++i) { | ||
235 | lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i); | ||
236 | } | ||
237 | } | ||
238 | |||
239 | printf("static int8_t lut_ctxno_mag[4096] = {\n "); | ||
240 | for (i = 0; i < 4095; ++i) { | ||
241 | printf("%i, ", lut_ctxno_mag[i]); | ||
242 | if(!((i+1)&0xf)) | ||
243 | printf("\n "); | ||
244 | } | ||
245 | printf("%i\n};\n\n", lut_ctxno_mag[4095]); | ||
246 | |||
247 | // lut_spb | ||
248 | printf("static int8_t lut_spb[256] = {\n "); | ||
249 | for (i = 0; i < 255; ++i) { | ||
250 | printf("%i, ", t1_init_spb(i << 4)); | ||
251 | if(!((i+1)&0x1f)) | ||
252 | printf("\n "); | ||
253 | } | ||
254 | printf("%i\n};\n\n", t1_init_spb(255 << 4)); | ||
255 | |||
256 | /* FIXME FIXME FIXME */ | ||
257 | /* fprintf(stdout,"nmsedec luts:\n"); */ | ||
258 | for (i = 0; i < (1 << T1_NMSEDEC_BITS); ++i) { | ||
259 | t = i / pow(2, T1_NMSEDEC_FRACBITS); | ||
260 | u = t; | ||
261 | v = t - 1.5; | ||
262 | lut_nmsedec_sig[i] = | ||
263 | int_max(0, | ||
264 | (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); | ||
265 | lut_nmsedec_sig0[i] = | ||
266 | int_max(0, | ||
267 | (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); | ||
268 | u = t - 1.0; | ||
269 | if (i & (1 << (T1_NMSEDEC_BITS - 1))) { | ||
270 | v = t - 1.5; | ||
271 | } else { | ||
272 | v = t - 0.5; | ||
273 | } | ||
274 | lut_nmsedec_ref[i] = | ||
275 | int_max(0, | ||
276 | (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); | ||
277 | lut_nmsedec_ref0[i] = | ||
278 | int_max(0, | ||
279 | (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); | ||
280 | } | ||
281 | |||
282 | printf("static int16_t lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = {\n "); | ||
283 | dump_array16(&lut_nmsedec_sig, 1 << T1_NMSEDEC_BITS); | ||
284 | |||
285 | printf("static int16_t lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = {\n "); | ||
286 | dump_array16(&lut_nmsedec_sig0, 1 << T1_NMSEDEC_BITS); | ||
287 | |||
288 | printf("static int16_t lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = {\n "); | ||
289 | dump_array16(&lut_nmsedec_ref, 1 << T1_NMSEDEC_BITS); | ||
290 | |||
291 | printf("static int16_t lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = {\n "); | ||
292 | dump_array16(&lut_nmsedec_ref0, 1 << T1_NMSEDEC_BITS); | ||
293 | |||
294 | return 0; | ||
295 | } | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/t1_luts.h b/libraries/openjpeg-libsl/libopenjpeg/t1_luts.h new file mode 100644 index 0000000..3f957f3 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/t1_luts.h | |||
@@ -0,0 +1,402 @@ | |||
1 | /* This file was automatically generated by t1_generate_luts.c */ | ||
2 | |||
3 | static char lut_ctxno_zc[1024] = { | ||
4 | 0, 1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | ||
5 | 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
6 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | ||
7 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
8 | 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
9 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
10 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
11 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
12 | 0, 1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | ||
13 | 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
14 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | ||
15 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
16 | 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
17 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
18 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
19 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
20 | 0, 1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | ||
21 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
22 | 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
23 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
24 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
25 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | ||
26 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
27 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | ||
28 | 0, 3, 3, 6, 3, 6, 6, 8, 3, 6, 6, 8, 6, 8, 8, 8, 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, | ||
29 | 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, | ||
30 | 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, | ||
31 | 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, | ||
32 | 1, 4, 4, 7, 4, 7, 7, 8, 4, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, | ||
33 | 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, | ||
34 | 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, | ||
35 | 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8, 2, 5, 5, 7, 5, 7, 7, 8, 5, 7, 7, 8, 7, 8, 8, 8 | ||
36 | }; | ||
37 | |||
38 | static char lut_ctxno_sc[256] = { | ||
39 | 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xd, 0xc, 0xd, 0xd, 0xd, 0xd, 0xd, | ||
40 | 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xb, 0xc, 0xb, 0xd, 0xc, 0xd, 0xc, | ||
41 | 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xd, 0x9, 0xa, 0xd, 0xd, 0xa, 0xa, | ||
42 | 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xb, 0x9, 0xa, 0xd, 0xc, 0xa, 0x9, | ||
43 | 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xd, 0xc, 0xd, 0xb, 0xc, 0xb, 0xc, | ||
44 | 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xb, 0xc, 0xb, 0xb, 0xb, 0xb, 0xb, | ||
45 | 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xd, 0x9, 0xa, 0xb, 0xc, 0xa, 0x9, | ||
46 | 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xb, 0x9, 0xa, 0xb, 0xb, 0xa, 0xa, | ||
47 | 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xb, 0x9, 0xa, 0xb, 0xb, 0xa, 0xa, | ||
48 | 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xd, 0x9, 0xa, 0xb, 0xc, 0xa, 0x9, | ||
49 | 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xb, 0xc, 0xb, 0xb, 0xb, 0xb, 0xb, | ||
50 | 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xd, 0xc, 0xd, 0xb, 0xc, 0xb, 0xc, | ||
51 | 0x9, 0xa, 0xc, 0xd, 0xa, 0x9, 0xb, 0xc, 0xc, 0xb, 0x9, 0xa, 0xd, 0xc, 0xa, 0x9, | ||
52 | 0x9, 0xa, 0xc, 0xb, 0xa, 0xa, 0xb, 0xb, 0xc, 0xd, 0x9, 0xa, 0xd, 0xd, 0xa, 0xa, | ||
53 | 0x9, 0xa, 0xc, 0xb, 0xa, 0x9, 0xd, 0xc, 0xc, 0xb, 0xc, 0xb, 0xd, 0xc, 0xd, 0xc, | ||
54 | 0x9, 0xa, 0xc, 0xd, 0xa, 0xa, 0xd, 0xd, 0xc, 0xd, 0xc, 0xd, 0xd, 0xd, 0xd, 0xd | ||
55 | }; | ||
56 | |||
57 | static char lut_ctxno_mag[4096] = { | ||
58 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
59 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
60 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
61 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
62 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
63 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
64 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
65 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
66 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
67 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
68 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
69 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
70 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
71 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
72 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
73 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
74 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
75 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
76 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
77 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
78 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
79 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
80 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
81 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
82 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
83 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
84 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
85 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
86 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
87 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
88 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
89 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
90 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
91 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
92 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
93 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
94 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
95 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
96 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
97 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
98 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
99 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
100 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
101 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
102 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
103 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
104 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
105 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
106 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
107 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
108 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
109 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
110 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
111 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
112 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
113 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
114 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
115 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
116 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
117 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
118 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
119 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
120 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
121 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
122 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
123 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
124 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
125 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
126 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
127 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
128 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
129 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
130 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
131 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
132 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
133 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
134 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
135 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
136 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
137 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
138 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
139 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
140 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
141 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
142 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
143 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
144 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
145 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
146 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
147 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
148 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
149 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
150 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
151 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
152 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
153 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
154 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
155 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
156 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
157 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
158 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
159 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
160 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
161 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
162 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
163 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
164 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
165 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
166 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
167 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
168 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
169 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
170 | 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
171 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
172 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
173 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
174 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
175 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
176 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
177 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
178 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
179 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
180 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
181 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
182 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
183 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
184 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
185 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, | ||
186 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
187 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
188 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
189 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
190 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
191 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
192 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
193 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
194 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
195 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
196 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
197 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
198 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
199 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
200 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
201 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
202 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
203 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
204 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
205 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
206 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
207 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
208 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
209 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
210 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
211 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
212 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
213 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
214 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
215 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
216 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
217 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
218 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
219 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
220 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
221 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
222 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
223 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
224 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
225 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
226 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
227 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
228 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
229 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
230 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
231 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
232 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
233 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
234 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
235 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
236 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
237 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
238 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
239 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
240 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
241 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
242 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
243 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
244 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
245 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
246 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
247 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
248 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
249 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
250 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
251 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
252 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
253 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
254 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
255 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
256 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
257 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
258 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
259 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
260 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
261 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
262 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
263 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
264 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
265 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
266 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
267 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
268 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
269 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
270 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
271 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
272 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
273 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
274 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
275 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
276 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
277 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
278 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
279 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
280 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
281 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
282 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
283 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
284 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
285 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
286 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
287 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
288 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
289 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
290 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
291 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
292 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
293 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
294 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
295 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
296 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
297 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
298 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
299 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
300 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
301 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
302 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
303 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
304 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
305 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
306 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
307 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
308 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
309 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
310 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
311 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
312 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, | ||
313 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 | ||
314 | }; | ||
315 | |||
316 | static char lut_spb[256] = { | ||
317 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
318 | 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, | ||
319 | 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
320 | 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, | ||
321 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, | ||
322 | 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
323 | 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, | ||
324 | 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 | ||
325 | }; | ||
326 | |||
327 | static short lut_nmsedec_sig[1 << T1_NMSEDEC_BITS] = { | ||
328 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
329 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
330 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
331 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
332 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
333 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
334 | 0x0000, 0x0180, 0x0300, 0x0480, 0x0600, 0x0780, 0x0900, 0x0a80, | ||
335 | 0x0c00, 0x0d80, 0x0f00, 0x1080, 0x1200, 0x1380, 0x1500, 0x1680, | ||
336 | 0x1800, 0x1980, 0x1b00, 0x1c80, 0x1e00, 0x1f80, 0x2100, 0x2280, | ||
337 | 0x2400, 0x2580, 0x2700, 0x2880, 0x2a00, 0x2b80, 0x2d00, 0x2e80, | ||
338 | 0x3000, 0x3180, 0x3300, 0x3480, 0x3600, 0x3780, 0x3900, 0x3a80, | ||
339 | 0x3c00, 0x3d80, 0x3f00, 0x4080, 0x4200, 0x4380, 0x4500, 0x4680, | ||
340 | 0x4800, 0x4980, 0x4b00, 0x4c80, 0x4e00, 0x4f80, 0x5100, 0x5280, | ||
341 | 0x5400, 0x5580, 0x5700, 0x5880, 0x5a00, 0x5b80, 0x5d00, 0x5e80, | ||
342 | 0x6000, 0x6180, 0x6300, 0x6480, 0x6600, 0x6780, 0x6900, 0x6a80, | ||
343 | 0x6c00, 0x6d80, 0x6f00, 0x7080, 0x7200, 0x7380, 0x7500, 0x7680 | ||
344 | }; | ||
345 | |||
346 | static short lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS] = { | ||
347 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0080, | ||
348 | 0x0080, 0x0080, 0x0100, 0x0100, 0x0100, 0x0180, 0x0180, 0x0200, | ||
349 | 0x0200, 0x0280, 0x0280, 0x0300, 0x0300, 0x0380, 0x0400, 0x0400, | ||
350 | 0x0480, 0x0500, 0x0580, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780, | ||
351 | 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b80, 0x0c00, | ||
352 | 0x0c80, 0x0d00, 0x0e00, 0x0e80, 0x0f00, 0x1000, 0x1080, 0x1180, | ||
353 | 0x1200, 0x1300, 0x1380, 0x1480, 0x1500, 0x1600, 0x1700, 0x1780, | ||
354 | 0x1880, 0x1980, 0x1a80, 0x1b00, 0x1c00, 0x1d00, 0x1e00, 0x1f00, | ||
355 | 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2680, 0x2780, | ||
356 | 0x2880, 0x2980, 0x2b00, 0x2c00, 0x2d00, 0x2e80, 0x2f80, 0x3100, | ||
357 | 0x3200, 0x3380, 0x3480, 0x3600, 0x3700, 0x3880, 0x3a00, 0x3b00, | ||
358 | 0x3c80, 0x3e00, 0x3f80, 0x4080, 0x4200, 0x4380, 0x4500, 0x4680, | ||
359 | 0x4800, 0x4980, 0x4b00, 0x4c80, 0x4e00, 0x4f80, 0x5180, 0x5300, | ||
360 | 0x5480, 0x5600, 0x5800, 0x5980, 0x5b00, 0x5d00, 0x5e80, 0x6080, | ||
361 | 0x6200, 0x6400, 0x6580, 0x6780, 0x6900, 0x6b00, 0x6d00, 0x6e80, | ||
362 | 0x7080, 0x7280, 0x7480, 0x7600, 0x7800, 0x7a00, 0x7c00, 0x7e00 | ||
363 | }; | ||
364 | |||
365 | static short lut_nmsedec_ref[1 << T1_NMSEDEC_BITS] = { | ||
366 | 0x1800, 0x1780, 0x1700, 0x1680, 0x1600, 0x1580, 0x1500, 0x1480, | ||
367 | 0x1400, 0x1380, 0x1300, 0x1280, 0x1200, 0x1180, 0x1100, 0x1080, | ||
368 | 0x1000, 0x0f80, 0x0f00, 0x0e80, 0x0e00, 0x0d80, 0x0d00, 0x0c80, | ||
369 | 0x0c00, 0x0b80, 0x0b00, 0x0a80, 0x0a00, 0x0980, 0x0900, 0x0880, | ||
370 | 0x0800, 0x0780, 0x0700, 0x0680, 0x0600, 0x0580, 0x0500, 0x0480, | ||
371 | 0x0400, 0x0380, 0x0300, 0x0280, 0x0200, 0x0180, 0x0100, 0x0080, | ||
372 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
373 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
374 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
375 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
376 | 0x0000, 0x0080, 0x0100, 0x0180, 0x0200, 0x0280, 0x0300, 0x0380, | ||
377 | 0x0400, 0x0480, 0x0500, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780, | ||
378 | 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b00, 0x0b80, | ||
379 | 0x0c00, 0x0c80, 0x0d00, 0x0d80, 0x0e00, 0x0e80, 0x0f00, 0x0f80, | ||
380 | 0x1000, 0x1080, 0x1100, 0x1180, 0x1200, 0x1280, 0x1300, 0x1380, | ||
381 | 0x1400, 0x1480, 0x1500, 0x1580, 0x1600, 0x1680, 0x1700, 0x1780 | ||
382 | }; | ||
383 | |||
384 | static short lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS] = { | ||
385 | 0x2000, 0x1f00, 0x1e00, 0x1d00, 0x1c00, 0x1b00, 0x1a80, 0x1980, | ||
386 | 0x1880, 0x1780, 0x1700, 0x1600, 0x1500, 0x1480, 0x1380, 0x1300, | ||
387 | 0x1200, 0x1180, 0x1080, 0x1000, 0x0f00, 0x0e80, 0x0e00, 0x0d00, | ||
388 | 0x0c80, 0x0c00, 0x0b80, 0x0a80, 0x0a00, 0x0980, 0x0900, 0x0880, | ||
389 | 0x0800, 0x0780, 0x0700, 0x0680, 0x0600, 0x0580, 0x0580, 0x0500, | ||
390 | 0x0480, 0x0400, 0x0400, 0x0380, 0x0300, 0x0300, 0x0280, 0x0280, | ||
391 | 0x0200, 0x0200, 0x0180, 0x0180, 0x0100, 0x0100, 0x0100, 0x0080, | ||
392 | 0x0080, 0x0080, 0x0080, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
393 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0080, | ||
394 | 0x0080, 0x0080, 0x0100, 0x0100, 0x0100, 0x0180, 0x0180, 0x0200, | ||
395 | 0x0200, 0x0280, 0x0280, 0x0300, 0x0300, 0x0380, 0x0400, 0x0400, | ||
396 | 0x0480, 0x0500, 0x0580, 0x0580, 0x0600, 0x0680, 0x0700, 0x0780, | ||
397 | 0x0800, 0x0880, 0x0900, 0x0980, 0x0a00, 0x0a80, 0x0b80, 0x0c00, | ||
398 | 0x0c80, 0x0d00, 0x0e00, 0x0e80, 0x0f00, 0x1000, 0x1080, 0x1180, | ||
399 | 0x1200, 0x1300, 0x1380, 0x1480, 0x1500, 0x1600, 0x1700, 0x1780, | ||
400 | 0x1880, 0x1980, 0x1a80, 0x1b00, 0x1c00, 0x1d00, 0x1e00, 0x1f00 | ||
401 | }; | ||
402 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/t2.c b/libraries/openjpeg-libsl/libopenjpeg/t2.c new file mode 100644 index 0000000..dc8e24e --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/t2.c | |||
@@ -0,0 +1,721 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /** @defgroup T2 T2 - Implementation of a tier-2 coding */ | ||
35 | /*@{*/ | ||
36 | |||
37 | /** @name Local static functions */ | ||
38 | /*@{*/ | ||
39 | |||
40 | static void t2_putcommacode(opj_bio_t *bio, int n); | ||
41 | static int t2_getcommacode(opj_bio_t *bio); | ||
42 | /** | ||
43 | Variable length code for signalling delta Zil (truncation point) | ||
44 | @param bio Bit Input/Output component | ||
45 | @param n delta Zil | ||
46 | */ | ||
47 | static void t2_putnumpasses(opj_bio_t *bio, int n); | ||
48 | static int t2_getnumpasses(opj_bio_t *bio); | ||
49 | /** | ||
50 | Encode a packet of a tile to a destination buffer | ||
51 | @param tile Tile for which to write the packets | ||
52 | @param tcp Tile coding parameters | ||
53 | @param pi Packet identity | ||
54 | @param dest Destination buffer | ||
55 | @param len Length of the destination buffer | ||
56 | @param image_info Structure to create an index file | ||
57 | @param tileno Number of the tile encoded | ||
58 | @return | ||
59 | */ | ||
60 | static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_image_info_t *image_info, int tileno); | ||
61 | /** | ||
62 | @param seg | ||
63 | @param cblksty | ||
64 | @param first | ||
65 | */ | ||
66 | static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first); | ||
67 | /** | ||
68 | Decode a packet of a tile from a source buffer | ||
69 | @param t2 T2 handle | ||
70 | @param src Source buffer | ||
71 | @param len Length of the source buffer | ||
72 | @param tile Tile for which to write the packets | ||
73 | @param tcp Tile coding parameters | ||
74 | @param pi Packet identity | ||
75 | @return | ||
76 | */ | ||
77 | static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi); | ||
78 | |||
79 | /*@}*/ | ||
80 | |||
81 | /*@}*/ | ||
82 | |||
83 | /* ----------------------------------------------------------------------- */ | ||
84 | |||
85 | /* #define RESTART 0x04 */ | ||
86 | |||
87 | static void t2_putcommacode(opj_bio_t *bio, int n) { | ||
88 | while (--n >= 0) { | ||
89 | bio_write(bio, 1, 1); | ||
90 | } | ||
91 | bio_write(bio, 0, 1); | ||
92 | } | ||
93 | |||
94 | static int t2_getcommacode(opj_bio_t *bio) { | ||
95 | int n; | ||
96 | for (n = 0; bio_read(bio, 1); n++) { | ||
97 | ; | ||
98 | } | ||
99 | return n; | ||
100 | } | ||
101 | |||
102 | static void t2_putnumpasses(opj_bio_t *bio, int n) { | ||
103 | if (n == 1) { | ||
104 | bio_write(bio, 0, 1); | ||
105 | } else if (n == 2) { | ||
106 | bio_write(bio, 2, 2); | ||
107 | } else if (n <= 5) { | ||
108 | bio_write(bio, 0xc | (n - 3), 4); | ||
109 | } else if (n <= 36) { | ||
110 | bio_write(bio, 0x1e0 | (n - 6), 9); | ||
111 | } else if (n <= 164) { | ||
112 | bio_write(bio, 0xff80 | (n - 37), 16); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | static int t2_getnumpasses(opj_bio_t *bio) { | ||
117 | int n; | ||
118 | if (!bio_read(bio, 1)) | ||
119 | return 1; | ||
120 | if (!bio_read(bio, 1)) | ||
121 | return 2; | ||
122 | if ((n = bio_read(bio, 2)) != 3) | ||
123 | return (3 + n); | ||
124 | if ((n = bio_read(bio, 5)) != 31) | ||
125 | return (6 + n); | ||
126 | return (37 + bio_read(bio, 7)); | ||
127 | } | ||
128 | |||
129 | static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_image_info_t * image_info, int tileno) { | ||
130 | int bandno, cblkno; | ||
131 | unsigned char *sop = 0, *eph = 0; | ||
132 | unsigned char *c = dest; | ||
133 | |||
134 | int compno = pi->compno; /* component value */ | ||
135 | int resno = pi->resno; /* resolution level value */ | ||
136 | int precno = pi->precno; /* precinct value */ | ||
137 | int layno = pi->layno; /* quality layer value */ | ||
138 | |||
139 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
140 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
141 | |||
142 | opj_bio_t *bio = NULL; /* BIO component */ | ||
143 | |||
144 | /* <SOP 0xff91> */ | ||
145 | if (tcp->csty & J2K_CP_CSTY_SOP) { | ||
146 | sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char)); | ||
147 | sop[0] = 255; | ||
148 | sop[1] = 145; | ||
149 | sop[2] = 0; | ||
150 | sop[3] = 4; | ||
151 | sop[4] = (image_info->num % 65536) / 256; | ||
152 | sop[5] = (image_info->num % 65536) % 256; | ||
153 | memcpy(c, sop, 6); | ||
154 | opj_free(sop); | ||
155 | c += 6; | ||
156 | } | ||
157 | /* </SOP> */ | ||
158 | |||
159 | if (!layno) { | ||
160 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
161 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
162 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
163 | tgt_reset(prc->incltree); | ||
164 | tgt_reset(prc->imsbtree); | ||
165 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
166 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
167 | cblk->numpasses = 0; | ||
168 | tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | |||
173 | bio = bio_create(); | ||
174 | bio_init_enc(bio, c, length); | ||
175 | bio_write(bio, 1, 1); /* Empty header bit */ | ||
176 | |||
177 | /* Writing Packet header */ | ||
178 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
179 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
180 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
181 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
182 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
183 | opj_tcd_layer_t *layer = &cblk->layers[layno]; | ||
184 | if (!cblk->numpasses && layer->numpasses) { | ||
185 | tgt_setvalue(prc->incltree, cblkno, layno); | ||
186 | } | ||
187 | } | ||
188 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
189 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
190 | opj_tcd_layer_t *layer = &cblk->layers[layno]; | ||
191 | int increment = 0; | ||
192 | int nump = 0; | ||
193 | int len = 0, passno; | ||
194 | /* cblk inclusion bits */ | ||
195 | if (!cblk->numpasses) { | ||
196 | tgt_encode(bio, prc->incltree, cblkno, layno + 1); | ||
197 | } else { | ||
198 | bio_write(bio, layer->numpasses != 0, 1); | ||
199 | } | ||
200 | /* if cblk not included, go to the next cblk */ | ||
201 | if (!layer->numpasses) { | ||
202 | continue; | ||
203 | } | ||
204 | /* if first instance of cblk --> zero bit-planes information */ | ||
205 | if (!cblk->numpasses) { | ||
206 | cblk->numlenbits = 3; | ||
207 | tgt_encode(bio, prc->imsbtree, cblkno, 999); | ||
208 | } | ||
209 | /* number of coding passes included */ | ||
210 | t2_putnumpasses(bio, layer->numpasses); | ||
211 | |||
212 | /* computation of the increase of the length indicator and insertion in the header */ | ||
213 | for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { | ||
214 | opj_tcd_pass_t *pass = &cblk->passes[passno]; | ||
215 | nump++; | ||
216 | len += pass->len; | ||
217 | if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { | ||
218 | increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); | ||
219 | len = 0; | ||
220 | nump = 0; | ||
221 | } | ||
222 | } | ||
223 | t2_putcommacode(bio, increment); | ||
224 | |||
225 | /* computation of the new Length indicator */ | ||
226 | cblk->numlenbits += increment; | ||
227 | |||
228 | /* insertion of the codeword segment length */ | ||
229 | for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { | ||
230 | opj_tcd_pass_t *pass = &cblk->passes[passno]; | ||
231 | nump++; | ||
232 | len += pass->len; | ||
233 | if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { | ||
234 | bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); | ||
235 | len = 0; | ||
236 | nump = 0; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | } | ||
241 | |||
242 | if (bio_flush(bio)) { | ||
243 | return -999; /* modified to eliminate longjmp !! */ | ||
244 | } | ||
245 | |||
246 | c += bio_numbytes(bio); | ||
247 | |||
248 | bio_destroy(bio); | ||
249 | |||
250 | /* <EPH 0xff92> */ | ||
251 | if (tcp->csty & J2K_CP_CSTY_EPH) { | ||
252 | eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char)); | ||
253 | eph[0] = 255; | ||
254 | eph[1] = 146; | ||
255 | memcpy(c, eph, 2); | ||
256 | opj_free(eph); | ||
257 | c += 2; | ||
258 | } | ||
259 | /* </EPH> */ | ||
260 | |||
261 | /* Writing the packet body */ | ||
262 | |||
263 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
264 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
265 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
266 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
267 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
268 | opj_tcd_layer_t *layer = &cblk->layers[layno]; | ||
269 | if (!layer->numpasses) { | ||
270 | continue; | ||
271 | } | ||
272 | if (c + layer->len > dest + length) { | ||
273 | return -999; | ||
274 | } | ||
275 | |||
276 | memcpy(c, layer->data, layer->len); | ||
277 | cblk->numpasses += layer->numpasses; | ||
278 | c += layer->len; | ||
279 | /* ADD for index Cfr. Marcela --> delta disto by packet */ | ||
280 | if(image_info && image_info->index_write && image_info->index_on) { | ||
281 | opj_tile_info_t *info_TL = &image_info->tile[tileno]; | ||
282 | opj_packet_info_t *info_PK = &info_TL->packet[image_info->num]; | ||
283 | info_PK->disto += layer->disto; | ||
284 | if (image_info->D_max < info_PK->disto) { | ||
285 | image_info->D_max = info_PK->disto; | ||
286 | } | ||
287 | } | ||
288 | /* </ADD> */ | ||
289 | } | ||
290 | } | ||
291 | |||
292 | return (c - dest); | ||
293 | } | ||
294 | |||
295 | static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) { | ||
296 | seg->numpasses = 0; | ||
297 | seg->len = 0; | ||
298 | if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { | ||
299 | seg->maxpasses = 1; | ||
300 | } | ||
301 | else if (cblksty & J2K_CCP_CBLKSTY_LAZY) { | ||
302 | if (first) { | ||
303 | seg->maxpasses = 10; | ||
304 | } else { | ||
305 | seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; | ||
306 | } | ||
307 | } else { | ||
308 | seg->maxpasses = 109; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) { | ||
313 | int bandno, cblkno; | ||
314 | unsigned char *c = src; | ||
315 | |||
316 | opj_cp_t *cp = t2->cp; | ||
317 | |||
318 | int compno = pi->compno; /* component value */ | ||
319 | int resno = pi->resno; /* resolution level value */ | ||
320 | int precno = pi->precno; /* precinct value */ | ||
321 | int layno = pi->layno; /* quality layer value */ | ||
322 | |||
323 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
324 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
325 | |||
326 | unsigned char *hd = NULL; | ||
327 | int present; | ||
328 | |||
329 | opj_bio_t *bio = NULL; /* BIO component */ | ||
330 | |||
331 | if (layno == 0) { | ||
332 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
333 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
334 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
335 | |||
336 | if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; | ||
337 | |||
338 | tgt_reset(prc->incltree); | ||
339 | tgt_reset(prc->imsbtree); | ||
340 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
341 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
342 | cblk->numsegs = 0; | ||
343 | } | ||
344 | } | ||
345 | } | ||
346 | |||
347 | /* SOP markers */ | ||
348 | |||
349 | if (tcp->csty & J2K_CP_CSTY_SOP) { | ||
350 | if ((*c) != 0xff || (*(c + 1) != 0x91)) { | ||
351 | opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); | ||
352 | } else { | ||
353 | c += 6; | ||
354 | } | ||
355 | |||
356 | /** TODO : check the Nsop value */ | ||
357 | } | ||
358 | |||
359 | /* | ||
360 | When the marker PPT/PPM is used the packet header are store in PPT/PPM marker | ||
361 | This part deal with this caracteristic | ||
362 | step 1: Read packet header in the saved structure | ||
363 | step 2: Return to codestream for decoding | ||
364 | */ | ||
365 | |||
366 | bio = bio_create(); | ||
367 | |||
368 | if (cp->ppm == 1) { /* PPM */ | ||
369 | hd = cp->ppm_data; | ||
370 | bio_init_dec(bio, hd, cp->ppm_len); | ||
371 | } else if (tcp->ppt == 1) { /* PPT */ | ||
372 | hd = tcp->ppt_data; | ||
373 | bio_init_dec(bio, hd, tcp->ppt_len); | ||
374 | } else { /* Normal Case */ | ||
375 | hd = c; | ||
376 | bio_init_dec(bio, hd, src+len-hd); | ||
377 | } | ||
378 | |||
379 | present = bio_read(bio, 1); | ||
380 | |||
381 | if (!present) { | ||
382 | bio_inalign(bio); | ||
383 | hd += bio_numbytes(bio); | ||
384 | bio_destroy(bio); | ||
385 | |||
386 | /* EPH markers */ | ||
387 | |||
388 | if (tcp->csty & J2K_CP_CSTY_EPH) { | ||
389 | if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { | ||
390 | printf("Error : expected EPH marker\n"); | ||
391 | } else { | ||
392 | hd += 2; | ||
393 | } | ||
394 | } | ||
395 | |||
396 | if (cp->ppm == 1) { /* PPM case */ | ||
397 | cp->ppm_len += cp->ppm_data-hd; | ||
398 | cp->ppm_data = hd; | ||
399 | return (c - src); | ||
400 | } | ||
401 | if (tcp->ppt == 1) { /* PPT case */ | ||
402 | tcp->ppt_len+=tcp->ppt_data-hd; | ||
403 | tcp->ppt_data = hd; | ||
404 | return (c - src); | ||
405 | } | ||
406 | |||
407 | return (hd - src); | ||
408 | } | ||
409 | |||
410 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
411 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
412 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
413 | |||
414 | if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; | ||
415 | |||
416 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
417 | int included, increment, n; | ||
418 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
419 | opj_tcd_seg_t *seg = NULL; | ||
420 | /* if cblk not yet included before --> inclusion tagtree */ | ||
421 | if (!cblk->numsegs) { | ||
422 | included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); | ||
423 | /* else one bit */ | ||
424 | } else { | ||
425 | included = bio_read(bio, 1); | ||
426 | } | ||
427 | /* if cblk not included */ | ||
428 | if (!included) { | ||
429 | cblk->numnewpasses = 0; | ||
430 | continue; | ||
431 | } | ||
432 | /* if cblk not yet included --> zero-bitplane tagtree */ | ||
433 | if (!cblk->numsegs) { | ||
434 | int i, numimsbs; | ||
435 | for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) { | ||
436 | ; | ||
437 | } | ||
438 | numimsbs = i - 1; | ||
439 | cblk->numbps = band->numbps - numimsbs; | ||
440 | cblk->numlenbits = 3; | ||
441 | } | ||
442 | /* number of coding passes */ | ||
443 | cblk->numnewpasses = t2_getnumpasses(bio); | ||
444 | increment = t2_getcommacode(bio); | ||
445 | /* length indicator increment */ | ||
446 | cblk->numlenbits += increment; | ||
447 | if (!cblk->numsegs) { | ||
448 | seg = &cblk->segs[0]; | ||
449 | t2_init_seg(seg, tcp->tccps[compno].cblksty, 1); | ||
450 | } else { | ||
451 | seg = &cblk->segs[cblk->numsegs - 1]; | ||
452 | if (seg->numpasses == seg->maxpasses) { | ||
453 | t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); | ||
454 | } | ||
455 | } | ||
456 | n = cblk->numnewpasses; | ||
457 | |||
458 | do { | ||
459 | seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n); | ||
460 | seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses)); | ||
461 | n -= seg->numnewpasses; | ||
462 | if (n > 0) { | ||
463 | t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); | ||
464 | } | ||
465 | } while (n > 0); | ||
466 | } | ||
467 | } | ||
468 | |||
469 | if (bio_inalign(bio)) { | ||
470 | bio_destroy(bio); | ||
471 | return -999; | ||
472 | } | ||
473 | |||
474 | hd += bio_numbytes(bio); | ||
475 | bio_destroy(bio); | ||
476 | |||
477 | /* EPH markers */ | ||
478 | if (tcp->csty & J2K_CP_CSTY_EPH) { | ||
479 | if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { | ||
480 | opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); | ||
481 | } else { | ||
482 | hd += 2; | ||
483 | } | ||
484 | } | ||
485 | |||
486 | if (cp->ppm==1) { | ||
487 | cp->ppm_len+=cp->ppm_data-hd; | ||
488 | cp->ppm_data = hd; | ||
489 | } else if (tcp->ppt == 1) { | ||
490 | tcp->ppt_len+=tcp->ppt_data-hd; | ||
491 | tcp->ppt_data = hd; | ||
492 | } else { | ||
493 | c=hd; | ||
494 | } | ||
495 | |||
496 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
497 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
498 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
499 | |||
500 | if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; | ||
501 | |||
502 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
503 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
504 | opj_tcd_seg_t *seg = NULL; | ||
505 | if (!cblk->numnewpasses) | ||
506 | continue; | ||
507 | if (!cblk->numsegs) { | ||
508 | seg = &cblk->segs[0]; | ||
509 | cblk->numsegs++; | ||
510 | cblk->len = 0; | ||
511 | } else { | ||
512 | seg = &cblk->segs[cblk->numsegs - 1]; | ||
513 | if (seg->numpasses == seg->maxpasses) { | ||
514 | seg++; | ||
515 | cblk->numsegs++; | ||
516 | } | ||
517 | } | ||
518 | |||
519 | do { | ||
520 | if (c + seg->newlen > src + len) { | ||
521 | return -999; | ||
522 | } | ||
523 | |||
524 | #ifdef USE_JPWL | ||
525 | /* we need here a j2k handle to verify if making a check to | ||
526 | the validity of cblocks parameters is selected from user (-W) */ | ||
527 | |||
528 | /* let's check that we are not exceeding */ | ||
529 | if ((cblk->len + seg->newlen) > 8192) { | ||
530 | opj_event_msg(t2->cinfo, EVT_WARNING, | ||
531 | "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", | ||
532 | seg->newlen, cblkno, precno, bandno, resno, compno); | ||
533 | if (!JPWL_ASSUME) { | ||
534 | opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); | ||
535 | return -999; | ||
536 | } | ||
537 | seg->newlen = 8192 - cblk->len; | ||
538 | opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen); | ||
539 | break; | ||
540 | }; | ||
541 | |||
542 | #endif /* USE_JPWL */ | ||
543 | |||
544 | memcpy(cblk->data + cblk->len, c, seg->newlen); | ||
545 | if (seg->numpasses == 0) { | ||
546 | seg->data = cblk->data + cblk->len; | ||
547 | } | ||
548 | c += seg->newlen; | ||
549 | cblk->len += seg->newlen; | ||
550 | seg->len += seg->newlen; | ||
551 | seg->numpasses += seg->numnewpasses; | ||
552 | cblk->numnewpasses -= seg->numnewpasses; | ||
553 | if (cblk->numnewpasses > 0) { | ||
554 | seg++; | ||
555 | cblk->numsegs++; | ||
556 | } | ||
557 | } while (cblk->numnewpasses > 0); | ||
558 | } | ||
559 | } | ||
560 | |||
561 | return (c - src); | ||
562 | } | ||
563 | |||
564 | /* ----------------------------------------------------------------------- */ | ||
565 | |||
566 | int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode){ | ||
567 | unsigned char *c = dest; | ||
568 | int e = 0; | ||
569 | int compno; | ||
570 | int comp_len = 0; | ||
571 | opj_pi_iterator_t *pi = NULL; | ||
572 | int poc; | ||
573 | opj_image_t *image = t2->image; | ||
574 | opj_cp_t *cp = t2->cp; | ||
575 | int pocno = cp->cinema == CINEMA4K_24? 2: 1; | ||
576 | int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1; | ||
577 | |||
578 | pi = pi_initialise_encode(image, cp, tileno, t2_mode); | ||
579 | if(!pi) { | ||
580 | /* TODO: throw an error */ | ||
581 | return -999; | ||
582 | } | ||
583 | |||
584 | if(image_info) { | ||
585 | image_info->num = 0; | ||
586 | } | ||
587 | |||
588 | if(t2_mode == THRESH_CALC ){ | ||
589 | for(compno = 0; compno < maxcomp; compno++ ){ | ||
590 | for(poc = 0; poc < pocno ; poc++){ | ||
591 | int comp_len = 0; | ||
592 | int tpnum = compno; | ||
593 | pi_create_encode(pi, cp,tileno,poc,tpnum,tppos); | ||
594 | while (pi_next(&pi[poc])) { | ||
595 | if (pi[poc].layno < maxlayers) { | ||
596 | e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, image_info, tileno); | ||
597 | comp_len = comp_len + e; | ||
598 | if (e == -999) { | ||
599 | break; | ||
600 | } else { | ||
601 | c += e; | ||
602 | } | ||
603 | } | ||
604 | } | ||
605 | if (e == -999) break; | ||
606 | if (cp->max_comp_size){ | ||
607 | if (comp_len > cp->max_comp_size){ | ||
608 | e = -999; | ||
609 | break; | ||
610 | } | ||
611 | } | ||
612 | } | ||
613 | if (e == -999) break; | ||
614 | } | ||
615 | }else{ | ||
616 | pi_create_encode(pi, cp,tileno,pino,tpnum,tppos); | ||
617 | while (pi_next(&pi[pino])) { | ||
618 | if (pi[pino].layno < maxlayers) { | ||
619 | e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, image_info, tileno); | ||
620 | if (e == -999) { | ||
621 | break; | ||
622 | } else { | ||
623 | c += e; | ||
624 | } | ||
625 | } | ||
626 | } | ||
627 | } | ||
628 | |||
629 | /* INDEX >> */ | ||
630 | if(image_info && image_info->index_on) { | ||
631 | if(image_info->index_write) { | ||
632 | opj_tile_info_t *info_TL = &image_info->tile[tileno]; | ||
633 | opj_packet_info_t *info_PK = &info_TL->packet[image_info->num]; | ||
634 | if (!image_info->num) { | ||
635 | info_PK->start_pos = info_TL->end_header + 1; | ||
636 | } else { | ||
637 | info_PK->start_pos = info_TL->packet[image_info->num - 1].end_pos + 1; | ||
638 | } | ||
639 | info_PK->end_pos = info_PK->start_pos + e - 1; | ||
640 | } | ||
641 | |||
642 | image_info->num++; | ||
643 | } | ||
644 | /* << INDEX */ | ||
645 | pi_destroy(pi, cp, tileno); | ||
646 | |||
647 | if (e == -999) { | ||
648 | return e; | ||
649 | } | ||
650 | |||
651 | return (c - dest); | ||
652 | } | ||
653 | |||
654 | int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) { | ||
655 | unsigned char *c = src; | ||
656 | opj_pi_iterator_t *pi; | ||
657 | int pino, e = 0; | ||
658 | int n = 0; | ||
659 | |||
660 | opj_image_t *image = t2->image; | ||
661 | opj_cp_t *cp = t2->cp; | ||
662 | |||
663 | /* create a packet iterator */ | ||
664 | pi = pi_create_decode(image, cp, tileno); | ||
665 | if(!pi) { | ||
666 | /* TODO: throw an error */ | ||
667 | return -999; | ||
668 | } | ||
669 | |||
670 | for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { | ||
671 | while (pi_next(&pi[pino])) { | ||
672 | if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) { | ||
673 | e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]); | ||
674 | } else { | ||
675 | e = 0; | ||
676 | } | ||
677 | |||
678 | /* progression in resolution */ | ||
679 | image->comps[pi[pino].compno].resno_decoded = | ||
680 | (e > 0) ? | ||
681 | int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) | ||
682 | : image->comps[pi[pino].compno].resno_decoded; | ||
683 | n++; | ||
684 | |||
685 | if (e == -999) { /* ADD */ | ||
686 | break; | ||
687 | } else { | ||
688 | c += e; | ||
689 | } | ||
690 | } | ||
691 | } | ||
692 | |||
693 | /* don't forget to release pi */ | ||
694 | pi_destroy(pi, cp, tileno); | ||
695 | |||
696 | if (e == -999) { | ||
697 | return e; | ||
698 | } | ||
699 | |||
700 | return (c - src); | ||
701 | } | ||
702 | |||
703 | /* ----------------------------------------------------------------------- */ | ||
704 | |||
705 | opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) { | ||
706 | /* create the tcd structure */ | ||
707 | opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t)); | ||
708 | if(!t2) return NULL; | ||
709 | t2->cinfo = cinfo; | ||
710 | t2->image = image; | ||
711 | t2->cp = cp; | ||
712 | |||
713 | return t2; | ||
714 | } | ||
715 | |||
716 | void t2_destroy(opj_t2_t *t2) { | ||
717 | if(t2) { | ||
718 | opj_free(t2); | ||
719 | } | ||
720 | } | ||
721 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/t2.h b/libraries/openjpeg-libsl/libopenjpeg/t2.h new file mode 100644 index 0000000..87ed2d2 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/t2.h | |||
@@ -0,0 +1,102 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | #ifndef __T2_H | ||
32 | #define __T2_H | ||
33 | /** | ||
34 | @file t2.h | ||
35 | @brief Implementation of a tier-2 coding (packetization of code-block data) (T2) | ||
36 | |||
37 | */ | ||
38 | |||
39 | /** @defgroup T2 T2 - Implementation of a tier-2 coding */ | ||
40 | /*@{*/ | ||
41 | |||
42 | /** | ||
43 | Tier-2 coding | ||
44 | */ | ||
45 | typedef struct opj_t2 { | ||
46 | /** codec context */ | ||
47 | opj_common_ptr cinfo; | ||
48 | |||
49 | /** Encoding: pointer to the src image. Decoding: pointer to the dst image. */ | ||
50 | opj_image_t *image; | ||
51 | /** pointer to the image coding parameters */ | ||
52 | opj_cp_t *cp; | ||
53 | } opj_t2_t; | ||
54 | |||
55 | /** @name Exported functions */ | ||
56 | /*@{*/ | ||
57 | /* ----------------------------------------------------------------------- */ | ||
58 | |||
59 | /** | ||
60 | Encode the packets of a tile to a destination buffer | ||
61 | @param t2 T2 handle | ||
62 | @param tileno number of the tile encoded | ||
63 | @param tile the tile for which to write the packets | ||
64 | @param maxlayers maximum number of layers | ||
65 | @param dest the destination buffer | ||
66 | @param len the length of the destination buffer | ||
67 | @param image_info structure to create an index file | ||
68 | @param tpnum Tile part number of the current tile | ||
69 | @param tppos The position of the tile part flag in the progression order | ||
70 | @param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass | ||
71 | */ | ||
72 | int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info,int tpnum, int tppos,int pino,J2K_T2_MODE t2_mode); | ||
73 | /** | ||
74 | Decode the packets of a tile from a source buffer | ||
75 | @param t2 T2 handle | ||
76 | @param src the source buffer | ||
77 | @param len length of the source buffer | ||
78 | @param tileno number that identifies the tile for which to decode the packets | ||
79 | @param tile tile for which to decode the packets | ||
80 | */ | ||
81 | int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile); | ||
82 | |||
83 | /** | ||
84 | Create a T2 handle | ||
85 | @param cinfo Codec context info | ||
86 | @param image Source or destination image | ||
87 | @param cp Image coding parameters | ||
88 | @return Returns a new T2 handle if successful, returns NULL otherwise | ||
89 | */ | ||
90 | opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp); | ||
91 | /** | ||
92 | Destroy a T2 handle | ||
93 | @param t2 T2 handle to destroy | ||
94 | */ | ||
95 | void t2_destroy(opj_t2_t *t2); | ||
96 | |||
97 | /* ----------------------------------------------------------------------- */ | ||
98 | /*@}*/ | ||
99 | |||
100 | /*@}*/ | ||
101 | |||
102 | #endif /* __T2_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/tcd.c b/libraries/openjpeg-libsl/libopenjpeg/tcd.c new file mode 100644 index 0000000..525ec91 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/tcd.c | |||
@@ -0,0 +1,1409 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * Copyright (c) 2006-2007, Parvatha Elangovan | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
30 | * POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include "opj_includes.h" | ||
34 | |||
35 | void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) { | ||
36 | int tileno, compno, resno, bandno, precno, cblkno; | ||
37 | |||
38 | fprintf(fd, "image {\n"); | ||
39 | fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", | ||
40 | img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1); | ||
41 | |||
42 | for (tileno = 0; tileno < img->th * img->tw; tileno++) { | ||
43 | opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno]; | ||
44 | fprintf(fd, " tile {\n"); | ||
45 | fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", | ||
46 | tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); | ||
47 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
48 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
49 | fprintf(fd, " tilec {\n"); | ||
50 | fprintf(fd, | ||
51 | " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", | ||
52 | tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); | ||
53 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
54 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
55 | fprintf(fd, "\n res {\n"); | ||
56 | fprintf(fd, | ||
57 | " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", | ||
58 | res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); | ||
59 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
60 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
61 | fprintf(fd, " band {\n"); | ||
62 | fprintf(fd, | ||
63 | " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n", | ||
64 | band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); | ||
65 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
66 | opj_tcd_precinct_t *prec = &band->precincts[precno]; | ||
67 | fprintf(fd, " prec {\n"); | ||
68 | fprintf(fd, | ||
69 | " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", | ||
70 | prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); | ||
71 | for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { | ||
72 | opj_tcd_cblk_t *cblk = &prec->cblks[cblkno]; | ||
73 | fprintf(fd, " cblk {\n"); | ||
74 | fprintf(fd, | ||
75 | " x0=%d, y0=%d, x1=%d, y1=%d\n", | ||
76 | cblk->x0, cblk->y0, cblk->x1, cblk->y1); | ||
77 | fprintf(fd, " }\n"); | ||
78 | } | ||
79 | fprintf(fd, " }\n"); | ||
80 | } | ||
81 | fprintf(fd, " }\n"); | ||
82 | } | ||
83 | fprintf(fd, " }\n"); | ||
84 | } | ||
85 | fprintf(fd, " }\n"); | ||
86 | } | ||
87 | fprintf(fd, " }\n"); | ||
88 | } | ||
89 | fprintf(fd, "}\n"); | ||
90 | } | ||
91 | |||
92 | /* ----------------------------------------------------------------------- */ | ||
93 | |||
94 | /** | ||
95 | Create a new TCD handle | ||
96 | */ | ||
97 | opj_tcd_t* tcd_create(opj_common_ptr cinfo) { | ||
98 | /* create the tcd structure */ | ||
99 | opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t)); | ||
100 | if(!tcd) return NULL; | ||
101 | tcd->cinfo = cinfo; | ||
102 | tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t)); | ||
103 | if(!tcd->tcd_image) { | ||
104 | opj_free(tcd); | ||
105 | return NULL; | ||
106 | } | ||
107 | |||
108 | return tcd; | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | Destroy a previously created TCD handle | ||
113 | */ | ||
114 | void tcd_destroy(opj_tcd_t *tcd) { | ||
115 | if(tcd) { | ||
116 | opj_free(tcd->tcd_image); | ||
117 | opj_free(tcd); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | /* ----------------------------------------------------------------------- */ | ||
122 | |||
123 | void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) { | ||
124 | int tileno, compno, resno, bandno, precno, cblkno; | ||
125 | |||
126 | tcd->image = image; | ||
127 | tcd->cp = cp; | ||
128 | tcd->tcd_image->tw = cp->tw; | ||
129 | tcd->tcd_image->th = cp->th; | ||
130 | tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t)); | ||
131 | |||
132 | for (tileno = 0; tileno < 1; tileno++) { | ||
133 | opj_tcp_t *tcp = &cp->tcps[curtileno]; | ||
134 | int j; | ||
135 | |||
136 | /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ | ||
137 | int p = curtileno % cp->tw; /* si numerotation matricielle .. */ | ||
138 | int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ | ||
139 | |||
140 | /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */ | ||
141 | opj_tcd_tile_t *tile = tcd->tcd_image->tiles; | ||
142 | |||
143 | /* 4 borders of the tile rescale on the image if necessary */ | ||
144 | tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); | ||
145 | tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); | ||
146 | tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); | ||
147 | tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); | ||
148 | tile->numcomps = image->numcomps; | ||
149 | /* tile->PPT=image->PPT; */ | ||
150 | |||
151 | /* Modification of the RATE >> */ | ||
152 | for (j = 0; j < tcp->numlayers; j++) { | ||
153 | tcp->rates[j] = tcp->rates[j] ? | ||
154 | ((float) (tile->numcomps | ||
155 | * (tile->x1 - tile->x0) | ||
156 | * (tile->y1 - tile->y0) | ||
157 | * image->comps[0].prec))/ | ||
158 | (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) | ||
159 | : 0; | ||
160 | |||
161 | if (tcp->rates[j]) { | ||
162 | if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { | ||
163 | tcp->rates[j] = tcp->rates[j - 1] + 20; | ||
164 | } else { | ||
165 | if (!j && tcp->rates[j] < 30) | ||
166 | tcp->rates[j] = 30; | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | /* << Modification of the RATE */ | ||
171 | |||
172 | tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t)); | ||
173 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
174 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
175 | |||
176 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
177 | |||
178 | /* border of each tile component (global) */ | ||
179 | tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); | ||
180 | tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); | ||
181 | tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); | ||
182 | tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); | ||
183 | |||
184 | tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); | ||
185 | tilec->numresolutions = tccp->numresolutions; | ||
186 | |||
187 | tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t)); | ||
188 | |||
189 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
190 | int pdx, pdy; | ||
191 | int levelno = tilec->numresolutions - 1 - resno; | ||
192 | int tlprcxstart, tlprcystart, brprcxend, brprcyend; | ||
193 | int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; | ||
194 | int cbgwidthexpn, cbgheightexpn; | ||
195 | int cblkwidthexpn, cblkheightexpn; | ||
196 | |||
197 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
198 | |||
199 | /* border for each resolution level (global) */ | ||
200 | res->x0 = int_ceildivpow2(tilec->x0, levelno); | ||
201 | res->y0 = int_ceildivpow2(tilec->y0, levelno); | ||
202 | res->x1 = int_ceildivpow2(tilec->x1, levelno); | ||
203 | res->y1 = int_ceildivpow2(tilec->y1, levelno); | ||
204 | |||
205 | res->numbands = resno == 0 ? 1 : 3; | ||
206 | /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ | ||
207 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
208 | pdx = tccp->prcw[resno]; | ||
209 | pdy = tccp->prch[resno]; | ||
210 | } else { | ||
211 | pdx = 15; | ||
212 | pdy = 15; | ||
213 | } | ||
214 | /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ | ||
215 | tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; | ||
216 | tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; | ||
217 | |||
218 | brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; | ||
219 | brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; | ||
220 | |||
221 | res->pw = (brprcxend - tlprcxstart) >> pdx; | ||
222 | res->ph = (brprcyend - tlprcystart) >> pdy; | ||
223 | |||
224 | if (resno == 0) { | ||
225 | tlcbgxstart = tlprcxstart; | ||
226 | tlcbgystart = tlprcystart; | ||
227 | brcbgxend = brprcxend; | ||
228 | brcbgyend = brprcyend; | ||
229 | cbgwidthexpn = pdx; | ||
230 | cbgheightexpn = pdy; | ||
231 | } else { | ||
232 | tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); | ||
233 | tlcbgystart = int_ceildivpow2(tlprcystart, 1); | ||
234 | brcbgxend = int_ceildivpow2(brprcxend, 1); | ||
235 | brcbgyend = int_ceildivpow2(brprcyend, 1); | ||
236 | cbgwidthexpn = pdx - 1; | ||
237 | cbgheightexpn = pdy - 1; | ||
238 | } | ||
239 | |||
240 | cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); | ||
241 | cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); | ||
242 | |||
243 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
244 | int x0b, y0b, i; | ||
245 | int gain, numbps; | ||
246 | opj_stepsize_t *ss = NULL; | ||
247 | |||
248 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
249 | |||
250 | band->bandno = resno == 0 ? 0 : bandno + 1; | ||
251 | x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; | ||
252 | y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; | ||
253 | |||
254 | if (band->bandno == 0) { | ||
255 | /* band border (global) */ | ||
256 | band->x0 = int_ceildivpow2(tilec->x0, levelno); | ||
257 | band->y0 = int_ceildivpow2(tilec->y0, levelno); | ||
258 | band->x1 = int_ceildivpow2(tilec->x1, levelno); | ||
259 | band->y1 = int_ceildivpow2(tilec->y1, levelno); | ||
260 | } else { | ||
261 | /* band border (global) */ | ||
262 | band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); | ||
263 | band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); | ||
264 | band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); | ||
265 | band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); | ||
266 | } | ||
267 | |||
268 | ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; | ||
269 | gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); | ||
270 | numbps = image->comps[compno].prec + gain; | ||
271 | |||
272 | band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); | ||
273 | band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ | ||
274 | |||
275 | band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t)); | ||
276 | |||
277 | for (i = 0; i < res->pw * res->ph * 3; i++) { | ||
278 | band->precincts[i].imsbtree = NULL; | ||
279 | band->precincts[i].incltree = NULL; | ||
280 | } | ||
281 | |||
282 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
283 | int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; | ||
284 | |||
285 | int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); | ||
286 | int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); | ||
287 | int cbgxend = cbgxstart + (1 << cbgwidthexpn); | ||
288 | int cbgyend = cbgystart + (1 << cbgheightexpn); | ||
289 | |||
290 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
291 | |||
292 | /* precinct size (global) */ | ||
293 | prc->x0 = int_max(cbgxstart, band->x0); | ||
294 | prc->y0 = int_max(cbgystart, band->y0); | ||
295 | prc->x1 = int_min(cbgxend, band->x1); | ||
296 | prc->y1 = int_min(cbgyend, band->y1); | ||
297 | |||
298 | tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; | ||
299 | tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; | ||
300 | brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; | ||
301 | brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; | ||
302 | prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; | ||
303 | prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; | ||
304 | |||
305 | prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t)); | ||
306 | prc->incltree = tgt_create(prc->cw, prc->ch); | ||
307 | prc->imsbtree = tgt_create(prc->cw, prc->ch); | ||
308 | |||
309 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
310 | int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); | ||
311 | int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); | ||
312 | int cblkxend = cblkxstart + (1 << cblkwidthexpn); | ||
313 | int cblkyend = cblkystart + (1 << cblkheightexpn); | ||
314 | |||
315 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
316 | |||
317 | /* code-block size (global) */ | ||
318 | cblk->x0 = int_max(cblkxstart, prc->x0); | ||
319 | cblk->y0 = int_max(cblkystart, prc->y0); | ||
320 | cblk->x1 = int_min(cblkxend, prc->x1); | ||
321 | cblk->y1 = int_min(cblkyend, prc->y1); | ||
322 | } | ||
323 | } | ||
324 | } | ||
325 | } | ||
326 | } | ||
327 | } | ||
328 | |||
329 | /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ | ||
330 | } | ||
331 | |||
332 | void tcd_free_encode(opj_tcd_t *tcd) { | ||
333 | int tileno, compno, resno, bandno, precno; | ||
334 | |||
335 | for (tileno = 0; tileno < 1; tileno++) { | ||
336 | opj_tcd_tile_t *tile = tcd->tcd_image->tiles; | ||
337 | |||
338 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
339 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
340 | |||
341 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
342 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
343 | |||
344 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
345 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
346 | |||
347 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
348 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
349 | |||
350 | if (prc->incltree != NULL) { | ||
351 | tgt_destroy(prc->incltree); | ||
352 | prc->incltree = NULL; | ||
353 | } | ||
354 | if (prc->imsbtree != NULL) { | ||
355 | tgt_destroy(prc->imsbtree); | ||
356 | prc->imsbtree = NULL; | ||
357 | } | ||
358 | opj_free(prc->cblks); | ||
359 | prc->cblks = NULL; | ||
360 | } /* for (precno */ | ||
361 | opj_free(band->precincts); | ||
362 | band->precincts = NULL; | ||
363 | } /* for (bandno */ | ||
364 | } /* for (resno */ | ||
365 | opj_free(tilec->resolutions); | ||
366 | tilec->resolutions = NULL; | ||
367 | } /* for (compno */ | ||
368 | opj_free(tile->comps); | ||
369 | tile->comps = NULL; | ||
370 | } /* for (tileno */ | ||
371 | opj_free(tcd->tcd_image->tiles); | ||
372 | tcd->tcd_image->tiles = NULL; | ||
373 | } | ||
374 | |||
375 | void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) { | ||
376 | int tileno, compno, resno, bandno, precno, cblkno; | ||
377 | |||
378 | for (tileno = 0; tileno < 1; tileno++) { | ||
379 | opj_tcp_t *tcp = &cp->tcps[curtileno]; | ||
380 | int j; | ||
381 | /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ | ||
382 | int p = curtileno % cp->tw; | ||
383 | int q = curtileno / cp->tw; | ||
384 | |||
385 | opj_tcd_tile_t *tile = tcd->tcd_image->tiles; | ||
386 | |||
387 | /* 4 borders of the tile rescale on the image if necessary */ | ||
388 | tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); | ||
389 | tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); | ||
390 | tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); | ||
391 | tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); | ||
392 | |||
393 | tile->numcomps = image->numcomps; | ||
394 | /* tile->PPT=image->PPT; */ | ||
395 | |||
396 | /* Modification of the RATE >> */ | ||
397 | for (j = 0; j < tcp->numlayers; j++) { | ||
398 | tcp->rates[j] = tcp->rates[j] ? | ||
399 | ((float) (tile->numcomps | ||
400 | * (tile->x1 - tile->x0) | ||
401 | * (tile->y1 - tile->y0) | ||
402 | * image->comps[0].prec))/ | ||
403 | (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) | ||
404 | : 0; | ||
405 | |||
406 | if (tcp->rates[j]) { | ||
407 | if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { | ||
408 | tcp->rates[j] = tcp->rates[j - 1] + 20; | ||
409 | } else { | ||
410 | if (!j && tcp->rates[j] < 30) | ||
411 | tcp->rates[j] = 30; | ||
412 | } | ||
413 | } | ||
414 | } | ||
415 | /* << Modification of the RATE */ | ||
416 | |||
417 | /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */ | ||
418 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
419 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
420 | |||
421 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
422 | |||
423 | /* border of each tile component (global) */ | ||
424 | tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); | ||
425 | tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); | ||
426 | tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); | ||
427 | tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); | ||
428 | |||
429 | tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); | ||
430 | tilec->numresolutions = tccp->numresolutions; | ||
431 | /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */ | ||
432 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
433 | int pdx, pdy; | ||
434 | |||
435 | int levelno = tilec->numresolutions - 1 - resno; | ||
436 | int tlprcxstart, tlprcystart, brprcxend, brprcyend; | ||
437 | int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; | ||
438 | int cbgwidthexpn, cbgheightexpn; | ||
439 | int cblkwidthexpn, cblkheightexpn; | ||
440 | |||
441 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
442 | |||
443 | /* border for each resolution level (global) */ | ||
444 | res->x0 = int_ceildivpow2(tilec->x0, levelno); | ||
445 | res->y0 = int_ceildivpow2(tilec->y0, levelno); | ||
446 | res->x1 = int_ceildivpow2(tilec->x1, levelno); | ||
447 | res->y1 = int_ceildivpow2(tilec->y1, levelno); | ||
448 | res->numbands = resno == 0 ? 1 : 3; | ||
449 | |||
450 | /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ | ||
451 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
452 | pdx = tccp->prcw[resno]; | ||
453 | pdy = tccp->prch[resno]; | ||
454 | } else { | ||
455 | pdx = 15; | ||
456 | pdy = 15; | ||
457 | } | ||
458 | /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ | ||
459 | tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; | ||
460 | tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; | ||
461 | brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; | ||
462 | brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; | ||
463 | |||
464 | res->pw = (brprcxend - tlprcxstart) >> pdx; | ||
465 | res->ph = (brprcyend - tlprcystart) >> pdy; | ||
466 | |||
467 | if (resno == 0) { | ||
468 | tlcbgxstart = tlprcxstart; | ||
469 | tlcbgystart = tlprcystart; | ||
470 | brcbgxend = brprcxend; | ||
471 | brcbgyend = brprcyend; | ||
472 | cbgwidthexpn = pdx; | ||
473 | cbgheightexpn = pdy; | ||
474 | } else { | ||
475 | tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); | ||
476 | tlcbgystart = int_ceildivpow2(tlprcystart, 1); | ||
477 | brcbgxend = int_ceildivpow2(brprcxend, 1); | ||
478 | brcbgyend = int_ceildivpow2(brprcyend, 1); | ||
479 | cbgwidthexpn = pdx - 1; | ||
480 | cbgheightexpn = pdy - 1; | ||
481 | } | ||
482 | |||
483 | cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); | ||
484 | cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); | ||
485 | |||
486 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
487 | int x0b, y0b; | ||
488 | int gain, numbps; | ||
489 | opj_stepsize_t *ss = NULL; | ||
490 | |||
491 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
492 | |||
493 | band->bandno = resno == 0 ? 0 : bandno + 1; | ||
494 | x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; | ||
495 | y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; | ||
496 | |||
497 | if (band->bandno == 0) { | ||
498 | /* band border */ | ||
499 | band->x0 = int_ceildivpow2(tilec->x0, levelno); | ||
500 | band->y0 = int_ceildivpow2(tilec->y0, levelno); | ||
501 | band->x1 = int_ceildivpow2(tilec->x1, levelno); | ||
502 | band->y1 = int_ceildivpow2(tilec->y1, levelno); | ||
503 | } else { | ||
504 | band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); | ||
505 | band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); | ||
506 | band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); | ||
507 | band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); | ||
508 | } | ||
509 | |||
510 | ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; | ||
511 | gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); | ||
512 | numbps = image->comps[compno].prec + gain; | ||
513 | band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); | ||
514 | band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ | ||
515 | |||
516 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
517 | int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; | ||
518 | |||
519 | int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); | ||
520 | int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); | ||
521 | int cbgxend = cbgxstart + (1 << cbgwidthexpn); | ||
522 | int cbgyend = cbgystart + (1 << cbgheightexpn); | ||
523 | |||
524 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
525 | |||
526 | /* precinct size (global) */ | ||
527 | prc->x0 = int_max(cbgxstart, band->x0); | ||
528 | prc->y0 = int_max(cbgystart, band->y0); | ||
529 | prc->x1 = int_min(cbgxend, band->x1); | ||
530 | prc->y1 = int_min(cbgyend, band->y1); | ||
531 | |||
532 | tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; | ||
533 | tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; | ||
534 | brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; | ||
535 | brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; | ||
536 | prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; | ||
537 | prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; | ||
538 | |||
539 | opj_free(prc->cblks); | ||
540 | prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t)); | ||
541 | |||
542 | if (prc->incltree != NULL) { | ||
543 | tgt_destroy(prc->incltree); | ||
544 | } | ||
545 | if (prc->imsbtree != NULL) { | ||
546 | tgt_destroy(prc->imsbtree); | ||
547 | } | ||
548 | |||
549 | prc->incltree = tgt_create(prc->cw, prc->ch); | ||
550 | prc->imsbtree = tgt_create(prc->cw, prc->ch); | ||
551 | |||
552 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
553 | int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); | ||
554 | int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); | ||
555 | int cblkxend = cblkxstart + (1 << cblkwidthexpn); | ||
556 | int cblkyend = cblkystart + (1 << cblkheightexpn); | ||
557 | |||
558 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
559 | |||
560 | /* code-block size (global) */ | ||
561 | cblk->x0 = int_max(cblkxstart, prc->x0); | ||
562 | cblk->y0 = int_max(cblkystart, prc->y0); | ||
563 | cblk->x1 = int_min(cblkxend, prc->x1); | ||
564 | cblk->y1 = int_min(cblkyend, prc->y1); | ||
565 | } | ||
566 | } /* precno */ | ||
567 | } /* bandno */ | ||
568 | } /* resno */ | ||
569 | } /* compno */ | ||
570 | } /* tileno */ | ||
571 | |||
572 | /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ | ||
573 | } | ||
574 | |||
575 | void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) { | ||
576 | int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q; | ||
577 | unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h; | ||
578 | |||
579 | tcd->image = image; | ||
580 | tcd->cp = cp; | ||
581 | tcd->tcd_image->tw = cp->tw; | ||
582 | tcd->tcd_image->th = cp->th; | ||
583 | tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t)); | ||
584 | |||
585 | for (i = 0; i < cp->tileno_size; i++) { | ||
586 | opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]); | ||
587 | opj_tcd_tile_t *tile = &(tcd->tcd_image->tiles[cp->tileno[i]]); | ||
588 | |||
589 | /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ | ||
590 | tileno = cp->tileno[i]; | ||
591 | p = tileno % cp->tw; /* si numerotation matricielle .. */ | ||
592 | q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ | ||
593 | |||
594 | /* 4 borders of the tile rescale on the image if necessary */ | ||
595 | tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); | ||
596 | tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); | ||
597 | tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); | ||
598 | tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); | ||
599 | |||
600 | tile->numcomps = image->numcomps; | ||
601 | tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t)); | ||
602 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
603 | opj_tccp_t *tccp = &tcp->tccps[compno]; | ||
604 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
605 | |||
606 | /* border of each tile component (global) */ | ||
607 | tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); | ||
608 | tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); | ||
609 | tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); | ||
610 | tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); | ||
611 | |||
612 | tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); | ||
613 | tilec->numresolutions = tccp->numresolutions; | ||
614 | tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t)); | ||
615 | |||
616 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
617 | int pdx, pdy; | ||
618 | int levelno = tilec->numresolutions - 1 - resno; | ||
619 | int tlprcxstart, tlprcystart, brprcxend, brprcyend; | ||
620 | int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; | ||
621 | int cbgwidthexpn, cbgheightexpn; | ||
622 | int cblkwidthexpn, cblkheightexpn; | ||
623 | |||
624 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
625 | |||
626 | /* border for each resolution level (global) */ | ||
627 | res->x0 = int_ceildivpow2(tilec->x0, levelno); | ||
628 | res->y0 = int_ceildivpow2(tilec->y0, levelno); | ||
629 | res->x1 = int_ceildivpow2(tilec->x1, levelno); | ||
630 | res->y1 = int_ceildivpow2(tilec->y1, levelno); | ||
631 | res->numbands = resno == 0 ? 1 : 3; | ||
632 | |||
633 | /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ | ||
634 | if (tccp->csty & J2K_CCP_CSTY_PRT) { | ||
635 | pdx = tccp->prcw[resno]; | ||
636 | pdy = tccp->prch[resno]; | ||
637 | } else { | ||
638 | pdx = 15; | ||
639 | pdy = 15; | ||
640 | } | ||
641 | |||
642 | /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ | ||
643 | tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; | ||
644 | tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; | ||
645 | brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; | ||
646 | brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; | ||
647 | |||
648 | res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); | ||
649 | res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); | ||
650 | |||
651 | if (resno == 0) { | ||
652 | tlcbgxstart = tlprcxstart; | ||
653 | tlcbgystart = tlprcystart; | ||
654 | brcbgxend = brprcxend; | ||
655 | brcbgyend = brprcyend; | ||
656 | cbgwidthexpn = pdx; | ||
657 | cbgheightexpn = pdy; | ||
658 | } else { | ||
659 | tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); | ||
660 | tlcbgystart = int_ceildivpow2(tlprcystart, 1); | ||
661 | brcbgxend = int_ceildivpow2(brprcxend, 1); | ||
662 | brcbgyend = int_ceildivpow2(brprcyend, 1); | ||
663 | cbgwidthexpn = pdx - 1; | ||
664 | cbgheightexpn = pdy - 1; | ||
665 | } | ||
666 | |||
667 | cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); | ||
668 | cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); | ||
669 | |||
670 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
671 | int x0b, y0b; | ||
672 | int gain, numbps; | ||
673 | opj_stepsize_t *ss = NULL; | ||
674 | |||
675 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
676 | band->bandno = resno == 0 ? 0 : bandno + 1; | ||
677 | x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; | ||
678 | y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; | ||
679 | |||
680 | if (band->bandno == 0) { | ||
681 | /* band border (global) */ | ||
682 | band->x0 = int_ceildivpow2(tilec->x0, levelno); | ||
683 | band->y0 = int_ceildivpow2(tilec->y0, levelno); | ||
684 | band->x1 = int_ceildivpow2(tilec->x1, levelno); | ||
685 | band->y1 = int_ceildivpow2(tilec->y1, levelno); | ||
686 | } else { | ||
687 | /* band border (global) */ | ||
688 | band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); | ||
689 | band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); | ||
690 | band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); | ||
691 | band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); | ||
692 | } | ||
693 | |||
694 | ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; | ||
695 | gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); | ||
696 | numbps = image->comps[compno].prec + gain; | ||
697 | band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); | ||
698 | band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ | ||
699 | |||
700 | band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t)); | ||
701 | |||
702 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
703 | int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; | ||
704 | int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); | ||
705 | int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); | ||
706 | int cbgxend = cbgxstart + (1 << cbgwidthexpn); | ||
707 | int cbgyend = cbgystart + (1 << cbgheightexpn); | ||
708 | |||
709 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
710 | /* precinct size (global) */ | ||
711 | prc->x0 = int_max(cbgxstart, band->x0); | ||
712 | prc->y0 = int_max(cbgystart, band->y0); | ||
713 | prc->x1 = int_min(cbgxend, band->x1); | ||
714 | prc->y1 = int_min(cbgyend, band->y1); | ||
715 | |||
716 | tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; | ||
717 | tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; | ||
718 | brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; | ||
719 | brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; | ||
720 | prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; | ||
721 | prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; | ||
722 | |||
723 | prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t)); | ||
724 | |||
725 | prc->incltree = tgt_create(prc->cw, prc->ch); | ||
726 | prc->imsbtree = tgt_create(prc->cw, prc->ch); | ||
727 | |||
728 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
729 | int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); | ||
730 | int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); | ||
731 | int cblkxend = cblkxstart + (1 << cblkwidthexpn); | ||
732 | int cblkyend = cblkystart + (1 << cblkheightexpn); | ||
733 | |||
734 | /* code-block size (global) */ | ||
735 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
736 | cblk->x0 = int_max(cblkxstart, prc->x0); | ||
737 | cblk->y0 = int_max(cblkystart, prc->y0); | ||
738 | cblk->x1 = int_min(cblkxend, prc->x1); | ||
739 | cblk->y1 = int_min(cblkyend, prc->y1); | ||
740 | } | ||
741 | } /* precno */ | ||
742 | } /* bandno */ | ||
743 | } /* resno */ | ||
744 | } /* compno */ | ||
745 | } /* i = 0..cp->tileno_size */ | ||
746 | |||
747 | /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ | ||
748 | |||
749 | /* | ||
750 | Allocate place to store the decoded data = final image | ||
751 | Place limited by the tile really present in the codestream | ||
752 | */ | ||
753 | |||
754 | for (i = 0; i < image->numcomps; i++) { | ||
755 | for (j = 0; j < cp->tileno_size; j++) { | ||
756 | tileno = cp->tileno[j]; | ||
757 | x0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x0 : int_min(x0, | ||
758 | (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x0); | ||
759 | y0 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y0 : int_min(y0, | ||
760 | (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y0); | ||
761 | x1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].x1 : int_max(x1, | ||
762 | (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].x1); | ||
763 | y1 = j == 0 ? tcd->tcd_image->tiles[tileno].comps[i].y1 : int_max(y1, | ||
764 | (unsigned int) tcd->tcd_image->tiles[tileno].comps[i].y1); | ||
765 | } | ||
766 | |||
767 | w = x1 - x0; | ||
768 | h = y1 - y0; | ||
769 | |||
770 | image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int)); | ||
771 | image->comps[i].w = w; | ||
772 | image->comps[i].h = h; | ||
773 | image->comps[i].x0 = x0; | ||
774 | image->comps[i].y0 = y0; | ||
775 | } | ||
776 | } | ||
777 | |||
778 | void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) { | ||
779 | int compno, resno, bandno, precno, cblkno; | ||
780 | int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */ | ||
781 | int matrice[10][10][3]; | ||
782 | int i, j, k; | ||
783 | |||
784 | opj_cp_t *cp = tcd->cp; | ||
785 | opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; | ||
786 | opj_tcp_t *tcd_tcp = tcd->tcp; | ||
787 | |||
788 | /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */ | ||
789 | |||
790 | for (compno = 0; compno < tcd_tile->numcomps; compno++) { | ||
791 | opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; | ||
792 | for (i = 0; i < tcd_tcp->numlayers; i++) { | ||
793 | for (j = 0; j < tilec->numresolutions; j++) { | ||
794 | for (k = 0; k < 3; k++) { | ||
795 | matrice[i][j][k] = | ||
796 | (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] | ||
797 | * (float) (tcd->image->comps[compno].prec / 16.0)); | ||
798 | } | ||
799 | } | ||
800 | } | ||
801 | |||
802 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
803 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
804 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
805 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
806 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
807 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
808 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
809 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
810 | opj_tcd_layer_t *layer = &cblk->layers[layno]; | ||
811 | int n; | ||
812 | int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ | ||
813 | /* Correction of the matrix of coefficient to include the IMSB information */ | ||
814 | if (layno == 0) { | ||
815 | value = matrice[layno][resno][bandno]; | ||
816 | if (imsb >= value) { | ||
817 | value = 0; | ||
818 | } else { | ||
819 | value -= imsb; | ||
820 | } | ||
821 | } else { | ||
822 | value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno]; | ||
823 | if (imsb >= matrice[layno - 1][resno][bandno]) { | ||
824 | value -= (imsb - matrice[layno - 1][resno][bandno]); | ||
825 | if (value < 0) { | ||
826 | value = 0; | ||
827 | } | ||
828 | } | ||
829 | } | ||
830 | |||
831 | if (layno == 0) { | ||
832 | cblk->numpassesinlayers = 0; | ||
833 | } | ||
834 | |||
835 | n = cblk->numpassesinlayers; | ||
836 | if (cblk->numpassesinlayers == 0) { | ||
837 | if (value != 0) { | ||
838 | n = 3 * value - 2 + cblk->numpassesinlayers; | ||
839 | } else { | ||
840 | n = cblk->numpassesinlayers; | ||
841 | } | ||
842 | } else { | ||
843 | n = 3 * value + cblk->numpassesinlayers; | ||
844 | } | ||
845 | |||
846 | layer->numpasses = n - cblk->numpassesinlayers; | ||
847 | |||
848 | if (!layer->numpasses) | ||
849 | continue; | ||
850 | |||
851 | if (cblk->numpassesinlayers == 0) { | ||
852 | layer->len = cblk->passes[n - 1].rate; | ||
853 | layer->data = cblk->data; | ||
854 | } else { | ||
855 | layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; | ||
856 | layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; | ||
857 | } | ||
858 | if (final) | ||
859 | cblk->numpassesinlayers = n; | ||
860 | } | ||
861 | } | ||
862 | } | ||
863 | } | ||
864 | } | ||
865 | } | ||
866 | |||
867 | void tcd_rateallocate_fixed(opj_tcd_t *tcd) { | ||
868 | int layno; | ||
869 | for (layno = 0; layno < tcd->tcp->numlayers; layno++) { | ||
870 | tcd_makelayer_fixed(tcd, layno, 1); | ||
871 | } | ||
872 | } | ||
873 | |||
874 | void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) { | ||
875 | int compno, resno, bandno, precno, cblkno, passno; | ||
876 | |||
877 | opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; | ||
878 | |||
879 | tcd_tile->distolayer[layno] = 0; /* fixed_quality */ | ||
880 | |||
881 | for (compno = 0; compno < tcd_tile->numcomps; compno++) { | ||
882 | opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; | ||
883 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
884 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
885 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
886 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
887 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
888 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
889 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
890 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
891 | opj_tcd_layer_t *layer = &cblk->layers[layno]; | ||
892 | |||
893 | int n; | ||
894 | if (layno == 0) { | ||
895 | cblk->numpassesinlayers = 0; | ||
896 | } | ||
897 | n = cblk->numpassesinlayers; | ||
898 | for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) { | ||
899 | int dr; | ||
900 | double dd; | ||
901 | opj_tcd_pass_t *pass = &cblk->passes[passno]; | ||
902 | if (n == 0) { | ||
903 | dr = pass->rate; | ||
904 | dd = pass->distortiondec; | ||
905 | } else { | ||
906 | dr = pass->rate - cblk->passes[n - 1].rate; | ||
907 | dd = pass->distortiondec - cblk->passes[n - 1].distortiondec; | ||
908 | } | ||
909 | if (!dr) { | ||
910 | if (dd != 0) | ||
911 | n = passno + 1; | ||
912 | continue; | ||
913 | } | ||
914 | if (dd / dr >= thresh) | ||
915 | n = passno + 1; | ||
916 | } | ||
917 | layer->numpasses = n - cblk->numpassesinlayers; | ||
918 | |||
919 | if (!layer->numpasses) { | ||
920 | layer->disto = 0; | ||
921 | continue; | ||
922 | } | ||
923 | if (cblk->numpassesinlayers == 0) { | ||
924 | layer->len = cblk->passes[n - 1].rate; | ||
925 | layer->data = cblk->data; | ||
926 | layer->disto = cblk->passes[n - 1].distortiondec; | ||
927 | } else { | ||
928 | layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; | ||
929 | layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; | ||
930 | layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec; | ||
931 | } | ||
932 | |||
933 | tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */ | ||
934 | |||
935 | if (final) | ||
936 | cblk->numpassesinlayers = n; | ||
937 | } | ||
938 | } | ||
939 | } | ||
940 | } | ||
941 | } | ||
942 | } | ||
943 | |||
944 | bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) { | ||
945 | int compno, resno, bandno, precno, cblkno, passno, layno; | ||
946 | double min, max; | ||
947 | double cumdisto[100]; /* fixed_quality */ | ||
948 | const double K = 1; /* 1.1; fixed_quality */ | ||
949 | double maxSE = 0; | ||
950 | |||
951 | opj_cp_t *cp = tcd->cp; | ||
952 | opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; | ||
953 | opj_tcp_t *tcd_tcp = tcd->tcp; | ||
954 | |||
955 | min = DBL_MAX; | ||
956 | max = 0; | ||
957 | |||
958 | tcd_tile->nbpix = 0; /* fixed_quality */ | ||
959 | |||
960 | for (compno = 0; compno < tcd_tile->numcomps; compno++) { | ||
961 | opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; | ||
962 | tilec->nbpix = 0; | ||
963 | |||
964 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
965 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
966 | |||
967 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
968 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
969 | |||
970 | for (precno = 0; precno < res->pw * res->ph; precno++) { | ||
971 | opj_tcd_precinct_t *prc = &band->precincts[precno]; | ||
972 | |||
973 | for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { | ||
974 | opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; | ||
975 | |||
976 | for (passno = 0; passno < cblk->totalpasses; passno++) { | ||
977 | opj_tcd_pass_t *pass = &cblk->passes[passno]; | ||
978 | int dr; | ||
979 | double dd, rdslope; | ||
980 | if (passno == 0) { | ||
981 | dr = pass->rate; | ||
982 | dd = pass->distortiondec; | ||
983 | } else { | ||
984 | dr = pass->rate - cblk->passes[passno - 1].rate; | ||
985 | dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec; | ||
986 | } | ||
987 | if (dr == 0) { | ||
988 | continue; | ||
989 | } | ||
990 | rdslope = dd / dr; | ||
991 | if (rdslope < min) { | ||
992 | min = rdslope; | ||
993 | } | ||
994 | if (rdslope > max) { | ||
995 | max = rdslope; | ||
996 | } | ||
997 | } /* passno */ | ||
998 | |||
999 | /* fixed_quality */ | ||
1000 | tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); | ||
1001 | tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); | ||
1002 | } /* cbklno */ | ||
1003 | } /* precno */ | ||
1004 | } /* bandno */ | ||
1005 | } /* resno */ | ||
1006 | |||
1007 | maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) | ||
1008 | * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) | ||
1009 | * ((double)(tilec->nbpix)); | ||
1010 | } /* compno */ | ||
1011 | |||
1012 | /* index file */ | ||
1013 | if(image_info && image_info->index_on) { | ||
1014 | opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno]; | ||
1015 | tile_info->nbpix = tcd_tile->nbpix; | ||
1016 | tile_info->distotile = tcd_tile->distotile; | ||
1017 | tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double)); | ||
1018 | } | ||
1019 | |||
1020 | for (layno = 0; layno < tcd_tcp->numlayers; layno++) { | ||
1021 | double lo = min; | ||
1022 | double hi = max; | ||
1023 | int success = 0; | ||
1024 | /* TODO: remove maxlen */ | ||
1025 | int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len; | ||
1026 | double goodthresh = 0; | ||
1027 | double stable_thresh = 0; | ||
1028 | int i; | ||
1029 | double distotarget; /* fixed_quality */ | ||
1030 | |||
1031 | /* fixed_quality */ | ||
1032 | distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10)); | ||
1033 | |||
1034 | if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) { | ||
1035 | opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp); | ||
1036 | |||
1037 | for (i = 0; i < 32; i++) { | ||
1038 | double thresh = (lo + hi) / 2; | ||
1039 | int l = 0; | ||
1040 | double distoachieved = 0; /* fixed_quality */ | ||
1041 | |||
1042 | tcd_makelayer(tcd, layno, thresh, 0); | ||
1043 | |||
1044 | if (cp->fixed_quality) { /* fixed_quality */ | ||
1045 | if(cp->cinema){ | ||
1046 | l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC); | ||
1047 | if (l == -999) { | ||
1048 | lo = thresh; | ||
1049 | continue; | ||
1050 | }else{ | ||
1051 | distoachieved = layno == 0 ? | ||
1052 | tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; | ||
1053 | if (distoachieved < distotarget) { | ||
1054 | hi=thresh; | ||
1055 | stable_thresh = thresh; | ||
1056 | continue; | ||
1057 | }else{ | ||
1058 | lo=thresh; | ||
1059 | } | ||
1060 | } | ||
1061 | }else{ | ||
1062 | distoachieved = (layno == 0) ? | ||
1063 | tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); | ||
1064 | if (distoachieved < distotarget) { | ||
1065 | hi = thresh; | ||
1066 | stable_thresh = thresh; | ||
1067 | continue; | ||
1068 | } | ||
1069 | lo = thresh; | ||
1070 | } | ||
1071 | } else { | ||
1072 | l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC); | ||
1073 | /* TODO: what to do with l ??? seek / tell ??? */ | ||
1074 | /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ | ||
1075 | if (l == -999) { | ||
1076 | lo = thresh; | ||
1077 | continue; | ||
1078 | } | ||
1079 | hi = thresh; | ||
1080 | stable_thresh = thresh; | ||
1081 | } | ||
1082 | } | ||
1083 | success = 1; | ||
1084 | goodthresh = stable_thresh; | ||
1085 | t2_destroy(t2); | ||
1086 | } else { | ||
1087 | success = 1; | ||
1088 | goodthresh = min; | ||
1089 | } | ||
1090 | |||
1091 | if (!success) { | ||
1092 | return false; | ||
1093 | } | ||
1094 | |||
1095 | if(image_info && image_info->index_on) { /* Threshold for Marcela Index */ | ||
1096 | image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh; | ||
1097 | } | ||
1098 | tcd_makelayer(tcd, layno, goodthresh, 1); | ||
1099 | |||
1100 | /* fixed_quality */ | ||
1101 | cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); | ||
1102 | } | ||
1103 | |||
1104 | return true; | ||
1105 | } | ||
1106 | |||
1107 | int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) { | ||
1108 | int compno; | ||
1109 | int l, i, npck = 0; | ||
1110 | opj_tcd_tile_t *tile = NULL; | ||
1111 | opj_tcp_t *tcd_tcp = NULL; | ||
1112 | opj_cp_t *cp = NULL; | ||
1113 | |||
1114 | opj_tcp_t *tcp = &tcd->cp->tcps[0]; | ||
1115 | opj_tccp_t *tccp = &tcp->tccps[0]; | ||
1116 | opj_image_t *image = tcd->image; | ||
1117 | |||
1118 | opj_t1_t *t1 = NULL; /* T1 component */ | ||
1119 | opj_t2_t *t2 = NULL; /* T2 component */ | ||
1120 | |||
1121 | tcd->tcd_tileno = tileno; | ||
1122 | tcd->tcd_tile = tcd->tcd_image->tiles; | ||
1123 | tcd->tcp = &tcd->cp->tcps[tileno]; | ||
1124 | |||
1125 | tile = tcd->tcd_tile; | ||
1126 | tcd_tcp = tcd->tcp; | ||
1127 | cp = tcd->cp; | ||
1128 | |||
1129 | if(tcd->cur_tp_num == 0){ | ||
1130 | tcd->encoding_time = opj_clock(); /* time needed to encode a tile */ | ||
1131 | /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ | ||
1132 | if(image_info && image_info->index_on) { | ||
1133 | opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */ | ||
1134 | for (i = 0; i < tilec_idx->numresolutions; i++) { | ||
1135 | opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; | ||
1136 | |||
1137 | image_info->tile[tileno].pw[i] = res_idx->pw; | ||
1138 | image_info->tile[tileno].ph[i] = res_idx->ph; | ||
1139 | |||
1140 | npck += res_idx->pw * res_idx->ph; | ||
1141 | |||
1142 | image_info->tile[tileno].pdx[i] = tccp->prcw[i]; | ||
1143 | image_info->tile[tileno].pdy[i] = tccp->prch[i]; | ||
1144 | } | ||
1145 | image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t)); | ||
1146 | } | ||
1147 | /* << INDEX */ | ||
1148 | |||
1149 | /*---------------TILE-------------------*/ | ||
1150 | |||
1151 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1152 | int x, y; | ||
1153 | |||
1154 | int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1); | ||
1155 | int offset_x = int_ceildiv(image->x0, image->comps[compno].dx); | ||
1156 | int offset_y = int_ceildiv(image->y0, image->comps[compno].dy); | ||
1157 | |||
1158 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1159 | int tw = tilec->x1 - tilec->x0; | ||
1160 | int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx); | ||
1161 | |||
1162 | /* extract tile data */ | ||
1163 | |||
1164 | if (tcd_tcp->tccps[compno].qmfbid == 1) { | ||
1165 | for (y = tilec->y0; y < tilec->y1; y++) { | ||
1166 | /* start of the src tile scanline */ | ||
1167 | int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w]; | ||
1168 | /* start of the dst tile scanline */ | ||
1169 | int *tile_data = &tilec->data[(y - tilec->y0) * tw]; | ||
1170 | for (x = tilec->x0; x < tilec->x1; x++) { | ||
1171 | *tile_data++ = *data++ - adjust; | ||
1172 | } | ||
1173 | } | ||
1174 | } else if (tcd_tcp->tccps[compno].qmfbid == 0) { | ||
1175 | for (y = tilec->y0; y < tilec->y1; y++) { | ||
1176 | /* start of the src tile scanline */ | ||
1177 | int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w]; | ||
1178 | /* start of the dst tile scanline */ | ||
1179 | int *tile_data = &tilec->data[(y - tilec->y0) * tw]; | ||
1180 | for (x = tilec->x0; x < tilec->x1; x++) { | ||
1181 | *tile_data++ = (*data++ - adjust) << 11; | ||
1182 | } | ||
1183 | |||
1184 | } | ||
1185 | } | ||
1186 | } | ||
1187 | |||
1188 | /*----------------MCT-------------------*/ | ||
1189 | if (tcd_tcp->mct) { | ||
1190 | int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0); | ||
1191 | if (tcd_tcp->tccps[0].qmfbid == 0) { | ||
1192 | mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples); | ||
1193 | } else { | ||
1194 | mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples); | ||
1195 | } | ||
1196 | } | ||
1197 | |||
1198 | /*----------------DWT---------------------*/ | ||
1199 | |||
1200 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1201 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1202 | if (tcd_tcp->tccps[compno].qmfbid == 1) { | ||
1203 | dwt_encode(tilec); | ||
1204 | } else if (tcd_tcp->tccps[compno].qmfbid == 0) { | ||
1205 | dwt_encode_real(tilec); | ||
1206 | } | ||
1207 | } | ||
1208 | |||
1209 | /*------------------TIER1-----------------*/ | ||
1210 | t1 = t1_create(tcd->cinfo); | ||
1211 | t1_encode_cblks(t1, tile, tcd_tcp); | ||
1212 | t1_destroy(t1); | ||
1213 | |||
1214 | /*-----------RATE-ALLOCATE------------------*/ | ||
1215 | |||
1216 | /* INDEX */ | ||
1217 | if(image_info) { | ||
1218 | image_info->index_write = 0; | ||
1219 | } | ||
1220 | if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */ | ||
1221 | /* Normal Rate/distortion allocation */ | ||
1222 | tcd_rateallocate(tcd, dest, len, image_info); | ||
1223 | } else { | ||
1224 | /* Fixed layer allocation */ | ||
1225 | tcd_rateallocate_fixed(tcd); | ||
1226 | } | ||
1227 | } | ||
1228 | /*--------------TIER2------------------*/ | ||
1229 | |||
1230 | /* INDEX */ | ||
1231 | if(image_info) { | ||
1232 | image_info->index_write = 1; | ||
1233 | } | ||
1234 | |||
1235 | t2 = t2_create(tcd->cinfo, image, cp); | ||
1236 | l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, image_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS); | ||
1237 | t2_destroy(t2); | ||
1238 | |||
1239 | /*---------------CLEAN-------------------*/ | ||
1240 | |||
1241 | |||
1242 | if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){ | ||
1243 | tcd->encoding_time = opj_clock() - tcd->encoding_time; | ||
1244 | opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time); | ||
1245 | |||
1246 | /* cleaning memory */ | ||
1247 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1248 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1249 | opj_free(tilec->data); | ||
1250 | } | ||
1251 | } | ||
1252 | |||
1253 | return l; | ||
1254 | } | ||
1255 | |||
1256 | bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) { | ||
1257 | int l; | ||
1258 | int compno; | ||
1259 | int eof = 0; | ||
1260 | double tile_time, t1_time, dwt_time; | ||
1261 | opj_tcd_tile_t *tile = NULL; | ||
1262 | |||
1263 | opj_t1_t *t1 = NULL; /* T1 component */ | ||
1264 | opj_t2_t *t2 = NULL; /* T2 component */ | ||
1265 | |||
1266 | tcd->tcd_tileno = tileno; | ||
1267 | tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]); | ||
1268 | tcd->tcp = &(tcd->cp->tcps[tileno]); | ||
1269 | tile = tcd->tcd_tile; | ||
1270 | |||
1271 | tile_time = opj_clock(); /* time needed to decode a tile */ | ||
1272 | opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th); | ||
1273 | |||
1274 | /*--------------TIER2------------------*/ | ||
1275 | |||
1276 | t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp); | ||
1277 | l = t2_decode_packets(t2, src, len, tileno, tile); | ||
1278 | t2_destroy(t2); | ||
1279 | |||
1280 | if (l == -999) { | ||
1281 | eof = 1; | ||
1282 | opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n"); | ||
1283 | } | ||
1284 | |||
1285 | /*------------------TIER1-----------------*/ | ||
1286 | |||
1287 | t1_time = opj_clock(); /* time needed to decode a tile */ | ||
1288 | t1 = t1_create(tcd->cinfo); | ||
1289 | t1_decode_cblks(t1, tile, tcd->tcp); | ||
1290 | t1_destroy(t1); | ||
1291 | t1_time = opj_clock() - t1_time; | ||
1292 | opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time); | ||
1293 | |||
1294 | /*----------------DWT---------------------*/ | ||
1295 | |||
1296 | dwt_time = opj_clock(); /* time needed to decode a tile */ | ||
1297 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1298 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1299 | if (tcd->cp->reduce != 0) { | ||
1300 | tcd->image->comps[compno].resno_decoded = | ||
1301 | tile->comps[compno].numresolutions - tcd->cp->reduce - 1; | ||
1302 | } | ||
1303 | |||
1304 | if (tcd->tcp->tccps[compno].qmfbid == 1) { | ||
1305 | dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded); | ||
1306 | } else { | ||
1307 | dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded); | ||
1308 | } | ||
1309 | |||
1310 | if (tile->comps[compno].numresolutions > 0) { | ||
1311 | tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1); | ||
1312 | } | ||
1313 | } | ||
1314 | dwt_time = opj_clock() - dwt_time; | ||
1315 | opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time); | ||
1316 | |||
1317 | /*----------------MCT-------------------*/ | ||
1318 | |||
1319 | if (tcd->tcp->mct) { | ||
1320 | if (tcd->tcp->tccps[0].qmfbid == 1) { | ||
1321 | mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, | ||
1322 | (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)); | ||
1323 | } else { | ||
1324 | mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, | ||
1325 | (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)); | ||
1326 | } | ||
1327 | } | ||
1328 | |||
1329 | /*---------------TILE-------------------*/ | ||
1330 | |||
1331 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1332 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1333 | opj_tcd_resolution_t *res = &tilec->resolutions[tcd->image->comps[compno].resno_decoded]; | ||
1334 | int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1); | ||
1335 | int min = tcd->image->comps[compno].sgnd ? | ||
1336 | -(1 << (tcd->image->comps[compno].prec - 1)) : 0; | ||
1337 | int max = tcd->image->comps[compno].sgnd ? | ||
1338 | (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1; | ||
1339 | |||
1340 | int tw = tilec->x1 - tilec->x0; | ||
1341 | int w = tcd->image->comps[compno].w; | ||
1342 | |||
1343 | int i, j; | ||
1344 | int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor); | ||
1345 | int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor); | ||
1346 | |||
1347 | for (j = res->y0; j < res->y1; j++) { | ||
1348 | for (i = res->x0; i < res->x1; i++) { | ||
1349 | int v; | ||
1350 | float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0); | ||
1351 | |||
1352 | if (tcd->tcp->tccps[compno].qmfbid == 1) { | ||
1353 | v = tilec->data[i - res->x0 + (j - res->y0) * tw]; | ||
1354 | } else { | ||
1355 | int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); | ||
1356 | v = ((tmp < 0) ? -tmp2:tmp2); | ||
1357 | } | ||
1358 | v += adjust; | ||
1359 | |||
1360 | tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max); | ||
1361 | } | ||
1362 | } | ||
1363 | } | ||
1364 | |||
1365 | tile_time = opj_clock() - tile_time; /* time needed to decode a tile */ | ||
1366 | opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time); | ||
1367 | |||
1368 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1369 | opj_free(tcd->tcd_image->tiles[tileno].comps[compno].data); | ||
1370 | tcd->tcd_image->tiles[tileno].comps[compno].data = NULL; | ||
1371 | } | ||
1372 | |||
1373 | if (eof) { | ||
1374 | return false; | ||
1375 | } | ||
1376 | |||
1377 | return true; | ||
1378 | } | ||
1379 | |||
1380 | void tcd_free_decode(opj_tcd_t *tcd) { | ||
1381 | int tileno,compno,resno,bandno,precno; | ||
1382 | |||
1383 | opj_tcd_image_t *tcd_image = tcd->tcd_image; | ||
1384 | |||
1385 | for (tileno = 0; tileno < tcd_image->tw * tcd_image->th; tileno++) { | ||
1386 | opj_tcd_tile_t *tile = &tcd_image->tiles[tileno]; | ||
1387 | for (compno = 0; compno < tile->numcomps; compno++) { | ||
1388 | opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; | ||
1389 | for (resno = 0; resno < tilec->numresolutions; resno++) { | ||
1390 | opj_tcd_resolution_t *res = &tilec->resolutions[resno]; | ||
1391 | for (bandno = 0; bandno < res->numbands; bandno++) { | ||
1392 | opj_tcd_band_t *band = &res->bands[bandno]; | ||
1393 | for (precno = 0; precno < res->ph * res->pw; precno++) { | ||
1394 | opj_tcd_precinct_t *prec = &band->precincts[precno]; | ||
1395 | if (prec->cblks != NULL) opj_free(prec->cblks); | ||
1396 | if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree); | ||
1397 | if (prec->incltree != NULL) tgt_destroy(prec->incltree); | ||
1398 | } | ||
1399 | if (band->precincts != NULL) opj_free(band->precincts); | ||
1400 | } | ||
1401 | } | ||
1402 | if (tilec->resolutions != NULL) opj_free(tilec->resolutions); | ||
1403 | } | ||
1404 | if (tile->comps != NULL) opj_free(tile->comps); | ||
1405 | } | ||
1406 | |||
1407 | if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles); | ||
1408 | } | ||
1409 | |||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/tcd.h b/libraries/openjpeg-libsl/libopenjpeg/tcd.h new file mode 100644 index 0000000..ae66709 --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/tcd.h | |||
@@ -0,0 +1,270 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | #ifndef __TCD_H | ||
32 | #define __TCD_H | ||
33 | /** | ||
34 | @file tcd.h | ||
35 | @brief Implementation of a tile coder/decoder (TCD) | ||
36 | |||
37 | The functions in TCD.C have for goal to encode or decode each tile independently from | ||
38 | each other. The functions in TCD.C are used by some function in J2K.C. | ||
39 | */ | ||
40 | |||
41 | /** @defgroup TCD TCD - Implementation of a tile coder/decoder */ | ||
42 | /*@{*/ | ||
43 | |||
44 | /** | ||
45 | FIXME: documentation | ||
46 | */ | ||
47 | typedef struct opj_tcd_seg { | ||
48 | int numpasses; | ||
49 | int len; | ||
50 | unsigned char *data; | ||
51 | int maxpasses; | ||
52 | int numnewpasses; | ||
53 | int newlen; | ||
54 | } opj_tcd_seg_t; | ||
55 | |||
56 | /** | ||
57 | FIXME: documentation | ||
58 | */ | ||
59 | typedef struct opj_tcd_pass { | ||
60 | int rate; | ||
61 | double distortiondec; | ||
62 | int term, len; | ||
63 | } opj_tcd_pass_t; | ||
64 | |||
65 | /** | ||
66 | FIXME: documentation | ||
67 | */ | ||
68 | typedef struct opj_tcd_layer { | ||
69 | int numpasses; /* Number of passes in the layer */ | ||
70 | int len; /* len of information */ | ||
71 | double disto; /* add for index (Cfr. Marcela) */ | ||
72 | unsigned char *data; /* data */ | ||
73 | } opj_tcd_layer_t; | ||
74 | |||
75 | /** | ||
76 | FIXME: documentation | ||
77 | */ | ||
78 | typedef struct opj_tcd_cblk { | ||
79 | int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ | ||
80 | int numbps; | ||
81 | int numlenbits; | ||
82 | int len; /* length */ | ||
83 | int numpasses; /* number of pass already done for the code-blocks */ | ||
84 | int numnewpasses; /* number of pass added to the code-blocks */ | ||
85 | int numsegs; /* number of segments */ | ||
86 | opj_tcd_seg_t segs[100]; /* segments informations */ | ||
87 | unsigned char data[8192]; /* Data */ | ||
88 | int numpassesinlayers; /* number of passes in the layer */ | ||
89 | opj_tcd_layer_t layers[100]; /* layer information */ | ||
90 | int totalpasses; /* total number of passes */ | ||
91 | opj_tcd_pass_t passes[100]; /* information about the passes */ | ||
92 | } opj_tcd_cblk_t; | ||
93 | |||
94 | /** | ||
95 | FIXME: documentation | ||
96 | */ | ||
97 | typedef struct opj_tcd_precinct { | ||
98 | int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */ | ||
99 | int cw, ch; /* number of precinct in width and heigth */ | ||
100 | opj_tcd_cblk_t *cblks; /* code-blocks informations */ | ||
101 | opj_tgt_tree_t *incltree; /* inclusion tree */ | ||
102 | opj_tgt_tree_t *imsbtree; /* IMSB tree */ | ||
103 | } opj_tcd_precinct_t; | ||
104 | |||
105 | /** | ||
106 | FIXME: documentation | ||
107 | */ | ||
108 | typedef struct opj_tcd_band { | ||
109 | int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */ | ||
110 | int bandno; | ||
111 | opj_tcd_precinct_t *precincts; /* precinct information */ | ||
112 | int numbps; | ||
113 | float stepsize; | ||
114 | } opj_tcd_band_t; | ||
115 | |||
116 | /** | ||
117 | FIXME: documentation | ||
118 | */ | ||
119 | typedef struct opj_tcd_resolution { | ||
120 | int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */ | ||
121 | int pw, ph; | ||
122 | int numbands; /* number sub-band for the resolution level */ | ||
123 | opj_tcd_band_t bands[3]; /* subband information */ | ||
124 | } opj_tcd_resolution_t; | ||
125 | |||
126 | /** | ||
127 | FIXME: documentation | ||
128 | */ | ||
129 | typedef struct opj_tcd_tilecomp { | ||
130 | int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */ | ||
131 | int numresolutions; /* number of resolutions level */ | ||
132 | opj_tcd_resolution_t *resolutions; /* resolutions information */ | ||
133 | int *data; /* data of the component */ | ||
134 | int nbpix; /* add fixed_quality */ | ||
135 | } opj_tcd_tilecomp_t; | ||
136 | |||
137 | /** | ||
138 | FIXME: documentation | ||
139 | */ | ||
140 | typedef struct opj_tcd_tile { | ||
141 | int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */ | ||
142 | int numcomps; /* number of components in tile */ | ||
143 | opj_tcd_tilecomp_t *comps; /* Components information */ | ||
144 | int nbpix; /* add fixed_quality */ | ||
145 | double distotile; /* add fixed_quality */ | ||
146 | double distolayer[100]; /* add fixed_quality */ | ||
147 | } opj_tcd_tile_t; | ||
148 | |||
149 | /** | ||
150 | FIXME: documentation | ||
151 | */ | ||
152 | typedef struct opj_tcd_image { | ||
153 | int tw, th; /* number of tiles in width and heigth */ | ||
154 | opj_tcd_tile_t *tiles; /* Tiles information */ | ||
155 | } opj_tcd_image_t; | ||
156 | |||
157 | /** | ||
158 | Tile coder/decoder | ||
159 | */ | ||
160 | typedef struct opj_tcd { | ||
161 | /** Position of the tilepart flag in Progression order*/ | ||
162 | int tp_pos; | ||
163 | /** Tile part number*/ | ||
164 | int tp_num; | ||
165 | /** Current tile part number*/ | ||
166 | int cur_tp_num; | ||
167 | /** Total number of tileparts of the current tile*/ | ||
168 | int cur_totnum_tp; | ||
169 | /** Current Packet iterator number */ | ||
170 | int cur_pino; | ||
171 | /** codec context */ | ||
172 | opj_common_ptr cinfo; | ||
173 | |||
174 | /** info on each image tile */ | ||
175 | opj_tcd_image_t *tcd_image; | ||
176 | /** image */ | ||
177 | opj_image_t *image; | ||
178 | /** coding parameters */ | ||
179 | opj_cp_t *cp; | ||
180 | /** pointer to the current encoded/decoded tile */ | ||
181 | opj_tcd_tile_t *tcd_tile; | ||
182 | /** coding/decoding parameters common to all tiles */ | ||
183 | opj_tcp_t *tcp; | ||
184 | /** current encoded/decoded tile */ | ||
185 | int tcd_tileno; | ||
186 | /** Time taken to encode a tile*/ | ||
187 | double encoding_time; | ||
188 | } opj_tcd_t; | ||
189 | |||
190 | /** @name Exported functions */ | ||
191 | /*@{*/ | ||
192 | /* ----------------------------------------------------------------------- */ | ||
193 | |||
194 | /** | ||
195 | Dump the content of a tcd structure | ||
196 | */ | ||
197 | void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img); | ||
198 | /** | ||
199 | Create a new TCD handle | ||
200 | @param cinfo Codec context info | ||
201 | @return Returns a new TCD handle if successful returns NULL otherwise | ||
202 | */ | ||
203 | opj_tcd_t* tcd_create(opj_common_ptr cinfo); | ||
204 | /** | ||
205 | Destroy a previously created TCD handle | ||
206 | @param tcd TCD handle to destroy | ||
207 | */ | ||
208 | void tcd_destroy(opj_tcd_t *tcd); | ||
209 | /** | ||
210 | Initialize the tile coder (allocate the memory) | ||
211 | @param tcd TCD handle | ||
212 | @param image Raw image | ||
213 | @param cp Coding parameters | ||
214 | @param curtileno Number that identifies the tile that will be encoded | ||
215 | */ | ||
216 | void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno); | ||
217 | /** | ||
218 | Free the memory allocated for encoding | ||
219 | @param tcd TCD handle | ||
220 | */ | ||
221 | void tcd_free_encode(opj_tcd_t *tcd); | ||
222 | /** | ||
223 | Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode) | ||
224 | @param tcd TCD handle | ||
225 | @param image Raw image | ||
226 | @param cp Coding parameters | ||
227 | @param curtileno Number that identifies the tile that will be encoded | ||
228 | */ | ||
229 | void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno); | ||
230 | /** | ||
231 | Initialize the tile decoder | ||
232 | @param tcd TCD handle | ||
233 | @param image Raw image | ||
234 | @param cp Coding parameters | ||
235 | */ | ||
236 | void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp); | ||
237 | void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final); | ||
238 | void tcd_rateallocate_fixed(opj_tcd_t *tcd); | ||
239 | void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final); | ||
240 | bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info); | ||
241 | /** | ||
242 | Encode a tile from the raw image into a buffer | ||
243 | @param tcd TCD handle | ||
244 | @param tileno Number that identifies one of the tiles to be encoded | ||
245 | @param dest Destination buffer | ||
246 | @param len Length of destination buffer | ||
247 | @param image_info Creation of index file | ||
248 | @return | ||
249 | */ | ||
250 | int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info); | ||
251 | /** | ||
252 | Decode a tile from a buffer into a raw image | ||
253 | @param tcd TCD handle | ||
254 | @param src Source buffer | ||
255 | @param len Length of source buffer | ||
256 | @param tileno Number that identifies one of the tiles to be decoded | ||
257 | */ | ||
258 | bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno); | ||
259 | /** | ||
260 | Free the memory allocated for decoding | ||
261 | @param tcd TCD handle | ||
262 | */ | ||
263 | void tcd_free_decode(opj_tcd_t *tcd); | ||
264 | |||
265 | /* ----------------------------------------------------------------------- */ | ||
266 | /*@}*/ | ||
267 | |||
268 | /*@}*/ | ||
269 | |||
270 | #endif /* __TCD_H */ | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/tgt.c b/libraries/openjpeg-libsl/libopenjpeg/tgt.c new file mode 100644 index 0000000..30b0aee --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/tgt.c | |||
@@ -0,0 +1,213 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include "opj_includes.h" | ||
33 | |||
34 | /* | ||
35 | ========================================================== | ||
36 | Tag-tree coder interface | ||
37 | ========================================================== | ||
38 | */ | ||
39 | |||
40 | opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) { | ||
41 | int nplh[32]; | ||
42 | int nplv[32]; | ||
43 | opj_tgt_node_t *node = NULL; | ||
44 | opj_tgt_node_t *parentnode = NULL; | ||
45 | opj_tgt_node_t *parentnode0 = NULL; | ||
46 | opj_tgt_tree_t *tree = NULL; | ||
47 | int i, j, k; | ||
48 | int numlvls; | ||
49 | int n; | ||
50 | |||
51 | tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t)); | ||
52 | if(!tree) return NULL; | ||
53 | tree->numleafsh = numleafsh; | ||
54 | tree->numleafsv = numleafsv; | ||
55 | |||
56 | numlvls = 0; | ||
57 | nplh[0] = numleafsh; | ||
58 | nplv[0] = numleafsv; | ||
59 | tree->numnodes = 0; | ||
60 | do { | ||
61 | n = nplh[numlvls] * nplv[numlvls]; | ||
62 | nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; | ||
63 | nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; | ||
64 | tree->numnodes += n; | ||
65 | ++numlvls; | ||
66 | } while (n > 1); | ||
67 | |||
68 | /* ADD */ | ||
69 | if (tree->numnodes == 0) { | ||
70 | opj_free(tree); | ||
71 | return NULL; | ||
72 | } | ||
73 | |||
74 | tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t)); | ||
75 | if(!tree->nodes) { | ||
76 | opj_free(tree); | ||
77 | return NULL; | ||
78 | } | ||
79 | |||
80 | node = tree->nodes; | ||
81 | parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv]; | ||
82 | parentnode0 = parentnode; | ||
83 | |||
84 | for (i = 0; i < numlvls - 1; ++i) { | ||
85 | for (j = 0; j < nplv[i]; ++j) { | ||
86 | k = nplh[i]; | ||
87 | while (--k >= 0) { | ||
88 | node->parent = parentnode; | ||
89 | ++node; | ||
90 | if (--k >= 0) { | ||
91 | node->parent = parentnode; | ||
92 | ++node; | ||
93 | } | ||
94 | ++parentnode; | ||
95 | } | ||
96 | if ((j & 1) || j == nplv[i] - 1) { | ||
97 | parentnode0 = parentnode; | ||
98 | } else { | ||
99 | parentnode = parentnode0; | ||
100 | parentnode0 += nplh[i]; | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | node->parent = 0; | ||
105 | |||
106 | tgt_reset(tree); | ||
107 | |||
108 | return tree; | ||
109 | } | ||
110 | |||
111 | void tgt_destroy(opj_tgt_tree_t *tree) { | ||
112 | opj_free(tree->nodes); | ||
113 | opj_free(tree); | ||
114 | } | ||
115 | |||
116 | void tgt_reset(opj_tgt_tree_t *tree) { | ||
117 | int i; | ||
118 | |||
119 | if (NULL == tree) | ||
120 | return; | ||
121 | |||
122 | for (i = 0; i < tree->numnodes; i++) { | ||
123 | tree->nodes[i].value = 999; | ||
124 | tree->nodes[i].low = 0; | ||
125 | tree->nodes[i].known = 0; | ||
126 | } | ||
127 | } | ||
128 | |||
129 | void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) { | ||
130 | opj_tgt_node_t *node; | ||
131 | node = &tree->nodes[leafno]; | ||
132 | while (node && node->value > value) { | ||
133 | node->value = value; | ||
134 | node = node->parent; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) { | ||
139 | opj_tgt_node_t *stk[31]; | ||
140 | opj_tgt_node_t **stkptr; | ||
141 | opj_tgt_node_t *node; | ||
142 | int low; | ||
143 | |||
144 | stkptr = stk; | ||
145 | node = &tree->nodes[leafno]; | ||
146 | while (node->parent) { | ||
147 | *stkptr++ = node; | ||
148 | node = node->parent; | ||
149 | } | ||
150 | |||
151 | low = 0; | ||
152 | for (;;) { | ||
153 | if (low > node->low) { | ||
154 | node->low = low; | ||
155 | } else { | ||
156 | low = node->low; | ||
157 | } | ||
158 | |||
159 | while (low < threshold) { | ||
160 | if (low >= node->value) { | ||
161 | if (!node->known) { | ||
162 | bio_write(bio, 1, 1); | ||
163 | node->known = 1; | ||
164 | } | ||
165 | break; | ||
166 | } | ||
167 | bio_write(bio, 0, 1); | ||
168 | ++low; | ||
169 | } | ||
170 | |||
171 | node->low = low; | ||
172 | if (stkptr == stk) | ||
173 | break; | ||
174 | node = *--stkptr; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) { | ||
179 | opj_tgt_node_t *stk[31]; | ||
180 | opj_tgt_node_t **stkptr; | ||
181 | opj_tgt_node_t *node; | ||
182 | int low; | ||
183 | |||
184 | stkptr = stk; | ||
185 | node = &tree->nodes[leafno]; | ||
186 | while (node->parent) { | ||
187 | *stkptr++ = node; | ||
188 | node = node->parent; | ||
189 | } | ||
190 | |||
191 | low = 0; | ||
192 | for (;;) { | ||
193 | if (low > node->low) { | ||
194 | node->low = low; | ||
195 | } else { | ||
196 | low = node->low; | ||
197 | } | ||
198 | while (low < threshold && low < node->value) { | ||
199 | if (bio_read(bio, 1)) { | ||
200 | node->value = low; | ||
201 | } else { | ||
202 | ++low; | ||
203 | } | ||
204 | } | ||
205 | node->low = low; | ||
206 | if (stkptr == stk) { | ||
207 | break; | ||
208 | } | ||
209 | node = *--stkptr; | ||
210 | } | ||
211 | |||
212 | return (node->value < threshold) ? 1 : 0; | ||
213 | } | ||
diff --git a/libraries/openjpeg-libsl/libopenjpeg/tgt.h b/libraries/openjpeg-libsl/libopenjpeg/tgt.h new file mode 100644 index 0000000..c08c8da --- /dev/null +++ b/libraries/openjpeg-libsl/libopenjpeg/tgt.h | |||
@@ -0,0 +1,114 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium | ||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq | ||
4 | * Copyright (c) 2001-2003, David Janssens | ||
5 | * Copyright (c) 2002-2003, Yannick Verschueren | ||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe | ||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team | ||
8 | * All rights reserved. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef __TGT_H | ||
33 | #define __TGT_H | ||
34 | /** | ||
35 | @file tgt.h | ||
36 | @brief Implementation of a tag-tree coder (TGT) | ||
37 | |||
38 | The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C | ||
39 | are used by some function in T2.C. | ||
40 | */ | ||
41 | |||
42 | /** @defgroup TGT TGT - Implementation of a tag-tree coder */ | ||
43 | /*@{*/ | ||
44 | |||
45 | /** | ||
46 | Tag node | ||
47 | */ | ||
48 | typedef struct opj_tgt_node { | ||
49 | struct opj_tgt_node *parent; | ||
50 | int value; | ||
51 | int low; | ||
52 | int known; | ||
53 | } opj_tgt_node_t; | ||
54 | |||
55 | /** | ||
56 | Tag tree | ||
57 | */ | ||
58 | typedef struct opj_tgt_tree { | ||
59 | int numleafsh; | ||
60 | int numleafsv; | ||
61 | int numnodes; | ||
62 | opj_tgt_node_t *nodes; | ||
63 | } opj_tgt_tree_t; | ||
64 | |||
65 | /** @name Exported functions */ | ||
66 | /*@{*/ | ||
67 | /* ----------------------------------------------------------------------- */ | ||
68 | /** | ||
69 | Create a tag-tree | ||
70 | @param numleafsh Width of the array of leafs of the tree | ||
71 | @param numleafsv Height of the array of leafs of the tree | ||
72 | @return Returns a new tag-tree if successful, returns NULL otherwise | ||
73 | */ | ||
74 | opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv); | ||
75 | /** | ||
76 | Destroy a tag-tree, liberating memory | ||
77 | @param tree Tag-tree to destroy | ||
78 | */ | ||
79 | void tgt_destroy(opj_tgt_tree_t *tree); | ||
80 | /** | ||
81 | Reset a tag-tree (set all leaves to 0) | ||
82 | @param tree Tag-tree to reset | ||
83 | */ | ||
84 | void tgt_reset(opj_tgt_tree_t *tree); | ||
85 | /** | ||
86 | Set the value of a leaf of a tag-tree | ||
87 | @param tree Tag-tree to modify | ||
88 | @param leafno Number that identifies the leaf to modify | ||
89 | @param value New value of the leaf | ||
90 | */ | ||
91 | void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value); | ||
92 | /** | ||
93 | Encode the value of a leaf of the tag-tree up to a given threshold | ||
94 | @param bio Pointer to a BIO handle | ||
95 | @param tree Tag-tree to modify | ||
96 | @param leafno Number that identifies the leaf to encode | ||
97 | @param threshold Threshold to use when encoding value of the leaf | ||
98 | */ | ||
99 | void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold); | ||
100 | /** | ||
101 | Decode the value of a leaf of the tag-tree up to a given threshold | ||
102 | @param bio Pointer to a BIO handle | ||
103 | @param tree Tag-tree to decode | ||
104 | @param leafno Number that identifies the leaf to decode | ||
105 | @param threshold Threshold to use when decoding value of the leaf | ||
106 | @return Returns 1 if the node's value < threshold, returns 0 otherwise | ||
107 | */ | ||
108 | int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold); | ||
109 | /* ----------------------------------------------------------------------- */ | ||
110 | /*@}*/ | ||
111 | |||
112 | /*@}*/ | ||
113 | |||
114 | #endif /* __TGT_H */ | ||
diff --git a/libraries/openjpeg-libsl/libsl/libsl.cpp b/libraries/openjpeg-libsl/libsl/libsl.cpp new file mode 100644 index 0000000..f3cae74 --- /dev/null +++ b/libraries/openjpeg-libsl/libsl/libsl.cpp | |||
@@ -0,0 +1,259 @@ | |||
1 | // This is the main DLL file. | ||
2 | |||
3 | #include "libsl.h" | ||
4 | extern "C" { | ||
5 | #include "../libopenjpeg/openjpeg.h" | ||
6 | } | ||
7 | |||
8 | #define NULL 0 | ||
9 | |||
10 | |||
11 | struct image_wrapper | ||
12 | { | ||
13 | opj_image* image; | ||
14 | |||
15 | image_wrapper(int numcmpts, opj_image_cmptparm_t* cmptparms, OPJ_COLOR_SPACE clrspc) | ||
16 | { | ||
17 | image = opj_image_create(numcmpts, cmptparms, clrspc); | ||
18 | |||
19 | if (image == NULL) | ||
20 | throw "opj_image_create failed"; | ||
21 | } | ||
22 | |||
23 | image_wrapper(opj_dinfo* dinfo, opj_cio* cio) | ||
24 | { | ||
25 | image = opj_decode(dinfo,cio); | ||
26 | |||
27 | if (image == NULL) | ||
28 | throw "opj_decode failed"; | ||
29 | } | ||
30 | |||
31 | ~image_wrapper() | ||
32 | { | ||
33 | opj_image_destroy(image); | ||
34 | } | ||
35 | }; | ||
36 | |||
37 | struct cinfo_wrapper | ||
38 | { | ||
39 | opj_cinfo* cinfo; | ||
40 | |||
41 | cinfo_wrapper(CODEC_FORMAT format) | ||
42 | { | ||
43 | cinfo = opj_create_compress(format); | ||
44 | |||
45 | if (cinfo == NULL) | ||
46 | throw "opj_create_compress failed"; | ||
47 | } | ||
48 | |||
49 | ~cinfo_wrapper() | ||
50 | { | ||
51 | opj_destroy_compress(cinfo); | ||
52 | } | ||
53 | }; | ||
54 | |||
55 | struct dinfo_wrapper | ||
56 | { | ||
57 | opj_dinfo* dinfo; | ||
58 | |||
59 | dinfo_wrapper(CODEC_FORMAT format) | ||
60 | { | ||
61 | dinfo = opj_create_decompress(format); | ||
62 | |||
63 | if (dinfo == NULL) | ||
64 | throw "opj_create_decompress failed"; | ||
65 | } | ||
66 | |||
67 | ~dinfo_wrapper() | ||
68 | { | ||
69 | opj_destroy_decompress(dinfo); | ||
70 | } | ||
71 | }; | ||
72 | |||
73 | struct cio_wrapper | ||
74 | { | ||
75 | opj_cio* cio; | ||
76 | |||
77 | cio_wrapper(opj_cinfo* cinfo, unsigned char* buffer, int length) | ||
78 | { | ||
79 | cio = opj_cio_open((opj_common_ptr)cinfo,buffer,length); | ||
80 | |||
81 | if (cio == NULL) | ||
82 | throw "opj_cio_open failed"; | ||
83 | } | ||
84 | |||
85 | cio_wrapper(opj_dinfo* dinfo, unsigned char* buffer, int length) | ||
86 | { | ||
87 | cio = opj_cio_open((opj_common_ptr)dinfo,buffer,length); | ||
88 | |||
89 | if (cio == NULL) | ||
90 | throw "opj_cio_open failed"; | ||
91 | } | ||
92 | |||
93 | ~cio_wrapper() | ||
94 | { | ||
95 | opj_cio_close(cio); | ||
96 | } | ||
97 | }; | ||
98 | |||
99 | bool LibslAllocEncoded(LibslImage* image) | ||
100 | { | ||
101 | try | ||
102 | { | ||
103 | image->encoded = new unsigned char[image->length]; | ||
104 | image->decoded = 0; | ||
105 | } | ||
106 | |||
107 | catch (...) | ||
108 | { | ||
109 | return false; | ||
110 | } | ||
111 | |||
112 | return true; | ||
113 | } | ||
114 | |||
115 | bool LibslAllocDecoded(LibslImage* image) | ||
116 | { | ||
117 | try | ||
118 | { | ||
119 | image->decoded = new unsigned char[image->width * image->height * image->components]; | ||
120 | image->encoded = 0; | ||
121 | } | ||
122 | |||
123 | catch (...) | ||
124 | { | ||
125 | return false; | ||
126 | } | ||
127 | |||
128 | return true; | ||
129 | } | ||
130 | |||
131 | void LibslFree(LibslImage* image) | ||
132 | { | ||
133 | if (image->encoded != 0) delete image->encoded; | ||
134 | if (image->decoded != 0) delete image->decoded; | ||
135 | } | ||
136 | |||
137 | |||
138 | bool LibslEncode(LibslImage* image, bool lossless) | ||
139 | { | ||
140 | try | ||
141 | { | ||
142 | opj_cparameters cparameters; | ||
143 | opj_set_default_encoder_parameters(&cparameters); | ||
144 | cparameters.cp_disto_alloc = 1; | ||
145 | |||
146 | if (lossless) | ||
147 | { | ||
148 | cparameters.tcp_numlayers = 1; | ||
149 | cparameters.tcp_rates[0] = 0; | ||
150 | } | ||
151 | else | ||
152 | { | ||
153 | cparameters.tcp_numlayers = 6; | ||
154 | cparameters.tcp_rates[0] = 1280; | ||
155 | cparameters.tcp_rates[1] = 640; | ||
156 | cparameters.tcp_rates[2] = 320; | ||
157 | cparameters.tcp_rates[3] = 160; | ||
158 | cparameters.tcp_rates[4] = 80; | ||
159 | cparameters.tcp_rates[5] = 40; | ||
160 | } | ||
161 | |||
162 | cparameters.cp_comment = "LL_RGBHM"; | ||
163 | |||
164 | if (image->components > 5) | ||
165 | return false; | ||
166 | |||
167 | opj_image_comptparm comptparm[5]; | ||
168 | |||
169 | for (int i = 0; i < image->components; i++) | ||
170 | { | ||
171 | comptparm[i].bpp = 8; | ||
172 | comptparm[i].prec = 8; | ||
173 | comptparm[i].sgnd = 0; | ||
174 | comptparm[i].dx = 1; | ||
175 | comptparm[i].dy = 1; | ||
176 | comptparm[i].x0 = 0; | ||
177 | comptparm[i].y0 = 0; | ||
178 | comptparm[i].w = image->width; | ||
179 | comptparm[i].h = image->height; | ||
180 | } | ||
181 | |||
182 | image_wrapper cimage(image->components,comptparm,CLRSPC_SRGB); | ||
183 | cimage.image->x0 = 0; | ||
184 | cimage.image->y0 = 0; | ||
185 | cimage.image->x1 = image->width; | ||
186 | cimage.image->y1 = image->height; | ||
187 | |||
188 | int dataIndex = 0, compIndex = 0, x, y, c; | ||
189 | |||
190 | for (y = 0; y < image->height; y++) | ||
191 | { | ||
192 | for (x = 0; x < image->width; x++) | ||
193 | { | ||
194 | for (c = 0; c < image->components; c++) | ||
195 | cimage.image->comps[c].data[compIndex] = image->decoded[dataIndex++]; | ||
196 | |||
197 | compIndex++; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | cinfo_wrapper cinfo(CODEC_J2K); | ||
202 | opj_setup_encoder(cinfo.cinfo,&cparameters,cimage.image); | ||
203 | cio_wrapper cio(cinfo.cinfo,NULL,0); | ||
204 | |||
205 | if (!opj_encode(cinfo.cinfo,cio.cio,cimage.image,cparameters.index)) | ||
206 | return false; | ||
207 | |||
208 | image->length = cio_tell(cio.cio); | ||
209 | image->encoded = new unsigned char[image->length]; | ||
210 | |||
211 | for (int i = 0; i < image->length; i++) | ||
212 | image->encoded[i] = cio.cio->buffer[i]; | ||
213 | |||
214 | return true; | ||
215 | } | ||
216 | |||
217 | catch (...) | ||
218 | { | ||
219 | return false; | ||
220 | } | ||
221 | } | ||
222 | |||
223 | bool LibslDecode(LibslImage* image) | ||
224 | { | ||
225 | opj_dparameters dparameters; | ||
226 | |||
227 | try | ||
228 | { | ||
229 | opj_set_default_decoder_parameters(&dparameters); | ||
230 | dinfo_wrapper dinfo(CODEC_J2K); | ||
231 | opj_setup_decoder(dinfo.dinfo, &dparameters); | ||
232 | cio_wrapper cio(dinfo.dinfo,image->encoded,image->length); | ||
233 | image_wrapper cimage(dinfo.dinfo, cio.cio); // decode happens here | ||
234 | |||
235 | int dataIndex = 0, compIndex = 0, x, y, c; | ||
236 | image->width = cimage.image->x1 - cimage.image->x0; | ||
237 | image->height = cimage.image->y1 - cimage.image->y0; | ||
238 | image->components = cimage.image->numcomps; | ||
239 | image->decoded = new unsigned char[image->width*image->height*image->components]; | ||
240 | |||
241 | for (y = 0; y < image->height; y++) | ||
242 | { | ||
243 | for (x = 0; x < image->width; x++) | ||
244 | { | ||
245 | for (c = 0; c < image->components; c++) | ||
246 | image->decoded[dataIndex++] = cimage.image->comps[c].data[compIndex]; | ||
247 | |||
248 | compIndex++; | ||
249 | } | ||
250 | } | ||
251 | |||
252 | return true; | ||
253 | } | ||
254 | |||
255 | catch (...) | ||
256 | { | ||
257 | return false; | ||
258 | } | ||
259 | } | ||
diff --git a/libraries/openjpeg-libsl/libsl/libsl.h b/libraries/openjpeg-libsl/libsl/libsl.h new file mode 100644 index 0000000..d6e293c --- /dev/null +++ b/libraries/openjpeg-libsl/libsl/libsl.h | |||
@@ -0,0 +1,32 @@ | |||
1 | |||
2 | |||
3 | #ifndef LIBSL_H | ||
4 | #define LIBSL_H | ||
5 | |||
6 | |||
7 | struct LibslImage | ||
8 | { | ||
9 | unsigned char* encoded; | ||
10 | int length; | ||
11 | |||
12 | unsigned char* decoded; | ||
13 | int width; | ||
14 | int height; | ||
15 | int components; | ||
16 | }; | ||
17 | |||
18 | #ifdef WIN32 | ||
19 | #define DLLEXPORT extern "C" __declspec(dllexport) | ||
20 | #else | ||
21 | #define DLLEXPORT extern "C" | ||
22 | #endif | ||
23 | |||
24 | // uncompresed images are raw RGBA 8bit/channel | ||
25 | DLLEXPORT bool LibslEncode(LibslImage* image, bool lossless); | ||
26 | DLLEXPORT bool LibslDecode(LibslImage* image); | ||
27 | DLLEXPORT bool LibslAllocEncoded(LibslImage* image); | ||
28 | DLLEXPORT bool LibslAllocDecoded(LibslImage* image); | ||
29 | DLLEXPORT void LibslFree(LibslImage* image); | ||
30 | |||
31 | |||
32 | #endif | ||