Jump to content


Photo

Image generator


  • Please log in to reply
6 replies to this topic

#1 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 01 August 2006 - 09:59 PM

Theoretically, this should generate a nice smooth image, but instead it draws some semi-smooth stuff on the left and then pretty much random noise..
Any ideas or help? :mellow:

Here's the code, and a test page, if you want:
<?	header ("Content-type: image/png");	function rgbclr($from, $from2, $var)	{		if($from != '' && $from2 != '')		{				$average = $from + $from2 / 2;			return mt_rand($average - $var, $average + $var);		}		elseif($from != '')		{			return mt_rand($from - $var, $from + $var);		}		elseif($from2 != '')		{			return mt_rand($from2 - $var, $from2 + $var);		}	}		$diff = 2;	$im = imagecreatetruecolor($_GET['w'], $_GET['h']);	for($y = 0; $y < $_GET['h']; $y++)	{		for($x = 0; $x < $_GET['w']; $x++)		{			if($y == 0 && $x == 0)			{				$r[$y][$x] = mt_rand(0, 255);				$g[$y][$x] = mt_rand(0, 255);				$b[$y][$x] = mt_rand(0, 255);				$color = imagecolorallocate($im, $r[$y][$x], $g[$y][$x], $b[$y][$x]);				imagesetpixel($im, $x, $y, $color);			}			elseif($y == 0)			{				$r[$y][$x] = rgbclr($r[$y][$x-1], '', $diff);				$g[$y][$x] = rgbclr($g[$y][$x-1], '', $diff);				$b[$y][$x] = rgbclr($b[$y][$x-1], '', $diff);				$color = imagecolorallocate($im, $r[$y][$x], $g[$y][$x], $b[$y][$x]);				imagesetpixel($im, $x, $y, $color);			}			elseif($x == 0)			{				$r[$y][$x] = rgbclr('', $r[$y-1][$x], $diff);				$g[$y][$x] = rgbclr('', $g[$y-1][$x], $diff);				$b[$y][$x] = rgbclr('', $b[$y-1][$x], $diff);				$color = imagecolorallocate($im, $r[$y][$x], $g[$y][$x], $b[$y][$x]);				imagesetpixel($im, $x, $y, $color);			}			else			{				$r[$y][$x] = rgbclr($r[$y][$x-1], $r[$y-1][$x], $diff);				$g[$y][$x] = rgbclr($g[$y][$x-1], $g[$y-1][$x], $diff);				$b[$y][$x] = rgbclr($b[$y][$x-1], $b[$y-1][$x], $diff);				$color = imagecolorallocate($im, $r[$y][$x], $g[$y][$x], $b[$y][$x]);				imagesetpixel($im, $x, $y, $color);			}		}	}	imagepng($im);	imagedestroy($im);?>

http://d2kstudios.co.../...p?w=64&h=64


Edit: Heheh, that was happening because I didn't put brackets around $from + $from2 :lol: Anyways, now it still has some really weird results, like large chunks suddenly changing to a different colour...

Edited by DeathRay2K, 01 August 2006 - 10:12 PM.


#2 Destroyer

Destroyer

    Byte me

  • Members
  • 2,305 posts
  • Location:My back pocket
  • Projects:Having fun
  •  Just this guy, you know?

Posted 01 August 2006 - 11:33 PM

That's cool, but it seems to generate some problems on larger images

http://img447.images...4/genphpxt9.png

Edited by Destroyer, 01 August 2006 - 11:35 PM.


#3 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 02 August 2006 - 02:45 AM

That's exactly what I meant by "large chunks suddenly changing to a different colour" :| I can't figure out what's causing it either...
I've figured out that for some reason, there's a chance of the one colour suddenly jumping to 255 and the others to 0, or all of them to 0... Weird.

Oh, and I added an extra variable, quality of generator. The possibilities are fast, veryfast, superfast, slow, veryslow, and superslow.
For example, if I wanted a very fast image, I'd use http://d2kstudios.co.../...&m=veryfast

Edited by DeathRay2K, 02 August 2006 - 02:48 AM.


#4 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 02 August 2006 - 03:26 AM

I think you're seeing wraparound on your colors. If they get too large (ie over 255) it will probably just modulus it to get a number in the correct range. That would cause the sudden banding that you see as one channel will suddenly be down to 0 where before it was nearing the maximum.

Edit: I was wrong, but in the right way. It was actually a problem with underflow, not overflow. I generated an image and looked in Photoshop, and you were going from a value of 0 in a channel to 253. I'm pretty sure that's because you were actually then returning a value of -2 for the color, which gets converted to 253. Use this function instead:
function rgbclr($from, $from2, $var)
{
if($from != '' && $from2 != '')
{ 
$average = ($from + $from2) / 2;
$rand =  mt_rand($average - $var, $average + $var);
}
elseif($from != '')
{
$rand =  mt_rand($from - $var, $from + $var);
}
elseif($from2 != '')
{
$rand = mt_rand($from2 - $var, $from2 + $var);
}
$rand = max($rand, 0);
return min($rand, 255);
}

Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#5 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 02 August 2006 - 06:33 AM

Aha, you're right!
Thanks very much. :lol:
Now I just have to finish working on this, and making color selections work right...

#6 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 03 August 2006 - 01:22 AM

I know nothing about PHP, but for what its worth, I think that things damn cool :D

World Domination Status: 2.7%


#7 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 04 August 2006 - 04:45 AM

Heh, thanks. :p

Anyone know how IMG_FILTER_COLORIZE actually works? It doesn't seem to do anything for me.

Also, apparently your modified function doesn't always work for some reason Mastermind. :(
http://d2kstudios.co...ne/undermin.png

Edited by DeathRay2K, 04 August 2006 - 04:52 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users