25 #include <tqpainter.h>
35 static bool kdither_32_to_8(
const TQImage *src, TQImage *dst )
41 if ( !dst->create(src->width(), src->height(), 8, 256) ) {
42 tqWarning(
"KPixmap: destination image not valid\n");
46 dst->setNumColors( 256 );
51 #define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b))
55 for ( rc=0; rc<=MAX_R; rc++ )
56 for ( gc=0; gc<=MAX_G; gc++ )
57 for ( bc=0; bc<=MAX_B; bc++ ) {
58 dst->setColor( INDEXOF(rc,gc,bc),
59 tqRgb( rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B ) );
62 int sw = src->width();
67 line1[0] =
new int[src->width()];
68 line2[0] =
new int[src->width()];
69 line1[1] =
new int[src->width()];
70 line2[1] =
new int[src->width()];
71 line1[2] =
new int[src->width()];
72 line2[2] =
new int[src->width()];
77 for ( y=0; y < src->height(); y++ ) {
80 int endian = (TQImage::systemBitOrder() == TQImage::BigEndian);
82 uchar* q =
const_cast<TQImage*
>(src)->scanLine(y);
83 uchar* q2 =
const_cast<TQImage*
>(src)->scanLine(y+1 < src->height() ? y + 1 : 0);
85 for (
int chan = 0; chan < 3; chan++) {
87 int *l1 = (y&1) ? line2[chan] : line1[chan];
88 int *l2 = (y&1) ? line1[chan] : line2[chan];
90 for (
int i=0; i<sw; i++)
91 l1[i] = q[i*4+chan+endian];
93 if ( y+1 < src->height() ) {
94 for (
int i=0; i<sw; i++)
95 l2[i] = q2[i*4+chan+endian];
100 for (x=0; x<sw; x++) {
101 int pix = TQMAX(TQMIN(2, (l1[x] * 2 + 128)/ 255), 0);
102 int err = l1[x] - pix * 255 / 2;
107 l1[x+1] += (err*7)>>4;
115 for (x=sw; x-->0; ) {
116 int pix = TQMAX(TQMIN(2, (l1[x] * 2 + 128)/ 255), 0);
117 int err = l1[x] - pix * 255 / 2;
122 l1[x-1] += (err*7)>>4;
134 *b++ = INDEXOF(pv[2][x],pv[1][x],pv[0][x]);
137 *b++ = INDEXOF(pv[0][x],pv[1][x],pv[2][x]);
165 int conversion_flags )
167 TQImageIO io( fileName, format );
169 bool result = io.read();
181 int conversion_flags = 0;
184 conversion_flags |= ColorOnly;
187 conversion_flags |= MonoOnly;
190 conversion_flags |= LowOnly;
193 conversion_flags |= WebOnly;
198 return load( fileName, format, conversion_flags );
203 int conversion_flags = 0;
206 conversion_flags |= ColorOnly;
209 conversion_flags |= MonoOnly;
212 conversion_flags |= LowOnly;
215 conversion_flags |= WebOnly;
225 if ( img.isNull() ) {
226 #if defined(CHECK_NULL)
227 tqWarning(
"KPixmap::convertFromImage: Cannot convert a null image" );
233 int dd = defaultDepth();
236 if ( ( conversion_flags & KColorMode_Mask ) != LowOnly &&
237 ( conversion_flags & KColorMode_Mask ) != WebOnly ) {
238 return TQPixmap::convertFromImage ( img, conversion_flags );
244 if ( ( conversion_flags & KColorMode_Mask ) == LowOnly ||
245 ( conversion_flags & KColorMode_Mask ) == WebOnly )
246 conversion_flags = (conversion_flags & ~KColorMode_Mask) |
Auto;
247 return TQPixmap::convertFromImage ( img, conversion_flags );
250 if ( ( conversion_flags & KColorMode_Mask ) == LowOnly ) {
257 if( img.numColors() > 0 && img.numColors() <=40 ) {
259 return TQPixmap::convertFromImage( img, TQPixmap::Auto );
265 TQImage image = img.convertDepth(32);
266 TQImage tImage( image.width(), image.height(), 8, 256 );
268 if( img.hasAlphaBuffer() ) {
269 image.setAlphaBuffer(
true );
270 tImage.setAlphaBuffer(
true );
271 isMask = mask.convertFromImage( img.createAlphaMask() );
274 kdither_32_to_8( &image, &tImage );
276 if( TQPixmap::convertFromImage( tImage ) ) {
277 if ( isMask ) TQPixmap::setMask( mask );
282 TQImage image = img.convertDepth( 32 );
283 image.setAlphaBuffer( img.hasAlphaBuffer() );
284 conversion_flags = (conversion_flags & ~ColorMode_Mask) |
Auto;
285 return TQPixmap::convertFromImage ( image, conversion_flags );
289 static TQColor* kpixmap_iconPalette = 0;
295 if (kpixmap_iconPalette == 0) {
296 kpixmap_iconPalette =
new TQColor[40];
299 kpixmap_iconPalette[i++] = red;
300 kpixmap_iconPalette[i++] = green;
301 kpixmap_iconPalette[i++] = blue;
302 kpixmap_iconPalette[i++] = cyan;
303 kpixmap_iconPalette[i++] = magenta;
304 kpixmap_iconPalette[i++] = yellow;
305 kpixmap_iconPalette[i++] = darkRed;
306 kpixmap_iconPalette[i++] = darkGreen;
307 kpixmap_iconPalette[i++] = darkBlue;
308 kpixmap_iconPalette[i++] = darkCyan;
309 kpixmap_iconPalette[i++] = darkMagenta;
310 kpixmap_iconPalette[i++] = darkYellow;
311 kpixmap_iconPalette[i++] = white;
312 kpixmap_iconPalette[i++] = lightGray;
313 kpixmap_iconPalette[i++] = gray;
314 kpixmap_iconPalette[i++] = darkGray;
315 kpixmap_iconPalette[i++] = black;
318 kpixmap_iconPalette[i++] = TQColor( 255, 192, 192 );
319 kpixmap_iconPalette[i++] = TQColor( 192, 255, 192 );
320 kpixmap_iconPalette[i++] = TQColor( 192, 192, 255 );
321 kpixmap_iconPalette[i++] = TQColor( 255, 255, 192 );
322 kpixmap_iconPalette[i++] = TQColor( 255, 192, 255 );
323 kpixmap_iconPalette[i++] = TQColor( 192, 255, 255 );
326 kpixmap_iconPalette[i++] = TQColor( 64, 0, 0 );
327 kpixmap_iconPalette[i++] = TQColor( 192, 0, 0 );
330 kpixmap_iconPalette[i++] = TQColor( 255, 128, 0 );
331 kpixmap_iconPalette[i++] = TQColor( 192, 88, 0 );
332 kpixmap_iconPalette[i++] = TQColor( 255, 168, 88 );
333 kpixmap_iconPalette[i++] = TQColor( 255, 220, 168 );
336 kpixmap_iconPalette[i++] = TQColor( 0, 0, 192 );
339 kpixmap_iconPalette[i++] = TQColor( 0, 64, 64 );
340 kpixmap_iconPalette[i++] = TQColor( 0, 192, 192 );
343 kpixmap_iconPalette[i++] = TQColor( 64, 64, 0 );
344 kpixmap_iconPalette[i++] = TQColor( 192, 192, 0 );
347 kpixmap_iconPalette[i++] = TQColor( 0, 64, 0 );
348 kpixmap_iconPalette[i++] = TQColor( 0, 192, 0 );
351 kpixmap_iconPalette[i++] = TQColor( 192, 0, 192 );
354 kpixmap_iconPalette[i++] = TQColor( 88, 88, 88 );
355 kpixmap_iconPalette[i++] = TQColor( 48, 48, 48 );
356 kpixmap_iconPalette[i++] = TQColor( 220, 220, 220 );
360 TQRgb* ctable = image.colorTable();
362 int ncols = image.numColors();
368 for ( i=0; i<ncols; i++ ) {
369 for ( j=0; j<40; j++ ) {
370 if ( kpixmap_iconPalette[j].red() == tqRed( ctable[i] ) &&
371 kpixmap_iconPalette[j].green() == tqGreen( ctable[i] ) &&
372 kpixmap_iconPalette[j].blue() == tqBlue( ctable[i] ) ) {
382 return ( failures <= 1 );
KPixmap()
Constructs a null pixmap.
bool convertFromImage(const TQImage &img, int conversion_flags)
Converts an image and sets this pixmap.
~KPixmap()
Destructs the pixmap.
bool checkColorTable(const TQImage &image)
Returns true if the image posesses a color table that matches the Icon palette or false otherwise.
ColorMode
This enumeration provides a color pallete specification.
@ LowColor
3x3x3 color cube (or monochrome)
@ WebColor
Netscape pallete (or monochrome)
@ Auto
Convert to monochrome if possible.
@ Color
Native display depth.
bool load(const TQString &fileName, const char *format, int conversion_flags)
Loads a pixmap from the file fileName.