Recent

Author Topic: Raw image pixel access  (Read 3950 times)

user5

  • Sr. Member
  • ****
  • Posts: 357
Raw image pixel access
« on: December 03, 2019, 11:26:03 pm »
    Raw image pixel access is similar to BGRABitmap and in all the examples of it that I have seen it is used in an xy loop as shown in the first code example shown below. My question is: Can it be used outside of an xy loop and if so, how? If possible I would like to use it in conjunction with some code that opens a text file which provides xy coordinates such as shown in the second code example. Is this possible? I've been using TLazIntfImage as shown in the third bit of code and it's fast but sometimes it occasionally fails and an error message appears which says "Invalid horizontal index..." or something like that.


 
Code: Pascal  [Select][+][-]
  1. while (county < image302.height) do
  2.  begin
  3.   PixelPtr := PixelRowPtr;
  4.   while (countx < image302.width) do
  5.    begin
  6.     tempcolor := PixelPtr^;
  7.     tempcolor := SwapRedBlue(tempcolor);
  8.     Inc(PByte(PixelPtr), BytePerPixel);
  9.     countx := countx + 1;
  10.    end;
  11.   county := county + 1;
  12.  end;
  13.  
  14.  
  15.  
  16. while not eof(readfile) do
  17.  begin
  18.   readln(readfile,tempstr);
  19.   val(tempstr,x,code);
  20.   readln(readln,tempstr);
  21.   val(tempstr,y,code);
  22.  
  23.   //other code here
  24.  
  25.  end;
  26.  
  27.  
  28.  
  29. while not eof(readfile) do
  30.  begin
  31.   readln(readfile,tempstr);
  32.   val(tempstr,x,code);
  33.   readln(readln,tempstr);
  34.   val(tempstr,y,code);
  35.  
  36.   lazimage.colors[x,y] := lazcolor;
  37.  
  38.  end;


winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Raw image pixel access
« Reply #1 on: December 04, 2019, 12:53:33 am »
Hi!

I show you a solution with BGRAbitmap. I don't know if it exists with other bitmaps:

Code: Pascal  [Select][+][-]
  1. var
  2. img: TBGRABitmap;
  3. i,n : Integer;
  4. p.q: PBGRAPixel;
  5. begin
  6. p := img.data;
  7.     for i := 0 to  img.NbPixels -1 { img.Width * img.Height - 1 } do
  8.         begin
  9.          p^ := DoSomething;
  10.          inc(p);
  11.         end;    
  12.    img.InvalidateBitmap;
  13. end;
  14.  

But you can access every single pixel by

n := y * Img.width + x;

Then you do

q := p + n;
q^ := clRed;

Winni
« Last Edit: December 04, 2019, 01:41:50 am by winni »

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #2 on: December 04, 2019, 06:25:41 am »
    Hmmmm... Your suggestion is interesting but I don't know much about TBGRABitmap or if it can be applied to this case. What I have is a long text file that has information about various pixels in a TImage which is assigned to a bitmap and then to a TLazIntfImage (or TBGRABitmap). The number of entries in the text file don't usually equal the number of pixels in the image.
    Each entry in the text file represents a pixel in the image and each entry provides x and y. An example of an entry is shown below. The second line in the sample entry gives a particular color. Lines 5 and 6 respectively provide x and y of the image pixel to be changed. The 7th line provides another color. The program uses these colors to produce a third color. The code I'm currently using looks something like the code shown below following the sample file entry.
    I could have some part of the program count the number of entries in the file and then use that count number instead of useing while not eof(readfile). Can you think of a way to apply TBGRABitmap to this case?
    Thank you so much for helping me. The last few times that I have asked for help on this forum I received some solutions that were of critical importance to me.

Code: Pascal  [Select][+][-]
  1.  
  2. 6
  3. clWhite
  4. 337
  5. 148
  6. 94
  7. 18
  8. clBlue
  9.  
  10. while not eof(readfile) do //or for i := to 1 to numberofentries do
  11.  begin
  12.   //the program reads each entry in the text file to get 2 colors along with x and y
  13.   //code which uses those 2 colors to produce a third color
  14.   lazcolor := TColorToFPColor(thirdcolor);
  15.   lazimage.colors[x,y] := lazcolor; //the line that sometimes fails but not always
  16.  end;
  17.  

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #3 on: December 04, 2019, 06:40:07 am »
    Another idea occurred to me. If I can't come up with a way to use TBGRABitmap in this case then I could put "lazimage.colors[x,y] := lazcolor" in a try statement and if it fails then have the program use "mybitmap.canvas.pixels[x,y] := thirdcolor" instead though this wouldn't be the best solution.

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #4 on: December 04, 2019, 07:35:58 am »
    I just realized that my last idea wouldn't work because the code is working on the TLazIntfImage, not the bitmap. The best that I could do if it failed would be to just pass up that attempt and then try again. Duh.

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Raw image pixel access
« Reply #5 on: December 04, 2019, 04:57:30 pm »
You can use TBGRABitmap.SetPixel(x,y,color) for that.

 8-)
