]> granicus.if.org Git - php/commitdiff
- add tests for:
authorPierre Joye <pajoye@php.net>
Thu, 22 Dec 2005 23:22:26 +0000 (23:22 +0000)
committerPierre Joye <pajoye@php.net>
Thu, 22 Dec 2005 23:22:26 +0000 (23:22 +0000)
 - imagecolorclosest
 - imagecolorresolve
 - imagecopy
 - imageline
 -imagesetstyle and dashed line
- remove HAVE_GDIMAGECOLORRESOLVE, always present

ext/gd/config.m4
ext/gd/gd.c
ext/gd/tests/colorclosest.phpt [new file with mode: 0644]
ext/gd/tests/colorresolve.phpt [new file with mode: 0644]
ext/gd/tests/copy.phpt [new file with mode: 0644]
ext/gd/tests/dashedlines.phpt [new file with mode: 0644]
ext/gd/tests/lines.phpt [new file with mode: 0644]

index f290893f0b952373409964cfe635a5f1f1655b70..aaf48f013a3c0a4b08c2cb572ad54ad39ed41194 100644 (file)
@@ -333,7 +333,6 @@ dnl FIXME: Cleanup the sources so that these are not needed!
   AC_DEFINE(HAVE_GD_IMAGEELLIPSE,     1, [ ])
   AC_DEFINE(HAVE_GD_IMAGESETBRUSH,    1, [ ])
   AC_DEFINE(HAVE_COLORCLOSESTHWB,     1, [ ])
-  AC_DEFINE(HAVE_GDIMAGECOLORRESOLVE, 1, [ ])
   AC_DEFINE(HAVE_GD_DYNAMIC_CTX_EX,   1, [ ])
 
 dnl T1LIB support is gdlib independent
index 7440c53bbd068a3fe43bee179ca93d5f84209eb9..eb93d2a8cc027733c414a9a63bd5744db79a0464 100644 (file)
@@ -98,10 +98,6 @@ typedef FILE gdIOCtx;
 #define CTX_PUTC(c, fp) fputc(c, fp)
 #endif
 
-#ifndef HAVE_GDIMAGECOLORRESOLVE
-extern int gdImageColorResolve(gdImagePtr, int, int, int);
-#endif
-
 #if HAVE_COLORCLOSESTHWB
 int gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b);
 #endif
@@ -584,62 +580,6 @@ PHP_GD_API int phpi_get_le_gd(void)
        return le_gd;
 }
 
