Jump to content


Photo

Counting the same values in an array


  • Please log in to reply
9 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 06 September 2006 - 07:21 AM

Alright, I have an array that looks like this:
array('1', '1', '1', '1', '1', '1', '1', '2', '2', '7', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '3', '2', '4', '8', '8', '234', '234', '234', '234', '234', '128,128,128', '128,128,128', '128,128,128')
And I need to make it look like this:
array('1-7', '2-2', '7', '5-12', '3', '2', '4', '8-2', '234-5', '128,128,128-3')
Anyone have some bright and simple ideas?

#2 DeathRay2K

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

Posted 07 September 2006 - 12:47 AM

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.png
Became: http://d2kstudios.co...l=save/test.dpf
It can be found at http://d2kstudios.co...f/save/test.dpf
One I converted by hand (not quite the same image though) is http://d2kstudios.co...f/?url=test.dpf

The 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.


#3 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 07 September 2006 - 04:29 AM

<?php

$o = array('1', '1', '1', '1', '1', '1', '1', '2', '2', '7', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '3', '2', '4', '8', '8', '234', '234', '234', '234', '234', '128,128,128', '128,128,128', '128,128,128');



$last = $o[0];

$n = array();



function calc($a, $k)

{

 global $n;

 global $last;

 static $count;



 if($a == $last)

 {

  $count++;

 }

 else

 {

  $line = $count > 1

   ? "{$last}-{$count}"

   : $last;

  $n[] = $line;

  $count = 1;

  $last = $a;

 }

}



array_walk(array_merge($o, array('dummy' => 'dummy')), 'calc');

print_r($n);

?>
gives me
Array ( [0] => 1-7 [1] => 2-2 [2] => 7 [3] => 5-12 [4] => 3 [5] => 2 [6] => 4 [7] => 8-2 [8] => 234-5 [9] => 128,128,128-3 )
which looks just like what you need.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#4 DeathRay2K

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

Posted 08 September 2006 - 02:13 AM

Yay DCoder!
That code is quite a bit (very far) above my knowledge... :)
Thanks yet again!

Now, do you know why the last number of a series (in $colours) is always unaffected by this code?
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);
	}
}

This is the most essential part of the process and it doesn't quite work.


Edit: Hmm, your code either removes the first sequence of numbers, or 0, even though it does add the "-whatever" to the array.
For instance, this:
Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 1 [21] => 1 [22] => 1 [23] => 1 [24] => 1 [25] => 1 [26] => 1 [27] => 1 [28] => 1 [29] => 7,12,12 [30] => 2 [31] => 2 [32] => 2 [33] => 2 [34] => 2 [35] => 2 [36] => 2 [37] => 2 [38] => 2 [39] => 130,107,107 [40] => 3 [41] => 3 [42] => 3 [43] => 3 [44] => 3 [45] => 3 [46] => 3 [47] => 3 [48] => 3 [49] => 239,21,21 [50] => 4 [51] => 4 [52] => 4 [53] => 4 [54] => 4 [55] => 4 [56] => 4 [57] => 4 [58] => 4 [59] => 137,114,114 [60] => 5 [61] => 5 [62] => 5 [63] => 5 [64] => 5 [65] => 5 [66] => 5 [67] => 5 [68] => 5 [69] => 135,139,139 [70] => 6 [71] => 6 [72] => 6 [73] => 6 [74] => 6 [75] => 6 [76] => 6 [77] => 6 [78] => 6 [79] => 246,246,246 [80] => 7 [81] => 7 [82] => 7 [83] => 7 [84] => 7 [85] => 7 [86] => 7 [87] => 7 [88] => 7 [89] => 7 [90] => 7 [91] => 7 [92] => 7 [93] => 7 [94] => 7 [95] => 7 [96] => 7 [97] => 7 [98] => 7 [99] => 255,255,255 ) 
became this:
Array ( [0] => -20 [1] => 1-9 [2] => 7,12,12 [3] => 2-9 [4] => 130,107,107 [5] => 3-9 [6] => 239,21,21 [7] => 4-9 [8] => 137,114,114 [9] => 5-9 [10] => 135,139,139 [11] => 6-9 [12] => 246,246,246 [13] => 7-19 [14] => 255,255,255 )
Note [0]'s value.

