Check-in [ab072025de]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:update imgjp2: add format options for writing, add brief readme file
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ab072025de575a644e11e1f4b147984d9f4e8fa4
User & Date: chw 2019-09-09 06:55:17
Context
2019-09-09
06:58
add tklib upstream changes check-in: 05ccc5c54e user: chw tags: trunk
06:55
update imgjp2: add format options for writing, add brief readme file check-in: ab072025de user: chw tags: trunk
2019-09-08
16:02
add imgjp2 module to undroid/vanillawish build scripts for win32/64 check-in: 2c1f681145 user: chw tags: trunk
Changes

Added jni/imgjp2/README.txt.

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
imgjp2
======

Tk photo image format handler for the JPEG2000 (.jp2) format using
libopenjp2 from http://www.openjpeg.org

The image format name is "jp2", i.e.

  package require imgjp2
  image create photo MyImage
  MyImage read filename -format jp2
  MyImage filename -format jp2[<compress-options>]

Supported <compress-options> are "/r:<num>,...", "/q:<num>,...",
and "/I". For further information consult the description of the
"opj_compress" command in http://www.openjpeg.org

Changes to jni/imgjp2/imgjp2.c.

9
10
11
12
13
14
15

16
17
18
19
20
21
22
..
32
33
34
35
36
37
38



39
40
41
42
43
44
45
..
53
54
55
56
57
58
59


60


61

62
63
64
65
66
67
68
..
89
90
91
92
93
94
95




96
97
98
99
100
101
102
...
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
...
223
224
225
226
227
228
229

230


231

232
233
234
235
236
237
238
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include <tcl.h>
#include <tk.h>
#include <string.h>

#include <openjpeg.h>

static void
LogHandler(const char *msg, void *udata)
{
#ifndef NDEBUG
    fprintf(stderr, "%s", msg);
................................................................................
{
    int match = 0;
    opj_codec_t *codec;
    opj_dparameters_t par;
    opj_image_t *img = NULL;

    codec = opj_create_decompress(OPJ_CODEC_JP2);



    opj_set_info_handler(codec, LogHandler, 0);
    opj_set_warning_handler(codec, LogHandler, 0);
    opj_set_error_handler(codec, LogHandler, 0);
    memset(&par, 0, sizeof(par));
    opj_set_default_decoder_parameters(&par);
    opj_setup_decoder(codec, &par);
    if (opj_read_header(stream, codec, &img)) {
................................................................................
	    *widthPtr = w;
	}
	if (heightPtr != NULL) {
	    *heightPtr = h;
	}
	match = 1;
    }


    opj_destroy_codec(codec);


    opj_image_destroy(img);

    return match;
}

static void
DownShift(
    opj_image_comp_t *comp)
{
................................................................................
    opj_codec_t *codec;
    opj_dparameters_t par;
    opj_image_t *img = NULL;

    memset(&par, 0, sizeof(par));
    opj_set_default_decoder_parameters(&par);
    codec = opj_create_decompress(OPJ_CODEC_JP2);




    opj_set_info_handler(codec, LogHandler, 0);
    opj_set_warning_handler(codec, LogHandler, 0);
    opj_set_error_handler(codec, LogHandler, 0);
    opj_setup_decoder(codec, &par);
    if (opj_read_header(stream, codec, &img) &&
	opj_set_decode_area(codec, img, srcX, srcY,
			    srcX + width, srcY + height)) {
................................................................................
	    }
	    Tk_PhotoExpand(interp, imgHandle,
			   destX + blk.width, destY + blk.height);
	    Tk_PhotoPutBlock_NoComposite(imgHandle, &blk, destX, destY,
					 blk.width, blk.height);
	}
    }


    opj_destroy_codec(codec);


    opj_image_destroy(img);

    return result;
}

