-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtkImgPhInstance.c
More file actions
2032 lines (1804 loc) · 55 KB
/
Copy pathtkImgPhInstance.c
File metadata and controls
2032 lines (1804 loc) · 55 KB
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
/*
* tkImgPhInstance.c --
*
* Implements the rendering of images of type "photo" for Tk. Photo
* images are stored in full color (32 bits per pixel including alpha
* channel) and displayed using dithering if necessary.
*
* Copyright © 1994 The Australian National University.
* Copyright © 1994-1997 Sun Microsystems, Inc.
* Copyright © 2002-2008 Donal K. Fellows
* Copyright © 2003 ActiveState Corporation.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* Author: Paul Mackerras (paulus@cs.anu.edu.au),
* Department of Computer Science,
* Australian National University.
*/
#include "tkImgPhoto.h"
#include "tkPort.h"
#ifdef _WIN32
#include "tkWinInt.h"
#endif
/*
* Declaration for internal Xlib function used here:
*/
#if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(MAC_OSX_TK)
#ifdef __cplusplus
extern "C" {
#endif
extern int _XInitImageFuncPtrs(XImage *image);
#ifdef __cplusplus
}
#endif
#endif
/*
* Forward declarations
*/
#ifndef TK_CAN_RENDER_RGBA
static void BlendComplexAlpha(XImage *bgImg, PhotoInstance *iPtr,
int xOffset, int yOffset, int width, int height);
#endif
static int IsValidPalette(PhotoInstance *instancePtr,
const char *palette);
static int CountBits(unsigned mask);
static void GetColorTable(PhotoInstance *instancePtr);
static void FreeColorTable(ColorTable *colorPtr, int force);
static void AllocateColors(ColorTable *colorPtr);
static void DisposeColorTable(void *clientData);
static int ReclaimColors(ColorTableId *id, int numColors);
/*
* Hash table used to hash from (display, colormap, palette, gamma) to
* ColorTable address.
*/
static Tcl_HashTable imgPhotoColorHash;
static bool imgPhotoColorHashInitialized;
#define N_COLOR_HASH (sizeof(ColorTableId) / sizeof(int))
/*
*----------------------------------------------------------------------
*
* TkImgPhotoConfigureInstance --
*
* This function is called to create displaying information for a photo
* image instance based on the configuration information in the model.
* It is invoked both when new instances are created and when the model
* is reconfigured.
*
* Results:
* None.
*
* Side effects:
* Generates errors via Tcl_BackgroundException if there are problems in
* setting up the instance.
*
*----------------------------------------------------------------------
*/
void
TkImgPhotoConfigureInstance(
PhotoInstance *instancePtr) /* Instance to reconfigure. */
{
PhotoModel *modelPtr = instancePtr->modelPtr;
XImage *imagePtr;
int bitsPerPixel;
ColorTable *colorTablePtr;
XRectangle validBox;
/*
* If the -palette configuration option has been set for the model, use
* the value specified for our palette, but only if it is a valid palette
* for our windows. Use the gamma value specified the model.
*/
if ((modelPtr->palette && modelPtr->palette[0])
&& IsValidPalette(instancePtr, modelPtr->palette)) {
instancePtr->palette = modelPtr->palette;
} else {
instancePtr->palette = instancePtr->defaultPalette;
}
instancePtr->gamma = modelPtr->gamma;
/*
* If we don't currently have a color table, or if the one we have no
* longer applies (e.g. because our palette or gamma has changed), get a
* new one.
*/
colorTablePtr = instancePtr->colorTablePtr;
if ((colorTablePtr == NULL)
|| (instancePtr->colormap != colorTablePtr->id.colormap)
|| (instancePtr->palette != colorTablePtr->id.palette)
|| (instancePtr->gamma != colorTablePtr->id.gamma)) {
/*
* Free up our old color table, and get a new one.
*/
if (colorTablePtr != NULL) {
colorTablePtr->liveRefCount--;
FreeColorTable(colorTablePtr, 0);
}
GetColorTable(instancePtr);
/*
* Create a new XImage structure for sending data to the X server, if
* necessary.
*/
if (instancePtr->colorTablePtr->flags & BLACK_AND_WHITE) {
bitsPerPixel = 1;
} else {
bitsPerPixel = instancePtr->visualInfo.depth;
}
if ((instancePtr->imagePtr == NULL)
|| (instancePtr->imagePtr->bits_per_pixel != bitsPerPixel)) {
if (instancePtr->imagePtr != NULL) {
XDestroyImage(instancePtr->imagePtr);
}
imagePtr = XCreateImage(instancePtr->display,
instancePtr->visualInfo.visual, (unsigned) bitsPerPixel,
(bitsPerPixel > 1? ZPixmap: XYBitmap), 0, NULL,
1, 1, 32, 0);
instancePtr->imagePtr = imagePtr;
/*
* We create images using the local host's endianness, rather than
* the endianness of the server; otherwise we would have to
* byte-swap any 16 or 32 bit values that we store in the image
* if the server's endianness is different from ours.
*/
if (imagePtr != NULL) {
#ifdef WORDS_BIGENDIAN
imagePtr->byte_order = MSBFirst;
#else
imagePtr->byte_order = LSBFirst;
#endif
_XInitImageFuncPtrs(imagePtr);
}
}
}
/*
* If the user has specified a width and/or height for the model which is
* different from our current width/height, set the size to the values
* specified by the user. If we have no pixmap, we do this also, since it
* has the side effect of allocating a pixmap for us.
*/
if ((instancePtr->pixels == None) || (instancePtr->error == NULL)
|| (instancePtr->width != modelPtr->width)
|| (instancePtr->height != modelPtr->height)) {
TkImgPhotoInstanceSetSize(instancePtr);
}
/*
* Redither this instance if necessary.
*/
if ((modelPtr->flags & IMAGE_CHANGED)
|| (instancePtr->colorTablePtr != colorTablePtr)) {
XClipBox(modelPtr->validRegion, &validBox);
if ((validBox.width > 0) && (validBox.height > 0)) {
TkImgDitherInstance(instancePtr, validBox.x, validBox.y,
validBox.width, validBox.height);
}
}
}
/*
*----------------------------------------------------------------------
*
* TkImgPhotoGet --
*
* This function is called for each use of a photo image in a widget.
*
* Results:
* The return value is a token for the instance, which is passed back to
* us in calls to TkImgPhotoDisplay and ImgPhotoFree.
*
* Side effects:
* A data structure is set up for the instance (or, an existing instance
* is re-used for the new one).
*
*----------------------------------------------------------------------
*/
void *
TkImgPhotoGet(
Tk_Window tkwin, /* Window in which the instance will be
* used. */
void *modelData) /* Pointer to our model structure for the
* image. */
{
PhotoModel *modelPtr = (PhotoModel *)modelData;
PhotoInstance *instancePtr;
Colormap colormap;
bool mono;
int nRed, nGreen, nBlue, numVisuals;
XVisualInfo visualInfo, *visInfoPtr;
char buf[TCL_INTEGER_SPACE * 3];
XColor *white, *black;
XGCValues gcValues;
#if (!defined(_WIN32) && !defined(MAC_OSX_TK))
int gcmask;
#endif
/*
* Table of "best" choices for palette for PseudoColor displays with
* between 3 and 15 bits/pixel.
*/
static const int paletteChoice[13][3] = {
/* #red, #green, #blue */
{2, 2, 2, /* 3 bits, 8 colors */},
{2, 3, 2, /* 4 bits, 12 colors */},
{3, 4, 2, /* 5 bits, 24 colors */},
{4, 5, 3, /* 6 bits, 60 colors */},
{5, 6, 4, /* 7 bits, 120 colors */},
{7, 7, 4, /* 8 bits, 198 colors */},
{8, 10, 6, /* 9 bits, 480 colors */},
{10, 12, 8, /* 10 bits, 960 colors */},
{14, 15, 9, /* 11 bits, 1890 colors */},
{16, 20, 12, /* 12 bits, 3840 colors */},
{20, 24, 16, /* 13 bits, 7680 colors */},
{26, 30, 20, /* 14 bits, 15600 colors */},
{32, 32, 30, /* 15 bits, 30720 colors */}
};
/*
* See if there is already an instance for windows using the same
* colormap. If so then just re-use it.
*/
colormap = Tk_Colormap(tkwin);
for (instancePtr = modelPtr->instancePtr; instancePtr != NULL;
instancePtr = instancePtr->nextPtr) {
if ((colormap == instancePtr->colormap)
&& (Tk_Display(tkwin) == instancePtr->display)
#if (!defined(_WIN32) && !defined(MAC_OSX_TK))
&& (Tk_Visual(tkwin) == instancePtr->visualInfo.visual)
#endif
) {
/*
* Re-use this instance.
*/
if (instancePtr->refCount == 0) {
/*
* We are resurrecting this instance.
*/
Tcl_CancelIdleCall(TkImgDisposeInstance, instancePtr);
if (instancePtr->colorTablePtr != NULL) {
FreeColorTable(instancePtr->colorTablePtr, 0);
}
GetColorTable(instancePtr);
}
instancePtr->refCount++;
return instancePtr;
}
}
/*
* The image isn't already in use in a window with the same colormap. Make
* a new instance of the image.
*/
instancePtr = (PhotoInstance *)ckalloc(sizeof(PhotoInstance));
instancePtr->modelPtr = modelPtr;
instancePtr->display = Tk_Display(tkwin);
instancePtr->colormap = Tk_Colormap(tkwin);
Tk_PreserveColormap(instancePtr->display, instancePtr->colormap);
instancePtr->refCount = 1;
instancePtr->colorTablePtr = NULL;
instancePtr->pixels = None;
instancePtr->error = NULL;
instancePtr->width = 0;
instancePtr->height = 0;
instancePtr->imagePtr = 0;
instancePtr->nextPtr = modelPtr->instancePtr;
modelPtr->instancePtr = instancePtr;
/*
* Obtain information about the visual and decide on the default palette.
*/
visualInfo.screen = Tk_ScreenNumber(tkwin);
visualInfo.visualid = XVisualIDFromVisual(Tk_Visual(tkwin));
visInfoPtr = XGetVisualInfo(Tk_Display(tkwin),
VisualScreenMask | VisualIDMask, &visualInfo, &numVisuals);
if (visInfoPtr == NULL) {
Tcl_Panic("TkImgPhotoGet couldn't find visual for window");
}
nRed = 2;
nGreen = nBlue = 0;
mono = true;
instancePtr->visualInfo = *visInfoPtr;
#if (!defined(_WIN32) && !defined(MAC_OSX_TK))
gcmask = 0;
instancePtr->visualInfo.visual = Tk_Visual(tkwin);
#endif
switch (visInfoPtr->c_class) {
case DirectColor:
case TrueColor:
nRed = 1 << CountBits(visInfoPtr->red_mask);
nGreen = 1 << CountBits(visInfoPtr->green_mask);
nBlue = 1 << CountBits(visInfoPtr->blue_mask);
mono = false;
#if (!defined(_WIN32) && !defined(MAC_OSX_TK))
if (visInfoPtr->depth > 24) {
gcValues.plane_mask = visInfoPtr->red_mask
| visInfoPtr->green_mask
| visInfoPtr->blue_mask;
gcmask = GCPlaneMask;
}
#endif
break;
case PseudoColor:
case StaticColor:
if (visInfoPtr->depth > 15) {
nRed = 32;
nGreen = 32;
nBlue = 32;
mono = false;
} else if (visInfoPtr->depth >= 3) {
const int *ip = paletteChoice[visInfoPtr->depth - 3];
nRed = ip[0];
nGreen = ip[1];
nBlue = ip[2];
mono = false;
}
break;
case GrayScale:
case StaticGray:
nRed = 1 << visInfoPtr->depth;
break;
}
XFree(visInfoPtr);
if (mono) {
snprintf(buf, sizeof(buf), "%d", nRed);
} else {
snprintf(buf, sizeof(buf), "%d/%d/%d", nRed, nGreen, nBlue);
}
instancePtr->defaultPalette = Tk_GetUid(buf);
/*
* Make a GC with background = black and foreground = white.
*/
white = Tk_GetColor(modelPtr->interp, tkwin, "white");
black = Tk_GetColor(modelPtr->interp, tkwin, "black");
gcValues.foreground = (white != NULL)? white->pixel:
WhitePixelOfScreen(Tk_Screen(tkwin));
gcValues.background = (black != NULL)? black->pixel:
BlackPixelOfScreen(Tk_Screen(tkwin));
Tk_FreeColor(white);
Tk_FreeColor(black);
gcValues.graphics_exposures = False;
#if (!defined(_WIN32) && !defined(MAC_OSX_TK))
instancePtr->gc = Tk_GetGC(tkwin,
gcmask|GCForeground|GCBackground|GCGraphicsExposures, &gcValues);
#else
instancePtr->gc = Tk_GetGC(tkwin,
GCForeground|GCBackground|GCGraphicsExposures, &gcValues);
#endif
/*
* Set configuration options and finish the initialization of the
* instance. This will also dither the image if necessary.
*/
TkImgPhotoConfigureInstance(instancePtr);
/*
* If this is the first instance, must set the size of the image.
*/
if (instancePtr->nextPtr == NULL) {
Tk_ImageChanged(modelPtr->tkModel, 0, 0, 0, 0,
modelPtr->width, modelPtr->height);
}
return instancePtr;
}
/*
*----------------------------------------------------------------------
*
* BlendComplexAlpha --
*
* This function is called when an image with partially transparent
* pixels must be drawn over another image. It blends the photo data onto
* a local copy of the surface that we are drawing on, *including* the
* pixels drawn by everything that should be drawn underneath the image.
*
* Much of this code has hard-coded values in for speed because this
* routine is performance critical for complex image drawing.
*
* Results:
* None.
*
* Side effects:
* Background image passed in gets drawn over with image data.
*
* Notes:
* This should work on all platforms that set mask and shift data
* properly from the visualInfo. RGB is really only a 24+ bpp version
* whereas RGB15 is the correct version and works for 15bpp+, but it
* slower, so it's only used for 15bpp+.
*
* Note that Win32 pre-defines those operations that we really need.
*
*----------------------------------------------------------------------
*/
#ifndef TK_CAN_RENDER_RGBA
#ifndef _WIN32
#define GetRValue(rgb) (UCHAR(((rgb) & red_mask) >> red_shift))
#define GetGValue(rgb) (UCHAR(((rgb) & green_mask) >> green_shift))
#define GetBValue(rgb) (UCHAR(((rgb) & blue_mask) >> blue_shift))
#define RGB(r, g, b) ((unsigned)( \
(UCHAR(r) << red_shift) | \
(UCHAR(g) << green_shift) | \
(UCHAR(b) << blue_shift) ))
#define RGB15(r, g, b) ((unsigned)( \
(((r) * red_mask / 255) & red_mask) | \
(((g) * green_mask / 255) & green_mask) | \
(((b) * blue_mask / 255) & blue_mask) ))
#endif /* !_WIN32 */
static void
BlendComplexAlpha(
XImage *bgImg, /* Background image to draw on. */
PhotoInstance *iPtr, /* Image instance to draw. */
int xOffset, int yOffset, /* X & Y offset into image instance to
* draw. */
int width, int height) /* Width & height of image to draw. */
{
int x, y, line;
unsigned long pixel;
unsigned char r, g, b, alpha, unalpha, *modelPtr;
unsigned char *alphaAr = iPtr->modelPtr->pix32;
/*
* This blending is an integer version of the Source-Over compositing rule
* (see Porter&Duff, "Compositing Digital Images", proceedings of SIGGRAPH
* 1984) that has been hard-coded (for speed) to work with targetting a
* solid surface.
*
* The 'unalpha' field must be 255-alpha; it is separated out to encourage
* more efficient compilation.
*/
#define ALPHA_BLEND(bgPix, imgPix, alpha, unalpha) \
((bgPix * unalpha + imgPix * alpha) / 255)
/*
* We have to get the mask and shift info from the visual on non-Win32 so
* that the macros Get*Value(), RGB() and RGB15() work correctly. This
* might be cached for better performance.
*/
#ifndef _WIN32
unsigned long red_mask, green_mask, blue_mask;
unsigned long red_shift, green_shift, blue_shift;
Visual *visual = iPtr->visualInfo.visual;
red_mask = visual->red_mask;
green_mask = visual->green_mask;
blue_mask = visual->blue_mask;
red_shift = 0;
green_shift = 0;
blue_shift = 0;
while ((0x0001 & (red_mask >> red_shift)) == 0) {
red_shift++;
}
while ((0x0001 & (green_mask >> green_shift)) == 0) {
green_shift++;
}
while ((0x0001 & (blue_mask >> blue_shift)) == 0) {
blue_shift++;
}
#endif /* !_WIN32 */
/*
* Only UNIX requires the special case for <24bpp. It varies with 3 extra
* shifts and uses RGB15. The 24+bpp version could also then be further
* optimized.
*/
#if !defined(_WIN32)
if (bgImg->depth < 24) {
unsigned char red_mlen, green_mlen, blue_mlen;
red_mlen = 8 - CountBits(red_mask >> red_shift);
green_mlen = 8 - CountBits(green_mask >> green_shift);
blue_mlen = 8 - CountBits(blue_mask >> blue_shift);
for (y = 0; y < height; y++) {
line = (y + yOffset) * iPtr->modelPtr->width;
for (x = 0; x < width; x++) {
modelPtr = alphaAr + ((line + x + xOffset) * 4);
alpha = modelPtr[3];
/*
* Ignore pixels that are fully transparent
*/
if (alpha) {
/*
* We could perhaps be more efficient than XGetPixel for
* 24 and 32 bit displays, but this seems "fast enough".
*/
r = modelPtr[0];
g = modelPtr[1];
b = modelPtr[2];
if (alpha != 255) {
/*
* Only blend pixels that have some transparency
*/
unsigned char ra, ga, ba;
pixel = XGetPixel(bgImg, x, y);
ra = GetRValue(pixel) << red_mlen;
ga = GetGValue(pixel) << green_mlen;
ba = GetBValue(pixel) << blue_mlen;
unalpha = 255 - alpha; /* Calculate once. */
r = ALPHA_BLEND(ra, r, alpha, unalpha);
g = ALPHA_BLEND(ga, g, alpha, unalpha);
b = ALPHA_BLEND(ba, b, alpha, unalpha);
}
XPutPixel(bgImg, x, y, RGB15(r, g, b));
}
}
}
return;
}
#endif /* !_WIN32 */
for (y = 0; y < height; y++) {
line = (y + yOffset) * iPtr->modelPtr->width;
for (x = 0; x < width; x++) {
modelPtr = alphaAr + ((line + x + xOffset) * 4);
alpha = modelPtr[3];
/*
* Ignore pixels that are fully transparent
*/
if (alpha) {
/*
* We could perhaps be more efficient than XGetPixel for 24
* and 32 bit displays, but this seems "fast enough".
*/
r = modelPtr[0];
g = modelPtr[1];
b = modelPtr[2];
if (alpha != 255) {
/*
* Only blend pixels that have some transparency
*/
unsigned char ra, ga, ba;
pixel = XGetPixel(bgImg, x, y);
ra = GetRValue(pixel);
ga = GetGValue(pixel);
ba = GetBValue(pixel);
unalpha = 255 - alpha; /* Calculate once. */
r = ALPHA_BLEND(ra, r, alpha, unalpha);
g = ALPHA_BLEND(ga, g, alpha, unalpha);
b = ALPHA_BLEND(ba, b, alpha, unalpha);
}
XPutPixel(bgImg, x, y, RGB(r, g, b));
}
}
}
#undef ALPHA_BLEND
}
#endif /* TK_CAN_RENDER_RGBA */
/*
*----------------------------------------------------------------------
*
* TkImgPhotoDisplay --
*
* This function is invoked to draw a photo image.
*
* Results:
* None.
*
* Side effects:
* A portion of the image gets rendered in a pixmap or window.
*
*----------------------------------------------------------------------
*/
void
TkImgPhotoDisplay(
void *clientData, /* Pointer to PhotoInstance structure for
* instance to be displayed. */
Display *display, /* Display on which to draw image. */
Drawable drawable, /* Pixmap or window in which to draw image. */
int imageX, int imageY, /* Upper-left corner of region within image to
* draw. */
int width, int height, /* Dimensions of region within image to
* draw. */
int drawableX,int drawableY)/* Coordinates within drawable that correspond
* to imageX and imageY. */
{
PhotoInstance *instancePtr = (PhotoInstance *)clientData;
#ifndef TK_CAN_RENDER_RGBA
XVisualInfo visInfo = instancePtr->visualInfo;
#endif
/*
* If there's no pixmap, it means that an error occurred while creating
* the image instance so it can't be displayed.
*/
if (instancePtr->pixels == None) {
return;
}
#ifdef TK_CAN_RENDER_RGBA
/*
* We can use TkpPutRGBAImage to render RGBA Ximages directly so there is
* no need to call XGetImage or to do the Porter-Duff compositing by hand.
*/
unsigned char *rgbaPixels = instancePtr->modelPtr->pix32;
XImage *photo = XCreateImage(display, NULL, 32, ZPixmap, 0, (char*)rgbaPixels,
(unsigned int)instancePtr->width,
(unsigned int)instancePtr->height,
0, (unsigned int)(4 * instancePtr->width));
TkpPutRGBAImage(display, drawable, instancePtr->gc,
photo, imageX, imageY, drawableX, drawableY,
(unsigned int) width, (unsigned int) height);
photo->data = NULL;
XDestroyImage(photo);
#else
if ((instancePtr->modelPtr->flags & COMPLEX_ALPHA)
&& visInfo.depth >= 15
&& (visInfo.c_class == DirectColor || visInfo.c_class == TrueColor)) {
Tk_ErrorHandler handler;
XImage *bgImg = NULL;
/*
* Create an error handler to suppress the case where the input was
* not properly constrained, which can cause an X error. [Bug 979239]
*/
handler = Tk_CreateErrorHandler(display, -1, -1, -1, NULL, NULL);
/*
* Pull the current background from the display to blend with
*/
bgImg = XGetImage(display, drawable, drawableX, drawableY,
(unsigned int)width, (unsigned int)height, AllPlanes, ZPixmap);
if (bgImg == NULL) {
Tk_DeleteErrorHandler(handler);
/* We failed to get the image, so draw without blending alpha.
* It's the best we can do.
*/
goto fallBack;
}
BlendComplexAlpha(bgImg, instancePtr, imageX, imageY, width, height);
/*
* Color info is unimportant as we only do this operation for depth >=
* 15.
*/
TkPutImage(NULL, 0, display, drawable, instancePtr->gc,
bgImg, 0, 0, drawableX, drawableY,
(unsigned int) width, (unsigned int) height);
XDestroyImage(bgImg);
Tk_DeleteErrorHandler(handler);
} else {
/*
* modelPtr->validRegion describes which parts of the image contain valid
* data. We set this region as the clip mask for the gc, setting its
* origin appropriately, and use it when drawing the image.
*/
fallBack:
XSetRegion(display, instancePtr->gc,
instancePtr->modelPtr->validRegion);
XSetClipOrigin(display, instancePtr->gc, drawableX - imageX,
drawableY - imageY);
XCopyArea(display, instancePtr->pixels, drawable, instancePtr->gc,
imageX, imageY, (unsigned) width, (unsigned) height,
drawableX, drawableY);
XSetClipMask(display, instancePtr->gc, None);
XSetClipOrigin(display, instancePtr->gc, 0, 0);
}
(void)XFlush(display);
#endif
}
/*
*----------------------------------------------------------------------
*
* TkImgPhotoFree --
*
* This function is called when a widget ceases to use a particular
* instance of an image. We don't actually get rid of the instance until
* later because we may be about to get this instance again.
*
* Results:
* None.
*
* Side effects:
* Internal data structures get cleaned up, later.
*
*----------------------------------------------------------------------
*/
void
TkImgPhotoFree(
void *clientData, /* Pointer to PhotoInstance structure for
* instance to be displayed. */
TCL_UNUSED(Display *)) /* Display containing window that used
* image. */
{
PhotoInstance *instancePtr = (PhotoInstance *)clientData;
ColorTable *colorPtr;
if (instancePtr->refCount-- > 1) {
return;
}
/*
* There are no more uses of the image within this widget. Decrement the
* count of live uses of its color table, so that its colors can be
* reclaimed if necessary, and set up an idle call to free the instance
* structure.
*/
colorPtr = instancePtr->colorTablePtr;
if (colorPtr != NULL) {
colorPtr->liveRefCount--;
}
Tcl_DoWhenIdle(TkImgDisposeInstance, instancePtr);
}
/*
*----------------------------------------------------------------------
*
* TkImgPhotoInstanceSetSize --
*
* This function reallocates the instance pixmap and dithering error
* array for a photo instance, as necessary, to change the image's size
* to `width' x `height' pixels.
*
* Results:
* None.
*
* Side effects:
* Storage gets reallocated, here and in the X server.
*
*----------------------------------------------------------------------
*/
void
TkImgPhotoInstanceSetSize(
PhotoInstance *instancePtr) /* Instance whose size is to be changed. */
{
PhotoModel *modelPtr;
schar *newError, *errSrcPtr, *errDestPtr;
int h, offset;
XRectangle validBox;
Pixmap newPixmap;
modelPtr = instancePtr->modelPtr;
XClipBox(modelPtr->validRegion, &validBox);
if ((instancePtr->width != modelPtr->width)
|| (instancePtr->height != modelPtr->height)
|| (instancePtr->pixels == None)) {
newPixmap = Tk_GetPixmap(instancePtr->display,
RootWindow(instancePtr->display,
instancePtr->visualInfo.screen),
(modelPtr->width > 0) ? modelPtr->width: 1,
(modelPtr->height > 0) ? modelPtr->height: 1,
instancePtr->visualInfo.depth);
if (!newPixmap) {
Tcl_Panic("Fail to create pixmap with Tk_GetPixmap in TkImgPhotoInstanceSetSize");
}
/*
* The following is a gross hack needed to properly support colormaps
* under Windows. Before the pixels can be copied to the pixmap, the
* relevent colormap must be associated with the drawable. Normally we
* can infer this association from the window that was used to create
* the pixmap. However, in this case we're using the root window, so
* we have to be more explicit.
*/
TkSetPixmapColormap(newPixmap, instancePtr->colormap);
if (instancePtr->pixels != None) {
/*
* Copy any common pixels from the old pixmap and free it.
*/
XCopyArea(instancePtr->display, instancePtr->pixels, newPixmap,
instancePtr->gc, validBox.x, validBox.y,
validBox.width, validBox.height, validBox.x, validBox.y);
Tk_FreePixmap(instancePtr->display, instancePtr->pixels);
}
instancePtr->pixels = newPixmap;
}
if ((instancePtr->width != modelPtr->width)
|| (instancePtr->height != modelPtr->height)
|| (instancePtr->error == NULL)) {
if (modelPtr->height > 0 && modelPtr->width > 0) {
/*
* TODO: use attemptckalloc() here once there is a strategy that
* will allow us to recover from failure. Right now, there's no
* such possibility.
*/
newError = (schar *)ckalloc(modelPtr->height * modelPtr->width
* 3 * sizeof(schar));
/*
* Zero the new array so that we don't get bogus error values
* propagating into areas we dither later.
*/
if ((instancePtr->error != NULL)
&& ((instancePtr->width == modelPtr->width)
|| (validBox.width == modelPtr->width))) {
if (validBox.y > 0) {
memset(newError, 0, (size_t)
validBox.y * modelPtr->width * 3 * sizeof(schar));
}
h = validBox.y + validBox.height;
if (h < modelPtr->height) {
memset(newError + h*modelPtr->width*3, 0,
(size_t) (modelPtr->height - h)
* modelPtr->width * 3 * sizeof(schar));
}
} else {
memset(newError, 0, (size_t)
modelPtr->height * modelPtr->width *3*sizeof(schar));
}
} else {
newError = NULL;
}
if (instancePtr->error != NULL) {
/*
* Copy the common area over to the new array and free the old
* array.
*/
if (modelPtr->width == instancePtr->width) {
offset = validBox.y * modelPtr->width * 3;
memcpy(newError + offset, instancePtr->error + offset,
(size_t) validBox.height
* modelPtr->width * 3 * sizeof(schar));
} else if (validBox.width > 0 && validBox.height > 0) {
errDestPtr = newError +
(validBox.y * modelPtr->width + validBox.x) * 3;
errSrcPtr = instancePtr->error +
(validBox.y * instancePtr->width + validBox.x) * 3;
for (h = validBox.height; h > 0; --h) {
memcpy(errDestPtr, errSrcPtr,
validBox.width * 3 * sizeof(schar));
errDestPtr += modelPtr->width * 3;
errSrcPtr += instancePtr->width * 3;
}
}
ckfree(instancePtr->error);
}
instancePtr->error = newError;
}
instancePtr->width = modelPtr->width;
instancePtr->height = modelPtr->height;
}
/*
*----------------------------------------------------------------------
*
* IsValidPalette --
*
* This function is called to check whether a value given for the
* -palette option is valid for a particular instance of a photo image.
*
* Results:
* A boolean value: 1 if the palette is acceptable, 0 otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
IsValidPalette(
PhotoInstance *instancePtr, /* Instance to which the palette specification
* is to be applied. */
const char *palette) /* Palette specification string. */
{
int nRed, nGreen, nBlue, numColors;
bool mono;
char *endp;
/*
* First parse the specification: it must be of the form %d or %d/%d/%d.
*/
nRed = strtol(palette, &endp, 10);
if ((endp == palette) || ((*endp != 0) && (*endp != '/'))
|| (nRed < 2) || (nRed > 256)) {
return 0;
}
if (*endp == 0) {
mono = true;
nGreen = nBlue = nRed;
} else {
palette = endp + 1;
nGreen = strtol(palette, &endp, 10);
if ((endp == palette) || (*endp != '/') || (nGreen < 2)
|| (nGreen > 256)) {
return 0;
}
palette = endp + 1;
nBlue = strtol(palette, &endp, 10);
if ((endp == palette) || (*endp != 0) || (nBlue < 2)
|| (nBlue > 256)) {
return 0;
}
mono = false;
}
switch (instancePtr->visualInfo.c_class) {
case DirectColor:
case TrueColor:
if ((nRed > (1 << CountBits(instancePtr->visualInfo.red_mask)))
|| (nGreen>(1<<CountBits(instancePtr->visualInfo.green_mask)))
|| (nBlue>(1<<CountBits(instancePtr->visualInfo.blue_mask)))) {
return 0;
}
break;
case PseudoColor:
case StaticColor:
numColors = nRed;
if (!mono) {
numColors *= nGreen * nBlue;
}
if (numColors > (1 << instancePtr->visualInfo.depth)) {