-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.c
1350 lines (1252 loc) · 39.8 KB
/
img.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
/* img.c
* Generic image decoding and PNG and JPG decoders.
* (c) 2002 Karel 'Clock' Kulhavy
* This is a part of the Links program, released under GPL.
* Used in graphics mode of Links only
TODO: odstranit zbytecne ditherovani z strip_optimized header_dimensions_known,
protoze pozadi obrazku musi byt stejne jako pozadi stranky, a to se nikdy
neditheruje, protoze je to vzdy jednolita barva. Kdyz uz to nepujde
odstranit tak tam aspon dat fixne zaokrouhlovani.
TODO: dodelat stripy do jpegu a png a tiff.
*/
#include "links.h"
#ifdef G
#define RESTART_SIZE 8192
/* Size of biggest chunk of compressed data that is processed in one run */
static struct g_object_image *global_goi;
struct cached_image *global_cimg;
int end_callback_hit;
static int is_image_size_sane(ssize_t x, ssize_t y)
{
size_t a;
if (x < 0 || y < 0) return 0;
if (x >= MAXINT || y >= MAXINT) return 0;
a = (size_t)x * (size_t)y * (drv->depth & 7);
if (y && (size_t)a / (size_t)y / (drv->depth & 7) != (size_t)x) return 0;
return a <= MAX_SIZE_T / 2;
}
/* mem_free(cimg->decoder) */
static void destroy_decoder (struct cached_image *cimg)
{
if (cimg->decoder){
switch(cimg->image_type){
case IM_PNG:
png_destroy_decoder(cimg);
break;
#ifdef HAVE_JPEG
case IM_JPG:
jpeg_destroy_decoder(cimg);
break;
#endif /* #ifdef HAVE_JPEG */
case IM_GIF:
gif_destroy_decoder(cimg);
break;
case IM_XBM:
/* do nothing */
break;
#ifdef HAVE_TIFF
case IM_TIFF:
tiff_destroy_decoder(cimg);
break;
#endif
#ifdef HAVE_SVG
case IM_SVG:
svg_destroy_decoder(cimg);
break;
#endif
}
mem_free(cimg->decoder);
}
}
static void mem_free_buffer(struct cached_image *cimg)
{
mem_free(cimg->buffer);
}
static void img_destruct_image(struct g_object *object)
{
struct g_object_image *goi = get_struct(object, struct g_object_image, goti.go);
if (goi->orig_src) mem_free(goi->orig_src);
if (goi->alt) mem_free(goi->alt);
if (goi->name) mem_free(goi->name);
if (goi->src) mem_free(goi->src);
release_image_map(goi->goti.map);
if (goi->list_entry.next) del_from_list(goi);
if (goi->goti.go.xw && goi->goti.go.yw) {
/* At least one dimension is zero */
deref_cached_image(goi->cimg);
}
mem_free(goi);
}
/* Frees all data allocated by cached_image including cached image itself */
void img_destruct_cached_image(struct cached_image *cimg)
{
switch (cimg->state){
case 0:
case 1:
case 2:
case 3:
case 9:
case 11:
break;
case 12:
case 14:
if (cimg->gamma_table) mem_free(cimg->gamma_table);
if (cimg->bmp_used){
drv->unregister_bitmap(&(cimg->bmp));
}
if (cimg->strip_optimized){
if (cimg->dregs) mem_free(cimg->dregs);
}else{
mem_free_buffer(cimg);
}
/*-fallthrough*/
case 8:
case 10:
destroy_decoder(cimg);
break;
case 13:
case 15:
drv->unregister_bitmap(&(cimg->bmp));
break;
#ifdef DEBUG
default:
fprintf(stderr, "img_destruct_cached_image: state=%d\n", cimg->state);
internal_error("Invalid state in struct cached_image");
#endif /* #ifdef DEBUG */
}
mem_free(cimg->url);
mem_free(cimg);
}
/* You throw in a vertical dimension of image and it returns
* new dimension according to the aspect ratio and user-set image
* scaling factor. When scaling factor is 100% and screen pixels
* are non-square, the picture will be always in one dimension
* untouched and in the second _ENLARGED_. So that no information
* in the picture will be lost.
* Input may be <0. In this case output=input
* Input may be 0. In this case output=0.
* If input is >0 the output is also >0.
*/
static ssize_t img_scale_h(unsigned scale, ssize_t in)
{
ssize_t out;
/* We assume unsigned long holds at least 32 bits */
unsigned long pre;
if (in<=0) return in;
pre=((unsigned long)(aspect<65536UL?65536UL:aspect)*scale+128)>>8;
out=(size_t)(((unsigned long)in*pre+12800UL)/25600UL);
if (out<1) out=1;
return out;
}
static ssize_t img_scale_v(unsigned scale, ssize_t in)
{
ssize_t out;
unsigned long divisor;
if (in<=0) return in;
divisor=(100*(aspect>=65536UL?65536UL:aspect)+128)>>8;
out=(ssize_t)(((unsigned long)in*(scale*256)+(divisor>>1))/divisor);
if (out<1) out=1;
return out;
}
/* Returns height (pixels) for prescribed width (pixels). Honours aspect. */
static ssize_t width2height(double width_px, double width_mm, double height_mm)
{
ssize_t height_px;
if (width_px<=0) return 0;
height_px=(ssize_t)((height_mm*width_px*65536)/(aspect*width_mm));
if (height_px<1) height_px=1;
return height_px;
}
/* Returns width (pixels) for prescribed height (pixels). Honours aspect. */
static ssize_t height2width(double height_px, double width_mm, double height_mm)
{
ssize_t width_px;
if (height_px<=0) return 0;
width_px=(ssize_t)((width_mm*height_px*aspect)/(65536*height_mm));
if (width_px<1) width_px=1;
return width_px;
}
/* Compute 8-bit background for filling buffer with cimg->*_gamma
* (performs rounding) */
void compute_background_8(struct cached_image *cimg, unsigned char rgb[3])
{
unsigned short red, green, blue;
round_color_sRGB_to_48(&red, &green, &blue, cimg->background_color);
rgb[0] = ags_16_to_8(red, (float)(cimg->red_gamma/(float)(user_gamma tcc_hack)));
rgb[1] = ags_16_to_8(green, (float)(cimg->green_gamma/(float)(user_gamma tcc_hack)));
rgb[2] = ags_16_to_8(blue, (float)(cimg->blue_gamma/(float)(user_gamma tcc_hack)));
}
/* updates cimg state when header dimensions are know. Only allowed to be called
* in state 8 and 10.
* Allocates right amount of memory into buffer, formats it (with background or
* zeroes, depens on buffer_bytes_per_pixel). Updates dimensions (xww and yww)
* according to newly known header dimensions. Fills in gamma_stamp, bmp_used
* (zero because we not bother with generating bitmap here)
* and rows_added.
* Resets strip_optimized if image will be scaled or
* Allocates dregs if on exit strip_optimized is nonzero.
* Allocates and computes gamma_table, otherwise
* sets gamma_table to NULL. Also doesn't make gamma table if image contains less
* than 1024 pixels (it would be probably a waste of time).
* Output state is always 12 (from input state 8) or 14 (from input state 10).
*
* The caller must have set the following elements of cimg:
* width
* height
* buffer_bytes_per_pixel
* red_gamma
* green_gamma
* blue_gamma
* strip_optimized
*/
int header_dimensions_known(struct cached_image *cimg)
{
unsigned short red, green, blue;
#ifdef DEBUG
if ((cimg->state^8)&13){
fprintf(stderr,"cimg->state=%d\n",cimg->state);
internal_error("Invalid state in header_dimensions_known");
}
#endif /* #ifdef DEBUG */
if (cimg->width<1||cimg->height<1){
/*fprintf(stderr,"width=%d height=%d\n",cimg->width, cimg->height);*/
return 1;
}
if (!is_image_size_sane(cimg->width, cimg->height)) {
return 1;
}
if (cimg->wanted_xw<0){
/* Unspecified width */
if (cimg->wanted_yw<0){
/* Unspecified neither width nor height */
cimg->xww=img_scale_h(cimg->scale, cimg->width);
cimg->yww=img_scale_v(cimg->scale, cimg->height);
}else{
/* Unspecified width specified height */
cimg->xww=height2width(cimg->yww,
cimg->width, cimg->height);
if (cimg->xww<=0) cimg->xww=1;
}
}else{
/* Specified width */
if (cimg->wanted_yw<0){
/* Unspecified height, specified width */
cimg->yww=width2height(cimg->xww,
cimg->width, cimg->height);
if (cimg->yww<=0) cimg->yww=1;
}else if (cimg->wanted_xyw_meaning==MEANING_AUTOSCALE){
/* Specified height and width and autoscale meant */
/* Try first to nail the height */
cimg->yww=cimg->wanted_yw;
cimg->xww=height2width(cimg->yww,
cimg->width, cimg->height);
if (cimg->xww>cimg->wanted_xw)
{
/* Width too much, we nail the width */
cimg->xww=cimg->wanted_xw;
cimg->yww=width2height(cimg->xww,
cimg->width, cimg->height);
}
/* Some sanity checks */
if (cimg->xww<=0) cimg->xww=1;
if (cimg->yww<=0) cimg->yww=1;
}
}
if (!is_image_size_sane(cimg->xww, cimg->yww)) {
cimg->xww = cimg->width;
cimg->yww = cimg->height;
}
#ifdef HAVE_SVG
if (cimg->image_type == IM_SVG) {
/* SVG images are scaled using the cairo library, not the Links scaler */
cimg->width = cimg->xww;
cimg->height = cimg->yww;
}
#endif
if (cimg->width!=cimg->xww||cimg->height!=cimg->yww) cimg->strip_optimized=0;
cimg->gamma_stamp=gamma_stamp;
if (cimg->strip_optimized){
struct bitmap tmpbmp;
unsigned short *buf_16;
ssize_t i;
tmpbmp.x = (int)cimg->width;
tmpbmp.y = 1;
/* No buffer, bitmap is valid from the very beginning */
cimg->bmp.x = (int)cimg->width;
cimg->bmp.y = (int)cimg->height;
if (drv->get_empty_bitmap(&(cimg->bmp))) {
cimg->dregs = NULL;
goto skip_img;
}
if ((size_t)cimg->width > MAX_SIZE_T / sizeof(*buf_16) / 3) overalloc();
buf_16 = mem_alloc(sizeof(*buf_16) * 3 * (size_t)cimg->width);
round_color_sRGB_to_48(&red, &green, &blue
, cimg->background_color);
mix_one_color_48(buf_16,cimg->width, red, green, blue);
#ifdef DEBUG
if (cimg->height<=0){
fprintf(stderr,"cimg->height=%ld\n", (long)cimg->height);
internal_error("Invalid cimg->height in strip_optimized section of header_dimensions_known");
}
#endif /* #ifdef DEBUG */
/* The skip is uninitialized here and is read by dither_start
* but is not used in any malicious way so it doesn't matter
*/
tmpbmp.data = cimg->bmp.data;
tmpbmp.skip = cimg->bmp.skip;
cimg->dregs=dither_images?dither_start(buf_16,&tmpbmp):NULL;
tmpbmp.data=(unsigned char *)tmpbmp.data+cimg->bmp.skip;
if (cimg->dregs)
for (i=cimg->height-1;i;i--){
dither_restart(buf_16,&tmpbmp,cimg->dregs);
tmpbmp.data=(unsigned char *)tmpbmp.data+cimg->bmp.skip;
}
else
for (i=cimg->height-1;i;i--){
(*round_fn)(buf_16,&tmpbmp);
tmpbmp.data=(unsigned char *)tmpbmp.data+cimg->bmp.skip;
}
mem_free(buf_16);
skip_img:
drv->register_bitmap(&(cimg->bmp));
if(cimg->dregs) memset(cimg->dregs,0,cimg->width*sizeof(*cimg->dregs)*3);
cimg->bmp_used=1; /* Nonzero value */
/* This ensures the dregs are none and because strip
* optimization is unusable in interlaced pictures,
* this saves the zeroing out at the beginning of the
* decoder itself.
*/
}else {
cimg->rows_added=1;
cimg->bmp_used=0;
if (cimg->width && (size_t)cimg->width * (size_t)cimg->height / (size_t)cimg->width != (size_t)cimg->height) overalloc();
cimg->buffer = mem_alloc_mayfail((size_t)cimg->width * (size_t)cimg->height * (size_t)cimg->buffer_bytes_per_pixel);
if (!cimg->buffer)
return 1;
if (cimg->buffer_bytes_per_pixel==4
||cimg->buffer_bytes_per_pixel==4
*sizeof(unsigned short))
{
/* Make the buffer contain full transparency */
memset(cimg->buffer, 0, (size_t)cimg->width * (size_t)cimg->height * (size_t)cimg->buffer_bytes_per_pixel);
}else{
/* Fill the buffer with background color */
if (cimg->buffer_bytes_per_pixel > 4){
/* 16-bit */
unsigned short red, green, blue;
round_color_sRGB_to_48(&red, &green, &blue, cimg->background_color);
red=ags_16_to_16(red, (float)(cimg->red_gamma / (float)(user_gamma tcc_hack)));
green=ags_16_to_16(green, (float)(cimg->green_gamma / (float)(user_gamma tcc_hack)));
blue=ags_16_to_16(blue, (float)(cimg->blue_gamma / (float)(user_gamma tcc_hack)));
mix_one_color_48((unsigned short *)cimg->buffer, cimg->width*cimg->height, red, green, blue);
} else {
unsigned char rgb[3];
/* 8-bit */
compute_background_8(cimg, rgb);
mix_one_color_24(cimg->buffer, cimg->width * cimg->height, rgb[0], rgb[1], rgb[2]);
}
}
}
if (cimg->buffer_bytes_per_pixel<=4&&cimg->width*cimg->height>=1024){
make_gamma_table(cimg);
}else if (cimg->buffer_bytes_per_pixel>=6&&cimg->width*cimg->height>=262144){
make_gamma_table(cimg);
}else cimg->gamma_table=NULL;
cimg->state|=4; /* Update state */
return 0;
}
/* Fills "tmp" buffer with the resulting data and does not free the input
* buffer. May be called only in states 12 and 14 of cimg
*/
static unsigned short *buffer_to_16(unsigned short *tmp, struct cached_image *cimg, unsigned char *buffer, ssize_t height)
{
unsigned short red, green, blue;
#ifdef DEBUG
if (cimg->state != 12 && cimg->state != 14){
fprintf(stderr, "cimg->state=%d\n", cimg->state);
internal_error("invalid state in buffer_to_16");
}
#endif /* #ifdef DEBUG */
switch (cimg->buffer_bytes_per_pixel) {
case 3:
if (cimg->gamma_table) {
agx_24_to_48_table(tmp, buffer, cimg->width*height, cimg->gamma_table);
} else {
agx_24_to_48(tmp, buffer, cimg->width * height,
(float)((float)(user_gamma tcc_hack) / cimg->red_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->green_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->blue_gamma));
}
break;
case 3 * sizeof(unsigned short):
if (cimg->gamma_table) {
agx_48_to_48_table(tmp, (unsigned short *)buffer, cimg->width * height, cimg->gamma_table);
} else {
agx_48_to_48(tmp, (unsigned short *)buffer, cimg->width * height,
(float)((float)(user_gamma tcc_hack) / cimg->red_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->green_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->blue_gamma));
}
break;
/* Alpha's: */
case 4:
round_color_sRGB_to_48(&red,&green,&blue,cimg->background_color);
if (cimg->gamma_table) {
agx_and_uc_32_to_48_table(tmp, buffer, cimg->width * height, cimg->gamma_table, red, green, blue);
} else {
agx_and_uc_32_to_48(tmp,buffer, cimg->width*height,
(float)((float)(user_gamma tcc_hack) / cimg->red_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->green_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->blue_gamma),
red, green, blue);
}
break;
case 4 * sizeof(unsigned short):
round_color_sRGB_to_48(&red, &green, &blue, cimg->background_color);
if (cimg->gamma_table) {
agx_and_uc_64_to_48_table(tmp, (unsigned short *)buffer, cimg->width * height, cimg->gamma_table, red, green, blue);
} else {
agx_and_uc_64_to_48(tmp, (unsigned short *)buffer, cimg->width * height,
(float)((float)(user_gamma tcc_hack) / cimg->red_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->green_gamma),
(float)((float)(user_gamma tcc_hack) / cimg->blue_gamma),
red,green,blue);
}
break;
#ifdef DEBUG
default:
internal_error("buffer_to_16: unknown mem organization");
#endif /* #ifdef DEBUG */
}
return tmp;
}
/* Returns allocated buffer with the resulting data and does not free the input
* buffer. May be called only in states 12 and 14 of cimg
* use_strip: 1 if the image is already registered and prepare_strip and
* commit_strip is to be used
* 0: if the image is not yet registered and instead one big register_bitmap
* will be used eventually
* dregs must be externally allocated and contain required value or must be
* NULL.
* if !dregs then rounding is performed instead of dithering.
* dregs are not freed.
* bottom dregs are placed back into dregs.
* Before return the bitmap will be in registered state and changes will be
* commited.
* height must be >=1 !!!
*/
void buffer_to_bitmap_incremental(struct cached_image *cimg,
unsigned char *buffer, ssize_t height, ssize_t yoff, int *dregs, int use_strip)
{
#define max_height 16
/* max_height must be at least 1 */
unsigned short *tmp;
struct bitmap tmpbmp;
ssize_t add1 = 0, add2;
#ifdef DEBUG
if (cimg->state!=12&&cimg->state!=14){
fprintf(stderr,"cimg->state=%d\n",cimg->state);
internal_error("Invalid state in buffer_to_bitmap_incremental\n");
}
if (height<1){
fprintf(stderr,"height=%ld\n", (long)height);
internal_error("Invalid height in buffer_to_bitmap_incremental\n");
}
if (cimg->width<1||cimg->height<1){
fprintf(stderr,"cimg->width=%ld, cimg->height=%ld\n", (long)cimg->width, (long)cimg->height);
internal_error("Invalid cimg->width x cimg->height in\
buffer_to_bitmap_incremental");
}
#endif /* #ifdef DEBUG */
if ((size_t)cimg->width > MAX_SIZE_T / (size_t)max_height / 3 / sizeof(*tmp)) overalloc();
tmp=mem_alloc((size_t)cimg->width * (size_t)(height < max_height ? height : max_height) * 3 * sizeof(*tmp));
/* Prepare a fake bitmap for dithering */
tmpbmp.x = (int)cimg->width;
if (!use_strip){
tmpbmp.data=(unsigned char *)cimg->bmp.data+cimg->bmp.skip*yoff;
add1=cimg->bmp.skip*max_height;
}
add2=cimg->buffer_bytes_per_pixel*cimg->width*max_height;
not_enough:
tmpbmp.y = (int)(height < max_height ? height : max_height);
if (use_strip) {
tmpbmp.data = drv->prepare_strip(&(cimg->bmp), (int)yoff, tmpbmp.y);
if (!tmpbmp.data) goto prepare_failed;
}
tmpbmp.skip=cimg->bmp.skip;
buffer_to_16(tmp, cimg, buffer, tmpbmp.y);
if (dregs) {
dither_restart(tmp, &tmpbmp, dregs);
} else {
(*round_fn)(tmp, &tmpbmp);
}
if (use_strip) {
prepare_failed:
drv->commit_strip(&(cimg->bmp), (int)yoff, tmpbmp.y);
}
height-=tmpbmp.y;
if (!height) goto end;
buffer+=add2;
yoff+=tmpbmp.y;
tmpbmp.data=(unsigned char *)tmpbmp.data+add1;
/* This has no effect if use_strip but it's faster
* to add to bogus value than to play with
* conditional jumps.
*/
goto not_enough;
end:
mem_free(tmp);
if (!use_strip) drv->register_bitmap(&(cimg->bmp));
}
/* Takes the buffer and resamples the data into the bitmap. Automatically
* destroys the previous bitmap. Must be called only when cimg->buffer is valid.
* Sets bmp_used to non-zero.
* If gamma_table is used, it must be still allocated here (take care if you
* want to destroy gamma table and call buffer_to_bitmap, first call buffer_to_bitmap
* and then destroy gamma_table).
*/
static void buffer_to_bitmap(struct cached_image *cimg)
{
unsigned short *tmp, *tmp1;
ssize_t ix, iy, ox, oy;
int gonna_be_smart;
int *dregs;
#ifdef DEBUG
if(cimg->state!=12&&cimg->state!=14){
fprintf(stderr,"cimg->state=%d\n",cimg->state);
internal_error("buffer_to_bitmap called in invalid state");
}
if (cimg->strip_optimized) internal_error("strip_optimized in buffer_to_bitmap");
if (cimg->width<1||cimg->height<1){
fprintf(stderr,"cimg->width=%ld, cimg->height=%ld\n", (long)cimg->width, (long)cimg->height);
internal_error("Invalid cimg->width x cimg->height in buffer_to_bitmap");
}
#endif /* #ifdef DEBUG */
if (!cimg->rows_added) return;
/* Here of course width and height must be already filled */
cimg->rows_added=0;
ix=cimg->width;
iy=cimg->height;
ox=cimg->xww;
oy=cimg->yww;
if (ix==ox&&iy==oy) gonna_be_smart=1;
else{
gonna_be_smart=0;
if (ix && (size_t)ix * (size_t)iy / (size_t)ix != (size_t)iy) overalloc();
if ((size_t)ix * (size_t)iy > MAX_SIZE_T / sizeof(*tmp) / 3) overalloc();
tmp = mem_alloc_mayfail((size_t)ix * (size_t)iy * 3 * sizeof(*tmp));
if (tmp) buffer_to_16(tmp, cimg, cimg->buffer, iy);
if (!cimg->decoder){
mem_free_buffer(cimg);
cimg->buffer=NULL;
}
/* Scale the image to said size */
#ifdef DEBUG
if (ox<=0||oy<=0){
internal_error("ox or oy <=0 before resampling image");
}
#endif /* #ifdef DEBUG */
if (tmp && (ix!=ox || iy!=oy)) {
/* We must really scale */
tmp1=tmp;
scale_color(tmp1,ix,iy,&tmp,ox,oy);
}
}
if (cimg->bmp_used) drv->unregister_bitmap(&cimg->bmp);
cimg->bmp.x = (int)ox;
cimg->bmp.y = (int)oy;
if (drv->get_empty_bitmap(&(cimg->bmp))) {
if (!gonna_be_smart) {
if (tmp) {
mem_free(tmp);
}
}
goto bitmap_failed;
}
if (gonna_be_smart){
if (dither_images) {
if ((size_t)cimg->width > MAX_SIZE_T / 3 / sizeof(*dregs)) overalloc();
dregs = mem_calloc(sizeof(*dregs) * 3 * (size_t)cimg->width);
} else {
dregs = NULL;
}
buffer_to_bitmap_incremental(cimg, cimg->buffer, cimg->height,
0, dregs, 0);
if (dregs) mem_free(dregs);
}else{
if (tmp) {
if (dither_images)
dither(tmp, &cimg->bmp);
else
(*round_fn)(tmp, &cimg->bmp);
mem_free(tmp);
} else {
int i;
unsigned char *ptr = cimg->bmp.data;
for (i = 0; i < cimg->bmp.y; i++) {
memset(ptr, 0, cimg->bmp.x * (drv->depth & 7));
ptr += cimg->bmp.skip;
}
}
bitmap_failed:
drv->register_bitmap(&(cimg->bmp));
}
cimg->bmp_used=1;
/* Indicate that the bitmap is valid. The value is just any
nonzero value */
cimg->rows_added=0;
/* Indicate the bitmap is up-to-date */
}
/* Performs state transition for end of stream or error in image or
* end of image */
void img_end(struct cached_image *cimg)
{
switch(cimg->state){
case 12:
case 14:
if (cimg->strip_optimized){
if (cimg->dregs) mem_free(cimg->dregs);
}
else{
buffer_to_bitmap(cimg);
mem_free_buffer(cimg);
}
if (cimg->gamma_table) mem_free(cimg->gamma_table);
/*-fallthrough*/
case 8:
case 10:
destroy_decoder(cimg);
case 0:
case 1:
case 2:
case 3:
case 9:
case 11:
case 13:
case 15:
break;
#ifdef DEBUG
default:
fprintf(stderr,"state=%d\n",cimg->state);
internal_error("Invalid state encountered in end");
#endif /* #ifdef DEBUG */
}
cimg->state|=1;
}
static void r3l0ad(struct cached_image *cimg, struct g_object_image *goi)
{
cimg->eof_hit=0;
cimg->last_count=goi->af->rq->ce->count;
cimg->last_count2=goi->af->rq->ce->count2;
cimg->gamma_stamp=gamma_stamp;
switch(cimg->state){
case 8:
case 10:
destroy_decoder(cimg);
case 1:
case 3:
case 9:
case 11:
case 0:
case 2:
break;
case 12:
if (cimg->gamma_table) mem_free(cimg->gamma_table);
destroy_decoder(cimg);
if (cimg->strip_optimized){
if (cimg->dregs) mem_free(cimg->dregs);
}else{
mem_free_buffer(cimg);
}
if (cimg->bmp_used){
case 13:
drv->unregister_bitmap(&cimg->bmp);
}
cimg->xww=img_scale_h(cimg->scale, cimg->wanted_xw<0?32:cimg->wanted_xw);
cimg->yww=img_scale_v(cimg->scale, cimg->wanted_yw<0?32:cimg->wanted_yw);
break;
case 14:
if (cimg->gamma_table) mem_free(cimg->gamma_table);
destroy_decoder(cimg);
if (cimg->strip_optimized){
if (cimg->dregs) mem_free(cimg->dregs);
}else{
mem_free_buffer(cimg);
}
if (cimg->bmp_used){
case 15:
drv->unregister_bitmap(&cimg->bmp);
}
break;
#ifdef DEBUG
default:
fprintf(stderr,"cimg->state=%d\n",cimg->state);
internal_error("Invalid state in r3l0ad()");
#endif /* #ifdef DEBUG */
}
cimg->state&=2;
}
/* Returns 1 if match.
* If doesn't return 1 then returns 0
*/
static inline int dtest(unsigned char *templat, unsigned char *test)
{
return !casestrcmp(templat, test);
}
/* This may be called only in state 0 or 2 */
static void type(struct cached_image *cimg, unsigned char *content_type, unsigned char *data /* at least 4 bytes */)
{
#ifdef DEBUG
if (cimg->state!=0&&cimg->state!=2){
fprintf(stderr,"cimg->state=%d\n",cimg->state);
internal_error("Invalid state encountered in type()");
}
#endif /* #ifdef DEBUG */
#ifdef HAVE_JPEG
if (data[0] == 0xff && data[1] == 0xd8)
goto have_jpeg;
#endif
#ifdef HAVE_TIFF
if (data[0] == 'I' && data[1] == 'I')
goto have_tiff;
if (data[0] == 'M' && data[1] == 'M')
goto have_tiff;
#endif
#ifdef HAVE_SVG
if (data[0] == '<' && data[1] == '?')
goto have_svg;
#endif
if (data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G')
goto have_png;
if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F')
goto have_gif;
#ifdef HAVE_JPEG
if (dtest(cast_uchar "image/jpeg",content_type) ||
dtest(cast_uchar "image/jpg",content_type) ||
dtest(cast_uchar "image/jpe",content_type) ||
dtest(cast_uchar "image/pjpe",content_type) ||
dtest(cast_uchar "image/pjpeg",content_type) ||
dtest(cast_uchar "image/pjpg",content_type)) {
have_jpeg:
cimg->image_type=IM_JPG;
jpeg_start(cimg);
} else
#endif /* #ifdef HAVE_JPEG */
if (dtest(cast_uchar "image/png",content_type) ||
dtest(cast_uchar "image/x-png",content_type)) {
have_png:
cimg->image_type=IM_PNG;
png_start(cimg);
} else if (dtest(cast_uchar "image/gif",content_type)){
have_gif:
cimg->image_type=IM_GIF;
gif_start(cimg);
} else if (dtest(cast_uchar "image/x-xbitmap",content_type)){
cimg->image_type=IM_XBM;
xbm_start(cimg);
} else
#ifdef HAVE_TIFF
if (dtest(cast_uchar "image/tiff",content_type) ||
dtest(cast_uchar "image/tif",content_type)) {
have_tiff:
cimg->image_type=IM_TIFF;
tiff_start(cimg);
} else
#endif /* #ifdef HAVE_TIFF */
#ifdef HAVE_SVG
if (dtest(cast_uchar "image/svg+xml",content_type) ||
dtest(cast_uchar "image/svg",content_type)) {
have_svg:
cimg->image_type=IM_SVG;
svg_start(cimg);
} else
#endif /* #ifdef HAVE_SVG */
{
/* Error */
img_end(cimg);
return;
}
cimg->state|=8; /* Advance the state according to the table in
links-doc.html */
cimg->last_length=0;
}
/* Doesn't print anything. Downloads more data if available.
* Sets up cimg->reparse and cimg->xww and cimg->yww accordingly to
* the state of the decoder. When changing xww and yww also changes xw and yw
* in g_object_image.
* return value 1 means the data were chopped and the caller shall not redraw
* (because it would be too slow and because we are probably choked
* up with the data)
*/
static int img_process_download(struct g_object_image *goi, struct f_data_c *fdatac)
{
unsigned char *data, *ctype;
size_t total_len;
struct cached_image *cimg = goi->cimg;
int chopped=0;
#ifdef DEBUG
if (!goi->af) internal_error("NULL goi->af in process_download\n");
if (cimg->state>=16){ /* Negative don't occur because it's unsigned char */
fprintf(stderr,"cimg->state=%d\n",cimg->state);
internal_error("Invalid cimg->state in img_process_download\n");
}
#endif /* #ifdef DEBUG */
if (!goi->af->rq) return 0;
if (get_file(goi->af->rq, &data, &total_len)) goto end;
if (total_len < 4) goto end;
if (total_len > MAXINT) total_len = MAXINT;
/*fprintf(stderr, "processing: %s\n", goi->af->rq->ce->url);*/
if (goi->af->rq->ce->count2!=cimg->last_count2||
(goi->af->rq->ce->count!=cimg->last_count && cimg->eof_hit) ||
(cimg->state>=12&&gamma_stamp!=cimg->gamma_stamp)){
/* Reload */
r3l0ad(cimg,goi);
}
/*if (!goi->af->rq->ce->head) goto end;*/ /* Mikulas: head muze byt NULL*/ /* Mikulas: tak se to zpracuje a nebude se skakat na konec, kdyz je to NULL */
if (cimg->state==0||cimg->state==2){
/* Type still unknown */
ctype=get_content_type(goi->af->rq->ce->head,
goi->af->rq->url);
if (!ctype) ctype = stracpy(cast_uchar "application/octet-stream");
type(cimg,ctype,data);
mem_free(ctype);
}
/* Now, if we are in state where decoder is running (8, 10, 12, 14), we may feed
* some data into it.
*/
if (!((cimg->state^8)&9)){
int length = (int)total_len;
if (length<=cimg->last_length) goto end; /* No new data */
data+=cimg->last_length;
length-=cimg->last_length;
if (length>RESTART_SIZE){
length=RESTART_SIZE;
chopped=1;
if (fdatac) {
refresh_image(fdatac, &goi->goti.go, 0);
}
}
/* Decoder has been already started */
switch(cimg->image_type){
case IM_PNG:
png_restart(cimg,data,length);
break;
#ifdef HAVE_JPEG
case IM_JPG:
jpeg_restart(cimg,data,length);
break;
#endif /* #ifdef HAVE_JPEG */
case IM_XBM:
xbm_restart(cimg,data,length);
break;
case IM_GIF:
gif_restart(data,length);
break;
#ifdef HAVE_TIFF
case IM_TIFF:
tiff_restart(cimg,data,length);
break;
#endif /* #ifdef HAVE_TIFF */
#ifdef HAVE_SVG
case IM_SVG:
svg_restart(cimg,data,length);
break;
#endif /* #ifdef HAVE_SVG */
#ifdef DEBUG
default:
fprintf(stderr,"cimg->image_type=%d\n",cimg->state);
internal_error("Invalid image_type encountered when processing data in\
img_process_download.\n");
#endif /* #ifdef DEBUG */
}
cimg->last_length+=length;
}
end:
/* Test end */
if (!is_entry_used(goi->af->rq->ce) && (goi->af->rq->state < 0
||(goi->af->rq->ce&&goi->af->rq->stat.state<0))){
/* We must not perform end with chopped because some
* unprocessed data still wait for us :)
*/
if (!chopped){
if (!((cimg->state^8)&9)) {
#ifdef HAVE_TIFF
if (cimg->image_type==IM_TIFF)
tiff_finish(cimg);
#endif
#ifdef HAVE_SVG
if (cimg->image_type==IM_SVG)
svg_finish(cimg);
#endif
}
cimg->eof_hit=1;
if (goi->af->rq->ce)
cimg->last_count=goi->af->rq->ce->count;
img_end(cimg);
}
} else if (!chopped) {
if (fdatac) {
if (f_is_finished(fdatac->f_data)) {
refresh_image(fdatac, &goi->goti.go, 2000);
} else {
/*
* Fix a bug - if we have a text file with html and built-in image using
* the data:// url. If we press '\' to toggle the view from text to
* html, the image is not displayed correctly. We need to reset
* fdatac->done to force re-parse.
*/
fdatac->done = 0;
}
}
}
return chopped;
}
/* Input: rgb (sRGB) triplet (0...255)
* Returns a color that is very contrasty on that background sRGB color
*/
int get_foreground(int rgb)
{
int r,g,b;
r=(rgb>>16)&255;
g=(rgb>>8)&255;
b=rgb&255;
r=r<128?255:0;
g=g<128?255:0;
b=b<128?255:0;
return (r<<16)|(g<<8)|b;
}
static void draw_frame_mark(struct graphics_device *dev, int x, int y, int xw, int yw, long bg, long fg, int broken)
{
#ifdef DEBUG
if (xw<1||yw<1) internal_error("zero dimension in draw_frame_mark");
#endif /* #ifdef DEBUG */
if (broken == 1){
/* Draw between ( 0 and 1/4 ) and ( 3/4 and 1 ) of each
* side (0-1)
*/
int xl, xh, yl, yh;
xh=xw-(xl=xw>>2);
yh=yw-(yl=yw>>2);