summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bifj.c
blob: 260372dc184d175b7a72858bb79a73a51114237b (plain)
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
Copyright (C) 2017-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4bifj.ctr
*/

/**	@file dk4bifj.c The dk4bifj module.
*/


#line 7 "dk4bifj.ctr"

#include "dk4conf.h"

#if	DK4_HAVE_JPEGLIB_H

#ifndef	DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef	DK4MAASZ_H_INCLUDED
#include "dk4maasz.h"
#endif

#ifndef	DK4BIFTY_H_INCLUDED
#include "dk4bifty.h"
#endif

#ifndef	DK4BIF_H_INCLUDED
#include "dk4bif.h"
#endif

#ifndef	BIF_H_INCLUDED
#include "bif.h"
#endif

#ifndef	JPEGLIB_H_INCLUDED
#include <jpeglib.h>
#define	JPEGLIB_H_INCLUDED 1
#endif

#if	DK4_HAVE_SETJMP_H
#ifndef	SETJMP_H_INCLUDED
#include <setjmp.h>
#define	SETJMP_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif




#line 53 "dk4bifj.ctr"



/**	Error manager for jpeglib.
*/
typedef struct {
	struct jpeg_error_mgr	errmgr;		/**< Default error manager. */
#if DK4_HAVE_SETJMP_H
	jmp_buf					jump_buf;	/**< Longjump buffer. */
#endif
} dk4_bif_jpeg_error_mgr;



/**	Routine called instead of simple exit on failures.
	@param	cinfo	Object containing address of error manager.
*/
static
void
dk4bifjpb_error_exit(j_common_ptr cinfo)
{
	dk4_bif_jpeg_error_mgr	*errmgr;	/* Error manager structure */
	

#line 76 "dk4bifj.ctr"
	errmgr = (dk4_bif_jpeg_error_mgr *)(cinfo->err);
	if (NULL != errmgr) {
		longjmp(errmgr->jump_buf, 1);
	}
	

#line 81 "dk4bifj.ctr"
}



void
dk4bifjpeg_nc_release_per_frame_data(
	void			*tsdata,
	dk4_bif_frame_t	*frptr
)
{
	JSAMPLE						**rptr;		/* Array of row pointers */
	dk4_bif_jpeg_per_frame_t	 *jfptr;	/* Per-frame data */
	JDIMENSION					  i;		/* Traverse all lines */

	

#line 96 "dk4bifj.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != tsdata);
	assert(NULL != frptr);
#endif
	jfptr = (dk4_bif_jpeg_per_frame_t *)tsdata;
	if (NULL != jfptr) {
		rptr = jfptr->rows;
		if (NULL != rptr) {	

#line 104 "dk4bifj.ctr"
			for (i = 0; (dk4_bif_dim_t)i < frptr->dim_y; i++) {
				

#line 106 "dk4bifj.ctr"
				if (NULL != *rptr) { dk4mem_free(*rptr); }
				rptr++;
			}
			

#line 110 "dk4bifj.ctr"
			dk4mem_free(jfptr->rows);
		}
		

#line 113 "dk4bifj.ctr"
		dk4mem_free(jfptr);
	}
	

#line 116 "dk4bifj.ctr"
}



