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
|
/* vi: ts=8 sts=4 sw=4
*
*
* This file is part of the KDE project, module tdeui.
* Copyright (C) 2000 Geert Jansen <[email protected]>.
*
* You can Freely distribute this program under the GNU Library General
* Public License. See the file "COPYING.LIB" for the exact licensing terms.
*
* kpixmapio.cpp: Fast pixmap <-> image conversion.
*/
#include "kpixmapio.h"
#include "config.h"
#include <tqimage.h>
#include <tqpixmap.h>
#include <tqcolor.h>
#include <tqglobal.h>
#include <kglobal.h>
#include <kconfig.h>
#include <kdebug.h>
#include <sys/types.h>
#ifdef Q_OS_UNIX
#include <sys/ipc.h>
#include <sys/shm.h>
#endif
#ifdef Q_WS_X11
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_MITSHM
#include <X11/extensions/XShm.h>
#endif
#ifdef __osf__
extern "C" int XShmQueryExtension(Display *display);
#endif
#else
#undef HAVE_MITSHM
#endif
// d pointer
struct KPixmapIOPrivate
{
int shmsize;
int shmpolicy;
int threshold;
int bpp;
int byteorder;
#ifdef Q_WS_X11
XImage *ximage;
#ifdef HAVE_MITSHM
XShmSegmentInfo *shminfo;
bool first_try;
#endif
#else
void *ximage;
#endif
};
// From Qt: Returns the position of the lowest set bit in val.
typedef unsigned char uchar;
typedef unsigned int uint;
#ifdef HAVE_MITSHM
static int lowest_bit(uint val)
{
int i;
uint test = 1;
for (i=0; (!(val & test)) && i<32; i++, test<<=1);
return (i == 32) ? -1 : i;
}
#endif
/*** KPixmapIO ***/
KPixmapIO::KPixmapIO()
{
m_bShm = false;
d = new KPixmapIOPrivate;
#ifdef HAVE_MITSHM
setShmPolicy(ShmDontKeep);
KConfig *config = KGlobal::config();
if (!config->readBoolEntry("UseMitShm", true))
return;
int ignore;
if (XQueryExtension(tqt_xdisplay(), "MIT-SHM", &ignore, &ignore, &ignore))
{
if (XShmQueryExtension(tqt_xdisplay()))
m_bShm = true;
}
if (!m_bShm)
{
kdDebug(290) << k_lineinfo << "MIT-SHM not available!\n";
d->ximage = 0;
d->shminfo = 0;
d->shmsize = 0;
return;
}
// Sort out bit format. Create a temporary XImage for this.
d->shminfo = new XShmSegmentInfo;
d->ximage = XShmCreateImage(tqt_xdisplay(), (Visual *) TQPaintDevice::x11AppVisual(),
TQPaintDevice::x11AppDepth(), ZPixmap, 0L, d->shminfo, 10, 10);
d->bpp = d->ximage->bits_per_pixel;
d->first_try = true;
int bpp = d->bpp;
if (d->ximage->byte_order == LSBFirst)
bpp++;
int red_shift = lowest_bit(d->ximage->red_mask);
int green_shift = lowest_bit(d->ximage->green_mask);
int blue_shift = lowest_bit(d->ximage->blue_mask);
XDestroyImage(d->ximage); d->ximage = 0L;
d->shmsize = 0;
// Offer discrete possibilities for the bitformat. Each will have its
// own routine. The general algorithm using bitshifts is much too slow;
// this has to be done for every pixel!
if ((bpp == 32) && (red_shift == 16) && (green_shift == 8) &&
(blue_shift == 0))
d->byteorder = bo32_ARGB;
else if ((bpp == 32) && (red_shift == 0) && (green_shift == 8) &&
(blue_shift == 16))
d->byteorder = bo32_BGRA;
else if ((bpp == 33) && (red_shift == 16) && (green_shift == 8) &&
(blue_shift == 0))
d->byteorder = bo32_BGRA;
else if ((bpp == 24) && (red_shift == 16) && (green_shift == 8) &&
(blue_shift == 0))
d->byteorder = bo24_RGB;
else if ((bpp == 24) && (red_shift == 0) && (green_shift == 8) &&
(blue_shift == 16))
d->byteorder = bo24_BGR;
else if ((bpp == 25) && (red_shift == 16) && (green_shift == 8) &&
(blue_shift == 0))
d->byteorder = bo24_BGR;
else if ((bpp == 16) && (red_shift == 11) && (green_shift == 5) &&
(blue_shift == 0))
d->byteorder = bo16_RGB_565;
else if ((bpp == 16) && (red_shift == 10) && (green_shift == 5) &&
(blue_shift == 0))
d->byteorder = bo16_RGB_555;
else if ((bpp == 17) && (red_shift == 11) && (green_shift == 5) &&
(blue_shift == 0))
d->byteorder = bo16_BGR_565;
else if ((bpp == 17) && (red_shift == 10) && (green_shift == 5) &&
(blue_shift == 0))
d->byteorder = bo16_BGR_555;
else if ((bpp == 8) || (bpp == 9))
d->byteorder = bo8;
else
{
m_bShm = false;
kdWarning(290) << "Byte order not supported!" << endl;
kdWarning(290) << "red = " << red_shift
<< ", green = " << green_shift
<< ", blue = " << blue_shift << endl;
kdWarning(290) << "Please report to <[email protected]>\n";
}
#else
d->shmsize = 0;
d->ximage = 0;
#endif
}
KPixmapIO::~KPixmapIO()
{
destroyXImage();
destroyShmSegment();
#ifdef HAVE_MITSHM
delete d->shminfo;
#endif
delete d;
}
TQPixmap KPixmapIO::convertToPixmap(const TQImage &img)
{
int size = img.width() * img.height();
if (m_bShm && (img.depth() > 1) && (d->bpp > 8) && (size > d->threshold))
{
TQPixmap dst(img.width(), img.height());
putImage(&dst, 0, 0, &img);
return dst;
} else
{
TQPixmap dst;
dst.convertFromImage(img);
return dst;
}
}
TQImage KPixmapIO::convertToImage(const TQPixmap &pm)
{
TQImage image;
int size = pm.width() * pm.height();
if (m_bShm && (d->bpp >= 8) && (size > d->threshold))
image = getImage(&pm, 0, 0, pm.width(), pm.height());
else
image = pm.convertToImage();
return image;
}
void KPixmapIO::putImage(TQPixmap *dst, const TQPoint &offset,
const TQImage *src)
{
putImage(dst, offset.x(), offset.y(), src);
}
void KPixmapIO::putImage(TQPixmap *dst, int dx, int dy, const TQImage *src)
{
int size = src->width() * src->height();
bool fallback = true;
if (m_bShm && (src->depth() > 1) && (d->bpp > 8) && (size > d->threshold))
{
#ifdef HAVE_MITSHM
if( initXImage(src->width(), src->height()))
{
convertToXImage(*src);
XShmPutImage(tqt_xdisplay(), dst->handle(), tqt_xget_temp_gc(tqt_xscreen(), false), d->ximage,
dx, dy, 0, 0, src->width(), src->height(), false);
// coolo: do we really need this here? I see no good for it
XSync(tqt_xdisplay(), false);
doneXImage();
fallback = false;
}
#endif
}
if( fallback )
{
TQPixmap pix;
pix.convertFromImage(*src);
bitBlt(dst, dx, dy, &pix, 0, 0, pix.width(), pix.height());
}
}
TQImage KPixmapIO::getImage(const TQPixmap *src, const TQRect &rect)
{
return getImage(src, rect.x(), rect.y(), rect.width(), rect.height());
}
TQImage KPixmapIO::getImage(const TQPixmap *src, int sx, int sy, int sw, int sh)
{
TQImage image;
int size = src->width() * src->height();
bool fallback = true;
if ((m_bShm) && (d->bpp >= 8) && (size > d->threshold))
{
#ifdef HAVE_MITSHM
if( initXImage(sw, sh))
{
XShmGetImage(tqt_xdisplay(), src->handle(), d->ximage, sx, sy, AllPlanes);
image = convertFromXImage();
doneXImage();
fallback = false;
}
#endif
}
if( fallback )
{
TQPixmap pix(sw, sh);
bitBlt(&pix, 0, 0, src, sx, sy, sw, sh);
image = pix.convertToImage();
}
return image;
}
#ifdef HAVE_MITSHM
void KPixmapIO::preAllocShm(int size)
{
destroyXImage();
createShmSegment(size);
}
void KPixmapIO::setShmPolicy(int policy)
{
switch (policy)
{
case ShmDontKeep:
d->shmpolicy = ShmDontKeep;
d->threshold = 5000;
break;
case ShmKeepAndGrow:
d->shmpolicy = ShmKeepAndGrow;
d->threshold = 2000;
break;
default:
break;
}
}
bool KPixmapIO::initXImage(int w, int h)
{
if (d->ximage && (w == d->ximage->width) && (h == d->ximage->height))
return true;
if( !createXImage(w, h))
return false;
int size = d->ximage->bytes_per_line * d->ximage->height;
if (size > d->shmsize)
{
if( !createShmSegment(size))
{
destroyXImage();
return false;
}
}
d->ximage->data = d->shminfo->shmaddr;
return true;
}
void KPixmapIO::doneXImage()
{
if (d->shmpolicy == ShmDontKeep)
{
destroyXImage();
destroyShmSegment();
}
}
void KPixmapIO::destroyXImage()
{
if (d->ximage)
{
XDestroyImage(d->ximage);
d->ximage = 0L;
}
}
bool KPixmapIO::createXImage(int w, int h)
{
destroyXImage();
d->ximage = XShmCreateImage(tqt_xdisplay(), (Visual *) TQPaintDevice::x11AppVisual(),
TQPaintDevice::x11AppDepth(), ZPixmap, 0L, d->shminfo, w, h);
return d->ximage != None;
}
void KPixmapIO::destroyShmSegment()
{
if (d->shmsize)
{
XShmDetach(tqt_xdisplay(), d->shminfo);
shmdt(d->shminfo->shmaddr);
shmctl(d->shminfo->shmid, IPC_RMID, 0);
d->shmsize = 0;
}
}
static bool use_xshm = true;
static unsigned long kpixmapio_serial;
static int (*old_errhandler)(Display *dpy, XErrorEvent *ev) = 0;
static int kpixmapio_errorhandler(Display *dpy, XErrorEvent *ev)
{
if(ev->serial == kpixmapio_serial) {
/* assuming that xshm errors mean it can't be used at all
(e.g. remote display) */
use_xshm = false;
kdDebug(290) << "Disabling Xshm" << endl;
return 0;
} else {
// another error
return old_errhandler(dpy, ev);
}
}
bool KPixmapIO::createShmSegment(int size)
{
destroyShmSegment();
d->shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0600);
if ( d->shminfo->shmid < 0)
{
kdWarning(290) << "Could not get shared memory segment.\n";
m_bShm = false;
return false;
}
d->shminfo->shmaddr = (char *) shmat(d->shminfo->shmid, 0, 0);
if (d->shminfo->shmaddr == (char *)-1)
{
kdWarning(290) << "Could not attach shared memory segment.\n";
m_bShm = false;
shmctl(d->shminfo->shmid, IPC_RMID, 0);
return false;
}
d->shminfo->readOnly = false;
if (d->first_try) {
// make sure that we don't get errors of old stuff
XSync(tqt_xdisplay(), False);
old_errhandler = XSetErrorHandler(kpixmapio_errorhandler);
kpixmapio_serial = NextRequest(tqt_xdisplay());
}
if ( !XShmAttach(tqt_xdisplay(), d->shminfo))
{
kdWarning() << "X-Server could not attach shared memory segment.\n";
m_bShm = false;
shmdt(d->shminfo->shmaddr);
shmctl(d->shminfo->shmid, IPC_RMID, 0);
}
if (d->first_try) {
XSync(tqt_xdisplay(), false);
if (!use_xshm)
m_bShm = false;
XSetErrorHandler(old_errhandler);
d->first_try = false;
}
d->shmsize = size;
return m_bShm;
}
/*
* The following functions convertToXImage/convertFromXImage are a little
* long. This is because of speed, I want to get as much out of the inner
* loop as possible.
*/
TQImage KPixmapIO::convertFromXImage()
{
int x, y;
int width = d->ximage->width, height = d->ximage->height;
int bpl = d->ximage->bytes_per_line;
char *data = d->ximage->data;
TQImage image;
if (d->bpp == 8)
{
image.create(width, height, 8);
// Query color map. Don't remove unused entries as a speed
// optmization.
int i, ncells = 256;
XColor *cmap = new XColor[ncells];
for (i=0; i<ncells; i++)
cmap[i].pixel = i;
XQueryColors(tqt_xdisplay(), TQPaintDevice::x11AppColormap(),
cmap, ncells);
image.setNumColors(ncells);
for (i=0; i<ncells; i++)
image.setColor(i, tqRgb(cmap[i].red, cmap[i].green, cmap[i].blue >> 8));
} else
image.create(width, height, 32);
switch (d->byteorder)
{
case bo8:
{
for (y=0; y<height; y++)
memcpy(image.scanLine(y), data + y*bpl, width);
break;
}
case bo16_RGB_565:
case bo16_BGR_565:
{
TQ_INT32 pixel, *src;
TQRgb *dst, val;
for (y=0; y<height; y++)
{
src = (TQ_INT32 *) (data + y*bpl);
dst = (TQRgb *) image.scanLine(y);
for (x=0; x<width/2; x++)
{
pixel = *src++;
val = ((pixel & 0xf800) << 8) | ((pixel & 0x7e0) << 5) |
((pixel & 0x1f) << 3);
*dst++ = val;
pixel >>= 16;
val = ((pixel & 0xf800) << 8) | ((pixel & 0x7e0) << 5) |
((pixel & 0x1f) << 3);
*dst++ = val;
}
if (width%2)
{
pixel = *src++;
val = ((pixel & 0xf800) << 8) | ((pixel & 0x7e0) << 5) |
((pixel & 0x1f) << 3);
*dst++ = val;
}
}
break;
}
case bo16_RGB_555:
case bo16_BGR_555:
{
TQ_INT32 pixel, *src;
TQRgb *dst, val;
for (y=0; y<height; y++)
{
src = (TQ_INT32 *) (data + y*bpl);
dst = (TQRgb *) image.scanLine(y);
for (x=0; x<width/2; x++)
{
pixel = *src++;
val = ((pixel & 0x7c00) << 9) | ((pixel & 0x3e0) << 6) |
((pixel & 0x1f) << 3);
*dst++ = val;
pixel >>= 16;
val = ((pixel & 0x7c00) << 9) | ((pixel & 0x3e0) << 6) |
((pixel & 0x1f) << 3);
*dst++ = val;
}
if (width%2)
{
pixel = *src++;
val = ((pixel & 0x7c00) << 9) | ((pixel & 0x3e0) << 6) |
((pixel & 0x1f) << 3);
*dst++ = val;
}
}
break;
}
case bo24_RGB:
{
char *src;
TQRgb *dst;
int w1 = width/4;
TQ_INT32 d1, d2, d3;
for (y=0; y<height; y++)
{
src = data + y*bpl;
dst = (TQRgb *) image.scanLine(y);
for (x=0; x<w1; x++)
{
d1 = *((TQ_INT32 *)src);
d2 = *((TQ_INT32 *)src + 1);
d3 = *((TQ_INT32 *)src + 2);
src += 12;
*dst++ = d1;
*dst++ = (d1 >> 24) | (d2 << 8);
*dst++ = (d3 << 16) | (d2 >> 16);
*dst++ = d3 >> 8;
}
for (x=w1*4; x<width; x++)
{
d1 = *src++ << 16;
d1 += *src++ << 8;
d1 += *src++;
*dst++ = d1;
}
}
break;
}
case bo24_BGR:
{
char *src;
TQRgb *dst;
int w1 = width/4;
TQ_INT32 d1, d2, d3;
for (y=0; y<height; y++)
{
src = data + y*bpl;
dst = (TQRgb *) image.scanLine(y);
for (x=0; x<w1; x++)
{
d1 = *((TQ_INT32 *)src);
d2 = *((TQ_INT32 *)src + 1);
d3 = *((TQ_INT32 *)src + 2);
src += 12;
*dst++ = d1;
*dst++ = (d1 >> 24) | (d2 << 8);
*dst++ = (d3 << 16) | (d2 >> 16);
*dst++ = d3 >> 8;
}
for (x=w1*4; x<width; x++)
{
d1 = *src++;
d1 += *src++ << 8;
d1 += *src++ << 16;
*dst++ = d1;
}
}
break;
}
case bo32_ARGB:
case bo32_BGRA:
{
for (y=0; y<height; y++)
memcpy(image.scanLine(y), data + y*bpl, width*4);
break;
}
}
return image;
}
void KPixmapIO::convertToXImage(const TQImage &img)
{
int x, y;
int width = d->ximage->width, height = d->ximage->height;
int bpl = d->ximage->bytes_per_line;
char *data = d->ximage->data;
switch (d->byteorder)
{
case bo16_RGB_555:
case bo16_BGR_555:
if (img.depth() == 32)
{
TQRgb *src, pixel;
TQ_INT32 *dst, val;
for (y=0; y<height; y++)
{
src = (TQRgb *) img.scanLine(y);
dst = (TQ_INT32 *) (data + y*bpl);
for (x=0; x<width/2; x++)
{
pixel = *src++;
val = ((pixel & 0xf80000) >> 9) | ((pixel & 0xf800) >> 6) |
((pixel & 0xff) >> 3);
pixel = *src++;
val |= (((pixel & 0xf80000) >> 9) | ((pixel & 0xf800) >> 6) |
((pixel & 0xff) >> 3)) << 16;
*dst++ = val;
}
if (width%2)
{
pixel = *src++;
*((TQ_INT16 *)dst) = ((pixel & 0xf80000) >> 9) |
((pixel & 0xf800) >> 6) | ((pixel & 0xff) >> 3);
}
}
} else
{
uchar *src;
TQ_INT32 val, *dst;
TQRgb pixel, *clut = img.tqcolorTable();
for (y=0; y<height; y++)
{
src = const_cast<TQImage&>(img).scanLine(y);
dst = (TQ_INT32 *) (data + y*bpl);
for (x=0; x<width/2; x++)
{
pixel = clut[*src++];
val = ((pixel & 0xf80000) >> 9) | ((pixel & 0xf800) >> 6) |
((pixel & 0xff) >> 3);
pixel = clut[*src++];
val |= (((pixel & 0xf80000) >> 9) | ((pixel & 0xf800) >> 6) |
((pixel & 0xff) >> 3)) << 16;
*dst++ = val;
}
if (width%2)
{
pixel = clut[*src++];
*((TQ_INT16 *)dst) = ((pixel & 0xf80000) >> 9) |
((pixel & 0xf800) >> 6) | ((pixel & 0xff) >> 3);
}
}
}
break;
case bo16_RGB_565:
case bo16_BGR_565:
if (img.depth() == 32)
{
TQRgb *src, pixel;
TQ_INT32 *dst, val;
for (y=0; y<height; y++)
{
src = (TQRgb *) img.scanLine(y);
dst = (TQ_INT32 *) (data + y*bpl);
for (x=0; x<width/2; x++)
{
pixel = *src++;
val = ((pixel & 0xf80000) >> 8) | ((pixel & 0xfc00) >> 5) |
((pixel & 0xff) >> 3);
pixel = *src++;
val |= (((pixel & 0xf80000) >> 8) | ((pixel & 0xfc00) >> 5) |
((pixel & 0xff) >> 3)) << 16;
*dst++ = val;
}
if (width%2)
{
pixel = *src++;
*((TQ_INT16 *)dst) = ((pixel & 0xf80000) >> 8) |
((pixel & 0xfc00) >> 5) | ((pixel & 0xff) >> 3);
}
}
} else
{
uchar *src;
TQ_INT32 val, *dst;
TQRgb pixel, *clut = img.tqcolorTable();
for (y=0; y<height; y++)
{
src = const_cast<TQImage&>(img).scanLine(y);
dst = (TQ_INT32 *) (data + y*bpl);
for (x=0; x<width/2; x++)
{
pixel = clut[*src++];
val = ((pixel & 0xf80000) >> 8) | ((pixel & 0xfc00) >> 5) |
((pixel & 0xff) >> 3);
pixel = clut[*src++];
val |= (((pixel & 0xf80000) >> 8) | ((pixel & 0xfc00) >> 5) |
((pixel & 0xff) >> 3)) << 16;
*dst++ = val;
}
if (width%2)
{
pixel = clut[*src++];
*((TQ_INT16 *)dst) = ((pixel & 0xf80000) >> 8) |
((pixel & 0xfc00) >> 5) | ((pixel & 0xff) >> 3);
}
}
}
break;
case bo24_RGB:
if (img.depth() == 32)
{
char *dst;
int w1 = width/4;
TQRgb *src, d1, d2, d3, d4;
for (y=0; y<height; y++)
{
src = (TQRgb *) img.scanLine(y);
dst = data + y*bpl;
for (x=0; x<w1; x++)
{
d1 = (*src++ & 0xffffff);
d2 = (*src++ & 0xffffff);
d3 = (*src++ & 0xffffff);
d4 = (*src++ & 0xffffff);
*((TQ_INT32 *)dst) = d1 | (d2 << 24);
*((TQ_INT32 *)dst+1) = (d2 >> 8) | (d3 << 16);
*((TQ_INT32 *)dst+2) = (d4 << 8) | (d3 >> 16);
dst += 12;
}
for (x=w1*4; x<width; x++)
{
d1 = *src++;
*dst++ = tqRed(d1);
*dst++ = tqGreen(d1);
*dst++ = tqBlue(d1);
}
}
} else
{
uchar *src, *dst;
int w1 = width/4;
TQRgb *clut = img.tqcolorTable(), d1, d2, d3, d4;
for (y=0; y<height; y++)
{
src = const_cast<TQImage&>(img).scanLine(y);
dst = (uchar *) data + y*bpl;
for (x=0; x<w1; x++)
{
d1 = (clut[*src++] & 0xffffff);
d2 = (clut[*src++] & 0xffffff);
d3 = (clut[*src++] & 0xffffff);
d4 = (clut[*src++] & 0xffffff);
*((TQ_INT32 *)dst) = d1 | (d2 << 24);
*((TQ_INT32 *)dst+1) = (d2 >> 8) | (d3 << 16);
*((TQ_INT32 *)dst+2) = (d4 << 8) | (d3 >> 16);
dst += 12;
}
for (x=w1*4; x<width; x++)
{
d1 = clut[*src++];
*dst++ = tqRed(d1);
*dst++ = tqGreen(d1);
*dst++ = tqBlue(d1);
}
}
}
break;
case bo24_BGR:
if (img.depth() == 32)
{
char *dst;
TQRgb *src, d1, d2, d3, d4;
int w1 = width/4;
for (y=0; y<height; y++)
{
src = (TQRgb *) img.scanLine(y);
dst = data + y*bpl;
for (x=0; x<w1; x++)
{
d1 = (*src++ & 0xffffff);
d2 = (*src++ & 0xffffff);
d3 = (*src++ & 0xffffff);
d4 = (*src++ & 0xffffff);
*((TQ_INT32 *)dst) = d1 | (d2 << 24);
*((TQ_INT32 *)dst+1) = (d2 >> 8) | (d3 << 16);
*((TQ_INT32 *)dst+2) = (d4 << 8) | (d3 >> 16);
dst += 12;
}
for (x=w1*4; x<width; x++)
{
d1 = *src++;
*dst++ = tqBlue(d1);
*dst++ = tqGreen(d1);
*dst++ = tqRed(d1);
}
}
} else
{
uchar *src, *dst;
int w1 = width/4;
TQRgb *clut = img.tqcolorTable(), d1, d2, d3, d4;
for (y=0; y<height; y++)
{
src = const_cast<TQImage&>(img).scanLine(y);
dst = (uchar *) data + y*bpl;
for (x=0; x<w1; x++)
{
d1 = (clut[*src++] & 0xffffff);
d2 = (clut[*src++] & 0xffffff);
d3 = (clut[*src++] & 0xffffff);
d4 = (clut[*src++] & 0xffffff);
*((TQ_INT32 *)dst) = d1 | (d2 << 24);
*((TQ_INT32 *)dst+1) = (d2 >> 8) | (d3 << 16);
*((TQ_INT32 *)dst+2) = (d4 << 8) | (d3 >> 16);
dst += 12;
}
for (x=w1*4; x<width; x++)
{
d1 = clut[*src++];
*dst++ = tqBlue(d1);
*dst++ = tqGreen(d1);
*dst++ = tqRed(d1);
}
}
}
break;
case bo32_ARGB:
case bo32_BGRA:
if (img.depth() == 32)
{
for (y=0; y<height; y++)
memcpy(data + y*bpl, img.scanLine(y), width*4);
} else
{
uchar *src;
TQRgb *dst, *clut = img.tqcolorTable();
for (y=0; y<height; y++)
{
src = const_cast<TQImage&>(img).scanLine(y);
dst = (TQRgb *) (data + y*bpl);
for (x=0; x<width; x++)
*dst++ = clut[*src++];
}
}
break;
}
}
#else
void KPixmapIO::preAllocShm(int) {}
void KPixmapIO::setShmPolicy(int) {}
bool KPixmapIO::initXImage(int, int) { return false; }
void KPixmapIO::doneXImage() {}
bool KPixmapIO::createXImage(int, int) { return false; }
void KPixmapIO::destroyXImage() {}
bool KPixmapIO::createShmSegment(int) { return false; }
void KPixmapIO::destroyShmSegment() {}
TQImage KPixmapIO::convertFromXImage() { return TQImage(); }
void KPixmapIO::convertToXImage(const TQImage &) {}
#endif // HAVE_MITSHM
|