Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.

Public Report
Report From: Kylix/VCLX/TBitmap    [ Add a report in this area ]  
Report #:  6171   Status: Reported
Impossible create and work with grayscaled 8-bit TBitmap instances.
Project:  Kylix Build #:  3.4.186
Version:    3.0 Submitted By:   Andrew Fionik
Report Type:  Minor failure / Design problem Date Reported:  10/10/2003 7:20:34 AM
Severity:    Serious / Highly visible problem Last Updated:
Platform:    RedHat Linux Internal Tracking #:  
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: None
Description
After creation of TBitmap with PixelFormat=pf8bit it is impossible to correctly work with bitmap scanlines because color table is undefined.  If you try to fill data accessing to ScanLine propery most likely you will get incorrect colors instead of grayscale or Access Violation. And there is no "legal" way to create color table for TBitmap.
Steps to Reproduce:
None
Workarounds
Fortunately it is possible to "hack" TBitmap to force it accept pre-constructed with QImage and QPixMap grayscaled image. It use certain "features" of TBitmap, precisely - knowledge about internal methods of TBitmap.

function CreateGrayscaleBitmap(const Width,Height:Integer):TBitmap;
var

hImage:QImageH;

hColor:QColorH;

RGB:QRGB;

ColorIndex:Integer;
begin

// initially we create 8-bit Qt image

hImage:=QImage_create(Width,Height,8,256,QImageEndian_IgnoreEndian);

try


// now create grayscaled palette for image


hColor:=QColor_create;


try



for ColorIndex:=0 to 255 do




begin





// set color gray value





QColor_setRgb(hColor,ColorIndex,ColorIndex,ColorIndex);





// fill RGB structure





QColor_rgb(hColor,@RGB);





// set color in image palette





QImage_setColor(hImage,ColorIndex,@RGB);




end;



// now we have QImage with grayscaled palette


finally



QColor_destroy(hColor);


end;


{ We should convert QImage to QPixmap because it's only way to


inject Qt graphics data into TBitmap, due to poor TBitmap's design.


Usually here we may fill hImage with some useful data}


Result:=TBitmap.Create;


try



{We do this to force TBitmap to create both QImage and QPixmap



instances, so TBitmap.Handle will be valid}



Result.PixelFormat:=pf8bit;



{Drop out not necessary image instance, pixmap still valid.}



Result.FreeImage;



// Inject data from empty but grayscaled image to TBitmap's pixmap



QPixmap_convertFromImage(Result.Handle,hImage,QPixmapColorMode_Auto);



{ Now force TBitmap to create QImage and QPixmap instances again.



Because SetPixelFormat call ImageNeeded first, QImage will be



constructed from pixmap data.}



Result.PixelFormat:=pf8bit;


except



Result.Free;



raise;


end;

finally


QImage_destroy(hImage);

end;
end;
Attachment
None
Comments

None

Server Response from: ETNACODE01