/**	Transfer information from JPEG structures into BIF structures.
	@param	bifp	Bitmap image file to modify.
	@param	pfd		Per-frame data structure to modify.
	@param	cinfo	JPEG decompression structure (data source).
	@param	sov		Flag: Set output variables in cinfo.
*/
static
void
dk4bifjpeg_transfer_header_data(
	dk4_bif_t						*bifp,
	dk4_bif_jpeg_per_frame_t		*pfd,
	struct jpeg_decompress_struct	*cinfo,
	int								 sov
)
{
	

#line 136 "dk4bifj.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bifp);
	assert(NULL != pfd);
	assert(NULL != cinfo);
#endif
	bifp->cf->dim_x		= cinfo->image_width;
	

#line 143 "dk4bifj.ctr"
	bifp->cf->dim_y		= cinfo->image_height;
	

#line 145 "dk4bifj.ctr"
	bifp->cf->l_bdepth		= BITS_IN_JSAMPLE;
	

#line 147 "dk4bifj.ctr"
	bifp->cf->r_bdepth		= BITS_IN_JSAMPLE;
	

#line 149 "dk4bifj.ctr"
	

#line 150 "dk4bifj.ctr"
	bifp->cf->bg[0]			= MAXJSAMPLE;
	bifp->cf->bg[1]			= MAXJSAMPLE;
	bifp->cf->bg[2]			= MAXJSAMPLE;
	bifp->cf->bg[3]			= MAXJSAMPLE;
	bifp->cf->bglbd[0]		= MAXJSAMPLE;
	bifp->cf->bglbd[1]		= MAXJSAMPLE;
	bifp->cf->bglbd[2]		= MAXJSAMPLE;
	bifp->cf->bglbd[3]		= MAXJSAMPLE;
	dk4pxres_setup(&(bifp->cf->resampler), MAXJSAMPLE, MAXJSAMPLE);
	bifp->cf->n_comp			= cinfo->out_color_components;
	

#line 161 "dk4bifj.ctr"
	bifp->cf->hbg			= 0;
	bifp->cf->res_x			= -1.0;
	bifp->cf->res_y			= -1.0;
	if (0 != cinfo->saw_JFIF_marker) {
		if ((0 < cinfo->X_density) && (0 < cinfo->Y_density)) {
			bifp->cf->res_x = (double)(cinfo->X_density);
			bifp->cf->res_y = (double)(cinfo->Y_density);
			

#line 169 "dk4bifj.ctr"
			

#line 170 "dk4bifj.ctr"
			if (2 == cinfo->density_unit) {	

#line 171 "dk4bifj.ctr"
				bifp->cf->res_x *= 2.54;
				bifp->cf->res_y *= 2.54;
				

#line 174 "dk4bifj.ctr"
				

#line 175 "dk4bifj.ctr"
			}
			if (1 != cinfo->density_unit) {
				bifp->cf->res_x = -1.0;
				bifp->cf->res_y = -1.0;
			}
		}
	}
	switch (cinfo->jpeg_color_space) {
		case JCS_GRAYSCALE: {	

#line 184 "dk4bifj.ctr"
			bifp->cf->l_cs =	DK4_CS_GRAY;
			bifp->cf->f_cs =	DK4_CS_GRAY;
			if (0 != sov) {
				cinfo->out_color_space = JCS_GRAYSCALE;
			}
		} break;
		case JCS_CMYK: {		

#line 191 "dk4bifj.ctr"
			bifp->cf->l_cs =	DK4_CS_CMYK;
			bifp->cf->f_cs =	DK4_CS_CMYK;
			if (0 != sov) {
				cinfo->out_color_space = JCS_CMYK;
			}
		} break;
		/*
			2018-03-20
			If the library internal color space is JCS_YCCK or JCS_YCbCr
			the library functions to access pixel data deliver RGB values.
			So the default branch sets the color space to RGB.
		*/
#if 0
		case JCS_RGB: {			

#line 205 "dk4bifj.ctr"
			bifp->cf->l_cs =	DK4_CS_RGB;
			bifp->cf->f_cs =	DK4_CS_RGB;
		} break;
		case JCS_YCCK: {		

#line 209 "dk4bifj.ctr"
			bifp->cf->l_cs =	DK4_CS_CMYK;
			bifp->cf->f_cs =	DK4_CS_CMYK;
		} break;
		case JCS_YCbCr: {		

#line 213 "dk4bifj.ctr"
			bifp->cf->l_cs =	DK4_CS_CMYK;
			bifp->cf->f_cs =	DK4_CS_CMYK;
		} break;
#endif
		default: {				

#line 218 "dk4bifj.ctr"
			bifp->cf->l_cs =	DK4_CS_RGB;
			bifp->cf->f_cs =	DK4_CS_RGB;
			if (0 != sov) {
				cinfo->out_color_space = JCS_RGB;
			}
		} break;
	}
	pfd->num_comp		= cinfo->out_color_components;
	

#line 227 "dk4bifj.ctr"
	pfd->is_adobe		= ((0 != cinfo->saw_Adobe_marker) ? 1 : 0);
	

#line 229 "dk4bifj.ctr"
	

#line 230 "dk4bifj.ctr"
}



dk4_bif_t *
dk4bifjpeg_nc_open_file(
	FILE						*fipo,
	dkChar	const				*fn,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_bif_jpg_clobbered_t		*jpgcp,
	dk4_er_t					*erp
)
{
	struct jpeg_decompress_struct	  cinfo;			/* Jpeglib reader */
	dk4_bif_jpeg_error_mgr			  errmgr;			/* Jpeglib error man */
	dk4_er_t						  er;				/* Error report */
	JSAMPLE							**xrptr	= NULL;		/* Travers rows */
	dk4_bif_jpeg_per_frame_t		 *pfd	= NULL;		/* Per-frame data */
	size_t							  bpl	= 0;		/* Bits per line */
	size_t							  i		= 0;		/* Traverse rows */
	

#line 252 "dk4bifj.ctr"

	/*	Create bif and frame for one-frame image
	*/
	jpgcp->back = dk4biftool_nc_new_for_one_frame(
		fn, DK4_BIF_TYPE_JPEG, honly, pcsctx, erp
	);
	if (NULL == jpgcp->back) { goto finished; }
	

#line 260 "dk4bifj.ctr"

	/*	Allocate per-frame data structure
	*/
	pfd = dk4mem_new(dk4_bif_jpeg_per_frame_t,1,NULL);
	if (NULL == pfd) { goto finished; }
	(jpgcp->back->cf)->tsdata = pfd;
	

#line 267 "dk4bifj.ctr"

	/*	Initialize per-frame data structure components
	*/
	pfd->rows = NULL;
	pfd->num_comp = 0;
	pfd->is_adobe = 0;

	/*	Set up error management
	*/
	cinfo.err = jpeg_std_error(&(errmgr.errmgr));
	errmgr.errmgr.error_exit = dk4bifjpb_error_exit;
	

#line 279 "dk4bifj.ctr"

	/*	Code below calls setjmp on error
	*/
	if (0 != setjmp(errmgr.jump_buf)) {	

#line 283 "dk4bifj.ctr"
		dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		goto finished;
	}

	/*	Create decompression structure
	*/
	jpgcp->st = 1;
	jpeg_create_decompress(&cinfo);
	

#line 292 "dk4bifj.ctr"

	/*	Specify data source
	*/
	jpeg_stdio_src(&cinfo, fipo);
	

#line 297 "dk4bifj.ctr"

	/*	Read JPEG header data
	*/
	jpeg_read_header(&cinfo, TRUE);
	

#line 302 "dk4bifj.ctr"

	/*	Transfer header data to bif structures.
	*/
	dk4bifjpeg_transfer_header_data(jpgcp->back, pfd, &cinfo, 1);
	

#line 307 "dk4bifj.ctr"

	/*	When reading just the header we are done here
	*/
	if (0 != honly) {
		jpgcp->ok = 1;
		

#line 313 "dk4bifj.ctr"
		goto finished;
	}

	/*	Start decompression
	*/
	jpeg_start_decompress(&cinfo);
	

#line 320 "dk4bifj.ctr"

	/*	Header data are now corrected if necessary, read information again
	*/
	dk4bifjpeg_transfer_header_data(jpgcp->back, pfd, &cinfo, 0);
	

#line 325 "dk4bifj.ctr"

	/*	Allocate memory for image data
	*/
	dk4error_init(&er);
	bpl = dk4ma_size_t_mul(jpgcp->back->cf->dim_x, jpgcp->back->cf->n_comp, &er);
	if (DK4_E_NONE != er.ec) {
		dk4error_copy(erp, &er);
		goto finished;
	}
	pfd->rows = dk4mem_new(JSAMPROW,jpgcp->back->cf->dim_y,erp);
	if (NULL == pfd->rows) { goto finished; }
	xrptr = pfd->rows;
	for (i = 0; i < (size_t)(jpgcp->back->cf->dim_y); i++) { *(xrptr++) = NULL; }
	xrptr = pfd->rows;
	for (i = 0; i < (size_t)(jpgcp->back->cf->dim_y); i++) {
		*xrptr = dk4mem_new(JSAMPLE,bpl,erp);
		if (NULL == *xrptr) {
			goto finished;
		}
		xrptr++;
	}
	

#line 347 "dk4bifj.ctr"

	/* Read and decompress data
	*/
	xrptr = pfd->rows;
	for (i = 0; i < (size_t)(jpgcp->back->cf->dim_y); i++) {
		jpeg_read_scanlines(&cinfo, xrptr, 1);
		xrptr++;
	}
	

#line 356 "dk4bifj.ctr"

	/*	Finish decompression
	*/
	jpeg_finish_decompress(&cinfo);
	

#line 361 "dk4bifj.ctr"

	/*	Everything done, success
	*/
	jpgcp->ok = 1;

	finished:
	/*
		Destroy decompression structure
	*/
	if (0 < jpgcp->st) {
		jpeg_destroy_decompress(&cinfo);
	}
	if (0 == jpgcp->ok) {
		if (NULL != jpgcp->back) {
			dk4bif_close(jpgcp->back);
			jpgcp->back = NULL;
		}
	}

	

#line 381 "dk4bifj.ctr"
	return jpgcp->back;
}



int
dk4bifjpeg_nc_get_original_pixel(
	dk4_px_t				*dptr,
	size_t					 szdptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno
)
{
	dk4_er_t					 er;			/* Error report for math op */
	dk4_bif_jpeg_per_frame_t	*pfd	= NULL;	/* Per-frame data */
	size_t						 spos;			/* Number of values per row */
	size_t						 i;				/* Traverse all components */
	int							 back	= 0;
	

#line 401 "dk4bifj.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != dptr);
	assert(0 < szdptr);
	assert(NULL != bif);
#endif
	pfd = (dk4_bif_jpeg_per_frame_t *)((bif->cf)->tsdata);
	if (NULL != pfd) {
		if (NULL != pfd->rows) {
			dk4error_init(&er);
			spos = dk4ma_size_t_mul(colno, pfd->num_comp, &er);
			(void)dk4ma_size_t_add(spos, pfd->num_comp, &er);
			if (DK4_E_NONE == er.ec) {
				if (szdptr >= pfd->num_comp) {
					for (i = 0; i < pfd->num_comp; i++) {
						dptr[i] = ((pfd->rows)[rowno])[spos + i];
					}
					back = 1;
				}
			}
		}
	}
	

#line 423 "dk4bifj.ctr"
	return back;
}


#endif
/* if DK4_HAVE_JPEGLIB_H */


/* vim: set ai sw=4 ts=4 : */