static int
CommonWriteJP2(
    Tcl_Interp *interp,
    opj_stream_t *stream,
    Tcl_Obj *fmtObj,
    Tk_PhotoImageBlock *blkPtr)
{
    int x, y, result = TCL_ERROR;
    opj_cparameters_t par;
    opj_codec_t *codec = NULL;
    opj_image_t *img = NULL;
    opj_image_cmptparm_t comps[3];

    memset(&par, 0, sizeof(par));
    opj_set_default_encoder_parameters(&par);
    par.tcp_mct = 255;

















































    par.irreversible = 1;




    codec = opj_create_compress(OPJ_CODEC_JP2);




    opj_set_info_handler(codec, LogHandler, 0);
    opj_set_warning_handler(codec, LogHandler, 0);
    opj_set_error_handler(codec, LogHandler, 0);
    memset(comps, 0, sizeof(comps));
    comps[0].prec = comps[0].bpp = 8;
    comps[0].sgnd = 0;
    comps[0].dx = comps[0].dy = 1;
................................................................................
	Tcl_SetResult(interp, "encoding failed", TCL_STATIC);
    } else if (!opj_end_compress(codec, stream)) {
	Tcl_SetResult(interp, "end compress failed", TCL_STATIC);
    } else {
	result = TCL_OK;
    }
done:

    opj_destroy_codec(codec);


    opj_image_destroy(img);

    return result;
}

static OPJ_SIZE_T
ChanRead(void *buffer, OPJ_SIZE_T nbytes, void *udata)
{
    Tcl_Channel chan = (Tcl_Channel) udata;







>







 







>
>
>







 







>
>
|
>
>
|
>







 







>
>
>
>







 







>
>
|
>
>
|
>












|






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>

>
>
>
>







 







>
|
>
>
|
>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
..
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include <tcl.h>
#include <tk.h>
#include <string.h>
#include <stdlib.h>
#include <openjpeg.h>

static void
LogHandler(const char *msg, void *udata)
{
#ifndef NDEBUG
    fprintf(stderr, "%s", msg);
................................................................................
{
    int match = 0;
    opj_codec_t *codec;
    opj_dparameters_t par;
    opj_image_t *img = NULL;

    codec = opj_create_decompress(OPJ_CODEC_JP2);
    if (codec == NULL) {
	goto done;
    }
    opj_set_info_handler(codec, LogHandler, 0);
    opj_set_warning_handler(codec, LogHandler, 0);
    opj_set_error_handler(codec, LogHandler, 0);
    memset(&par, 0, sizeof(par));
    opj_set_default_decoder_parameters(&par);
    opj_setup_decoder(codec, &par);
    if (opj_read_header(stream, codec, &img)) {
................................................................................
	    *widthPtr = w;
	}
	if (heightPtr != NULL) {
	    *heightPtr = h;
	}
	match = 1;
    }
done:
    if (codec != NULL) {
	opj_destroy_codec(codec);
    }
    if (img != NULL) {
	opj_image_destroy(img);
    }
    return match;
}

static void
DownShift(
    opj_image_comp_t *comp)
{
................................................................................
    opj_codec_t *codec;
    opj_dparameters_t par;
    opj_image_t *img = NULL;

    memset(&par, 0, sizeof(par));
    opj_set_default_decoder_parameters(&par);
    codec = opj_create_decompress(OPJ_CODEC_JP2);
    if (codec == NULL) {
	Tcl_SetResult(interp, "error creating codec", TCL_STATIC);
	goto done;
    }
    opj_set_info_handler(codec, LogHandler, 0);
    opj_set_warning_handler(codec, LogHandler, 0);
    opj_set_error_handler(codec, LogHandler, 0);
    opj_setup_decoder(codec, &par);
    if (opj_read_header(stream, codec, &img) &&
	opj_set_decode_area(codec, img, srcX, srcY,
			    srcX + width, srcY + height)) {
................................................................................
	    }
	    Tk_PhotoExpand(interp, imgHandle,
			   destX + blk.width, destY + blk.height);
	    Tk_PhotoPutBlock_NoComposite(imgHandle, &blk, destX, destY,
					 blk.width, blk.height);
	}
    }
