-
Notifications
You must be signed in to change notification settings - Fork 4
/
epeg_main.c
1642 lines (1469 loc) · 44.7 KB
/
epeg_main.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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Epeg.h"
#include "epeg_private.h"
#include <jerror.h>
static Epeg_Image *_epeg_open_header (Epeg_Image *im);
static int _epeg_decode (Epeg_Image *im);
static int _epeg_scale (Epeg_Image *im);
static int _epeg_decode_for_trim (Epeg_Image *im);
static int _epeg_trim (Epeg_Image *im);
static int _epeg_encode (Epeg_Image *im);
static int _epeg_transform (Epeg_Image *im);
static void _epeg_fatal_error_handler (j_common_ptr cinfo);
#define MIN(__x,__y) ((__x) < (__y) ? (__x) : (__y))
#define MAX(__x,__y) ((__x) > (__y) ? (__x) : (__y))
static const JOCTET fake_EOI[2] = { 0xFF, JPEG_EOI };
/**
* Open a JPEG image by filename.
* @param file The file path to open.
* @return A handle to the opened JPEG file, with the header decoded.
*
* This function opens the file indicated by the @p file parameter, and
* attempts to decode it as a jpeg file. If this failes, NULL is returned.
* Otherwise a valid handle to an open JPEG file is returned that can be used
* by other Epeg calls.
*
* The @p file must be a pointer to a valid C string, NUL (0 byte) terminated
* thats is a relative or absolute file path. If not results are not
* determined.
*
* See also: epeg_memory_open(), epeg_close()
*/
EAPI Epeg_Image *
epeg_file_open(const char *file) {
Epeg_Image *im;
im = calloc(1, sizeof(Epeg_Image));
if (!im) {
return NULL;
}
im->in.file = strdup(file);
if (!im->in.file) {
free(im);
return NULL;
}
im->in.f = fopen(im->in.file, "rb");
if (!im->in.f) {
epeg_close(im);
return NULL;
}
fstat(fileno(im->in.f), &(im->stat_info));
im->out.quality = 75;
return _epeg_open_header(im);
}
/**
* Open a JPEG image stored in memory.
* @param data A pointer to the memory containing the JPEG data.
* @param size The size of the memory segment containing the JPEG.
* @return A handle to the opened JPEG, with the header decoded.
*
* This function opens a JPEG file that is stored in memory pointed to by
* @p data, and that is @p size bytes in size. If successful a valid handle
* is returned, or on failure NULL is returned.
*
* See also: epeg_file_open(), epeg_close()
*/
EAPI EAPI Epeg_Image *
epeg_memory_open(unsigned char *data, int size) {
Epeg_Image *im;
im = calloc(1, sizeof(Epeg_Image));
if (!im) {
return NULL;
}
im->out.quality = 75;
im->in.mem.data = (unsigned char **)data;
im->in.mem.size = size;
im->in.f = NULL;
im->in.w = 0;
im->in.h = 0;
return _epeg_open_header(im);
}
/**
* Return the original JPEG pixel size.
* @param im A handle to an opened Epeg image.
* @param w A pointer to the width value in pixels to be filled in.
* @param h A pointer to the height value in pixels to be filled in.
*
* Returns the image size in pixels.
*
*/
EAPI void
epeg_size_get(Epeg_Image *im, int *w, int *h) {
if (w) {
*w = im->in.w;
}
if (h) {
*h = im->in.h;
}
}
/**
* Return the original JPEG pixel color space.
* @param im A handle to an opened Epeg image.
* @param space A pointer to the color space value to be filled in.
*
* Returns the image color space.
*
*/
EAPI void
epeg_colorspace_get(Epeg_Image *im, int *space) {
if (space) {
*space = im->color_space;
}
}
/**
* Set the size of the image to decode in pixels.
* @param im A handle to an opened Epeg image.
* @param w The width of the image to decode at, in pixels.
* @param h The height of the image to decode at, in pixels.
*
* Sets the size at which to deocode the JPEG image, giving an optimised load
* that only decodes the pixels needed.
*
*/
EAPI void
epeg_decode_size_set(Epeg_Image *im, int w, int h) {
if (im->pixels) {
return;
}
if (w < 1) {
w = 1;
} else if (w > im->in.w) {
w = im->in.w;
}
if (h < 1) {
h = 1;
} else if (h > im->in.h) {
h = im->in.h;
}
im->out.w = w;
im->out.h = h;
im->out.x = 0;
im->out.y = 0;
}
EAPI void
epeg_decode_bounds_set(Epeg_Image *im, int x, int y, int w, int h) {
if (im->pixels) {
return;
}
if (w < 1) {
w = 1;
} else if (w > im->in.w) {
w = im->in.w;
}
if (h < 1) {
h = 1;
} else if (h > im->in.h) {
h = im->in.h;
}
im->out.w = w;
im->out.h = h;
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
im->out.x = x;
im->out.y = y;
}
/**
* Set the colorspace in which to decode the image.
* @param im A handle to an opened Epeg image.
* @param colorspace The colorspace to decode the image in.
*
* This sets the colorspace to decode the image in. The default is EPEG_YUV8,
* as this is normally the native colorspace of a JPEG file, avoiding any
* colorspace conversions for a faster load and/or save.
*/
EAPI void
epeg_decode_colorspace_set(Epeg_Image *im, Epeg_Colorspace colorspace) {
if (im->pixels) {
return;
}
if ((colorspace < EPEG_GRAY8) || (colorspace > EPEG_CMYK)) {
return;
}
im->color_space = colorspace;
}
/**
* Set the transformation of the image.
* @param im A handle to an opened Epeg image.
* @param transform The transformation of the image.
*
* Sets the transformation of the JPEG image.
*
*/
EAPI void
epeg_transform_set(Epeg_Image *im, Epeg_Transform transform) {
if (im->pixels) {
return;
}
im->out.transform = transform;
}
/**
* Get a segment of decoded pixels from an image.
* @param im A handle to an opened Epeg image.
* @param x Rectangle X.
* @param y Rectangle Y.
* @param w Rectangle width.
* @param h Rectangle height.
* @return Pointer to the top left of the requested pixel block.
*
* Return image pixels in the decoded format from the specified location
* rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
* packed with no row padding, and it organsied from top-left to bottom right,
* row by row. You must free the pixel block using epeg_pixels_free() before
* you close the image handle, and assume the pixels to be read-only memory.
*
* On success the pointer is returned, on failure, NULL is returned. Failure
* may be because the rectangle is out of the bounds of the image, memory
* allocations failed or the image data cannot be decoded.
*
*/
EAPI const void *
epeg_pixels_get(Epeg_Image *im, int x, int y, int w, int h) {
int xx, yy, ww, hh, bpp, ox, oy, ow, oh, iw, ih;
if (!im->pixels) {
if (_epeg_decode(im) != 0) {
return NULL;
}
}
if (!im->pixels) {
return NULL;
}
if ((im->out.w < 1) || (im->out.h < 1)) {
return NULL;
}
if (_epeg_scale(im) != 0) {
return NULL;
}
bpp = im->in.jinfo.output_components;
iw = im->out.w;
ih = im->out.h;
ow = w;
oh = h;
ox = 0;
oy = 0;
if ((x + ow) > iw) {
ow = iw - x;
}
if ((y + oh) > ih) {
oh = ih - y;
}
if (ow < 1) {
return NULL;
}
if (oh < 1) {
return NULL;
}
if (x < 0) {
ow += x;
ox = -x;
}
if (y < 0) {
oh += y;
oy = -y;
}
if (ow < 1) {
return NULL;
}
if (oh < 1) {
return NULL;
}
ww = x + ox + ow;
hh = y + oy + oh;
if (im->color_space == EPEG_GRAY8) {
unsigned char *pix, *p;
pix = malloc(w * h * 1);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox));
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p++;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_YUV8) {
unsigned char *pix, *p;
pix = malloc(w * h * 3);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 3);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p[1] = s[1];
p[2] = s[2];
p += 3;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_RGB8) {
unsigned char *pix, *p;
pix = malloc(w * h * 3);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 3);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p[1] = s[1];
p[2] = s[2];
p += 3;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_BGR8) {
unsigned char *pix, *p;
pix = malloc(w * h * 3);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 3);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[2];
p[1] = s[1];
p[2] = s[0];
p += 3;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_RGBA8) {
unsigned char *pix, *p;
pix = malloc(w * h * 4);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 4);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p[1] = s[1];
p[2] = s[2];
p[3] = 0xff;
p += 4;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_BGRA8) {
unsigned char *pix, *p;
pix = malloc(w * h * 4);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 4);
for (xx = x + ox; xx < ww; xx++) {
p[0] = 0xff;
p[1] = s[2];
p[2] = s[1];
p[3] = s[0];
p += 4;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_ARGB32) {
unsigned int *pix, *p;
pix = malloc(w * h * 4);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox));
for (xx = x + ox; xx < ww; xx++) {
p[0] = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2]);
p++;
s += bpp;
}
}
return pix;
} else if (im->color_space == EPEG_CMYK) {
unsigned char *pix, *p;
pix = malloc(w * h * 4);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 4);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p[1] = s[1];
p[2] = s[2];
p[3] = 0xff;
p += 4;
s += bpp;
}
}
return pix;
}
return NULL;
}
/**
* Get a segment of decoded pixels from an image.
* @param im A handle to an opened Epeg image.
* @param x Rectangle X.
* @param y Rectangle Y.
* @param w Rectangle width.
* @param h Rectangle height.
* @return Pointer to the top left of the requested pixel block.
*
* Return image pixels in the decoded format from the specified location
* rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
* packed with no row padding, and it organsied from top-left to bottom right,
* row by row. You must free the pixel block using epeg_pixels_free() before
* you close the image handle, and assume the pixels to be read-only memory.
*
* On success the pointer is returned, on failure, NULL is returned. Failure
* may be because the rectangle is out of the bounds of the image, memory
* allocations failed or the image data cannot be decoded.
*
*/
EAPI const void *
epeg_pixels_get_as_RGB8(Epeg_Image *im, int x, int y, int w, int h) {
int xx, yy, ww, hh, bpp, ox, oy, ow, oh, iw, ih;
if (!im->pixels) {
if (_epeg_decode(im) != 0) {
return NULL;
}
}
if (!im->pixels) {
return NULL;
}
if ((im->out.w < 1) || (im->out.h < 1)) {
return NULL;
}
bpp = im->in.jinfo.output_components;
iw = im->out.w;
ih = im->out.h;
ow = w;
oh = h;
ox = 0;
oy = 0;
if ((x + ow) > iw) {
ow = iw - x;
}
if ((y + oh) > ih) {
oh = ih - y;
}
if (ow < 1) {
return NULL;
}
if (oh < 1) {
return NULL;
}
if (x < 0) {
ow += x;
ox = -x;
}
if (y < 0) {
oh += y;
oy = -y;
}
if (ow < 1) {
return NULL;
}
if (oh < 1) {
return NULL;
}
ww = x + ox + ow;
hh = y + oy + oh;
if (im->color_space == EPEG_GRAY8) {
unsigned char *pix, *p;
pix = malloc(w * h * 3);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 3);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p[1] = s[0];
p[2] = s[0];
p += 3;
s += bpp;
}
}
return pix;
}
if (im->color_space == EPEG_RGB8) {
unsigned char *pix, *p;
pix = malloc(w * h * 3);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 3);
for (xx = x + ox; xx < ww; xx++) {
p[0] = s[0];
p[1] = s[1];
p[2] = s[2];
p += 3;
s += bpp;
}
}
return pix;
}
if (im->color_space == EPEG_CMYK) {
unsigned char *pix, *p;
pix = malloc(w * h * 3);
if (!pix) {
return NULL;
}
for (yy = y + oy; yy < hh; yy++) {
unsigned char *s;
s = im->lines[yy] + ((x + ox) * bpp);
p = pix + ((((yy - y) * w) + ox) * 3);
for (xx = x + ox; xx < ww; xx++) {
p[0] = (unsigned char)(MIN(255, (s[0] * s[3]) / 255));
p[1] = (unsigned char)(MIN(255, (s[1] * s[3]) / 255));
p[2] = (unsigned char)(MIN(255, (s[2] * s[3]) / 255));
p += 3;
s += bpp;
}
}
return pix;
}
return NULL;
}
/**
* Free requested pixel block from an image.
* @param im A handle to an opened Epeg image.
* @param data The pointer to the image pixels.
*
* This frees the data for a block of pixels requested from image @p im.
* @p data must be a valid (non NULL) pointer to a pixel block taken from the
* image @p im by epeg_pixels_get() and mustbe called before the image is
* closed by epeg_close().
*/
EAPI void
epeg_pixels_free(Epeg_Image *im, const void *data) {
free((void *)data);
}
/**
* Get the image comment field as a string.
* @param im A handle to an opened Epeg image.
* @return A pointer to the loaded image comments.
*
* This function returns the comment field as a string (NUL byte terminated)
* of the loaded image @p im, if there is a comment, or NULL if no comment is
* saved with the image. Consider the string returned to be read-only.
*
*/
EAPI const char *
epeg_comment_get(Epeg_Image *im) {
return im->in.comment;
}
/**
* Get thumbnail comments of loaded image.
* @param im A handle to an opened Epeg image.
* @param info Pointer to a thumbnail info struct to be filled in.
*
* This function retrieves thumbnail comments written by Epeg to any saved
* JPEG files. If no thumbnail comments were saved, the fields will be 0 in
* the @p info struct on return.
*
*/
EAPI void
epeg_thumbnail_comments_get(Epeg_Image *im, Epeg_Thumbnail_Info *info) {
if (!info) {
return;
}
info->uri = im->in.thumb_info.uri;
info->mtime = im->in.thumb_info.mtime;
info->w = im->in.thumb_info.w;
info->h = im->in.thumb_info.h;
info->mimetype = im->in.thumb_info.mime;
}
/**
* Set the comment field of the image for saving.
* @param im A handle to an opened Epeg image.
* @param comment The comment to set.
*
* Set the comment for the image file for when it gets saved. This is a NUL
* byte terminated C string. If @p comment is NULL the output file will have
* no comment field.
*
* The default comment will be any comment loaded from the input file.
*
*/
EAPI void
epeg_comment_set(Epeg_Image *im, const char *comment) {
if (im->out.comment) {
free(im->out.comment);
}
if (!comment) {
im->out.comment = NULL;
} else {
im->out.comment = strdup(comment);
}
}
/**
* Set the encoding quality of the saved image.
* @param im A handle to an opened Epeg image.
* @param quality The quality of encoding from 0 to 100.
*
* Set the quality of the output encoded image. Values from 0 to 100
* inclusive are valid, with 100 being the maximum quality, and 0 being the
* minimum. If the quality is set equal to or above 90%, the output U and V
* color planes are encoded at 1:1 with the Y plane.
*
* The default quality is 75.
*
*/
EAPI void
epeg_quality_set(Epeg_Image *im, int quality) {
if (quality < 0) {
quality = 0;
} else if (quality > 100) {
quality = 100;
}
im->out.quality = quality;
}
/**
* Enable thumbnail comments in saved image.
* @param im A handle to an opened Epeg image.
* @param onoff A boolean on and off enabling flag.
*
* if @p onoff is 1, the output file will have thumbnail comments added to
* it, and if it is 0, it will not. The default is 0.
*
*/
EAPI void
epeg_thumbnail_comments_enable(Epeg_Image *im, int onoff) {
im->out.thumbnail_info = onoff;
}
/**
* Set the output file path for the image when saved.
* @param im A handle to an opened Epeg image.
* @param file The path to the output file.
*
* This sets the output file path name (either a full or relative path name)
* to where the file will be written when saved. @p file must be a NUL
* terminated C string conatining the path to the file to be saved to. If it is
* NULL, the image will not be saved to a file when calling epeg_encode().
*/
EAPI void
epeg_file_output_set(Epeg_Image *im, const char *file) {
if (im->out.file) {
free(im->out.file);
}
if (!file) {
im->out.file = NULL;
} else {
im->out.file = strdup(file);
}
}
/**
* Set the output file to be a block of allocated memory.
* @param im A handle to an opened Epeg image.
* @param data A pointer to a pointer to a memory block.
* @param size A pointer to a counter of the size of the memory block.
*
* This sets the output encoding of the image when saved to be allocated
* memory. After epeg_close() is called the pointer pointed to by @p data
* and the integer pointed to by @p size will contain the pointer to the
* memory block and its size in bytes, respecitvely. The memory block can be
* freed with the free() function call. If the save fails the pointer to the
* memory block will be unaffected, as will the size.
*
*/
EAPI void
epeg_memory_output_set(Epeg_Image *im, unsigned char **data, int *size) {
im->out.mem.data = data;
im->out.mem.size = size;
im->out.file = NULL;
}
/**
* This saves the image to its specified destination.
* @param im A handle to an opened Epeg image.
*
* This saves the image @p im to its destination specified by
* epeg_file_output_set() or epeg_memory_output_set(). The image will be
* encoded at the deoded pixel size, using the quality, comment and thumbnail
* comment settings set on the image.
*
* retval 1 - error scale
* 2 - error encode
* 3 - error decode
* 4 - error decode ( setjmp )
*/
EAPI int
epeg_encode(Epeg_Image *im) {
int ret;
if ((ret = _epeg_decode(im)) != 0) {
return (ret == 2 ? 4 : 3);
}
if (_epeg_scale(im) != 0) {
return 1;
}
if (_epeg_encode(im) != 0) {
return 2;
}
return 0;
}
/**
* FIXME: Document this
* @param im A handle to an opened Epeg image.
*
* FIXME: Document this.
*/
EAPI int
epeg_trim(Epeg_Image *im) {
if (_epeg_decode_for_trim(im) != 0) {
return 1;
}
if (_epeg_trim(im) != 0) {
return 1;
}
if (_epeg_encode(im) != 0) {
return 1;
}
return 0;
}
/**
* This saves the transformed image to its specified destination.
* @param im A handle to an opened Epeg image.
*
* This saves the image @p im to its destination specified by
* epeg_file_output_set() or epeg_memory_output_set(). The image will be
* transformed using specified transformation.
*
* retval 1 - error transform
*/
EAPI int
epeg_transform(Epeg_Image *im) {
if (_epeg_transform(im) != 0) {
return 1;
}
return 0;
}
/**
* Get the last error message.
* @param im A handle to an opened Epeg image.
* @param buffer Char buffer for error message
*
* This copies the last error to the buffer parameter.
*/
EAPI void
epeg_error(Epeg_Image *im, char *buffer) {
if (!im->error) {
return;
}
strcpy(buffer, im->error_msg);
}
/**
* Close an image handle.
* @param im A handle to an opened Epeg image.
*
* This closes an opened image handle and frees all memory associated with it.
* It does not free encoded data generated by epeg_memory_output_set() followed
* by epeg_encode() nor does it guarantee to free any data recieved by
* epeg_pixels_get(). Once an image handle is closed consider it invalid.
*/
EAPI void
epeg_close(Epeg_Image *im) {
if (!im) {
return;
}
if (im->pixels) {
free(im->pixels);
}
if (im->lines) {
free(im->lines);
}
if (im->in.file) {
free(im->in.file);
}
if (!im->in.file) {
free(im->in.jinfo.src);
}
if (im->in.f || im->in.mem.data) {
jpeg_destroy_decompress(&(im->in.jinfo));
}
if (im->in.f) {
fclose(im->in.f);
}
if (im->in.comment) {
free(im->in.comment);
}
if (im->in.thumb_info.uri) {
free(im->in.thumb_info.uri);
}
if (im->in.thumb_info.mime) {
free(im->in.thumb_info.mime);
}
if (im->out.file) {
free(im->out.file);
}
if (!im->out.file) {
free(im->out.jinfo.dest);
}
if (im->out.f || im->in.mem.data) {
jpeg_destroy_compress(&(im->out.jinfo));
}
if (im->out.f) {
fclose(im->out.f);
}
if (im->out.comment) {
free(im->out.comment);
}
free(im);
}
static Epeg_Image *
_epeg_open_header(Epeg_Image *im) {
struct jpeg_marker_struct *m;
struct jpeg_source_mgr *src_mgr = NULL;
im->in.jinfo.err = jpeg_std_error(&(im->jerr.pub));
im->jerr.pub.error_exit = _epeg_fatal_error_handler;
#ifdef NOWARNINGS
im->jerr.pub.emit_message = _emit_message;
im->jerr.pub.output_message = _output_message;
im->jerr.pub.format_message = _format_message;
#endif
if (setjmp(im->jerr.setjmp_buffer)) {
error:
epeg_close(im);
im = NULL;
return NULL;
}
jpeg_create_decompress(&(im->in.jinfo));
jpeg_save_markers(&(im->in.jinfo), JPEG_APP0 + 7, 1024);
jpeg_save_markers(&(im->in.jinfo), JPEG_COM, 65535);
if (im->in.f != NULL) {
jpeg_stdio_src(&(im->in.jinfo), im->in.f);
} else {
/* Setup RAM source manager. */
src_mgr = calloc(1, sizeof(struct jpeg_source_mgr));
if (!src_mgr) {
goto error;
}
src_mgr->init_source = _jpeg_init_source;
src_mgr->fill_input_buffer = _jpeg_fill_input_buffer;
src_mgr->skip_input_data = _jpeg_skip_input_data;
src_mgr->resync_to_restart = jpeg_resync_to_restart;
src_mgr->term_source = _jpeg_term_source;
src_mgr->bytes_in_buffer = im->in.mem.size;
src_mgr->next_input_byte = (JOCTET *) im->in.mem.data;
im->in.jinfo.src = (struct jpeg_source_mgr *) src_mgr;
}
jpeg_read_header(&(im->in.jinfo), TRUE);
im->in.w = im->in.jinfo.image_width;
im->in.h = im->in.jinfo.image_height;
if (im->in.w < 1) {
goto error;
}
if (im->in.h < 1) {
goto error;
}
im->out.w = im->in.w;
im->out.h = im->in.h;
im->color_space = ((im->in.color_space = im->in.jinfo.out_color_space) == JCS_GRAYSCALE) ? EPEG_GRAY8 : EPEG_RGB8;
if (im->in.color_space == JCS_CMYK) {
im->color_space = EPEG_CMYK;
}
for (m = im->in.jinfo.marker_list; m; m = m->next) {
if (m->marker == JPEG_COM) {
if (im->in.comment) {
free(im->in.comment);
}
im->in.comment = malloc(m->data_length + 1);
if (im->in.comment) {
memcpy(im->in.comment, m->data, m->data_length);
im->in.comment[m->data_length] = 0;
}
} else if (m->marker == (JPEG_APP0 + 7)) {
if ((m->data_length > 7) &&
(!strncmp((char *)m->data, "Thumb::", 7))) {
char *p, *p2;
p = malloc(m->data_length + 1);
if (p) {
memcpy(p, m->data, m->data_length);
p[m->data_length] = 0;
p2 = strchr(p, '\n');
if (p2) {
p2[0] = 0;
if (!strcmp(p, "Thumb::URI"))
{
im->in.thumb_info.uri = strdup(p2 + 1);
} else if (!strcmp(p, "Thumb::MTime")) {
sscanf(p2 + 1, "%llu", &(im->in.thumb_info.mtime));
} else if (!strcmp(p, "Thumb::Image::Width")) {
im->in.thumb_info.w = atoi(p2 + 1);
} else if (!strcmp(p, "Thumb::Image::Height")) {
im->in.thumb_info.h = atoi(p2 + 1);
} else if (!strcmp(p, "Thumb::Mimetype")) {
im->in.thumb_info.mime = strdup(p2 + 1);