-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxvjpeg.c
832 lines (601 loc) · 21 KB
/
xvjpeg.c
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*
* xvjpeg.c - i/o routines for 'jpeg' format pictures
*/
/* based in part on 'example.c' from the IJG JPEG distribution */
#include "copyright.h"
#include "xv.h"
#ifdef HAVE_JPEG
#include <setjmp.h>
#include "jpeglib.h"
#include "jerror.h"
#define CREATOR_STR "CREATOR: "
#if BITS_IN_JSAMPLE != 8
Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
#endif
/*** Stuff for JPEG Dialog box ***/
#define JWIDE 400
#define JHIGH 200
#define J_NBUTTS 2
#define J_BOK 0
#define J_BCANC 1
#define BUTTH 24
/* minimum size compression when doing a 'quick' image load. (of course, if
the image *is* smaller than this, you'll get whatever size it actually is.
This is currently hardcoded to be twice the size of a schnauzer icon, as
the schnauzer's the only thing that does a quick load... */
#define QUICKWIDE 160
#define QUICKHIGH 120
struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
typedef struct my_error_mgr *my_error_ptr;
/*** local functions ***/
static void drawJD PARM((int, int, int, int));
static void clickJD PARM((int, int));
static void doCmd PARM((int));
static void writeJPEG PARM((void));
METHODDEF(void) xv_error_exit PARM((j_common_ptr));
METHODDEF(void) xv_error_output PARM((j_common_ptr));
METHODDEF(void) xv_prog_meter PARM((j_common_ptr));
static unsigned int j_getc PARM((j_decompress_ptr));
METHODDEF(boolean) xv_process_comment PARM((j_decompress_ptr));
static int writeJFIF PARM((FILE *, byte *, int,int,int));
/*** local variables ***/
static char *filename;
static char *fbasename;
static char *comment;
static int colorType;
static DIAL qDial, smDial;
static BUTT jbut[J_NBUTTS];
/***************************************************************************/
/* JPEG SAVE DIALOG ROUTINES ***********************************************/
/***************************************************************************/
/***************************************************/
void CreateJPEGW()
{
jpegW = CreateWindow("xv jpeg","XVjpeg",NULL,JWIDE,JHIGH,infofg,infobg,0);
if (!jpegW) FatalError("can't create jpeg window!");
XSelectInput(theDisp, jpegW, ExposureMask | ButtonPressMask | KeyPressMask);
DCreate(&qDial, jpegW, 10, 10, 80, 100, 1.0, 100.0, 75.0, 1.0, 5.0,
infofg, infobg, hicol, locol, "Quality", "%");
DCreate(&smDial, jpegW, 120, 10, 80, 100, 0.0, 100.0, 0.0, 1.0, 5.0,
infofg, infobg, hicol, locol, "Smoothing", "%");
BTCreate(&jbut[J_BOK], jpegW, JWIDE-180-1, JHIGH-10-BUTTH-1, 80, BUTTH,
"Ok", infofg, infobg, hicol, locol);
BTCreate(&jbut[J_BCANC], jpegW, JWIDE-90-1, JHIGH-10-BUTTH-1, 80, BUTTH,
"Cancel", infofg, infobg, hicol, locol);
XMapSubwindows(theDisp, jpegW);
}
/***************************************************/
void JPEGDialog(vis)
int vis;
{
if (vis) {
CenterMapWindow(jpegW, jbut[J_BOK].x + (int) jbut[J_BOK].w/2,
jbut[J_BOK].y + (int) jbut[J_BOK].h/2, JWIDE, JHIGH);
}
else XUnmapWindow(theDisp, jpegW);
jpegUp = vis;
}
/***************************************************/
int JPEGCheckEvent(xev)
XEvent *xev;
{
/* check event to see if it's for one of our subwindows. If it is,
deal accordingly, and return '1'. Otherwise, return '0' */
int rv;
rv = 1;
if (!jpegUp) return 0;
if (xev->type == Expose) {
int x,y,w,h;
XExposeEvent *e = (XExposeEvent *) xev;
x = e->x; y = e->y; w = e->width; h = e->height;
/* throw away excess expose events for 'dumb' windows */
if (e->count > 0 && (e->window == qDial.win ||
e->window == smDial.win)) {}
else if (e->window == jpegW) drawJD(x, y, w, h);
else if (e->window == qDial.win) DRedraw(&qDial);
else if (e->window == smDial.win) DRedraw(&smDial);
else rv = 0;
}
else if (xev->type == ButtonPress) {
XButtonEvent *e = (XButtonEvent *) xev;
int x,y;
x = e->x; y = e->y;
if (e->button == Button1) {
if (e->window == jpegW) clickJD(x,y);
else if (e->window == qDial.win) DTrack(&qDial, x,y);
else if (e->window == smDial.win) DTrack(&smDial, x,y);
else rv = 0;
} /* button1 */
else rv = 0;
} /* button press */
else if (xev->type == KeyPress) {
XKeyEvent *e = (XKeyEvent *) xev;
char buf[128]; KeySym ks;
int stlen;
stlen = XLookupString(e,buf,128,&ks,(XComposeStatus *) NULL);
buf[stlen] = '\0';
RemapKeyCheck(ks, buf, &stlen);
if (e->window == jpegW) {
if (stlen) {
if (buf[0] == '\r' || buf[0] == '\n') { /* enter */
FakeButtonPress(&jbut[J_BOK]);
}
else if (buf[0] == '\033') { /* ESC */
FakeButtonPress(&jbut[J_BCANC]);
}
}
}
else rv = 0;
}
else rv = 0;
if (rv==0 && (xev->type == ButtonPress || xev->type == KeyPress)) {
XBell(theDisp, 50);
rv = 1; /* eat it */
}
return rv;
}
/***************************************************/
void JPEGSaveParams(fname, col)
char *fname;
int col;
{
filename = fname;
colorType = col;
}
/***************************************************/
static void drawJD(x,y,w,h)
int x,y,w,h;
{
char *title = "Save JPEG file...";
char *title1 = "Quality value determines";
char *title2 = "compression rate: higher";
char *title3 = "quality = bigger file.";
char *title4 = "Use smoothing if saving";
char *title5 = "an 8-bit image (eg, a GIF).";
char *qtitle1 = "Default = 75.";
char *qtitle2 = "Useful range";
char *qtitle3 = "is 5-95.";
char *smtitle1 = "Default = 0 (none).";
char *smtitle2 = "10-30 is enough";
char *smtitle3 = "for typical GIFs.";
int i;
XRectangle xr;
xr.x = x; xr.y = y; xr.width = w; xr.height = h;
XSetClipRectangles(theDisp, theGC, 0,0, &xr, 1, Unsorted);
XSetForeground(theDisp, theGC, infofg);
XSetBackground(theDisp, theGC, infobg);
for (i=0; i<J_NBUTTS; i++) BTRedraw(&jbut[i]);
DrawString(jpegW, 220, 10+ASCENT, title);
DrawString(jpegW, 230, 10+ASCENT+LINEHIGH*1, title1);
DrawString(jpegW, 230, 10+ASCENT+LINEHIGH*2, title2);
DrawString(jpegW, 230, 10+ASCENT+LINEHIGH*3, title3);
DrawString(jpegW, 230, 10+ASCENT+LINEHIGH*4, title4);
DrawString(jpegW, 230, 10+ASCENT+LINEHIGH*5, title5);
DrawString(jpegW, 15, 10+100+10+ASCENT, qtitle1);
DrawString(jpegW, 15, 10+100+10+ASCENT+LINEHIGH, qtitle2);
DrawString(jpegW, 15, 10+100+10+ASCENT+LINEHIGH*2, qtitle3);
DrawString(jpegW, 115, 10+100+10+ASCENT+LINEHIGH*0, smtitle1);
DrawString(jpegW, 115, 10+100+10+ASCENT+LINEHIGH*1, smtitle2);
DrawString(jpegW, 115, 10+100+10+ASCENT+LINEHIGH*2, smtitle3);
XSetClipMask(theDisp, theGC, None);
}
/***************************************************/
static void clickJD(x,y)
int x,y;
{
int i;
BUTT *bp;
/* check BUTTs */
for (i=0; i<J_NBUTTS; i++) {
bp = &jbut[i];
if (PTINRECT(x, y, bp->x, bp->y, bp->w, bp->h)) break;
}
if (i<J_NBUTTS) { /* found one */
if (BTTrack(bp)) doCmd(i);
}
}
/***************************************************/
static void doCmd(cmd)
int cmd;
{
switch (cmd) {
case J_BOK: {
char *fullname;
writeJPEG();
JPEGDialog(0);
fullname = GetDirFullName();
if (!ISPIPE(fullname[0])) {
XVCreatedFile(fullname);
StickInCtrlList(0);
}
}
break;
case J_BCANC: JPEGDialog(0); break;
default: break;
}
}
/*******************************************/
static void writeJPEG()
{
FILE *fp;
int i, nc, rv, w, h, ptype, pfree;
register byte *ip, *ep;
byte *inpix, *rmap, *gmap, *bmap;
byte *image8, *image24;
/* get the XV image into a format that the JPEG software can grok on.
Also, open the output file, so we don't waste time doing this format
conversion if we won't be able to write it out */
fp = OpenOutFile(filename);
if (!fp) return;
fbasename = BaseName(filename);
WaitCursor();
inpix = GenSavePic(&ptype, &w, &h, &pfree, &nc, &rmap, &gmap, &bmap);
image8 = image24 = (byte *) NULL;
/* monocity: see if the image is mono, save it that way to save space */
if (colorType != F_GREYSCALE) {
if (ptype == PIC8) {
for (i=0; i<nc && rmap[i]==gmap[i] && rmap[i]==bmap[i]; i++);
if (i==nc) colorType = F_GREYSCALE; /* made it all the way through */
}
else { /* PIC24 */
for (i=0,ip=inpix; i<w*h && ip[0]==ip[1] && ip[1]==ip[2]; i++,ip+=3);
if (i==w*h) colorType = F_GREYSCALE; /* all the way through */
}
}
/* first thing to do is build an 8/24-bit Greyscale/TrueColor image
(meaning: non-colormapped) */
if (colorType == F_GREYSCALE) { /* build an 8-bit Greyscale image */
image8 = (byte *) malloc((size_t) w * h);
if (!image8) FatalError("writeJPEG: unable to malloc image8\n");
if (ptype == PIC8) {
for (i=0,ip=image8,ep=inpix; i<w * h; i++, ip++, ep++)
*ip = MONO(rmap[*ep], gmap[*ep], bmap[*ep]);
}
else { /* PIC24 */
for (i=0,ip=image8,ep=inpix; i<w*h; i++, ip++, ep+=3)
*ip = MONO(ep[0],ep[1],ep[2]);
}
}
else { /* *not* F_GREYSCALE */
if (ptype == PIC8) {
image24 = (byte *) malloc((size_t) w * h * 3);
if (!image24) { /* this simply isn't going to work */
FatalError("writeJPEG: unable to malloc image24\n");
}
for (i=0, ip=image24, ep=inpix; i<w*h; i++, ep++) {
*ip++ = rmap[*ep];
*ip++ = gmap[*ep];
*ip++ = bmap[*ep];
}
}
else { /* PIC24 */
image24 = inpix;
}
}
/* in any event, we've got some valid image. Do the JPEG Thing */
rv = writeJFIF(fp, (colorType==F_GREYSCALE) ? image8 : image24,
w, h, colorType);
if (colorType == F_GREYSCALE) free(image8);
else if (ptype == PIC8) free(image24);
if (pfree) free(inpix);
if (CloseOutFile(fp, filename, rv) == 0) DirBox(0);
SetCursors(-1);
}
/***************************************************************************/
/* JPEG INTERFACE ROUTINES *************************************************/
/***************************************************************************/
/**************************************************/
METHODDEF(void) xv_error_exit(cinfo)
j_common_ptr cinfo;
{
my_error_ptr myerr;
myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message)(cinfo); /* display error message */
longjmp(myerr->setjmp_buffer, 1); /* return from error */
}
/**************************************************/
METHODDEF(void) xv_error_output(cinfo)
j_common_ptr cinfo;
{
my_error_ptr myerr;
char buffer[JMSG_LENGTH_MAX];
myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->format_message)(cinfo, buffer);
SetISTR(ISTR_WARNING, "%s: %s", fbasename, buffer); /* send it to XV */
}
/**************************************************/
METHODDEF(void) xv_prog_meter(cinfo)
j_common_ptr cinfo;
{
struct jpeg_progress_mgr *prog;
prog = cinfo->progress;
if ((prog->pass_counter & 0x3f)==0) WaitCursor();
#ifdef FOO
fprintf(stderr,"xv_prog_meter: cnt=%ld, maxcnt=%ld, pass=%d, maxpass=%d\n",
prog->pass_counter, prog->pass_limit, prog->completed_passes,
prog->total_passes);
#endif
}
/***************************************************************************/
/* LOAD ROUTINES ***********************************************************/
/***************************************************************************/
/*******************************************/
int LoadJFIF(fname, pinfo, quick)
char *fname;
PICINFO *pinfo;
int quick;
{
/* returns '1' on success, '0' on failure */
struct jpeg_decompress_struct cinfo;
struct jpeg_progress_mgr prog;
struct my_error_mgr jerr;
JSAMPROW rowptr[1];
FILE *fp;
static byte *pic;
long filesize;
int i,w,h,bperpix;
fbasename = BaseName(fname);
pic = (byte *) NULL;
comment = (char *) NULL;
pinfo->type = PIC8;
if ((fp = xv_fopen(fname, "r")) == NULL) return 0;
fseek(fp, 0L, 2);
filesize = ftell(fp);
fseek(fp, 0L, 0);
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = xv_error_exit;
jerr.pub.output_message = xv_error_output;
if (setjmp(jerr.setjmp_buffer)) {
/* if we're here, it blowed up... */
jpeg_destroy_decompress(&cinfo);
fclose(fp);
if (pic) free(pic);
if (comment) free(comment);
pinfo->pic = (byte *) NULL;
pinfo->comment = (char *) NULL;
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_set_marker_processor(&cinfo, JPEG_COM, xv_process_comment);
/* hook up progress meter */
prog.progress_monitor = xv_prog_meter;
cinfo.progress = &prog;
jpeg_stdio_src(&cinfo, fp);
(void) jpeg_read_header(&cinfo, TRUE);
/* do various cleverness regarding decompression parameters & such... */
jpeg_calc_output_dimensions(&cinfo);
w = cinfo.output_width;
h = cinfo.output_height;
pinfo->normw = w; pinfo->normh = h;
if (quick) {
int wfac, hfac, fac;
wfac = w / QUICKWIDE;
hfac = h / QUICKHIGH;
fac = wfac; if (fac > hfac) fac = hfac;
if (fac > 8) fac = 8;
else if (fac > 4) fac = 4;
else if (fac > 2) fac = 2;
else fac = 1;
cinfo.scale_num = 1;
cinfo.scale_denom = fac;
cinfo.dct_method = JDCT_FASTEST;
cinfo.do_fancy_upsampling = FALSE;
pinfo->normw = w; pinfo->normh = h;
jpeg_calc_output_dimensions(&cinfo);
w = cinfo.output_width;
h = cinfo.output_height;
}
if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
cinfo.out_color_space = JCS_GRAYSCALE;
cinfo.quantize_colors = FALSE;
SetISTR(ISTR_INFO,"Loading %dx%d Greyscale JPEG (%ld bytes)...",
w,h,filesize);
for (i=0; i<256; i++) pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i;
}
else {
cinfo.out_color_space = JCS_RGB;
cinfo.quantize_colors = FALSE; /* default: give 24-bit image to XV */
if (!quick && picType==PIC8 && conv24MB.flags[CONV24_LOCK] == 1) {
/*
* we're locked into 8-bit mode:
* if CONV24_FAST, use JPEG's one-pass quantizer
* if CONV24_SLOW, use JPEG's two-pass quantizer
* if CONV24_BEST, or other, ask for 24-bit image and hand it to XV
*/
cinfo.desired_number_of_colors = 256;
if (conv24 == CONV24_FAST || conv24 == CONV24_SLOW) {
cinfo.quantize_colors = TRUE;
state824=1; /* image was converted from 24 to 8 bits */
cinfo.two_pass_quantize = (conv24 == CONV24_SLOW);
}
}
SetISTR(ISTR_INFO,"Loading %dx%d Color JPEG (%ld bytes)...",
w,h,filesize);
}
jpeg_calc_output_dimensions(&cinfo); /* note colorspace changes... */
if (cinfo.output_components != 1 && cinfo.output_components != 3) {
SetISTR(ISTR_WARNING, "%s: can't read %d-plane JPEG file!",
fbasename, cinfo.output_components);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
if (comment) free(comment);
return 0;
}
bperpix = cinfo.output_components;
pinfo->type = (bperpix == 1) ? PIC8 : PIC24;
pic = (byte *) malloc((size_t) (w * h * bperpix));
if (!pic) {
SetISTR(ISTR_WARNING, "%s: can't read JPEG file - out of memory",
fbasename);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
if (comment) free(comment);
return 0;
}
jpeg_start_decompress(&cinfo);
while (cinfo.output_scanline < cinfo.output_height) {
rowptr[0] = (JSAMPROW) &pic[cinfo.output_scanline * w * bperpix];
(void) jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
}
/* return 'PICINFO' structure to XV */
pinfo->pic = pic;
pinfo->w = w;
pinfo->h = h;
pinfo->frmType = F_JPEG;
if (cinfo.out_color_space == JCS_GRAYSCALE) {
sprintf(pinfo->fullInfo, "Greyscale JPEG. (%ld bytes)", filesize);
pinfo->colType = F_GREYSCALE;
for (i=0; i<256; i++) pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i;
}
else {
sprintf(pinfo->fullInfo, "Color JPEG. (%ld bytes)", filesize);
pinfo->colType = F_FULLCOLOR;
if (cinfo.quantize_colors) {
for (i=0; i<cinfo.actual_number_of_colors; i++) {
pinfo->r[i] = cinfo.colormap[0][i];
pinfo->g[i] = cinfo.colormap[1][i];
pinfo->b[i] = cinfo.colormap[2][i];
}
}
}
sprintf(pinfo->shrtInfo, "%dx%d %s JPEG. ", w,h,
(cinfo.out_color_space == JCS_GRAYSCALE) ? "Greyscale " : "Color ");
pinfo->comment = comment;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
comment = (char *) NULL;
return 1;
}
/**************************************************/
static unsigned int j_getc(cinfo)
j_decompress_ptr cinfo;
{
struct jpeg_source_mgr *datasrc = cinfo->src;
if (datasrc->bytes_in_buffer == 0) {
if (! (*datasrc->fill_input_buffer) (cinfo))
ERREXIT(cinfo, JERR_CANT_SUSPEND);
}
datasrc->bytes_in_buffer--;
return GETJOCTET(*datasrc->next_input_byte++);
}
/**************************************************/
METHODDEF(boolean) xv_process_comment(cinfo)
j_decompress_ptr cinfo;
{
int length, hasnull;
unsigned int ch;
char *oldsp, *sp;
length = j_getc(cinfo) << 8;
length += j_getc(cinfo);
length -= 2; /* discount the length word itself */
if (!comment) {
comment = (char *) malloc((size_t) length + 1);
if (comment) comment[0] = '\0';
}
else comment = (char *) realloc(comment, strlen(comment) + length + 1);
if (!comment) FatalError("out of memory in xv_process_comment");
oldsp = sp = comment + strlen(comment);
hasnull = 0;
while (length-- > 0) {
ch = j_getc(cinfo);
*sp++ = (char) ch;
if (ch == 0) hasnull = 1;
}
if (hasnull) sp = oldsp; /* swallow comment blocks that have nulls */
*sp++ = '\0';
return TRUE;
}
/***************************************************************************/
/* WRITE ROUTINES **********************************************************/
/***************************************************************************/
static int writeJFIF(fp, pic, w,h, coltype)
FILE *fp;
byte *pic;
int w,h,coltype;
{
struct jpeg_compress_struct cinfo;
struct jpeg_progress_mgr prog;
struct my_error_mgr jerr;
JSAMPROW rowptr[1];
int i, bperpix;
char xvcmt[256];
comment = (char *) NULL;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = xv_error_exit;
jerr.pub.output_message = xv_error_output;
if (setjmp(jerr.setjmp_buffer)) {
/* if we're here, it blowed up... */
jpeg_destroy_compress(&cinfo);
if (picComments && comment) free(comment);
return 1;
}
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, fp);
cinfo.image_width = w;
cinfo.image_height = h;
if (coltype == F_GREYSCALE) {
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE;
}
else {
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
}
bperpix = cinfo.input_components;
prog.progress_monitor = xv_prog_meter;
cinfo.progress = &prog;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, (int)qDial.val, TRUE);
cinfo.smoothing_factor = (int)smDial.val;
jpeg_start_compress(&cinfo, TRUE);
/*** COMMENT HANDLING ***/
sprintf(xvcmt, "%sXV %s Quality = %d, Smoothing = %d\n",
CREATOR_STR, REVDATE, (int)qDial.val, (int)smDial.val);
if (picComments) { /* append XV comment */
char *sp, *sp1; int done;
i = strlen(picComments);
comment = (char *) malloc(i + strlen(xvcmt) + 2 + 1);
if (!comment) FatalError("out of memory in writeJFIF()");
strcpy(comment, picComments);
/* see if there's a line that starts with 'CREATOR: ' in the
comments. If there is, rip it out. */
sp = comment; done = 0;
while (!done && *sp) {
if (strncmp(sp, CREATOR_STR, strlen(CREATOR_STR)) == 0) {
sp1 = sp;
while (*sp1 && *sp1 != '\n') sp1++; /* find end of this line */
if (*sp1 == '\n') sp1++; /* move past \n */
/* move comments from sp1 and on down to sp */
xvbcopy(sp1, sp, strlen(sp1) + 1); /* +1 to copy the trailing \0 */
done = 1;
}
else { /* skip ahead to next line */
while (*sp && *sp != '\n') sp++;
if (*sp == '\n') sp++;
}
}
/* count # of \n's at end of comment.
If none, add 2. If one, add 1. If two or more, add none. */
sp = comment + strlen(comment);
for (i=0; i<3 && ((size_t) i < strlen(comment)); i++) {
sp--;
if (*sp != '\n') break;
}
for ( ; i<2; i++) strcat(comment, "\n");
strcat(comment, xvcmt);
}
else comment = xvcmt;
jpeg_write_marker(&cinfo,JPEG_COM,(byte *) comment,(u_int) strlen(comment));
while (cinfo.next_scanline < cinfo.image_height) {
rowptr[0] = (JSAMPROW) &pic[cinfo.next_scanline * w * bperpix];
(void) jpeg_write_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return 0;
}
#endif /* HAVE_JPEG */