Recent changes RSS feed

 

Navigator

starthelpinformationmail

Devb Code Snippets

These code snippets have been used in GrafxShop for Image Editing through different filters. Please read the notice carefully 1) before using any of the contents.

Inverting Colors in an Image in PHP

  • The code snippet shows how you can invert the colors of all pixels in an image.
  • This is what the code does. It takes as input the image (src) whose colors need to be inverted.
  • It determines the size of the image. It creates a new image (im2) with the same dimensions.
  • It then traverses through every pixel in the image.
  • It extracts the colors (r, g and b).
  • It then employs the algorithm
  • It puts the resultant colorto the new image (im2).

original inverted

 
function Invert($image_path) {
	@header("Content-Type: image/jpeg");
	$m_width = 0;
	$m_height = 0;
	$i = 0;
	$j = 0;
 
	$src = imagecreatefromjpeg($image_path);
	$m_width = imagesx($src);
	$m_height = imagesy($src);
	$im2 = imagecreatetruecolor($m_width, $m_height);
 
	for ($i = 0; $i < $m_height; $i++) {
		for ($j = 0; $j < $m_width; $j++) {
			$rgb = imagecolorat($src, $j, $i);
			$r = ($rgb >> 16) & 0xFF;
			$g = ($rgb >> 8) & 0xFF;
			$b = $rgb & 0xFF;
			$r = 255 - $r;
			$g = 255 - $g;
			$b = 255 - $b;
			$newcolor = imagecolorallocate($im2, $r, $g, $b);
			imagesetpixel($im2, $j, $i, $newcolor);
		}
	}
	imagejpeg($im2);
	imagedestroy($im2);
	exit;
}

Inverting Colors in an Image in Delphi

 
procedure Invert(src: tbitmap);
var 
  w, h, x, y: integer;
  p: pbytearray;
begin
  w := src.Width;
  h := src.Height;
  src.PixelFormat := pf24bit;
  for y := 0 to h - 1 do 
  begin
    p := src.scanline[y];
    for x := 0 to w - 1 do 
    begin
      p[x * 3] := not p[x * 3];
      p[x * 3 + 1] := not p[x * 3 + 1];
      p[x * 3 + 2] := not p[x * 3 + 2];
    end;
  end;
end;
1) if you do not agree to the notice and disclaimers, please do not use the links to navigate to other pages
 
graphics.txt · Last modified: 2009/10/03 19:40 (external edit)