X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8208e181cb576ec6cda37624923f95a59af43072..4004f41e3ce1655f9fa05c55a697ac0f8677c23a:/src/common/image.cpp diff --git a/src/common/image.cpp b/src/common/image.cpp index 627e7ab6ad..58dec0c3b6 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -236,15 +236,15 @@ void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, for (int j = 0; j < h; j++) for (int i = 0; i < w; i++) - { - if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) - { - data[0] = r2; - data[1] = g2; - data[2] = b2; - } - data += 3; - } + { + if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) + { + data[0] = r2; + data[1] = g2; + data[2] = b2; + } + data += 3; + } } void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) @@ -402,7 +402,7 @@ bool wxImage::LoadFile( const wxString& filename, long type ) if (wxFileExists(filename)) { wxFileInputStream stream(filename); - wxBufferedInputStream bstream( stream ); + wxBufferedInputStream bstream( stream ); return LoadFile(bstream, type); } else @@ -422,7 +422,7 @@ bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype ) if (wxFileExists(filename)) { wxFileInputStream stream(filename); - wxBufferedInputStream bstream( stream ); + wxBufferedInputStream bstream( stream ); return LoadFile(bstream, mimetype); } else @@ -443,7 +443,7 @@ bool wxImage::SaveFile( const wxString& filename, int type ) if ( stream.LastError() == wxStream_NOERROR ) { - wxBufferedOutputStream bstream( stream ); + wxBufferedOutputStream bstream( stream ); return SaveFile(bstream, type); } else @@ -458,7 +458,7 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) if ( stream.LastError() == wxStream_NOERROR ) { - wxBufferedOutputStream bstream( stream ); + wxBufferedOutputStream bstream( stream ); return SaveFile(bstream, mimetype); } else @@ -486,7 +486,7 @@ bool wxImage::CanRead( wxInputStream &stream ) { wxImageHandler *handler=(wxImageHandler*)node->GetData(); if (handler->CanRead( stream )) - return TRUE; + return TRUE; } return FALSE; @@ -857,7 +857,7 @@ wxBitmap wxImage::ConvertToBitmap() const if( HasMask() ) { hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL ); - ::SelectObject( memdc, hbitmap); + HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap); if( numDIB == 1 ) height = bmpHeight; else height = sizeLimit/bytePerLine; lpDIBh->bmiHeader.biHeight = (DWORD)(-height); @@ -884,10 +884,10 @@ wxBitmap wxImage::ConvertToBitmap() const for(i=0; i> 3)+8) * height ); mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height ); @@ -1107,11 +1108,11 @@ wxBitmap wxImage::ConvertToBitmap() const int g_mask = GetMaskGreen(); int b_mask = GetMaskBlue(); - CGrafPtr origPort ; - GDHandle origDevice ; - - GetGWorld( &origPort , &origDevice ) ; - SetGWorld( bitmap.GetHBITMAP() , NULL ) ; + CGrafPtr origPort ; + GDHandle origDevice ; + + GetGWorld( &origPort , &origDevice ) ; + SetGWorld( bitmap.GetHBITMAP() , NULL ) ; register unsigned char* data = GetData(); @@ -1120,18 +1121,18 @@ wxBitmap wxImage::ConvertToBitmap() const { for (int x = 0; x < width; x++) { - unsigned char r = data[index++]; - unsigned char g = data[index++]; - unsigned char b = data[index++]; - RGBColor color ; - color.red = ( r << 8 ) + r ; - color.green = ( g << 8 ) + g ; - color.blue = ( b << 8 ) + b ; - SetCPixel( x , y , &color ) ; - } + unsigned char r = data[index++]; + unsigned char g = data[index++]; + unsigned char b = data[index++]; + RGBColor color ; + color.red = ( r << 8 ) + r ; + color.green = ( g << 8 ) + g ; + color.blue = ( b << 8 ) + b ; + SetCPixel( x , y , &color ) ; + } } // for height - SetGWorld( origPort , origDevice ) ; + SetGWorld( origPort , origDevice ) ; if ( HasMask() ) { @@ -1228,7 +1229,7 @@ wxImage::wxImage( const wxBitmap &bitmap ) ptbits += 3; } ptbits += padding; - } + } // similarly, set data according to the possible mask bitmap if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() ) @@ -1290,6 +1291,8 @@ wxImage::wxImage( const wxBitmap &bitmap ) #include #endif +extern GtkWidget *wxRootWindow; + wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue ) { wxBitmap bitmap; @@ -1302,16 +1305,19 @@ wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, u bitmap.SetHeight( height ); bitmap.SetWidth( width ); - bitmap.SetBitmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 ) ); + bitmap.SetBitmap( gdk_pixmap_new( wxRootWindow->window, width, height, 1 ) ); bitmap.SetDepth( 1 ); + GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window ); + wxASSERT( visual ); + // Create picture image unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); GdkImage *data_image = - gdk_image_new_bitmap( gdk_visual_get_system(), data_data, width, height ); + gdk_image_new_bitmap( visual, data_data, width, height ); // Create mask image @@ -1321,10 +1327,10 @@ wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, u { unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); - mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height ); + mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); wxMask *mask = new wxMask(); - mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 ); + mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 ); bitmap.SetMask( mask ); } @@ -1400,12 +1406,13 @@ wxBitmap wxImage::ConvertToBitmap() const bitmap.SetHeight( height ); bitmap.SetWidth( width ); - bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) ); + bitmap.SetPixmap( gdk_pixmap_new( wxRootWindow->window, width, height, -1 ) ); - // Retrieve depth + // Retrieve depth - GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() ); - if (visual == NULL) visual = gdk_visual_get_system(); + GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window ); + wxASSERT( visual ); + int bpp = visual->depth; bitmap.SetDepth( bpp ); @@ -1445,7 +1452,7 @@ wxBitmap wxImage::ConvertToBitmap() const // Create picture image GdkImage *data_image = - gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height ); + gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height ); // Create mask image @@ -1455,10 +1462,10 @@ wxBitmap wxImage::ConvertToBitmap() const { unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); - mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height ); + mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); wxMask *mask = new wxMask(); - mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 ); + mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 ); bitmap.SetMask( mask ); } @@ -1470,7 +1477,6 @@ wxBitmap wxImage::ConvertToBitmap() const if (bpp >= 24) { - GdkVisual *visual = gdk_visual_get_system(); if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB; else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; @@ -1634,19 +1640,36 @@ wxImage::wxImage( const wxBitmap &bitmap ) } int bpp = -1; + int red_shift_right = 0; + int green_shift_right = 0; + int blue_shift_right = 0; + int red_shift_left = 0; + int green_shift_left = 0; + int blue_shift_left = 0; + bool use_shift = FALSE; + if (bitmap.GetPixmap()) { GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() ); - if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent ); + if (visual == NULL) visual = gdk_window_get_visual( wxRootWindow->window ); bpp = visual->depth; - if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; + if (bpp == 16) bpp = visual->red_prec + visual->green_prec + visual->blue_prec; + red_shift_right = visual->red_shift; + red_shift_left = 8-visual->red_prec; + green_shift_right = visual->green_shift; + green_shift_left = 8-visual->green_prec; + blue_shift_right = visual->blue_shift; + blue_shift_left = 8-visual->blue_prec; + + use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR); } if (bitmap.GetBitmap()) { bpp = 1; } + GdkColormap *cmap = gtk_widget_get_default_colormap(); long pos = 0; @@ -1654,54 +1677,38 @@ wxImage::wxImage( const wxBitmap &bitmap ) { for (int i = 0; i < bitmap.GetWidth(); i++) { - wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j ); - if (bpp == 1) - { - if (pixel == 0) - { - data[pos] = 0; + wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j ); + if (bpp == 1) + { + if (pixel == 0) + { + data[pos] = 0; data[pos+1] = 0; data[pos+2] = 0; - } - else - { - data[pos] = 255; + } + else + { + data[pos] = 255; data[pos+1] = 255; data[pos+2] = 255; - } - } else if (bpp <= 8) - { - data[pos] = cmap->colors[pixel].red >> 8; - data[pos+1] = cmap->colors[pixel].green >> 8; - data[pos+2] = cmap->colors[pixel].blue >> 8; - } else if (bpp == 15) + } + } + else if (use_shift) { -#if (wxBYTE_ORDER == wxBIG_ENDIAN) - // ? -#endif - data[pos] = (pixel >> 7) & 0xf8; - data[pos+1] = (pixel >> 2) & 0xf8; - data[pos+2] = (pixel << 3) & 0xf8; - } else if (bpp == 16) + data[pos] = (pixel >> red_shift_right) << red_shift_left; + data[pos+1] = (pixel >> green_shift_right) << green_shift_left; + data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; + } + else if (cmap->colors) { -#if (wxBYTE_ORDER == wxBIG_ENDIAN) - // ? -#endif - data[pos] = (pixel >> 8) & 0xf8; - data[pos+1] = (pixel >> 3) & 0xfc; - data[pos+2] = (pixel << 3) & 0xf8; - } else + data[pos] = cmap->colors[pixel].red >> 8; + data[pos+1] = cmap->colors[pixel].green >> 8; + data[pos+2] = cmap->colors[pixel].blue >> 8; + } + else { -#if (wxBYTE_ORDER == wxBIG_ENDIAN) - data[pos] = (pixel) & 0xff; // Red - data[pos+1] = (pixel >> 8) & 0xff; // Green - data[pos+2] = (pixel >> 16) & 0xff; // Blue -#else - data[pos] = (pixel >> 16) & 0xff; - data[pos+1] = (pixel >> 8) & 0xff; - data[pos+2] = pixel & 0xff; -#endif - } + wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); + } if (gdk_image_mask) { @@ -1797,50 +1804,50 @@ private: wxSearchColor::wxSearchColor( void ) { - this->size = 0; - this->colors = (XColor*) NULL; - this->color = (unsigned int *) NULL; - this->entry = (int*) NULL; + size = 0; + colors = (XColor*) NULL; + color = (unsigned int *) NULL; + entry = (int*) NULL; - this->bottom = 0; - this->top = 0; + bottom = 0; + top = 0; } -wxSearchColor::wxSearchColor( int size, XColor *colors ) +wxSearchColor::wxSearchColor( int size_, XColor *colors_ ) { int i; - this->size = size; - this->colors = colors; - this->color = new unsigned int[size]; - this->entry = new int [size]; + size = size_; + colors = colors_; + color = new unsigned int[size]; + entry = new int [size]; - for (i = 0; i < this->size; i++ ) { - this->entry[i] = -1; - } + for (i = 0; i < size; i++ ) { + entry[i] = -1; + } - this->bottom = this->top = ( size >> 1 ); + bottom = top = ( size >> 1 ); } wxSearchColor::~wxSearchColor( void ) { - if ( this->color ) delete this->color; - if ( this->entry ) delete this->entry; + if ( color ) delete color; + if ( entry ) delete entry; } int wxSearchColor::SearchColor( int r, int g, int b ) { unsigned int value = ( ( ( r * 256 ) + g ) * 256 ) + b; - int begin = this->bottom; - int end = this->top; + int begin = bottom; + int end = top; int middle; while ( begin <= end ) { middle = ( begin + end ) >> 1; - if ( value == this->color[middle] ) { - return( this->entry[middle] ); - } else if ( value < this->color[middle] ) { + if ( value == color[middle] ) { + return( entry[middle] ); + } else if ( value < color[middle] ) { end = middle - 1; } else { begin = middle + 1; @@ -1864,47 +1871,47 @@ int wxSearchColor::AddColor( unsigned int value, int pos ) if (sum < max) { pixel = i; max = sum; } } - if ( this->entry[pos] < 0 ) { - this->color[pos] = value; - this->entry[pos] = pixel; - } else if ( value < this->color[pos] ) { + if ( entry[pos] < 0 ) { + color[pos] = value; + entry[pos] = pixel; + } else if ( value < color[pos] ) { - if ( this->bottom > 0 ) { - for ( i = this->bottom; i < pos; i++ ) { - this->color[i-1] = this->color[i]; - this->entry[i-1] = this->entry[i]; + if ( bottom > 0 ) { + for ( i = bottom; i < pos; i++ ) { + color[i-1] = color[i]; + entry[i-1] = entry[i]; } - this->bottom--; - this->color[pos-1] = value; - this->entry[pos-1] = pixel; - } else if ( this->top < this->size-1 ) { - for ( i = this->top; i >= pos; i-- ) { - this->color[i+1] = this->color[i]; - this->entry[i+1] = this->entry[i]; + bottom--; + color[pos-1] = value; + entry[pos-1] = pixel; + } else if ( top < size-1 ) { + for ( i = top; i >= pos; i-- ) { + color[i+1] = color[i]; + entry[i+1] = entry[i]; } - this->top++; - this->color[pos] = value; - this->entry[pos] = pixel; + top++; + color[pos] = value; + entry[pos] = pixel; } } else { - if ( this->top < this->size-1 ) { - for ( i = this->top; i > pos; i-- ) { - this->color[i+1] = this->color[i]; - this->entry[i+1] = this->entry[i]; + if ( top < size-1 ) { + for ( i = top; i > pos; i-- ) { + color[i+1] = color[i]; + entry[i+1] = entry[i]; } - this->top++; - this->color[pos+1] = value; - this->entry[pos+1] = pixel; - } else if ( this->bottom > 0 ) { - for ( i = this->bottom; i < pos; i++ ) { - this->color[i-1] = this->color[i]; - this->entry[i-1] = this->entry[i]; + top++; + color[pos+1] = value; + entry[pos+1] = pixel; + } else if ( bottom > 0 ) { + for ( i = bottom; i < pos; i++ ) { + color[i-1] = color[i]; + entry[i-1] = entry[i]; } - this->bottom--; - this->color[pos] = value; - this->entry[pos] = pixel; + bottom--; + color[pos] = value; + entry[pos] = pixel; } } @@ -2056,8 +2063,8 @@ wxBitmap wxImage::ConvertToBitmap() const */ #endif - // And this is all to get the 'right' color... - int pixel = scolor.SearchColor( r, g, b ); + // And this is all to get the 'right' color... + int pixel = scolor.SearchColor( r, g, b ); XPutPixel( data_image, x, y, pixel ); break; }