File: img2jpc.c

package info (click to toggle)
ayttm 0.6.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 8,372 kB
  • ctags: 8,851
  • sloc: ansic: 65,755; sh: 10,810; cpp: 3,092; makefile: 564; yacc: 294; lex: 53; sed: 16
file content (178 lines) | stat: -rw-r--r-- 4,456 bytes parent folder | download | duplicates (2)
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
/*
 * Img2JPC Plugin for Ayttm - codec for jpg2000 images.
 */

#include "intl.h"
#ifdef __MINGW32__
#define __IN_PLUGIN__ 1
#endif
#include "plugin_api.h"
#include "image_window.h"
#include "mem_util.h"
#include "debug.h"

#include <jasper/jasper.h>

/*******************************************************************************
 *                             Begin Module Code
 ******************************************************************************/
/*  Module defines */
#ifndef USE_POSIX_DLOPEN
#define plugin_info img2jpc_LTX_plugin_info
#define module_version img2jpc_LTX_module_version
#endif

static int plugin_init();
static int plugin_finish();

/*  Module Exports */
PLUGIN_INFO plugin_info = {
	PLUGIN_FILTER,
	"Img2JPC",
	"Codec for JPG2000 images",
	"$Revision: 1.10 $",
	"$Date: 2009/09/17 12:04:58 $",
	NULL,
	plugin_init,
	plugin_finish,
	NULL,
	NULL
};

/* End Module Exports */

unsigned int module_version()
{
	return CORE_VERSION;
}

static unsigned char *img_2_jpg(const unsigned char *in_img, long *size);
static unsigned char *img_2_jpc(const unsigned char *in_img, long *size);

static unsigned char *(*old_img_2_jpg) (const unsigned char *, long *) = NULL;
static unsigned char *(*old_img_2_jpc) (const unsigned char *, long *) = NULL;

static int do_jpc_encoding = 0;

static int plugin_init()
{
	input_list *il = g_new0(input_list, 1);

	eb_debug(DBG_MOD, "img2jpc\n");

	plugin_info.prefs = il;
	il->widget.checkbox.value = &do_jpc_encoding;
	il->name = "do_jpc_encoding";
	il->label = _("Send JPEG2000 images (creates huge data transfers)");
	il->type = EB_INPUT_CHECKBOX;

	if (!jas_init()) {
		old_img_2_jpg = image_2_jpg;
		image_2_jpg = img_2_jpg;

		old_img_2_jpc = image_2_jpc;
		image_2_jpc = img_2_jpc;
	}

	return 0;
}

static int plugin_finish()
{
	image_2_jpg = old_img_2_jpg;

	image_2_jpc = old_img_2_jpc;

	g_free(plugin_info.prefs);

	return 0;
}

/*******************************************************************************
 *                             End Module Code
 ******************************************************************************/

static unsigned char *img_2_img(const unsigned char *in_img, long *size,
	int outfmt, const char *soutfmt, const char *enc_opts)
{
	unsigned char *out_img = NULL;
	jas_stream_t *in, *out;
	jas_image_t *image;
	int infmt;

	/*
	   static int ctr;
	   char fn[100];
	   FILE *fp;
	   snprintf(fn, sizeof(fn), "ayttm-img-%03d.jpc", ctr++);
	   fp = fopen(fn, "wb");
	   if(fp) {
	   fwrite(in_img, 1, *size, fp);
	   fclose(fp);
	   }
	 */
	if (jas_init()) {
		eb_debug(DBG_MOD, "Could not init jasper\n");
		return ay_memdup(in_img, *size);
	}

	if (!(in = jas_stream_memopen((char *)in_img, *size))) {
		eb_debug(DBG_MOD, "Could not open jasper input stream\n");
		return ay_memdup(in_img, *size);
	}
	infmt = jas_image_getfmt(in);
	eb_debug(DBG_MOD, "Got input image format: %d %s\n", infmt,
		jas_image_fmttostr(infmt));
	if (infmt <= 0)
		return ay_memdup(in_img, *size);

	if (!strcmp(jas_image_fmttostr(infmt), soutfmt)) {
		/* image is already in correct format */
		jas_stream_close(in);
		return ay_memdup(in_img, *size);
	}

	if (!(image = jas_image_decode(in, infmt, NULL))) {
		eb_debug(DBG_MOD, "Could not decode image format\n");
		return ay_memdup(in_img, *size);
	}

	if (!(out = jas_stream_memopen((char *)out_img, 0))) {
		eb_debug(DBG_MOD, "Could not open output stream\n");
		return ay_memdup(in_img, *size);
	}

	eb_debug(DBG_MOD, "Encoding to format: %d %s\n", outfmt, soutfmt);
	if ((jas_image_encode(image, out, outfmt, (char *)enc_opts))) {
		eb_debug(DBG_MOD, "Could not encode image format\n");
		return ay_memdup(in_img, *size);
	}
	jas_stream_flush(out);

	*size = ((jas_stream_memobj_t *)out->obj_)->bufsize_;
	eb_debug(DBG_MOD, "Encoded size is: %ld\n", *size);
	jas_stream_close(in);
	out_img = ay_memdup(((jas_stream_memobj_t *)out->obj_)->buf_, *size);
	jas_stream_close(out);
	jas_image_destroy(image);

	return out_img;
}

static unsigned char *img_2_jpg(const unsigned char *in_img, long *size)
{
	static int outfmt;
	if (!outfmt)
		outfmt = jas_image_strtofmt("jpg");
	return img_2_img(in_img, size, outfmt, "jpg", NULL);
}

static unsigned char *img_2_jpc(const unsigned char *in_img, long *size)
{
	static int outfmt;
	if (!do_jpc_encoding)
		return ay_memdup(in_img, *size);
	if (!outfmt)
		outfmt = jas_image_strtofmt("jpc");
	return img_2_img(in_img, size, outfmt, "jpc", "rate=0.0219\nmode=real");
}