done:
    if (codec != NULL) {
	opj_destroy_codec(codec);
    }
    if (img != NULL) {
	opj_image_destroy(img);
    }
    return result;
}

static int
CommonWriteJP2(
    Tcl_Interp *interp,
    opj_stream_t *stream,
    Tcl_Obj *fmtObj,
    Tk_PhotoImageBlock *blkPtr)
{
    int x, y, result = TCL_ERROR;
    opj_cparameters_t par;
    opj_codec_t *codec;
    opj_image_t *img = NULL;
    opj_image_cmptparm_t comps[3];

    memset(&par, 0, sizeof(par));
    opj_set_default_encoder_parameters(&par);
    par.tcp_mct = 255;
    if (fmtObj != NULL) {
	char *fmtStr, *p, *q;
	int len, n;

	fmtStr = Tcl_GetString(fmtObj);
	p = strchr(fmtStr, '/');
	while (p != NULL) {
	    len = strlen(p);
	    if (len > 3 && strncmp(p, "/r:", 3) == 0) {
		n = 0;
		q = p + 3;
		while (n < sizeof(par.tcp_rates) / sizeof(par.tcp_rates[0])) {
		    char *end = q;
		    double d;

		    d = strtod(q, &end);
		    if (end == NULL || end == q) {
			break;
		    }
		    if (d < 1) {
			d = 1;
		    }
		    par.tcp_rates[n++] = d;
		    q = end + 1;
		}
		par.tcp_numlayers = n;
		par.cp_disto_alloc = 1;
		par.cp_fixed_quality = 0;
		memset(par.tcp_distoratio, 0, sizeof(par.tcp_distoratio));
	    } else if (len > 3 && strncmp(p, "/q:", 3) == 0) {
		n = 0;
		q = p + 3;
		while (n < sizeof(par.tcp_distoratio) /
		       sizeof(par.tcp_distoratio[0])) {
		    char *end = q;
		    double d;

		    d = strtod(q, &end);
		    if (end == NULL || end == q) {
			break;
		    }
		    par.tcp_distoratio[n++] = d;
		    q = end + 1;
		}
		par.tcp_numlayers = n;
		par.cp_fixed_quality = 1;
		par.cp_disto_alloc = 0;
		memset(par.tcp_rates, 0, sizeof(par.tcp_rates));
	    } else if (len > 1 && strncmp(p, "/I", 2) == 0) {
		par.irreversible = 1;
	    }
	    p = strchr(p + 1, '/');
	}
    }
    codec = opj_create_compress(OPJ_CODEC_JP2);
    if (codec == NULL) {
	Tcl_SetResult(interp, "error creating codec", TCL_STATIC);
	goto done;
    }
    opj_set_info_handler(codec, LogHandler, 0);
    opj_set_warning_handler(codec, LogHandler, 0);
    opj_set_error_handler(codec, LogHandler, 0);
    memset(comps, 0, sizeof(comps));
    comps[0].prec = comps[0].bpp = 8;
    comps[0].sgnd = 0;
    comps[0].dx = comps[0].dy = 1;
................................................................................
	Tcl_SetResult(interp, "encoding failed", TCL_STATIC);
    } else if (!opj_end_compress(codec, stream)) {
	Tcl_SetResult(interp, "end compress failed", TCL_STATIC);
    } else {
	result = TCL_OK;
    }
done:
    if (codec != NULL) {
	opj_destroy_codec(codec);
    }
    if (img != NULL) {
	opj_image_destroy(img);
    }
    return result;
}

static OPJ_SIZE_T
ChanRead(void *buffer, OPJ_SIZE_T nbytes, void *udata)
{
    Tcl_Channel chan = (Tcl_Channel) udata;