-#ifndef HAVE_GDIMAGECOLORRESOLVE
-
-/* {{{ gdImageColorResolve
- */
-/********************************************************************/
-/* gdImageColorResolve is a replacement for the old fragment:       */
-/*                                                                  */
-/*      if ((color=gdImageColorExact(im,R,G,B)) < 0)                */
-/*        if ((color=gdImageColorAllocate(im,R,G,B)) < 0)           */
-/*          color=gdImageColorClosest(im,R,G,B);                    */
-/*                                                                  */
-/* in a single function                                             */
-
-int gdImageColorResolve(gdImagePtr im, int r, int g, int b)
-{
-       int c;
-       int ct = -1;
-       int op = -1;
-       long rd, gd, bd, dist;
-       long mindist = 3*255*255;  /* init to max poss dist */
-
-       for (c = 0; c < im->colorsTotal; c++) {
-               if (im->open[c]) {
-                       op = c;             /* Save open slot */
-                       continue;           /* Color not in use */
-               }
-               rd = (long) (im->red  [c] - r);
-               gd = (long) (im->green[c] - g);
-               bd = (long) (im->blue [c] - b);
-               dist = rd * rd + gd * gd + bd * bd;
-               if (dist < mindist) {
-                       if (dist == 0) {
-                               return c;       /* Return exact match color */
-                       }
-                       mindist = dist;
-                       ct = c;
-               }
-       }
-       /* no exact match.  We now know closest, but first try to allocate exact */
-       if (op == -1) {
-               op = im->colorsTotal;
-               if (op == gdMaxColors) {    /* No room for more colors */
-                       return ct;          /* Return closest available color */
-               }
-               im->colorsTotal++;
-       }
-       im->red  [op] = r;
-       im->green[op] = g;
-       im->blue [op] = b;
-       im->open [op] = 0;
-       return op;                  /* Return newly allocated color */
-}
-/* }}} */
-
-#endif
-
 #define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
 
 /* {{{ proto int imageloadfont(string filename)
diff --git a/ext/gd/tests/colorclosest.phpt b/ext/gd/tests/colorclosest.phpt
new file mode 100644 (file)
index 0000000..f44912d
--- /dev/null
@@ -0,0 +1,71 @@
+--TEST--
+imagecopy
+--SKIPIF--
+<?php
+        if (!function_exists('imagecopy')) die("skip gd extension not available\n");
+?>
+--FILE--
+<?php
+
+$im = imagecreatetruecolor(5,5);
+$c = imagecolorclosest($im, 255,0,255);
+printf("%X\n", $c);
+imagedestroy($im);
+
+$im = imagecreate(5,5);
+$c = imagecolorclosest($im, 255,0,255);
+print_r(imagecolorsforindex($im, $c));
+imagedestroy($im);
+
+
+
+$im = imagecreate(5,5);
+imagecolorallocate($im, 255, 0, 255);
+$c = imagecolorclosest($im, 255,0,255);
+print_r(imagecolorsforindex($im, $c));
+imagedestroy($im);
+
+$im = imagecreate(5,5);
+for ($i=0; $i<255; $i++) imagecolorresolve($im, $i,0,0);
+$c = imagecolorclosest($im, 255,0,0);
+print_r(imagecolorsforindex($im, $c));
+
+
+$im = imagecreate(5,5);
+for ($i=0; $i<256; $i++) {
+       if ($i == 246) {
+               imagecolorallocate($im, $i,10,10);
+       } else {
+               imagecolorallocate($im, $i,0,0);
+       }
+}
+$c = imagecolorclosest($im, 255,10,10);
+print_r(imagecolorsforindex($im, $c));
+
+
+?>
+--EXPECTF--
+FF00FF
+
+Warning: imagecolorsforindex(): Color index -1 out of range in %s on line %d
+Array
+(
+    [red] => 255
+    [green] => 0
+    [blue] => 255
+    [alpha] => 0
+)
+Array
+(
+    [red] => 254
+    [green] => 0
+    [blue] => 0
+    [alpha] => 0
+)
+Array
+(
+    [red] => 246
+    [green] => 10
+    [blue] => 10
+    [alpha] => 0
+)
diff --git a/ext/gd/tests/colorresolve.phpt b/ext/gd/tests/colorresolve.phpt
new file mode 100644 (file)
index 0000000..41e1c23
--- /dev/null
@@ -0,0 +1,61 @@
+--TEST--
+imagecopy
+--SKIPIF--
+<?php
+        if (!function_exists('imagecopy')) die("skip gd extension not available\n");
+?>
+--FILE--
+<?php
+
+$im = imagecreatetruecolor(5,5);
+$c = imagecolorresolve($im, 255,0,255);
+printf("%X\n", $c);
+imagedestroy($im);
+
+$im = imagecreate(5,5);
+$c = imagecolorresolve($im, 255,0,255);
+print_r(imagecolorsforindex($im, $c));
+imagedestroy($im);
+
+$im = imagecreate(5,5);
+for ($i=0; $i<255; $i++) imagecolorresolve($im, $i,0,0);
+$c = imagecolorresolve($im, 255,0,0);
+print_r(imagecolorsforindex($im, $c));
+
+
+$im = imagecreate(5,5);
+for ($i=0; $i<256; $i++) {
+       if ($i == 246) {
+               imagecolorresolve($im, $i,10,10);
+       } else {
+               imagecolorresolve($im, $i,0,0);
+       }
+}
+$c = imagecolorresolve($im, 255,10,10);
+print_r(imagecolorsforindex($im, $c));
+
+
+?>
+--EXPECTF--
+FF00FF
+Array
+(
+    [red] => 255
+    [green] => 0
+    [blue] => 255
+    [alpha] => 0
+)
+Array
+(
+    [red] => 255
+    [green] => 0
+    [blue] => 0
+    [alpha] => 0
+)
+Array
+(
+    [red] => 246
+    [green] => 10
+    [blue] => 10
+    [alpha] => 0
+)
diff --git a/ext/gd/tests/copy.phpt b/ext/gd/tests/copy.phpt
new file mode 100644 (file)
index 0000000..5aafc31
--- /dev/null
@@ -0,0 +1,109 @@
+--TEST--
+imagecopy
+--SKIPIF--
+<?php
+        if (!function_exists('imagecopy')) die("skip gd extension not available\n");
+?>
+--FILE--
+<?php
+
+$src_tc = imagecreatetruecolor(5,5);
+imagefill($src_tc, 0,0, 0xffffff);
+imagesetpixel($src_tc, 3,3, 0xff0000);
+imagesetpixel($src_tc, 0,0, 0x0000ff);
+imagesetpixel($src_tc, 4,4, 0x00ff00);
+
+
+$dst_tc = imagecreatetruecolor(5,5);
+imagecopy($dst_tc, $src_tc, 0,0, 0,0, imagesx($src_tc), imagesy($src_tc));
+$p1 = imagecolorat($dst_tc, 3,3) == 0xff0000;
+$p2 = imagecolorat($dst_tc, 0,0) == 0x0000ff;
+$p3 = imagecolorat($dst_tc, 4,4) == 0x00ff00;
+
+if ($p1 && $p2 && $p3) {
+       echo "TC/TC: ok\n";
+}
+
+imagedestroy($src_tc); imagedestroy($dst_tc);
+
+
+$src_tc = imagecreatetruecolor(5,5);
+imagefill($src_tc, 0,0, 0xffffff);
+imagesetpixel($src_tc, 3,3, 0xff0000);
+imagesetpixel($src_tc, 0,0, 0x0000ff);
+imagesetpixel($src_tc, 4,4, 0x00ff00);
+
+
+$dst_tc = imagecreate(5,5);
+imagecopy($dst_tc, $src_tc, 0,0, 0,0, imagesx($src_tc), imagesy($src_tc));
+
+$c1 = imagecolorsforindex($dst_tc, imagecolorat($dst_tc, 3,3));
+$c2 = imagecolorsforindex($dst_tc, imagecolorat($dst_tc, 0,0));
+$c3 = imagecolorsforindex($dst_tc, imagecolorat($dst_tc, 4,4));
+
+$p1 = $c1['red'] == 0xff && $c1['blue']==0x00 && $c1['green']==0x00;
+$p2 = $c2['red'] == 0x00 && $c2['blue']==0xff && $c2['green']==0x00;
+$p3 = $c3['red'] == 0x00 && $c3['blue']==0x00 && $c3['green']==0xff;
+
+if ($p1 && $p2 && $p3) {
+       echo "TC/P: ok\n";
+}
+imagedestroy($src_tc); imagedestroy($dst_tc);
+
+
+
+$src_tc = imagecreate(5,5);
+$c0 = imagecolorallocate($src_tc, 0xff, 0xff, 0xff);
+$c1 = imagecolorallocate($src_tc, 0xff, 0x00, 0x00);
+$c2 = imagecolorallocate($src_tc, 0x00, 0x00, 0xff);
+$c3 = imagecolorallocate($src_tc, 0x00, 0xff, 0x00);
+
+imagesetpixel($src_tc, 3,3, $c1);
+imagesetpixel($src_tc, 0,0, $c2);
+imagesetpixel($src_tc, 4,4, $c3);
+
+
+$dst_tc = imagecreate(5,5);
+imagecopy($dst_tc, $src_tc, 0,0, 0,0, imagesx($src_tc), imagesy($src_tc));
+
+$c1 = imagecolorsforindex($dst_tc, imagecolorat($dst_tc, 3,3));
+$c2 = imagecolorsforindex($dst_tc, imagecolorat($dst_tc, 0,0));
+$c3 = imagecolorsforindex($dst_tc, imagecolorat($dst_tc, 4,4));
+
+$p1 = $c1['red'] == 0xff && $c1['blue']==0x00 && $c1['green']==0x00;
+$p2 = $c2['red'] == 0x00 && $c2['blue']==0xff && $c2['green']==0x00;
+$p3 = $c3['red'] == 0x00 && $c3['blue']==0x00 && $c3['green']==0xff;
+
+
+if ($p1 && $p2 && $p3) {
+       echo "P/P: ok\n";
+}
+
+
+
+$src_tc = imagecreate(5,5);
+$c0 = imagecolorallocate($src_tc, 0xff, 0xff, 0xff);
+$c1 = imagecolorallocate($src_tc, 0xff, 0x00, 0x00);
+$c2 = imagecolorallocate($src_tc, 0x00, 0x00, 0xff);
+$c3 = imagecolorallocate($src_tc, 0x00, 0xff, 0x00);
+
+imagesetpixel($src_tc, 3,3, $c1);
+imagesetpixel($src_tc, 0,0, $c2);
+imagesetpixel($src_tc, 4,4, $c3);
+
+
+$dst_tc = imagecreatetruecolor(5,5);
+imagecopy($dst_tc, $src_tc, 0,0, 0,0, imagesx($src_tc), imagesy($src_tc));
+$p1 = imagecolorat($dst_tc, 3,3) == 0xff0000;
+$p2 = imagecolorat($dst_tc, 0,0) == 0x0000ff;
+$p3 = imagecolorat($dst_tc, 4,4) == 0x00ff00;
+
+if ($p1 && $p2 && $p3) {
+       echo "P/TC: ok\n";
+}
+?>
+--EXPECTF--
+TC/TC: ok
+TC/P: ok
+P/P: ok
+P/TC: ok
diff --git a/ext/gd/tests/dashedlines.phpt b/ext/gd/tests/dashedlines.phpt
new file mode 100644 (file)
index 0000000..30db8f1
--- /dev/null
@@ -0,0 +1,79 @@
+--TEST--
+imageline, dashed 
+--SKIPIF--
+<?php
+        if (!function_exists('imagecreatefromstring')) die("skip gd extension not available\n");
+?>
+--FILE--
+<?php
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+
+$r  = 0xff0000;
+$b = 0x0000ff;
+
+$style = array($r, $b);
+imagesetstyle($im, $style);
+
+// Horizontal line
+imageline($im, 0,5, 5,5, IMG_COLOR_STYLED);
+$p1 = imagecolorat($im, 0,5) == $r;
+$p2 = imagecolorat($im, 1,5) == $b;
+$p3 = imagecolorat($im, 2,5) == $r;
+$p4 = imagecolorat($im, 3,5) == $b;
+$p5 = imagecolorat($im, 4,5) == $r;
+$p5 = imagecolorat($im, 5,5) == $b;
+
+
+if ($p1 && $p2 && $p3 && $p4 && $p5) {
+       echo "Horizontal: ok\n";
+}
+imagedestroy($im);
+
+
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+
+$style = array($r, $b);
+imagesetstyle($im, $style);
+
+
+imageline($im, 2,0, 2,5, IMG_COLOR_STYLED);
+$p1 = imagecolorat($im, 2,0) == $r;
+$p2 = imagecolorat($im, 2,1) == $b;
+$p3 = imagecolorat($im, 2,2) == $r;
+$p4 = imagecolorat($im, 2,3) == $b;
+$p5 = imagecolorat($im, 2,4) == $r;
+$p6 = imagecolorat($im, 2,5) == $b;
+imagepng($im, 'b.png');
+if ($p1 && $p2 && $p3 && $p4 && $p5 && $p6) {
+       echo "Vertical: ok\n";
+}
+imagedestroy($im);
+
+
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+
+$style = array($r, $b);
+imagesetstyle($im, $style);
+
+imageline($im, 0,0, 5,5, IMG_COLOR_STYLED);
+$p1 = imagecolorat($im, 0,0) == $r;
+$p2 = imagecolorat($im, 1,1) == $b;
+$p3 = imagecolorat($im, 2,2) == $r;
+$p4 = imagecolorat($im, 3,3) == $b;
+$p5 = imagecolorat($im, 4,4) == $r;
+$p6 = imagecolorat($im, 5,5) == $b;
+
+if ($p1 && $p2 && $p3 && $p4 && $p5 && $p6) {
+       echo "Diagonal: ok\n";
+}
+imagedestroy($im);
+
+
+?>
+--EXPECTF--
+Horizontal: ok
+Vertical: ok
+Diagonal: ok
diff --git a/ext/gd/tests/lines.phpt b/ext/gd/tests/lines.phpt
new file mode 100644 (file)
index 0000000..8f64e12
--- /dev/null
@@ -0,0 +1,116 @@
+--TEST--
+imageline no AA
+--SKIPIF--
+<?php
+        if (!function_exists('imageline')) die("skip gd extension not available\n");
+?>
+--FILE--
+<?php
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+
+// Wrong argument count
+imageline($im, 0,0, 5,5);
+
+
+// Horizontal line
+imageline($im, 0,5, 5,5, 0x00ff00);
+
+$p1 = imagecolorat($im, 0,5)==0x00ff00;
+$p2 = imagecolorat($im, 5,5)==0x00ff00;
+$p3 = true;
+for ($x=1; $x<5; $x++) {
+       $p3 = $p3 && (imagecolorat($im, $x,5)==0x00ff00);
+}
+if ($p1 && $p2 && $p3) {
+       echo "Horizontal: ok\n";
+}
+
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+
+imageline($im, 0,0, 0,5, 0x00ff00);
+$p1 = imagecolorat($im, 0,0)==0x00ff00;
+$p2 = imagecolorat($im, 0,5)==0x00ff00;
+$p3 = true;
+for ($y=1; $y<5; $y++) {
+       $p3 = $p3 && (imagecolorat($im, 0,$y)==0x00ff00);
+}
+
+if ($p1 && $p2 && $p3) {
+       echo "Vertical: ok\n";
+}
+
+
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+imageline($im, 0,0, 5,5, 0x00ff00);
+
+
+// Diagonal
+$p1 = imagecolorat($im, 0,0)==0x00ff00;
+$p2 = imagecolorat($im, 5,5)==0x00ff00;
+$x=1;
+$p3 = true;
+
+for ($y=1; $y<5; $y++) {
+       $p3 = $p3 && (imagecolorat($im, $x,$y)==0x00ff00);
+       $x++;
+}
+
+if ($p1 && $p2 && $p3) {
+       echo "Diagonal: ok\n";
+}
+imagepng($im, 'a.png');
+
+
+// Outside
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+imageline($im, 12, 12, 23,23, 0x00ff00);
+$p3 = true;
+for ($x=0; $x<6; $x++) {
+       for ($y=0; $y<6; $y++) {
+               $p3 = $p3 && (imagecolorat($im, $x,$y)!=0x00ff00);
+       }
+}
+if ($p3) {
+       echo "Outside 1: ok\n";
+}
+
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+imageline($im, -12, -12, -23,-23, 0x00ff00);
+$p3 = true;
+for ($x=0; $x<6; $x++) {
+       for ($y=0; $y<6; $y++) {
+               $p3 = $p3 && (imagecolorat($im, $x,$y)!=0x00ff00);
+       }
+}
+if ($p3) {
+       echo "Outside 2: ok\n";
+}
+
+$im = imagecreatetruecolor(6,6);
+imagefill($im, 0,0, 0xffffff);
+imageline($im, -1, -1, 4,4, 0x00ff00);
+$p3 = true;
+for ($x=0; $x<5; $x++) {
+       for ($y=0; $y<5; $y++) {
+               $p3 = $p3 && (imagecolorat($im, $x,$y)==0x00ff00);
+       }
+}
+if ($p3) {
+       echo "Outside 2: ok\n";
+}
+
+
+?>
+--EXPECTF--
+
+Warning: Wrong parameter count for imageline() in %s on line %d
+Horizontal: ok
+Vertical: ok
+Diagonal: ok
+Outside 1: ok
+Outside 2: ok