Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Bild: Umwandlung in Blaustufen (nicht Graustufen) (https://www.delphipraxis.net/91258-bild-umwandlung-blaustufen-nicht-graustufen.html)

BlueStarHH 2. Mai 2007 13:20


Bild: Umwandlung in Blaustufen (nicht Graustufen)
 
Hallo,

wie kann ich ein Bild in Blaustufen umwandeln? D.h. das Bild soll so wie ein Graustufenbild aussehen nur eben nicht in grau sondern in blau. Hat da jemand einen Tipp? Ganz gut wäre es auch, wenn die Lösung so allgemein wäre, dass die die Farbe, in die gewandelt werden soll, angeben könnte. Danke!

Graustufen mache ich so:
Delphi-Quellcode:
for X := 0 to Image.Width - 1 do
  for Y := 0 to Image.Height - 1 do
  begin
    PxlColor := ColorToRGB((Image as TBitmap).Canvas.Pixels[X, Y]);
    C := Round((((PxlColor shr 16) + ((PxlColor shr 8) and $00FF) + (PxlColor and $0000FF)) div 3)) div 2 + 96;
   (Image as TBitmap).Canvas.Pixels[X, Y] := RGB(C, C, C);
  end;

DGL-luke 2. Mai 2007 13:28

Re: Bild: Umwandlung in Blaustufen (nicht Graustufen)
 
Hallo, das ist ganz einfach:

Delphi-Quellcode:
for X := 0 to Image.Width - 1 do
  for Y := 0 to Image.Height - 1 do
  begin
    PxlColor := ColorToRGB((Image as TBitmap).Canvas.Pixels[X, Y]);
    C := Round((((PxlColor shr 16) + ((PxlColor shr 8) and $00FF) + (PxlColor and $0000FF)) div 3)) div 2 + 96;
   (Image as TBitmap).Canvas.Pixels[X, Y] := RGB(0, 0, C); //da spielt die musik
  end;
Ich würd das ganze aber mit Scanline machen:

Delphi-Quellcode:
var
  lineptr: PColor;

lineptr := (Image as TBitmap).ScanLine[0]; //erstes bildpixel
for Y := 0 to Image.Height - 1 do
begin
  for X := 0 to Image.Width - 1 do
  begin
    PxlColor := ColorToRGB(TColor(lineptr^));
    C := Round((((PxlColor shr 16) + ((PxlColor shr 8) and $00FF) + (PxlColor and $0000FF)) div 3)) div 2 + 96;
    lineptr^ := RGB(0, 0, C); //da spielt die musik
    Inc(lineptr); //nächstes bildpixel
  end;
end;
Ich hoffe das funktioniert so, hab das ausm Gedächtnics getippt, eventuell muss man da noch ein bisschen rumcasten.

Khabarakh 2. Mai 2007 13:37

Re: Bild: Umwandlung in Blaustufen (nicht Graustufen)
 
Die Helligkeitsberechnung kann ich zwar nicht ganz nachvollziehen (das ist weder der Mittelwert aller Kanäle noch der bekannte Grauwert = 0,299·Rot + 0,587·Grün + 0,114·Blau. Und überhaupt, Round bei int-Werten? div 3 div 2? Klammernwald *g* ?), aber sobald man den Helligkeitsanteil (0 <= x <= 1) in der Hand hat, muss man ihn nur noch mit der gewünschten Farbe multiplizieren.

grizzly 2. Mai 2007 14:21

Re: Bild: Umwandlung in Blaustufen (nicht Graustufen)
 
Hab's mal eben so versucht (Nur für 32bit Bilder):

Delphi-Quellcode:
procedure ConvertBitmap32(BM: TBitmap; NewCol: TColor);
var
  x, y : Integer;
  p   : pintegerarray;
  Gray : Integer;
  r,g,b: integer;
begin
  if BM.PixelFormat <> pf32bit
    then Exit;

  // Separate desired color into RGB
  b := (NewCol and $FF0000) shr 16;
  g := (NewCol and $FF00  ) shr 8;
  r := (NewCol and $FF   );

  // Change color for each pixel
  for y := BM.Height-1 downto 0 do
    begin
      p := BM.ScanLine[y];
      for x := 0 TO BM.Width-1 do
        begin
          // Determine gray value (Gray = 0.11*B + 0.50*G + 0.30*R)
          Gray := (((p^[x] and $FF)          ) *  28 +       // about 11% blue
                   ((p^[x] and $FF00)  shr 8) * 128 +       // about 50% green
                   ((p^[x] and $FF0000) shr 16) * 100) shr 8; // about 39% red
          // Gray now is between 0 and 255
          // Adjust color
          p^[x] := (((r * Gray) shr 8) shl 16 ) or
                   (((g * Gray) shr 8) shl 8 ) or
                   (((b * Gray) shr 8));
        end;
    end;
END;
NewCol bestimmt den Farbton, den das umgewandelte Bild haben soll. Für ein reines Graustufenbild muß also $00FFFFFF verwendet werden, für ein Blaustufenbild $00FF0000.
Die Bestimmung des Grauwertes folgt nur grob der von Khabarakh angesprochenen Umwandlung, aber solche Feinheiten sehen wir auf unseren unkalibrierten Monitoren eh nicht ;)
Allerdings ergibt sich durch diese Verwendung folgender Effekt: Ein helles blau wirkt im Blaustufenbild dunkler als im Original.

Gruß
Michael


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz