-
Notifications
You must be signed in to change notification settings - Fork 203
/
picload.cpp
1484 lines (1361 loc) · 41.2 KB
/
picload.cpp
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
#define PNG_SKIP_SETJMP_CHECK
#include <png.h>
#include <fcntl.h>
#include <lib/base/cfile.h>
#include <lib/base/wrappers.h>
#include <lib/gdi/picload.h>
extern "C" {
#define HAVE_BOOLEAN
#define boolean int
#include <jpeglib.h>
#include <gif_lib.h>
}
#define NANOSVG_ALL_COLOR_KEYWORDS
#define NANOSVG_IMPLEMENTATION
#include <nanosvg.h>
#define NANOSVGRAST_IMPLEMENTATION
#include <nanosvgrast.h>
extern const uint32_t crc32_table[256];
DEFINE_REF(ePicLoad);
static std::string getSize(const char* file)
{
struct stat64 s = {};
if (stat64(file, &s) < 0)
return "";
return std::to_string((long)(s.st_size / 1024)) + " kB";
}
static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
{
unsigned char* cr = new unsigned char[dx * dy * 3];
if (cr == NULL)
{
eDebug("[ePicLoad] resize Error malloc");
return orgin;
}
const int stride = 3 * dx;
#pragma omp parallel for
for (int j = 0; j < dy; j++)
{
unsigned char* p = cr + (j * stride);
int ya = j * oy / dy;
int yb = (j + 1) * oy / dy;
if (yb >= oy)
yb = oy - 1;
for (int i = 0; i < dx; i++, p += 3)
{
int xa = i * ox / dx;
int xb = (i + 1) * ox / dx;
if (xb >= ox)
xb = ox - 1;
int r = 0;
int g = 0;
int b = 0;
int sq = 0;
for (int l = ya; l <= yb; l++)
{
const unsigned char* q = orgin + ((l * ox + xa) * 3);
for (int k = xa; k <= xb; k++, q += 3, sq++)
{
r += q[0];
g += q[1];
b += q[2];
}
}
if (sq == 0) // prevent division by zero
sq = 1;
p[0] = r / sq;
p[1] = g / sq;
p[2] = b / sq;
}
}
delete [] orgin;
return cr;
}
//---------------------------------------------------------------------------------------------
#define BMP_TORASTER_OFFSET 10
#define BMP_SIZE_OFFSET 18
#define BMP_BPP_OFFSET 28
#define BMP_RLE_OFFSET 30
#define BMP_COLOR_OFFSET 54
#define fill4B(a) ((4 - ((a) % 4 )) & 0x03)
struct color {
unsigned char red;
unsigned char green;
unsigned char blue;
};
static void fetch_pallete(int fd, struct color pallete[], int count)
{
unsigned char buff[4];
lseek(fd, BMP_COLOR_OFFSET, SEEK_SET);
for (int i = 0; i < count; i++)
{
if (read(fd, buff, 4) != 4) // failed to read rgb
{
break;
}
pallete[i].red = buff[2];
pallete[i].green = buff[1];
pallete[i].blue = buff[0];
}
}
static unsigned char *bmp_load(const char *file, int *x, int *y)
{
unsigned char buff[4] = {};
struct color pallete[256] = {};
int fd = open(file, O_RDONLY);
if (fd == -1) return NULL;
if (lseek(fd, BMP_SIZE_OFFSET, SEEK_SET) == -1)
{
close(fd);
return NULL;
}
if (read(fd, buff, 4) != 4) // failed to read x
{
close(fd);
return NULL;
}
*x = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
if (read(fd, buff, 4) != 4) // failed to read y
{
close(fd);
return NULL;
}
*y = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
if (lseek(fd, BMP_TORASTER_OFFSET, SEEK_SET) == -1)
{
close(fd);
return NULL;
}
if (read(fd, buff, 4) != 4) // failed to read raster
{
close(fd);
return NULL;
}
int raster = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
if (lseek(fd, BMP_BPP_OFFSET, SEEK_SET) == -1)
{
close(fd);
return NULL;
}
if (read(fd, buff, 2) != 2) // failed to read bpp
{
close(fd);
return NULL;
}
int bpp = buff[0] + (buff[1] << 8);
unsigned char *pic_buffer = new unsigned char[(*x) * (*y) * 3];
unsigned char *wr_buffer = pic_buffer + (*x) * ((*y) - 1) * 3;
switch (bpp)
{
case 4:
{
int skip = fill4B((*x) / 2 + (*x) % 2);
fetch_pallete(fd, pallete, 16);
lseek(fd, raster, SEEK_SET);
unsigned char * tbuffer = new unsigned char[*x / 2 + 1];
if (tbuffer == NULL)
{
close(fd);
return NULL;
}
for (int i = 0; i < *y; i++)
{
if (read(fd, tbuffer, (*x) / 2 + *x % 2) != ((*x) / 2 + *x % 2))
{
eDebug("[ePicLoad] failed to read %d bytes...", ((*x) / 2 + *x % 2));
}
int j;
for (j = 0; j < (*x) / 2; j++)
{
unsigned char c1 = tbuffer[j] >> 4;
unsigned char c2 = tbuffer[j] & 0x0f;
*wr_buffer++ = pallete[c1].red;
*wr_buffer++ = pallete[c1].green;
*wr_buffer++ = pallete[c1].blue;
*wr_buffer++ = pallete[c2].red;
*wr_buffer++ = pallete[c2].green;
*wr_buffer++ = pallete[c2].blue;
}
if ((*x) % 2)
{
unsigned char c1 = tbuffer[j] >> 4;
*wr_buffer++ = pallete[c1].red;
*wr_buffer++ = pallete[c1].green;
*wr_buffer++ = pallete[c1].blue;
}
if (skip)
{
if (read(fd, buff, skip) != skip)
{
eDebug("[ePicLoad] failed to read %d bytes...", skip);
}
}
wr_buffer -= (*x) * 6;
}
delete [] tbuffer;
break;
}
case 8:
{
int skip = fill4B(*x);
fetch_pallete(fd, pallete, 256);
lseek(fd, raster, SEEK_SET);
unsigned char * tbuffer = new unsigned char[*x];
if (tbuffer == NULL)
{
close(fd);
return NULL;
}
for (int i = 0; i < *y; i++)
{
if (read(fd, tbuffer, *x) != *x)
{
eDebug("[ePicLoad] failed to read %d bytes...", *x);
}
for (int j = 0; j < *x; j++)
{
wr_buffer[j * 3] = pallete[tbuffer[j]].red;
wr_buffer[j * 3 + 1] = pallete[tbuffer[j]].green;
wr_buffer[j * 3 + 2] = pallete[tbuffer[j]].blue;
}
if (skip)
{
if (read(fd, buff, skip) != skip)
{
eDebug("[ePicLoad] failed to skip %d bytes...", skip);
}
}
wr_buffer -= (*x) * 3;
}
delete [] tbuffer;
break;
}
case 24:
{
int skip = fill4B((*x) * 3);
lseek(fd, raster, SEEK_SET);
for (int i = 0; i < (*y); i++)
{
if (read(fd, wr_buffer, (*x) * 3) != ((*x) * 3))
{
eDebug("[picload] failed to read %d bytes...", ((*x) * 3));
}
for (int j = 0; j < (*x) * 3 ; j = j + 3)
{
unsigned char c = wr_buffer[j];
wr_buffer[j] = wr_buffer[j + 2];
wr_buffer[j + 2] = c;
}
if (skip)
{
if (read(fd, buff, skip) != skip)
{
eDebug("[ePicLoad] failed to skip %d bytes...", skip);
}
}
wr_buffer -= (*x) * 3;
}
break;
}
default:
delete [] pic_buffer;
close(fd);
return NULL;
}
close(fd);
return(pic_buffer);
}
//---------------------------------------------------------------------
static void png_load(Cfilepara* filepara, int background, bool forceRGB=false)
{
png_uint_32 width, height;
unsigned int i;
int bit_depth, color_type, interlace_type;
png_byte *fbptr;
CFile fh(filepara->file, "rb");
if (!fh)
return;
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
return;
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
return;
}
png_init_io(png_ptr, fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
if (!forceRGB && (color_type == PNG_COLOR_TYPE_GRAY || color_type & PNG_COLOR_MASK_PALETTE))
{
if (bit_depth < 8)
{
png_set_packing(png_ptr);
bit_depth = 8;
}
unsigned char *pic_buffer = new unsigned char[height * width];
filepara->ox = width;
filepara->oy = height;
filepara->pic_buffer = pic_buffer;
filepara->bits = 8;
png_bytep *rowptr=new png_bytep[height];
for (unsigned int i=0; i!=height; i++)
{
rowptr[i]=(png_byte*)pic_buffer;
pic_buffer += width;
}
png_read_rows(png_ptr, rowptr, 0, height);
delete [] rowptr;
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))
{
png_color *palette;
int num_palette;
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
filepara->palette_size = num_palette;
if (num_palette)
filepara->palette = new gRGB[num_palette];
for (int i=0; i<num_palette; i++)
{
filepara->palette[i].a=0;
filepara->palette[i].r=palette[i].red;
filepara->palette[i].g=palette[i].green;
filepara->palette[i].b=palette[i].blue;
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_byte *trans;
png_get_tRNS(png_ptr, info_ptr, &trans, &num_palette, 0);
for (int i=0; i<num_palette; i++)
filepara->palette[i].a=255-trans[i];
}
}
}
else
{
if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (color_type & PNG_COLOR_MASK_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (color_type & PNG_COLOR_MASK_ALPHA || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_strip_alpha(png_ptr);
png_color_16 bg;
bg.red = (background >> 16) & 0xFF;
bg.green = (background >> 8) & 0xFF;
bg.blue = (background) & 0xFF;
bg.gray = bg.green;
bg.index = 0;
png_set_background(png_ptr, &bg, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
}
png_read_update_info(png_ptr, info_ptr);
if (width * 3 != png_get_rowbytes(png_ptr, info_ptr))
{
eDebug("[ePicLoad] Error processing (did not get RGB data from PNG file)");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
return;
}
unsigned char *pic_buffer = new unsigned char[height * width * 3];
filepara->ox = width;
filepara->oy = height;
filepara->pic_buffer = pic_buffer;
int number_passes = png_set_interlace_handling(png_ptr);
for(int pass = 0; pass < number_passes; pass++)
{
fbptr = (png_byte *)pic_buffer;
for (i = 0; i < height; i++, fbptr += width * 3)
png_read_row(png_ptr, fbptr, NULL);
}
png_read_end(png_ptr, info_ptr);
}
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
}
//-------------------------------------------------------------------
struct r_jpeg_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf envbuffer;
};
void jpeg_cb_error_exit(j_common_ptr cinfo)
{
struct r_jpeg_error_mgr *mptr;
mptr = (struct r_jpeg_error_mgr *) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(mptr->envbuffer, 1);
}
static unsigned char *jpeg_load(const char *file, int *ox, int *oy, unsigned int max_x, unsigned int max_y)
{
struct jpeg_decompress_struct cinfo = {};
struct jpeg_decompress_struct *ciptr = &cinfo;
struct r_jpeg_error_mgr emgr = {};
unsigned char *pic_buffer;
CFile fh(file, "rb");
pic_buffer = nullptr;
if (!fh)
return NULL;
ciptr->err = jpeg_std_error(&emgr.pub);
emgr.pub.error_exit = jpeg_cb_error_exit;
if (setjmp(emgr.envbuffer) == 1)
{
jpeg_destroy_decompress(ciptr);
return NULL;
}
jpeg_create_decompress(ciptr);
jpeg_stdio_src(ciptr, fh);
jpeg_read_header(ciptr, TRUE);
ciptr->out_color_space = JCS_RGB;
if (max_x == 0) max_x = 1280; // sensible default
if (max_y == 0) max_y = 720;
// define scale to always fit vertically or horizontally in all orientations
ciptr->scale_denom = 8;
unsigned int screenmax = max_x > max_y ? max_x : max_y;
unsigned int imagemin = ciptr->image_width < ciptr->image_height ? ciptr->image_width : ciptr->image_height;
ciptr->scale_num = (ciptr->scale_denom * screenmax + imagemin -1) / imagemin;
if (ciptr->scale_num < 1) ciptr->scale_num = 1;
if (ciptr->scale_num > 16) ciptr->scale_num = 16;
jpeg_start_decompress(ciptr);
*ox=ciptr->output_width;
*oy=ciptr->output_height;
//eDebug("[jpeg_load] ox=%d oy=%d w=%d (%d), h=%d (%d) scale=%d rec_outbuf_height=%d", ciptr->output_width, ciptr->output_height, ciptr->image_width, max_x, ciptr->image_height, max_y, ciptr->scale_num, ciptr->rec_outbuf_height);
if(ciptr->output_components == 3)
{
unsigned int stride = ciptr->output_width * ciptr->output_components;
pic_buffer = new unsigned char[ciptr->output_height * stride];
unsigned char *bp = pic_buffer;
while (ciptr->output_scanline < ciptr->output_height)
{
JDIMENSION lines = jpeg_read_scanlines(ciptr, &bp, ciptr->rec_outbuf_height);
bp += stride * lines;
}
}
jpeg_finish_decompress(ciptr);
jpeg_destroy_decompress(ciptr);
return(pic_buffer);
}
static int jpeg_save(const char * filename, int ox, int oy, unsigned char *pic_buffer)
{
struct jpeg_compress_struct cinfo = {};
struct jpeg_error_mgr jerr = {};
JSAMPROW row_pointer[1];
int row_stride;
CFile outfile(filename, "wb");
if (!outfile)
{
eDebug("[ePicLoad] jpeg can't write %s: %m", filename);
return 1;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
eDebug("[ePicLoad] save Thumbnail... %s",filename);
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = ox;
cinfo.image_height = oy;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 70, TRUE );
jpeg_start_compress(&cinfo, TRUE);
row_stride = ox * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = & pic_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return 0;
}
//-------------------------------------------------------------------
inline void m_rend_gif_decodecolormap(unsigned char *cmb, unsigned char *rgbb, ColorMapObject *cm, int s, int l)
{
GifColorType *cmentry;
int i;
for (i = 0; i < l; i++)
{
cmentry = &cm->Colors[cmb[i]];
*(rgbb++) = cmentry->Red;
*(rgbb++) = cmentry->Green;
*(rgbb++) = cmentry->Blue;
}
}
static void svg_load(Cfilepara* filepara, bool forceRGB = false)
{
NSVGimage *image = nullptr;
NSVGrasterizer *rast = nullptr;
unsigned char *pic_buffer = nullptr;
int w = 0;
int h = 0;
double xscale, yscale, scale;
image = nsvgParseFromFile(filepara->file, "px", 96.0f);
if (image == nullptr)
{
return;
}
rast = nsvgCreateRasterizer();
if (rast == nullptr)
{
nsvgDelete(image);
return;
}
xscale = ((double) filepara->max_x) / image->width;
yscale = ((double) filepara->max_y) / image->height;
scale = xscale > yscale ? yscale : xscale;
w = image->width*scale;
h = image->height*scale;
pic_buffer = (unsigned char*)malloc(w*h*4);
if (pic_buffer == nullptr)
{
nsvgDeleteRasterizer(rast);
nsvgDelete(image);
return;
}
eDebug("[ePicLoad] svg_load max %dx%d from %dx%d scale %f new %dx%d", filepara->max_x, filepara->max_y, (int)image->width, (int)image->height, scale, w, h);
// Rasterizes SVG image, returns RGBA image (non-premultiplied alpha)
nsvgRasterize(rast, image, 0, 0, scale, pic_buffer, w, h, w*4);
filepara->pic_buffer = pic_buffer;
filepara->bits = 32;
filepara->ox = w;
filepara->oy = h;
nsvgDeleteRasterizer(rast);
nsvgDelete(image);
if(forceRGB) // convert 32bit RGBA to 24bit RGB
{
unsigned char *pic_buffer2 = (unsigned char*)malloc(w*h*3); // 24bit RGB
if (pic_buffer2 == nullptr)
{
free(pic_buffer);
return;
}
for (int i=0; i<w*h; i++)
{
pic_buffer2[3*i] = pic_buffer[4*i];
pic_buffer2[3*i+1] = pic_buffer[4*i+1];
pic_buffer2[3*i+2] = pic_buffer[4*i+2];
}
filepara->bits = 24;
filepara->pic_buffer = pic_buffer2;
free(pic_buffer);
}
}
static void gif_load(Cfilepara* filepara, bool forceRGB = false)
{
unsigned char *pic_buffer = NULL;
int px, py, i, j;
unsigned char *slb=NULL;
GifFileType *gft;
GifRecordType rt;
GifByteType *extension;
ColorMapObject *cmap;
int cmaps;
int extcode;
#if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1
gft = DGifOpenFileName(filepara->file, &extcode);
#else
gft = DGifOpenFileName(filepara->file);
#endif
if (gft == NULL)
return;
do
{
if (DGifGetRecordType(gft, &rt) == GIF_ERROR)
goto ERROR_R;
switch(rt)
{
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(gft) == GIF_ERROR)
goto ERROR_R;
filepara->ox = px = gft->Image.Width;
filepara->oy = py = gft->Image.Height;
pic_buffer = new unsigned char[px * py];
filepara->pic_buffer = pic_buffer;
filepara->bits = 8;
slb = pic_buffer;
if (pic_buffer != NULL)
{
cmap = (gft->Image.ColorMap ? gft->Image.ColorMap : gft->SColorMap);
cmaps = cmap->ColorCount;
filepara->palette_size = cmaps;
filepara->palette = new gRGB[cmaps];
for (i = 0; i != cmaps; ++i)
{
filepara->palette[i].a = 0;
filepara->palette[i].r = cmap->Colors[i].Red;
filepara->palette[i].g = cmap->Colors[i].Green;
filepara->palette[i].b = cmap->Colors[i].Blue;
}
if (!(gft->Image.Interlace))
{
for (i = 0; i < py; i++)
{
if (DGifGetLine(gft, slb, px) == GIF_ERROR)
goto ERROR_R;
slb += px;
}
}
else
{
int IOffset[] = { 0, 4, 2, 1 }; // The way Interlaced image should.
int IJumps[] = { 8, 8, 4, 2 }; // be read - offsets and jumps...
for (j = 0; j < 4; j++)
{
for (i = IOffset[j]; i < py; i += IJumps[j])
{
if (DGifGetLine(gft, pic_buffer + i*px, px) == GIF_ERROR)
goto ERROR_R;
}
}
}
if (forceRGB) {
unsigned char *pic_buffer2 = new unsigned char[px * py * 3];
if (pic_buffer2 != NULL) {
unsigned char *slb2 = pic_buffer2;
slb = pic_buffer;
for (j = 0; j < py; j++) {
for (i = 0; i < px; i++) {
int c = *slb++;
*slb2++ = filepara->palette[c].r;
*slb2++ = filepara->palette[c].g;
*slb2++ = filepara->palette[c].b;
}
}
filepara->bits = 24;
filepara->pic_buffer = pic_buffer2;
delete [] pic_buffer;
delete [] filepara->palette;
filepara->palette = NULL;
}
}
}
break;
case EXTENSION_RECORD_TYPE:
if (DGifGetExtension(gft, &extcode, &extension) == GIF_ERROR)
goto ERROR_R;
while (extension != NULL)
if (DGifGetExtensionNext(gft, &extension) == GIF_ERROR)
goto ERROR_R;
break;
default:
break;
}
}
while (rt != TERMINATE_RECORD_TYPE);
#if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1
DGifCloseFile(gft, &extcode);
#else
DGifCloseFile(gft);
#endif
return;
ERROR_R:
eDebug("[ePicLoad] <Error gif>");
#if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1
DGifCloseFile(gft, &extcode);
#else
DGifCloseFile(gft);
#endif
}
//---------------------------------------------------------------------------------------------
ePicLoad::ePicLoad():
m_filepara(NULL),
m_exif(NULL),
threadrunning(false),
m_conf(),
msg_thread(this,1),
msg_main(eApp,1)
{
CONNECT(msg_thread.recv_msg, ePicLoad::gotMessage);
CONNECT(msg_main.recv_msg, ePicLoad::gotMessage);
}
ePicLoad::PConf::PConf():
max_x(0),
max_y(0),
aspect_ratio(1.066400), //4:3
background(0),
resizetype(1),
usecache(false),
auto_orientation(false),
thumbnailsize(180)
{
}
void ePicLoad::waitFinished()
{
msg_thread.send(Message(Message::quit));
kill();
}
ePicLoad::~ePicLoad()
{
if (threadrunning)
waitFinished();
if (m_filepara != NULL)
delete m_filepara;
if (m_exif != NULL) {
m_exif->ClearExif();
delete m_exif;
}
}
void ePicLoad::thread_finished()
{
threadrunning = false;
}
void ePicLoad::thread()
{
threadrunning = true;
hasStarted();
if (nice(4))
{
eDebug("[ePicLoad] thread failed to modify scheduling priority (%m)");
}
runLoop();
}
void ePicLoad::decodePic()
{
eDebug("[ePicLoad] decode picture... %s", m_filepara->file);
getExif(m_filepara->file, m_filepara->id);
switch(m_filepara->id)
{
case F_PNG: png_load(m_filepara, m_conf.background);
break;
case F_JPEG: m_filepara->pic_buffer = jpeg_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy, m_filepara->max_x, m_filepara->max_y);
break;
case F_BMP: m_filepara->pic_buffer = bmp_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);
break;
case F_GIF: gif_load(m_filepara);
break;
case F_SVG: svg_load(m_filepara);
break;
}
}
void ePicLoad::decodeThumb()
{
eDebug("[ePicLoad] get Thumbnail... %s", m_filepara->file);
bool exif_thumbnail = false;
bool cachefile_found = false;
std::string cachefile = "";
std::string cachedir = "/.Thumbnails";
getExif(m_filepara->file, m_filepara->id, 1);
if (m_exif && m_exif->m_exifinfo->IsExif)
{
if (m_exif->m_exifinfo->Thumnailstate == 2)
{
free(m_filepara->file);
m_filepara->file = strdup(THUMBNAILTMPFILE);
m_filepara->id = F_JPEG; // imbedded thumbnail seem to be jpeg
exif_thumbnail = true;
eDebug("[ePicLoad] decodeThumb: Exif Thumbnail found");
}
//else
// eDebug("[ePicLoad] decodeThumb: NO Exif Thumbnail found");
m_filepara->addExifInfo(m_exif->m_exifinfo->CameraMake);
m_filepara->addExifInfo(m_exif->m_exifinfo->CameraModel);
m_filepara->addExifInfo(m_exif->m_exifinfo->DateTime);
char buf[20];
snprintf(buf, 20, "%d x %d", m_exif->m_exifinfo->Width, m_exif->m_exifinfo->Height);
m_filepara->addExifInfo(buf);
}
else
eDebug("[ePicLoad] decodeThumb: NO Exif info");
if (!exif_thumbnail && m_conf.usecache)
{
if (FILE *f = fopen(m_filepara->file, "rb"))
{
int c;
int count = 1024*100; // get checksum data out of max 100kB
uint32_t crc32 = 0;
char crcstr[9];
*crcstr = 0;
while (count-- > 0 && (c = getc(f)) != EOF)
crc32 = crc32_table[((crc32) ^ (c)) & 0xFF] ^ ((crc32) >> 8);
fclose(f);
crc32 = ~crc32;
sprintf(crcstr, "%08X", crc32);
cachedir = m_filepara->file;
size_t pos = cachedir.find_last_of("/");
if (pos != std::string::npos)
cachedir = cachedir.substr(0, pos) + "/.Thumbnails";
cachefile = cachedir + std::string("/pc_") + crcstr;
if (!access(cachefile.c_str(), R_OK))
{
cachefile_found = true;
free(m_filepara->file);
m_filepara->file = strdup(cachefile.c_str());
m_filepara->id = F_JPEG;
eDebug("[ePicLoad] Cache File %s found", cachefile.c_str());
}
}
}
switch (m_filepara->id)
{
case F_PNG: png_load(m_filepara, m_conf.background, true);
break;
case F_JPEG: m_filepara->pic_buffer = jpeg_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy, m_filepara->max_x, m_filepara->max_y);
break;
case F_BMP: m_filepara->pic_buffer = bmp_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);
break;
case F_GIF: gif_load(m_filepara, true);
break;
case F_SVG: svg_load(m_filepara, true);
break;
}
//eDebug("[ePicLoad] getThumb picture loaded %s", m_filepara->file);
if (exif_thumbnail)
::unlink(THUMBNAILTMPFILE);
if (m_filepara->pic_buffer != NULL)
{
// Save cachefile
if (m_conf.usecache && !exif_thumbnail && !cachefile_found)
{
if (access(cachedir.c_str(), R_OK))
::mkdir(cachedir.c_str(), 0755);
// Resize for Thumbnail
int imx, imy;
if (m_filepara->ox <= m_filepara->oy)
{
imy = m_conf.thumbnailsize;
imx = (int)( (m_conf.thumbnailsize * ((double)m_filepara->ox)) / ((double)m_filepara->oy) );
}
else
{
imx = m_conf.thumbnailsize;
imy = (int)( (m_conf.thumbnailsize * ((double)m_filepara->oy)) / ((double)m_filepara->ox) );
}
// eDebug("[ePicLoad] getThumb resize from %dx%d to %dx%d", m_filepara->ox, m_filepara->oy, imx, imy);
m_filepara->pic_buffer = color_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy);
m_filepara->ox = imx;
m_filepara->oy = imy;
if (jpeg_save(cachefile.c_str(), m_filepara->ox, m_filepara->oy, m_filepara->pic_buffer))
eDebug("[ePicLoad] getThumb: error saving cachefile");
}
}
}
void ePicLoad::gotMessage(const Message &msg)
{
switch (msg.type)
{
case Message::decode_Pic:
decodePic();
msg_main.send(Message(Message::decode_finished));
break;
case Message::decode_Thumb:
decodeThumb();
msg_main.send(Message(Message::decode_finished));
break;
case Message::quit: // called from decode thread
eDebug("[ePicLoad] decode thread ... got quit msg");
quit(0);
break;
case Message::decode_finished: // called from main thread
//eDebug("[ePicLoad] decode finished... %s", m_filepara->file);
if(m_filepara->callback)
PictureData(m_filepara->picinfo.c_str());
else
{
if(m_filepara != NULL)
{
delete m_filepara;
m_filepara = NULL;
}
if (m_exif != NULL) {
m_exif->ClearExif();
delete m_exif;
m_exif = NULL;
}
}
break;
default:
eDebug("[ePicLoad] unhandled thread message");
}
}
int ePicLoad::startThread(int what, const char *file, int x, int y, bool async)
{
if(async && threadrunning && m_filepara != NULL)
{
eDebug("[ePicLoad] thread running");
m_filepara->callback = false;
return 1;
}
if(m_filepara != NULL)
{
delete m_filepara;
m_filepara = NULL;
}
if (m_exif != NULL) {
m_exif->ClearExif();
delete m_exif;
m_exif = NULL;
}
int file_id = getFileType(file);
if(file_id < 0)
{
eDebug("[ePicLoad] <format not supported>");
return 1;
}
m_filepara = new Cfilepara(file, file_id, getSize(file));
m_filepara->max_x = x > 0 ? x : m_conf.max_x;
m_filepara->max_y = x > 0 ? y : m_conf.max_y;
if(m_filepara->max_x <= 0 || m_filepara->max_y <= 0)
{
delete m_filepara;
m_filepara = NULL;
eDebug("[ePicLoad] <error in Para>");