File: bmjpeg.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (757 lines) | stat: -rw-r--r-- 18,582 bytes parent folder | download
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#   include	"bitmapConfig.h"

#   include	<stdlib.h>
#   include	<stdio.h>
#   include	<sioStdio.h>
#   include	"bmintern.h"
#   include	<time.h>

#   include	<jpeglib.h>
#   include	<setjmp.h>
#   include	"jerror.h"

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Declare additional routines.					*/
/*									*/
/************************************************************************/

#   define INPUT_BUF_SIZE	4096
#   define OUTPUT_BUF_SIZE	4096

typedef struct my_error_mgr
    {
    struct jpeg_error_mgr pub;	/* "public" fields */
    jmp_buf setjmp_buffer;	/* for return to caller */
    } my_error_mgr;

static void my_error_exit (j_common_ptr cinfo)
{
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  my_error_mgr * myerr = (my_error_mgr *) cinfo->err;

  /* Always display the message. */
  /* We could postpone this until after returning, if we chose. */
  (*cinfo->err->output_message) (cinfo);

  /* Return control to the setjmp point */
  longjmp(myerr->setjmp_buffer, 1);
}

typedef struct BmJpegInputSource
    {
    struct jpeg_source_mgr	pub;		/* public fields	*/

    BitmapDescription		bjisBd;
    unsigned char *		bjisBitmapBuffer;
    SimpleInputStream *		bjisSis;
    int				bjisRowsReceived;

    JOCTET *			bjisReadBuffer;
    int				bjisAtBeginOfInput;
    } BmJpegInputSource;

static int read_JPEG_file (	BmJpegInputSource * bjis );

/************************************************************************/

typedef struct BmJpegOutputDestination
    {
    struct jpeg_destination_mgr	pub;		/* public fields	*/

    const BitmapDescription *	bjodBd;
    const unsigned char *	bjodBuffer;
    SimpleOutputStream *	bjodSos;

    unsigned char *		bjodScratchBuffer;
    JOCTET *			bjodWriteBuffer;
    } BmJpegOutputDestination;

typedef struct
    {
    struct jpeg_destination_mgr	pub;		/* public fields	*/
    BmJpegOutputDestination *	bjod;		/* destination stream	*/
    JOCTET *			buffer;		/* start of buffer	*/
    } my_destination_mgr;

typedef my_destination_mgr * my_dest_ptr;

static int write_JPEG_file (	BmJpegOutputDestination *	bjod );

/**/

static void my_error_exit (j_common_ptr cinfo);

/************************************************************************/
/*									*/
/*  Read  a JPEG File.							*/
/*									*/
/************************************************************************/

static void bmJpegStartInputSource(	j_decompress_ptr	cinfo )
    {
    BmJpegInputSource *		bjis= (BmJpegInputSource *)cinfo->src;

    bjis->bjisAtBeginOfInput= TRUE;

    return;
    }

static void bmJpegTerminateInputSource(	j_decompress_ptr	cinfo )
    {
    return;
    }

static boolean bmJpegFillInputBuffer(	j_decompress_ptr	cinfo )
    {
    BmJpegInputSource *		bjis= (BmJpegInputSource *)cinfo->src;
    size_t			nbytes;

    nbytes= sioInReadBytes( bjis->bjisSis,
				bjis->bjisReadBuffer, INPUT_BUF_SIZE );

    if  ( nbytes <= 0 )
	{
	if  ( bjis->bjisAtBeginOfInput )	/* Treat empty input	*/
						/* file as fatal error	*/
	    { ERREXIT( cinfo, JERR_INPUT_EMPTY );	}

	WARNMS(cinfo, JWRN_JPEG_EOF);

	/* Insert a fake EOI marker */
	bjis->bjisReadBuffer[0] = (JOCTET) 0xFF;
	bjis->bjisReadBuffer[1] = (JOCTET) JPEG_EOI;
	nbytes = 2;
	}

    bjis->pub.next_input_byte= bjis->bjisReadBuffer;
    bjis->pub.bytes_in_buffer= nbytes;
    bjis->bjisAtBeginOfInput= FALSE;

    return TRUE;
    }

