Recent

Author Topic: Invalid horizontal pixel index  (Read 241 times)

user5

  • Sr. Member
  • ****
  • Posts: 254
Invalid horizontal pixel index
« on: December 16, 2019, 08:26:39 am »
    The four statements below do basically the same thing, which is to change a pixel color. The third and fourth statements occasionally fail for some unknown reason. I'm sure that x and y are never out of bounds. They are part of some code that I can run many times with no problem but sometimes they will fail even though the conditions are exactly the same as the successful runs. I would like to keep using the third and/or fourth statements because they are much faster than the other statements. When this error happens I get a critical error message which says "Invalid horizontal pixel index" and my question is: Will a try statement catch this kind of error?

 
Code: Pascal  [Select]
  1. image1.canvas.pixels[x,y] := tempbackcolor; //1
  2. mybitmap.canvas.pixels[x,y] := tempbackcolor; //2
  3. lazcolor := TColorToFPColor(tempbackcolor);
  4. lazimage.colors[x,y] := lazcolor; //3
  5. bgra.SetPixel(x,y,tempbackcolor); //4

user5

  • Sr. Member
  • ****
  • Posts: 254
Re: Invalid horizontal pixel index
« Reply #1 on: December 17, 2019, 06:37:28 am »
    I've done some research since I posted the previous message so I know that this is a rare problem but from what I've learned the index refers to a color. The reason that the 3rd and 4th statements sometimes fail may be because the color (tempbackcolor) is invalid. If the program could first make sure that tempbackcolor is a valid color before trying to change the pixel color then this problem could be avoided. In other words, if tempbackcolor <> a valid color then do something other than trying to change the color.
    Does anyone know if there's a way to do that quickly in the code?

user5

  • Sr. Member
  • ****
  • Posts: 254
Re: Invalid horizontal pixel index
« Reply #2 on: December 17, 2019, 08:50:34 am »
    The "Invalid horizontal pixel index" error happened again but it didn't happen with BGRABitmap. It occurred using the third shown technique "lazimage.colors[x,y] := lazcolor;" so I'm going to get rid of TLazIntfImage and replace it with BGRABitmap. There were two sections of code with the first one using TLazIntfImage and the second one using BGRABitmap. The error happened in the first section. BGRABitmap is pretty cool. I'd still like to know if there's a way to know if a color is a legit color.

winni

  • Hero Member
  • *****
  • Posts: 709
Re: Invalid horizontal pixel index
« Reply #3 on: December 17, 2019, 10:24:26 am »
Hi!

The "Invalid horizontal pixel index" error  happens if x < 0 or x >= the width of the image.

What do you mean with "valid color" ??

Winni