Hmm, the abundance of suggestions astounds me.
Well, maybe knowing the whole picture would help.
I made a way to write images as text files with greater compression on low bit-depth images than PNGs, basically, it works like this:
{width}x{height}[{pre1}|{pre2}|{pre3}...]{clr1}){clr2}){clr3})...
{width} and {height} of course are the width and height of the image, the {pre.}s are rgb colours, and the {clr.}s can either be rgb colours or the indices of {pre.} colours. Furthermore, {clr.}s can be represented singularly or as a range. For instance, '128,128,128-30' would mean 30 pixels would be filled with 128,128,128. Simple yes?
Well, I've made a php script to load them, which works alright (Although I'm not using preg_match or ereg_match, which I probably should be), but I'm having a lot of trouble saving to it. I'm not really sure what's wrong with it, but I don't know how to identify ranges of a colour, and for some reason when the script converts colours to one of the presets, it always leaves the last one.
Here's an example:
This image:
http://d2kstudios.com/dpf/test.pngBecame:
http://d2kstudios.co...l=save/test.dpfIt can be found at
http://d2kstudios.co...f/save/test.dpfOne I converted by hand (not quite the same image though) is
http://d2kstudios.co...f/?url=test.dpfThe codes are as follows:
The loading function:
function loaddpf($location)
{
$file = file_get_contents($location);
$file = trim($file);
$temp = explode('[', $file);
$size = explode('x', $temp[0]);
list($width, $height) = $size;
$temp = explode(']', $temp[1]);
$presets = explode('|', $temp[0]);
$colours = explode(')', $temp[1]);
$details = array('w' => $width, 'h' => $height, 'p' => $presets, 'c' => $colours);
return $details;
}
The display function:
function showdpf($location)
{
$dpf = loaddpf($location);
$im = imagecreatetruecolor($dpf['w'], $dpf['h']);
foreach($dpf['c'] as $colour)
{
if(substr_count($colour, '-') > 0)
{
$split = explode('-', $colour);
}
else
{
$split = array($colour, 1);
}
$reps = $split[1];
for($r = 0; $r < $reps; $r++)
{
if(substr_count($colour, ',') > 0)
{
$realclr = $colour;
}
else
{
$realclr = $dpf['p'][$split[0]];
}
$rgb = explode(',', $realclr);
$red[] = $rgb[0];
$green[] = $rgb[1];
$blue[] = $rgb[2];
}
}
$i = 0;
for($y = 0; $y < $dpf['h']; $y++)
{
for($x = 0; $x < $dpf['w']; $x++)
{
$clr = imagecolorallocate($im, $red[$i], $green[$i], $blue[$i]);
imagesetpixel($im, $x, $y, $clr);
$i++;
}
}
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
}
The save function (Which doesn't work correctly)
function converttodpf($location)
{
$orig = imagecreatefromany($location);
for($y = 0; $y < imagesy($orig); $y++)
{
for($x = 0; $x < imagesx($orig); $x++)
{
$rgb = ImageColorAt($orig, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$colours[] = "{$r},{$g},{$b}";
}
}
$presets = array();
foreach($colours as $num => $value)
{
$keys = array_keys($colours, $value);
if(count($keys) > 1)
{
if(!in_array($value, $presets))
{
$presets[] = $value;
}
$colours[$num] = array_search($value, $presets);
}
}
$output = imagesx($orig).'x'.imagesy($orig).'[';
foreach($presets as $prenum => $colour)
{
$output .= $colour;
if($prenum < count($presets) - 1)
{
$output .= '|';
}
}
$output .= ']';
foreach($colours as $clrnum => $clr)
{
$output .= $clr;
if($clrnum < count($colours) - 1)
{
$output .= ')';
}
}
$name = explode('.', basename($location));
$name = $name[0];
file_put_contents('./save/'.$name.'.dpf', $output);
}
Please, any help would be truly fantabulous.
Edited by DeathRay2K, 07 September 2006 - 12:59 AM.