Results 1 to 9 of 9

Thread: How to desaturate image?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    I'm not familiar with Asphyre but usualy delphi image classes expose 'scanline' property, it's faster than 'pixels'

  2. #2
    Figured it out guys. I had to use .Lock() on texture, which gave me back pointer to pixels, similar to what ScanLine does. Much faster, and with that gray multiplication trick I made it to work.

    Code:
    procedure TTableResources.DesaturateImage(const AImage: TAsphyreImage);
    type
      PPixelRec = ^TPixelRec;
      TPixelRec = packed record
        B: Byte;
        G: Byte;
        R: Byte;
        A: Byte;
      end;
    var
      C1, x, y: Integer;
      bitsp: pointer;
      pitch: Integer;
      bytes_per_pixel: Integer;
      pixel: PPixelRec;
      gray_value: Byte;
    begin
      for C1 := 0 to AImage.TextureCount - 1 do
      begin
        AImage.Texture[C1].Lock(Rect(0, 0, AImage.Texture[C1].Width, AImage.Texture[C1].Height), bitsp, pitch);
        try
          bytes_per_pixel := pitch div AImage.Texture[C1].Width;
          for y := 0 to AImage.Texture[C1].Height - 1 do
            for x := 0 to AImage.Texture[C1].Width - 1 do
            begin
              pixel := PPixelRec(Integer(bitsp) + y * pitch + x * bytes_per_pixel);
              gray_value := Round(0.30 * pixel^.r + 0.59 * pixel^.g + 0.11 * pixel^.b);
              pixel^.r := gray_value;
              pixel^.g := gray_value;
              pixel^.b := gray_value;
            end;
        finally
          AImage.Texture[C1].Unlock;
        end;
      end;
    end;
    Thanks!

  3. #3
    Quote Originally Posted by reiser View Post
    Figured it out guys. I had to use .Lock() on texture, which gave me back pointer to pixels, similar to what ScanLine does. Much faster, and with that gray multiplication trick I made it to work.
    I'm glad you found solution for this.
    You might wanna update the SO question with your answer so other pepole could make use of it.

  4. #4
    Did you try http://graphics32.org/wiki/Main/Graphics32? has a lot of filters and image manipulation methods.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •