-
Notifications
You must be signed in to change notification settings - Fork 1
/
openjpeg-js.patch
231 lines (222 loc) · 7.21 KB
/
openjpeg-js.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3ea2424..56f2f18 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -66,6 +66,7 @@ endif(NOT OPENJPEG_SOVERSION)
set(OPENJPEG_LIBRARY_PROPERTIES
VERSION "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}"
SOVERSION "${OPENJPEG_SOVERSION}"
+ POSITION_INDEPENDENT_CODE ON
)
# --------------------------------------------------------------------------
diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c
index 3d4aa0f..f31d13a 100644
--- a/src/lib/openjp2/j2k.c
+++ b/src/lib/openjp2/j2k.c
@@ -4728,9 +4728,8 @@ static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
/* Check enough bytes left in stream before allocation */
if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length >
opj_stream_get_number_byte_left(p_stream)) {
- opj_event_msg(p_manager, EVT_ERROR,
+ opj_event_msg(p_manager, EVT_WARNING,
"Tile part length size inconsistent with stream length\n");
- return OPJ_FALSE;
}
if (p_j2k->m_specific_param.m_decoder.m_sot_length >
UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) {
@@ -5451,8 +5450,8 @@ static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
/* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
if (opj_stream_read_data(p_stream,
p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
- opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
- return OPJ_FALSE;
+ opj_event_msg(p_manager, EVT_WARNING, "No EOC marker. Possibly a truncated stream.\n");
+ return OPJ_TRUE;
}
/* read 2 bytes as the new marker ID*/
diff --git a/src/lib/openjp2/openjpeg.c b/src/lib/openjp2/openjpeg.c
index 7b12303..1cd8d1c 100644
--- a/src/lib/openjp2/openjpeg.c
+++ b/src/lib/openjp2/openjpeg.c
@@ -87,7 +87,130 @@ OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
}
/* ---------------------------------------------------------------------- */
+/* Buffer-based */
+static OPJ_SIZE_T
+opj_read_from_buffer (void* pdst, OPJ_SIZE_T len, opj_buffer_info_t* psrc)
+{
+ OPJ_SIZE_T n = psrc->buf + psrc->len - psrc->cur;
+
+ if (n) {
+ if (n > len)
+ n = len;
+
+ memcpy (pdst, psrc->cur, n);
+ psrc->cur += n;
+ }
+ else
+ n = (OPJ_SIZE_T)-1;
+
+ return n;
+}
+
+static OPJ_SIZE_T
+opj_write_to_buffer (void* p_buffer, OPJ_SIZE_T p_nb_bytes,
+ opj_buffer_info_t* p_source_buffer)
+{
+ void* pbuf = p_source_buffer->buf;
+ void* pcur = p_source_buffer->cur;
+
+ OPJ_SIZE_T len = p_source_buffer->len;
+
+ if (0 == len)
+ len = 1;
+
+ OPJ_SIZE_T dist = pcur - pbuf, n = len - dist;
+ assert (dist <= len);
+
+ while (n < p_nb_bytes) {
+ len *= 2;
+ n = len - dist;
+ }
+
+ if (len != p_source_buffer->len) {
+ pbuf = opj_malloc (len);
+
+ if (0 == pbuf)
+ return (OPJ_SIZE_T)-1;
+
+ if (p_source_buffer->buf) {
+ memcpy (pbuf, p_source_buffer->buf, dist);
+ opj_free (p_source_buffer->buf);
+ }
+
+ p_source_buffer->buf = pbuf;
+ p_source_buffer->cur = pbuf + dist;
+ p_source_buffer->len = len;
+ }
+
+ memcpy (p_source_buffer->cur, p_buffer, p_nb_bytes);
+ p_source_buffer->cur += p_nb_bytes;
+
+ return p_nb_bytes;
+}
+
+static OPJ_SIZE_T
+opj_skip_from_buffer (OPJ_SIZE_T len, opj_buffer_info_t* psrc)
+{
+ OPJ_SIZE_T n = psrc->buf + psrc->len - psrc->cur;
+
+ if (n) {
+ if (n > len)
+ n = len;
+
+ psrc->cur += len;
+ }
+ else
+ n = (OPJ_SIZE_T)-1;
+
+ return n;
+}
+
+static OPJ_BOOL
+opj_seek_from_buffer (OPJ_OFF_T len, opj_buffer_info_t* psrc)
+{
+ OPJ_SIZE_T n = psrc->len;
+
+ if (n > len)
+ n = len;
+
+ psrc->cur = psrc->buf + n;
+
+ return OPJ_TRUE;
+}
+
+opj_stream_t* OPJ_CALLCONV
+opj_stream_create_buffer_stream (opj_buffer_info_t* psrc, OPJ_BOOL input)
+{
+ if (!psrc)
+ return 0;
+
+ opj_stream_t* ps = opj_stream_default_create (input);
+
+ if (0 == ps)
+ return 0;
+
+ opj_stream_set_user_data (ps, psrc, 0);
+ opj_stream_set_user_data_length (ps, psrc->len);
+
+ if (input)
+ opj_stream_set_read_function (
+ ps, (opj_stream_read_fn)opj_read_from_buffer);
+ else
+ opj_stream_set_write_function(
+ ps,(opj_stream_write_fn) opj_write_to_buffer);
+
+ opj_stream_set_skip_function (
+ ps, (opj_stream_skip_fn)opj_skip_from_buffer);
+
+ opj_stream_set_seek_function (
+ ps, (opj_stream_seek_fn)opj_seek_from_buffer);
+
+ return ps;
+}
+
+/* ---------------------------------------------------------------------- */
+
static OPJ_SIZE_T opj_read_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
FILE * p_file)
{
diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h
index dc1e206..688ef80 100644
--- a/src/lib/openjp2/openjpeg.h
+++ b/src/lib/openjp2/openjpeg.h
@@ -82,7 +82,11 @@ Most compilers implement their own version of this keyword ...
# if defined(OPJ_STATIC) /* static library uses "hidden" */
# define OPJ_API __attribute__ ((visibility ("hidden")))
# else
-# define OPJ_API __attribute__ ((visibility ("default")))
+# if defined(EMSCRIPTEN)
+# define OPJ_API __attribute__((used))
+# else
+# define OPJ_API __attribute__ ((visibility ("default")))
+# endif
# endif
# define OPJ_LOCAL __attribute__ ((visibility ("hidden")))
# else
@@ -571,6 +575,12 @@ typedef struct opj_dparameters {
} opj_dparameters_t;
+typedef struct opj_buffer_info {
+ OPJ_BYTE* buf;
+ OPJ_BYTE* cur;
+ OPJ_SIZE_T len;
+} opj_buffer_info_t;
+
/**
* JPEG2000 codec V2.
@@ -1222,6 +1232,9 @@ OPJ_API opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
OPJ_SIZE_T p_buffer_size,
OPJ_BOOL p_is_read_stream);
+OPJ_API opj_stream_t* OPJ_CALLCONV
+opj_stream_create_buffer_stream (opj_buffer_info_t*, OPJ_BOOL);
+
/*
==========================================================
event manager functions definitions
diff --git a/src/lib/openjp2/t2.c b/src/lib/openjp2/t2.c
index 0887b9f..17c9273 100644
--- a/src/lib/openjp2/t2.c
+++ b/src/lib/openjp2/t2.c
@@ -1352,11 +1352,11 @@ static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) <
(OPJ_SIZE_T)l_current_data) ||
(l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
- opj_event_msg(p_manager, EVT_ERROR,
+ opj_event_msg(p_manager, EVT_WARNING,
"read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno,
p_pi->compno);
- return OPJ_FALSE;
+ return OPJ_TRUE;
}
#ifdef USE_JPWL