Edited by DeathRay2K, 08 September 2006 - 02:30 AM.


#5 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 08 September 2006 - 05:07 AM

Just checked with that array of yours: (PHP 5.1.6)
Array ( [0] => 0-20 [1] => 1-9 [2] => 7,12,12 [3] => 2-9 [4] => 130,107,107 [5] => 3-9 [6] => 239,21,21 [7] => 4-9 [8] => 137,114,114 [9] => 5-9 [10] => 135,139,139 [11] => 6-9 [12] => 246,246,246 [13] => 7-19 [14] => 255,255,255 )
Are you using strings ('5') or integers (5) as values?

And what exactly in my code is "above your knowledge"? I can explain, if you catch me on MSN.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#6 DeathRay2K

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

Posted 09 September 2006 - 04:18 AM

The normal ones are integers and of course the ones like 246,246,246 are strings.
I've added you to my contact list, so hopefully you can explain. :lol: Basically though, I don't really understand array_walk very well, so I'm rather confused about $a, $k and how they relate to anything, and the 'dummy' => 'dummy' and the array, stuff like that. :p

And if it matters I'm using PHP 5.0.4.

Edited by DeathRay2K, 09 September 2006 - 04:22 AM.


#7 DeathRay2K

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

Posted 10 September 2006 - 07:59 PM

Well, I've now got it using hex instead of rgb so its even better. ;)
Optimally it can convert a 258 byte PNG to a 70 byte DPF, like this example (Actually its converting the 70 byte DPF to PNG)

Edited by DeathRay2K, 10 September 2006 - 08:04 PM.


#8 DeathRay2K

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

Posted 11 September 2006 - 03:12 AM

Yay, triple posting!
Anyways, I've now got it working with png, jpeg/jpg/jpe/jfif/jfi, gif, tga, bmp, wbmp, xpm, and xbm. :)

Known bugs:
Only the first frame of animated GIFs shows up
No GIF transparency
No PNG alpha support
Sometimes the log scrolls down on its own and sometimes it doesn't

Anyways, this is how to use it:
Right-click an image on the internet, and select 'Copy Image Location'. Then take that, and replace {url} with it while visiting this link:
http://d2kstudios.co.../?convert={url}
For example:
http://d2kstudios.co....m/dpf/test.bmp
The log will show up as it converts the image, and when its done it will give you a link for saving the .dpf file.
To view existing dpf files, save them on the internet and replace {url} in the following with their location:
http://d2kstudios.com/dpf/?url={url}
like so:
http://d2kstudios.co...f/save/test.dpf
That is all.

Edited by DeathRay2K, 11 September 2006 - 03:16 AM.


#9 Tree

Tree

    I pity you.

  • Members
  • 147 posts
  • Location:Atlanta, GA, USA
  • Projects:PHP, 5 different servers, Support
  •  Amazing

Posted 25 September 2006 - 12:06 AM

That's really cool, but how exactly is it compression?

dpf.PNG

I've tried multiple images, and the only time that I've seen positive compression has been with animated GIFs, that's largely attributed to the elimination of all frames but one.

Edited by Tree, 25 September 2006 - 12:15 AM.


#10 DeathRay2K

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

Posted 25 September 2006 - 02:09 AM

That's really cool, but how exactly is it compression?

dpf.PNG

I've tried multiple images, and the only time that I've seen positive compression has been with animated GIFs, that's largely attributed to the elimination of all frames but one.

It works best with images with more colours together, rather than, say, a horizontal gradient (Although a vertical gradient compresses fantastically).
Something like these:
http://d2kstudios.co...evit2/warn0.gif
http://d2kstudios.co...to_post_off.gif
http://d2kstudios.co...ons/blogger.gif

compresses well.

Edited by DeathRay2K, 25 September 2006 - 02:15 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users