static void bmJpegSkipInputData(	j_decompress_ptr	cinfo,
					long			num_bytes )
    {
    BmJpegInputSource *		bjis= (BmJpegInputSource *)cinfo->src;

    if  ( num_bytes > 0 )
	{
	while( num_bytes > (long) bjis->pub.bytes_in_buffer )
	    {
	    num_bytes -= (long) bjis->pub.bytes_in_buffer;
	    (void) bmJpegFillInputBuffer( cinfo );
	    }

	bjis->pub.next_input_byte += (size_t) num_bytes;
	bjis->pub.bytes_in_buffer -= (size_t) num_bytes;
	}
    }

/************************************************************************/
/*									*/
/*  Copy file information to the communication data areas		*/
/*									*/
/************************************************************************/

static int bmJpegGetReadParameters( struct jpeg_decompress_struct * cinfo )
    {
    BmJpegInputSource *		bjis= (BmJpegInputSource *)cinfo->src;

    bjis->bjisBd.bdPixelsWide= cinfo->image_width;
    bjis->bjisBd.bdPixelsHigh= cinfo->image_height;
    bjis->bjisBd.bdBitsPerSample= cinfo->data_precision;

    switch( cinfo->density_unit )
	{
	case 0:
	    /* LDEB(cinfo->density_unit); */
	    if  ( cinfo->X_density < 5 && cinfo->Y_density < 5 )
		{
		bjis->bjisBd.bdUnit= BMunPIXEL;
		bjis->bjisBd.bdXResolution= 1;
		bjis->bjisBd.bdYResolution= 1;
		}
	    else{
		bjis->bjisBd.bdUnit= BMunINCH;
		bjis->bjisBd.bdXResolution= cinfo->X_density;
		bjis->bjisBd.bdYResolution= cinfo->Y_density;
		}
	    break;

	case 1:
	    bjis->bjisBd.bdUnit= BMunINCH;
	    bjis->bjisBd.bdXResolution= cinfo->X_density;
	    bjis->bjisBd.bdYResolution= cinfo->Y_density;
	    break;

	case 2:
	    bjis->bjisBd.bdUnit= BMunM;
	    bjis->bjisBd.bdXResolution= 100* cinfo->X_density;
	    bjis->bjisBd.bdYResolution= 100* cinfo->Y_density;
	    break;

	default:
	    LDEB(cinfo->density_unit);
	    return -1;
	    break;
	}

    switch( cinfo->out_color_space )
	{
	case JCS_GRAYSCALE:
	    bjis->bjisBd.bdColorEncoding= BMcoWHITEBLACK;
	    bjis->bjisBd.bdSamplesPerPixel= 1;
	    bjis->bjisBd.bdBitsPerPixel= bjis->bjisBd.bdBitsPerSample;
	    break;

	case JCS_RGB:
	    if  ( cinfo->jpeg_color_space == JCS_GRAYSCALE )
		{
		cinfo->out_color_space= cinfo->jpeg_color_space;

		bjis->bjisBd.bdColorEncoding= BMcoWHITEBLACK;
		bjis->bjisBd.bdSamplesPerPixel= 1;
		bjis->bjisBd.bdBitsPerPixel= bjis->bjisBd.bdBitsPerSample;
		}
	    else{
		bjis->bjisBd.bdColorEncoding= BMcoRGB;
		bjis->bjisBd.bdSamplesPerPixel= 3;
		bjis->bjisBd.bdBitsPerPixel= 3* bjis->bjisBd.bdBitsPerSample;
		}
	    break;

	case JCS_UNKNOWN:
	case JCS_YCbCr:
	case JCS_YCCK:
	case JCS_CMYK:
	    LDEB(cinfo->out_color_space);
	    return -1;

	default:
	    LDEB(cinfo->out_color_space);
	    return -1;
	}

    bjis->bjisBd.bdBytesPerRow= (	bjis->bjisBd.bdPixelsWide*
					bjis->bjisBd.bdBitsPerPixel+ 7 )/8;
    bjis->bjisBd.bdBufferLength=	bjis->bjisBd.bdPixelsHigh*
					bjis->bjisBd.bdBytesPerRow;

    bjis->bjisBitmapBuffer= (unsigned char *)
				malloc( bjis->bjisBd.bdBufferLength );
    if  ( ! bjis->bjisBitmapBuffer )
	{
	LLDEB(bjis->bjisBd.bdBufferLength,bjis->bjisBitmapBuffer);
	return -1;
	}

    bjis->bjisRowsReceived= 0;

    return 0;
    }

/************************************************************************/
/*									*/
/*  Store one row of pixels.						*/
/*									*/
/************************************************************************/

static int bmJpegStoreScanline( JSAMPARRAY			pixel_data,
				struct jpeg_decompress_struct * cinfo	)
    {
    BmJpegInputSource *		bjis= (BmJpegInputSource *)cinfo->src;

    unsigned char *		to;
    register JSAMPROW		ptr0;
    long			col;

    switch( cinfo->out_color_space )
	{
	case JCS_GRAYSCALE:
	    switch( bjis->bjisBd.bdBitsPerSample )
		{
		case 8:
		    to= bjis->bjisBitmapBuffer+
			bjis->bjisBd.bdBytesPerRow*
					    (bjis->bjisRowsReceived++);

		    ptr0 = pixel_data[0];

		    for ( col = 0; col < cinfo->image_width; col++ )
			{ *(to++)= GETJSAMPLE(*(ptr0++));	}
		    break;
		default:
		    LDEB(bjis->bjisBd.bdBitsPerSample); 
		    return -1;
		}
	    break;

	case JCS_RGB:
	    switch( bjis->bjisBd.bdBitsPerSample )
		{
		case 8:
		    to= bjis->bjisBitmapBuffer+
			bjis->bjisBd.bdBytesPerRow*
					    (bjis->bjisRowsReceived++);

		    ptr0 = pixel_data[0];

		    for ( col = 0; col < cinfo->image_width; col++ )
			{
			*(to++)= GETJSAMPLE(*(ptr0++));	/* red */
			*(to++)= GETJSAMPLE(*(ptr0++));	/* green */
			*(to++)= GETJSAMPLE(*(ptr0++));	/* blue */
			}
		    break;
		default:
		    LDEB(bjis->bjisBd.bdBitsPerSample); 
		    return -1;
		}
	    break;

	case JCS_UNKNOWN:
	case JCS_YCbCr:
	case JCS_YCCK:
	case JCS_CMYK:
	    LDEB(cinfo->out_color_space);
	    return -1;
	    break;

	default:
	    LDEB(cinfo->out_color_space);
	    return -1;
	    break;
	}

    return 0;
    }

int bmJpegReadJfif(	BitmapDescription *	bd,
			unsigned char **	pBuffer,
			SimpleInputStream *	sis )
    {
    BmJpegInputSource	bjis;

    bmInitDescription( &(bjis.bjisBd) );

    bjis.bjisBitmapBuffer= (unsigned char *)0;
    bjis.bjisSis= (SimpleInputStream *)0;
    bjis.bjisRowsReceived= 0;

    bjis.pub.init_source= bmJpegStartInputSource;
    bjis.pub.fill_input_buffer= bmJpegFillInputBuffer;
    bjis.pub.skip_input_data= bmJpegSkipInputData;
    bjis.pub.resync_to_restart= jpeg_resync_to_restart;
					/* use default method		*/
    bjis.pub.term_source= bmJpegTerminateInputSource;
    bjis.pub.bytes_in_buffer= 0;	/* forces fill_input_buffer	*/
					/* on first read		*/
    bjis.pub.next_input_byte = NULL;	/* until buffer loaded		*/

    bjis.bjisSis= sis;

    if  ( ! read_JPEG_file ( &bjis ) )
	{
	XDEB(sis);
	if  ( bjis.bjisBitmapBuffer )
	    { free( bjis.bjisBitmapBuffer );	}
	bmCleanDescription( &(bjis.bjisBd) );

	return -1;
	}

    *bd= bjis.bjisBd;
    *pBuffer= bjis.bjisBitmapBuffer;

    return 0;
    }

int bmReadJpegFile(	const char *		filename,
			unsigned char **	pBuffer,
			BitmapDescription *	bd,
			int *			pPrivateFormat,
			double *		pCompressionFactor )
    {
    SimpleInputStream *		sis;

    sis= sioInStdioOpen( filename );
    if  ( ! sis )
	{ SXDEB(filename,sis); return -1;	}

    if  ( bmJpegReadJfif( bd, pBuffer, sis ) )
	{ SDEB(filename); sioInClose( sis ); return -1; }

    *pPrivateFormat= 1;

    sioInClose( sis );

    return 0;
    }

static int read_JPEG_file(	BmJpegInputSource * bjis )
    {
    struct jpeg_decompress_struct	cinfo;
    struct my_error_mgr			jerr;

    JSAMPARRAY				buffer;		/* Output row buffer */
    int					row_stride;	/* physical row */
							/* width in output */
							/* buffer */

    cinfo.err= jpeg_std_error( &jerr.pub );
    jerr.pub.error_exit = my_error_exit;

    if  ( setjmp( jerr.setjmp_buffer ) )
	{
	jpeg_destroy_decompress( &cinfo );
	return 0;
	}

    jpeg_create_decompress( &cinfo );

    cinfo.src= (struct jpeg_source_mgr *)bjis;

    bjis->bjisReadBuffer= (JOCTET *)
      (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
				  INPUT_BUF_SIZE * sizeof(JOCTET));

    (void) jpeg_read_header( &cinfo, TRUE );

    bmJpegGetReadParameters( &cinfo );

    jpeg_start_decompress( &cinfo );

    row_stride= cinfo.output_width * cinfo.output_components;
    buffer= (*cinfo.mem->alloc_sarray)
		    ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1 );

    while( cinfo.output_scanline < cinfo.output_height )
	{
	(void)jpeg_read_scanlines( &cinfo, buffer, 1 );

	bmJpegStoreScanline( buffer, &cinfo );
	}

    (void)jpeg_finish_decompress( &cinfo );

    jpeg_destroy_decompress( &cinfo );

    return 1;
    }

/************************************************************************/
/*									*/
/*  Can the file be written in JPEG format?				*/
/*									*/
/************************************************************************/

int bmCanWriteJpegFile(	const BitmapDescription *	bd,
			int				PrivateFormat,
			double				CompressionFactor )
    {
    if  ( bd->bdBitsPerSample != 8 )
	{ /* LDEB(bd->bdBitsPerSample); */ return -1;	}

		    /* colorspace of input image */
    switch( bd->bdColorEncoding )
	{
	case BMcoBLACKWHITE:
	case BMcoWHITEBLACK:
	case BMcoRGB:
	case BMcoRGB8PALETTE:
	    break;
	default:
	    /* LDEB(bd->bdColorEncoding); */ return -1;
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Write JPEG File.							*/
/*									*/
/************************************************************************/

/************************************************************************/
/*									*/
/*  Output: derive the contents of the jpeg compressor struct from the	*/
/*  bitmap description.							*/
/*									*/
/************************************************************************/

static int bmJpegFillCompressor(	struct jpeg_compress_struct *	cinfo,
					const BitmapDescription *	bd )
    {
    if  ( bd->bdBitsPerSample != 8 )
	{ LDEB(bd->bdBitsPerSample); return -1;	}

    cinfo->image_width= bd->bdPixelsWide;
    cinfo->image_height= bd->bdPixelsHigh;

    cinfo->input_components= bd->bdSamplesPerPixel;

    switch( bd->bdColorEncoding )
	{
	case BMcoBLACKWHITE:
	case BMcoWHITEBLACK:
	    cinfo->in_color_space= JCS_GRAYSCALE;
	    break;
	case BMcoRGB:
	case BMcoRGB8PALETTE:
	    cinfo->in_color_space= JCS_RGB;
	    break;
	default:
	    LDEB(bd->bdColorEncoding);
	    return -1;
	}

    jpeg_set_defaults( cinfo );

    switch( bd->bdUnit )
	{
	case BMunINCH:
	    cinfo->density_unit= 1;
	    cinfo->X_density= bd->bdXResolution;
	    cinfo->Y_density= bd->bdYResolution;
	    break;

	case BMunM:
	    cinfo->density_unit= 2;
	    cinfo->X_density= ( bd->bdXResolution+ 50 )/ 100;
	    cinfo->Y_density= ( bd->bdYResolution+ 50 )/ 100;
	    break;

	case BMunPOINT:
	    cinfo->density_unit= 1;
	    cinfo->X_density= 72* bd->bdXResolution;
	    cinfo->Y_density= 72* bd->bdYResolution;
	    break;

	case BMunPIXEL:
	    break;

	default:
	    LDEB(bd->bdUnit);
	    /************************************/
	    /*  giving nothing works for jpeg.	*/
	    /************************************/
	    break;
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Output: Handle one row to the jpeg compressor.			*/
/*									*/
/************************************************************************/

static int bmJpegCompressRow(	struct jpeg_compress_struct *	cinfo,
				BmJpegOutputDestination *	bjod )
    {
    JSAMPROW			row_pointer[1];
    const unsigned char *	from;
    unsigned char *		to;
    int				i;
    int				res;

    const BitmapDescription *	bd= bjod->bjodBd;
    const unsigned char *	buffer= bjod->bjodBuffer;

    from= &(buffer[cinfo->next_scanline* bd->bdBytesPerRow]);

    switch( bd->bdColorEncoding )
	{
	case BMcoBLACKWHITE:
	    to= bjod->bjodScratchBuffer;
	    for ( i= 0; i < bd->bdBytesPerRow; to++, from++, i++ )
		{ *to= 255- *from; }

	    row_pointer[0]= (JSAMPROW)bjod->bjodScratchBuffer;
	    res= jpeg_write_scanlines( cinfo, row_pointer, 1 );
	    break;

	case BMcoRGB8PALETTE:
	    bmExpandRGB8Palette( bjod->bjodScratchBuffer, from,
				    bd->bdPixelsWide, bd->bdBitsPerPixel,
				    bd->bdRGB8Palette, 0 );

	    row_pointer[0]= (JSAMPROW)bjod->bjodScratchBuffer;
	    res= jpeg_write_scanlines( cinfo, row_pointer, 1 );
	    break;

	default:
	    row_pointer[0]= (JSAMPROW)from;
	    res= jpeg_write_scanlines( cinfo, row_pointer, 1 );
	    break;
	}

    if  ( res != 1 )
	{ LDEB(res); return -1;	}

    return 0;
    }

static void bmJpegInitDestination(	j_compress_ptr	cinfo )
    {
    BmJpegOutputDestination *	bjod= (BmJpegOutputDestination *)cinfo->dest;

    bjod->bjodWriteBuffer = (JOCTET *)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
					  OUTPUT_BUF_SIZE * sizeof(JOCTET));

    bjod->pub.next_output_byte= bjod->bjodWriteBuffer;
    bjod->pub.free_in_buffer= OUTPUT_BUF_SIZE;

    return;
    }

static boolean bmJpegWriteOutputBuffer(	j_compress_ptr	cinfo )
    {
    BmJpegOutputDestination *	bjod= (BmJpegOutputDestination *)cinfo->dest;

    if  ( sioOutWriteBytes( bjod->bjodSos, bjod->bjodWriteBuffer,
			    OUTPUT_BUF_SIZE ) != (size_t) OUTPUT_BUF_SIZE )
	{ ERREXIT(cinfo, JERR_FILE_WRITE); }

    bjod->pub.next_output_byte= bjod->bjodWriteBuffer;
    bjod->pub.free_in_buffer= OUTPUT_BUF_SIZE;

    return TRUE;
    }

static void bmJpegFinishDestination(	j_compress_ptr	cinfo )
    {
    BmJpegOutputDestination *	bjod= (BmJpegOutputDestination *)cinfo->dest;
    size_t			datacount;

    datacount= OUTPUT_BUF_SIZE- bjod->pub.free_in_buffer;

    if  ( datacount > 0 )
	{
	if  ( sioOutWriteBytes( bjod->bjodSos, bjod->bjodWriteBuffer,
						    datacount ) != datacount )
	    { ERREXIT( cinfo, JERR_FILE_WRITE );	}
	}

    return;
    }

int bmJpegWriteJfif(	const BitmapDescription *	bd,
			const unsigned char *		buffer,
			SimpleOutputStream *		sos )
    {
    BmJpegOutputDestination	bjod;

    bjod.bjodBd= bd;
    bjod.bjodBuffer= buffer;
    bjod.bjodSos= sos;
    bjod.bjodScratchBuffer= (unsigned char *)0;

    bjod.pub.init_destination= bmJpegInitDestination;
    bjod.pub.empty_output_buffer= bmJpegWriteOutputBuffer;
    bjod.pub.term_destination= bmJpegFinishDestination;

    switch( bd->bdColorEncoding )
	{
	case BMcoBLACKWHITE:
	    bjod.bjodScratchBuffer= malloc( bd->bdBytesPerRow );
	    if  ( ! bjod.bjodScratchBuffer )
		{
		LXDEB(bd->bdBytesPerRow,bjod.bjodScratchBuffer);
		return -1;
		}
	    break;

	case BMcoRGB8PALETTE:
	    bjod.bjodScratchBuffer= malloc( 3* bd->bdPixelsWide );
	    if  ( ! bjod.bjodScratchBuffer )
		{
		LXDEB(bd->bdBytesPerRow,bjod.bjodScratchBuffer);
		return -1;
		}
	    break;

	default:
	    break;
	}

    if  ( write_JPEG_file( &bjod ) )
	{
	LDEB(1);

	if  ( bjod.bjodScratchBuffer )
	    { free( bjod.bjodScratchBuffer );	}

	return -1;
	}

    if  ( bjod.bjodScratchBuffer )
	{ free( bjod.bjodScratchBuffer );	}

    return 0;
    }

int bmWriteJpegFile(	const char *			filename,
			const unsigned char *		buffer,
			const BitmapDescription *	bd,
			int				privateFormat,
			double				compressionFactor )
    {
    SimpleOutputStream *	sos;

    sos= sioOutStdioOpen( filename );
    if  ( ! sos )
	{ SXDEB(filename,sos); return -1;	}

    if  ( bmJpegWriteJfif( bd, buffer, sos ) )
	{ SDEB(filename); sioOutClose( sos ); return -1; }

    sioOutClose( sos );

    return 0;
    }

static int
write_JPEG_file(	BmJpegOutputDestination *	bjod )
    {
    struct jpeg_compress_struct		cinfo;
    struct jpeg_error_mgr		jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);

#   if 0
    jpeg_sio_dest( &cinfo, bjod );
#   else
    cinfo.dest= (struct jpeg_destination_mgr *)bjod;
#   endif

    if  ( bmJpegFillCompressor( &cinfo, bjod->bjodBd ) )
	{ LDEB(1); return -1;	}

#   if 0
    jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
#   endif

    jpeg_start_compress( &cinfo, TRUE );

    while( cinfo.next_scanline < cinfo.image_height )
	{
	if( bmJpegCompressRow( &cinfo, bjod ) )
	  { LDEB(1); return -1;	}
	}

    jpeg_finish_compress( &cinfo );

    jpeg_destroy_compress( &cinfo );

    return 0;
    }