Recent

Author Topic: TBGRABitmap and BMP  (Read 17273 times)

Timewarp

  • Full Member
  • ***
  • Posts: 144
Re: TBGRABitmap and BMP
« Reply #15 on: August 16, 2012, 11:41:09 am »
Do you get the message "Unable to convert image to 24 bit" ?
There are no errors. Image is just broken after I use functions rotate, resample..
send us 1 image and sample program in which it doesn't work with that code
That's what I did, non working program is attached to post1. It's not so simple, but I see no reason why it should fail.

Here is more simpler, can you test this code in Windows, please! Just drop TImage to form, run code and see. Does it work for you? Now change pf24bit to pf32bit and see it working.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var b: TBitmap;
c: TBGRACustomBitmap;
begin
 b:=TBitmap.create;
 b.pixelformat:=pf24bit;
 b.width:=100;
 b.height:=50;
 b.Canvas.Brush.Color:=clskyblue;
 b.Canvas.FillRect(b.Canvas.ClipRect);
 b.Canvas.TextOut(30,20,'HELLO!');
 c:=TBGRABitmap.create(b);
 c:=c.RotateCW as TBGRABitmap;
 Image1.picture.bitmap.assign(c.bitmap);
 //c.free; //avoid another bug
 b.free;
end;
Should KpjComp's code in second post work?
Yes it works, but I can't use Loadfromfile with memory bitmaps.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: TBGRABitmap and BMP
« Reply #16 on: August 16, 2012, 12:36:23 pm »
Could you try this ->

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  bmpTmp: TBitmap;
  bmpBGRA: TBGRACustomBitmap;
begin
  bmpTmp:=TBitmap.create;
  try
    //first lets create our memory bitmap
    with BmpTmp do begin
      pixelformat:=pf24bit;
      SetSize(100,50);
      with Canvas do begin
        Brush.Color:=clskyblue;
        FillRect(bmpTmp.Canvas.ClipRect);
        TextOut(30,20,'HELLO!');
      end;
    end;
    //lets use BGRABitmap to do some magic
    bmpBGRA:=TBGRABitmap.create(bmpTmp);
    try
      bmpBGRA.Canvas.Draw(0,0,bmpTmp);
      //make sure we use BGRAReplace to avoid memory leak
      BGRAReplace(TBgraBitmap(bmpBGRA),bmpBGRA.RotateCW);
      //not lets draw our new BGRABitmap into a TBitmap that we can use with TImage
      with bmpTmp do
      begin
        Clear; //do this to avoid Bitmap going strange on resize, possible bug!!
        SetSize(bmpBGRA.Width,bmpBGRA.height);
        Canvas.Draw(0,0,bmpBGRA.Bitmap);
      end;
      //now assign it to TImage
      image1.picture.graphic := bmpTmp;
    finally
      bmpBGRA.free;
    end;
  finally
    bmpTmp.free;
  end;
end; 

Timewarp

  • Full Member
  • ***
  • Posts: 144
Re: TBGRABitmap and BMP
« Reply #17 on: August 16, 2012, 01:12:08 pm »
Could you try this ->
Thanks again, code does exactly what I need!

circular

  • Hero Member
  • *****
  • Posts: 3082
    • Personal webpage
Re: TBGRABitmap and BMP
« Reply #18 on: August 16, 2012, 03:40:31 pm »
That's a clever move KpjComp :

Code: [Select]
bmpBGRA:=TBGRABitmap.create(bmpTmp);
      bmpBGRA.Canvas.Draw(0,0,bmpTmp);

If the second line is needed, well, it confirms that the Assign method of TBGRABitmap does not work all the time. The bitmap data is probably organized in a special way. It could be added, but I have no idea what bitmap format it is.

As I said in a previous post, it would be needed to trace into LoadFromRawImage function and see what ARawImage.Description contains.
Conscience is the debugger of the mind

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: TBGRABitmap and BMP
« Reply #19 on: August 16, 2012, 04:11:32 pm »
Cheers, @circular :)

Yes, the second line is needed.
I could have just done bmpBGRA:=TBGRABitmap.create(bmpTmp.width,bmpTmp.height);  instead.

Here is the Description props.
Code: [Select]
<TRAWIMAGEDESCRIPTION> = {
  FORMAT = RICFRGBA,
  WIDTH = 100,
  HEIGHT = 50,
  DEPTH = 24,
  BITORDER = RIBOREVERSEDBITS,
  BYTEORDER = RIBOLSBFIRST,
  LINEORDER = RILOTOPTOBOTTOM,
  LINEEND = RILEDWORDBOUNDARY,
  BITSPERPIXEL = 24,
  REDPREC = 8,
  REDSHIFT = 16,
  GREENPREC = 8,
  GREENSHIFT = 8,
  BLUEPREC = 8,
  BLUESHIFT = 0,
  ALPHAPREC = 0,
  ALPHASHIFT = 0,
  MASKBITSPERPIXEL = 1,
  MASKSHIFT = 0,
  MASKLINEEND = RILEWORDBOUNDARY,
  MASKBITORDER = RIBOREVERSEDBITS,
  PALETTECOLORCOUNT = 0,
  PALETTEBITSPERINDEX = 0,
  PALETTESHIFT = 0,
  PALETTELINEEND = RILETIGHT,
  PALETTEBITORDER = RIBOBITSINORDER,
  PALETTEBYTEORDER = RIBOLSBFIRST}

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: TBGRABitmap and BMP
« Reply #20 on: August 16, 2012, 04:33:37 pm »
Ok,

I think I've found the error..  :D

Code: [Select]
  procedure CopyRGBAndSwapIfNecessary(psrc: PByte; pdest: PBGRAPixel; count: integer);
  begin
    if mustSwapRedBlue then
    begin
      while count > 0 do
      begin
        pdest^.blue := (psource_byte+2)^;
        pdest^.green := (psource_byte+1)^;
        pdest^.red := psource_byte^;
        pdest^.alpha := DefaultOpacity;
        inc(psrc,3);
        inc(pdest);
        dec(count);
      end;
    end else
    begin
      while count > 0 do
      begin
        PWord(pdest)^ := PWord(psource_byte)^;
        pdest^.red := (psource_byte+2)^;
        pdest^.alpha := DefaultOpacity;
        inc(psrc,3);
        inc(pdest);
        dec(count);
      end;
    end;
  end; 

Can you see how it's using psource_byte for referencing,  and then incrementing psrc?.
I beleive the psource_byte's want changing to psrc.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: TBGRABitmap and BMP
« Reply #21 on: August 16, 2012, 04:58:41 pm »
Confirmed.

Changing CopyRGBAndSwapIfNecessary to use psrc makes Assign of 24bit work.

Here it is modified..

Code: [Select]
  procedure CopyRGBAndSwapIfNecessary(psrc: PByte; pdest: PBGRAPixel; count: integer);
  begin
    if mustSwapRedBlue then
    begin
      while count > 0 do
      begin
        pdest^.blue := (psrc+2)^;
        pdest^.green := (psrc+1)^;
        pdest^.red := psrc^;
        pdest^.alpha := DefaultOpacity;
        inc(psrc,3);
        inc(pdest);
        dec(count);
      end;
    end else
    begin
      while count > 0 do
      begin
        PWord(pdest)^ := PWord(psrc)^;
        pdest^.red := (psrc+2)^;
        pdest^.alpha := DefaultOpacity;
        inc(psrc,3);
        inc(pdest);
        dec(count);
      end;
    end;
  end;     

circular

  • Hero Member
  • *****
  • Posts: 3082
    • Personal webpage
Re: TBGRABitmap and BMP
« Reply #22 on: August 16, 2012, 06:13:59 pm »
Thanks. I've updated on subversion  :)

EDIT: new zip file available (version 5.9.3)

http://sourceforge.net/projects/lazpaint/files/src/
« Last Edit: August 16, 2012, 06:20:28 pm by circular »
Conscience is the debugger of the mind

Timewarp

  • Full Member
  • ***
  • Posts: 144
Re: TBGRABitmap and BMP
« Reply #23 on: August 17, 2012, 09:53:07 am »
Thanks for support!

I believe there is more:
Code: [Select]
  while count > 0 do
      begin
        PWord(pdest)^ := PWord(psource_byte)^;
        pdest^.red := psrc^.red;
        pdest^.alpha := DefaultOpacity; //use default opacity
        inc(psrc);
        inc(pdest);
        dec(count);
      end;
change "psource_byte" to "psrc"

circular

  • Hero Member
  • *****
  • Posts: 3082
    • Personal webpage
Re: TBGRABitmap and BMP
« Reply #24 on: August 17, 2012, 10:56:53 am »
Was already there (see the scrollbar).
Conscience is the debugger of the mind

Timewarp

  • Full Member
  • ***
  • Posts: 144
Re: TBGRABitmap and BMP
« Reply #25 on: August 17, 2012, 11:25:22 am »
Was already there (see the scrollbar).
It's in CopyAndSwapIfNecessaryAndSetAlpha function. I noticed, because one problem remained in 5.9.3. After that change everything works!

circular

  • Hero Member
  • *****
  • Posts: 3082
    • Personal webpage
Re: TBGRABitmap and BMP
« Reply #26 on: August 17, 2012, 12:06:31 pm »
Oh ok. Updated on subversion. Zip file (5.9.4) here :
http://sourceforge.net/projects/lazpaint/files/src/
Conscience is the debugger of the mind