Conscience is the debugger of the mind

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #6 on: December 04, 2019, 06:59:40 pm »
    So the code would be something like this?
    I've had difficulty in installing bgrabitmappackage.lpk in the past but I'm going to try again.
    Thanks.

   
Code: Pascal  [Select][+][-]
  1. var img: TBGRABitmap;
  2.     mybitmap:TBitmap;
  3.     x,y:integer;
  4.     tempcolor,thirdcolor:TColor;
  5.     readfile:TextFile;
  6.  
  7. begin
  8.  
  9.  mybitmap := TBitmap.create;
  10.  mybitmap.width := image1.width;
  11.  mybitmap.height := image1.height;
  12.  mybitmap.assign(image1.picture.bitmap); //TImage
  13.  img := TBGRABitmap.create(mybitmap.width,mybitmap.height);
  14.  img.assign(mybitmap);
  15.  assignfile(readfile,'edges.txt');
  16.  reset(readfile);
  17.  
  18.  while not eof(readfile) do //or for i := to 1 to numberofentries do
  19.   begin
  20.    //the program reads each entry in the text file to get 2 colors along with x and y
  21.    //code which uses those 2 colors to produce a third color
  22.    img.SetPixel(x,y,thirdcolor);
  23.   end;
  24.  
  25.  img.free;
  26.  mybitmap.free;
  27.  closefile(readfile);
  28.  
  29. end;

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Raw image pixel access
« Reply #7 on: December 04, 2019, 07:22:58 pm »
    I've had difficulty in installing bgrabitmappackage.lpk in the past but I'm going to try again.

Do you know OPM (Online Package Manager)? It makes installing packages much easier.
https://wiki.freepascal.org/Online_Package_Manager

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #8 on: December 04, 2019, 07:38:35 pm »
    I'm once again having trouble installing bgrabitmappack.lpk
    I downloaded the lpk zip file and unzipped it to a folder
    I clicked on Package and then I clicked on Open Package File (.lpk)
    I then clicked on bgrabitmappack.lpk
    On the window that appeared I clicked on Use but the only highlighted item was Add to Project. Install was not highlighted.
    I clicked on Compile but got an error message that said: C:\Users\Art McGinnis\Desktop\temp9\bgrabitmap-master\bgrabitmap\bgramultifiletype.pas(70,58) Error: Identifier not found "RawByteString"
    I don't understand what's going on here. I'm not sure what to do now. Maybe I'll check out OPM. What a drag.

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #9 on: December 04, 2019, 08:06:21 pm »
    On the OPM page the instructions said: In Lazarus "Install/Uninstall Packages" window select "OnlinePackageManager" from the available packages, click "Install selection" and then rebuild the IDE.
    I did that but OnlinePackageManager was not listed as an available package. I did see an item titled:
LazarusPackageManager 0.0.1 and I tried to install it but got an error message that said: Broken dependency - Ome or more required packages were not found.
    If someone could provide me with step by step instructions then that might help.
    Otherwise I guess that I can't use BGRABitmap.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Raw image pixel access
« Reply #10 on: December 04, 2019, 08:13:41 pm »
Don't give up easily sir.

What is the version of your Lazarus? OPM can be found in the directory:
($LazarusDir)/components/onlinepackagemanager

But if you can't find it (because you're using older Lazarus version), you can download OPM 1.6 from here:
https://drive.google.com/file/d/0B9Me_c5onmWobVZOdVlXSlZRRmc/view

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Raw image pixel access
« Reply #11 on: December 04, 2019, 11:29:20 pm »
You don't need to install BGRABitmap. Just open the package once and that's it.

After that, you can add it to your project.

https://wiki.freepascal.org/BGRABitmap_tutorial_1
Conscience is the debugger of the mind

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Raw image pixel access
« Reply #12 on: December 04, 2019, 11:30:54 pm »
    Yes, you are correct; I had forgotten that I was using an older version of Lazarus because a project I was trying to use wouldn't compile with the newest version of Lazarus.
    I uninstalled the old version and installed the new version of Lazarus. I installed the component for OPM and then used it to install BGRABitmap.
    The code img.SetPixel(x,y,thirdcolor); compiled just fine so it looks like I'm in business.
    Thank you all for your patience and understanding. Once again this forum came through for me.

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Raw image pixel access
« Reply #13 on: December 04, 2019, 11:33:08 pm »
You're welcome  :)
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018