96e784b9b080ed6e0ba9c8127264971e8913f1e0
[yaffs-website] / vendor / lsolesen / pel / scripts / make-image-test.php
1 #!/usr/bin/php
2 <?php
3
4 /*
5  * PEL: PHP Exif Library. A library with support for reading and
6  * writing all Exif headers in JPEG and TIFF images using PHP.
7  *
8  * Copyright (C) 2005, 2006 Martin Geisler.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program in the file COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301 USA
24  */
25
26 /*
27  * This meta-program will generate a PHP script with unit tests for
28  * testing the image supplied on the command line. It works by
29  * loading the image, and traversing it, outputting test code at each
30  * step which will verify that a future parse of the image gives the
31  * same results.
32  */
33 if (count($argv) != 2) {
34     exit("Usage: $argv[0] <image>\n");
35 }
36
37 $basename = substr($argv[1], 0, - strlen(strrchr($argv[1], '.')));
38 $image_filename = $argv[1];
39 $thumb_filename = $basename . '-thumb.jpg';
40 $test_filename = $basename . '.php';
41 $test_name = str_replace('-', '_', $basename);
42
43 $indent = 0;
44
45 function println($args)
46 {
47     global $indent;
48     $args = func_get_args();
49     $str = array_shift($args);
50     vprintf(str_repeat('  ', $indent) . $str . "\n", $args);
51 }
52
53 function quote($str)
54 {
55     return str_replace(array(
56         '\\',
57         '\''
58     ), array(
59         '\\\\',
60         '\\\''
61     ), $str);
62 }
63
64 function entryToTest($name, PelEntry $entry)
65 {
66     println('$this->assertInstanceOf(\'%s\', %s);', $name, get_class($entry));
67
68     println('$this->assertEquals(%s->getValue(), %s);', $name, var_export($entry->getValue(), true));
69
70     println('$this->assertEquals(%s->getText(), \'%s\');', $name, quote($entry->getText()));
71 }
72
73 function ifdToTest($name, $number, PelIfd $ifd)
74 {
75     println();
76     println('/* Start of IDF %s%d. */', $name, $number);
77
78     $entries = $ifd->getEntries();
79     println('$this->assertEquals(count(%s%d->getEntries()), %d);', $name, $number, count($entries));
80
81     foreach ($entries as $tag => $entry) {
82         println();
83         println('$entry = %s%d->getEntry(%d); // %s', $name, $number, $tag, PelTag::getName($ifd->getType(), $tag));
84         entryToTest('$entry', $entry);
85     }
86
87     println();
88     println('/* Sub IFDs of %s%d. */', $name, $number);
89
90     $sub_ifds = $ifd->getSubIfds();
91     println('$this->assertEquals(count(%s%d->getSubIfds()), %d);', $name, $number, count($sub_ifds));
92
93     $n = 0;
94     $sub_name = $name . $number . '_';
95     foreach ($sub_ifds as $type => $sub_ifd) {
96         println('%s%d = %s%d->getSubIfd(%d); // IFD %s', $sub_name, $n, $name, $number, $type, $sub_ifd->getName());
97         println('$this->assertInstanceOf(\'PelIfd\', %s%d);', $sub_name, $n);
98         ifdToTest($sub_name, $n, $sub_ifd);
99         $n ++;
100     }
101
102     println();
103
104     if (strlen($ifd->getThumbnailData()) > 0) {
105         println('$thumb_data = file_get_contents(dirname(__FILE__) .');
106         println('                                \'/%s\');', $GLOBALS['thumb_filename']);
107         println('$this->assertEquals(%s%d->getThumbnailData(), $thumb_data);', $name, $number);
108     } else {
109         println('$this->assertEquals(%s%d->getThumbnailData(), \'\');', $name, $number);
110     }
111
112     println();
113     println('/* Next IFD. */');
114
115     $next = $ifd->getNextIfd();
116     println('%s%d = %s%d->getNextIfd();', $name, $number + 1, $name, $number);
117
118     if ($next instanceof PelIfd) {
119         println('$this->assertInstanceOf(\'PelIfd\', %s%d);', $name, $number + 1);
120         println('/* End of IFD %s%d. */', $name, $number);
121
122         ifdToTest($name, $number + 1, $next);
123     } else {
124         println('$this->assertNull(%s%d);', $name, $number + 1);
125         println('/* End of IFD %s%d. */', $name, $number);
126     }
127 }
128
129 function tiffToTest($name, PelTiff $tiff)
130 {
131     println();
132     println('/* The first IFD. */');
133     println('$ifd0 = %s->getIfd();', $name);
134     $ifd = $tiff->getIfd();
135     if ($ifd instanceof PelIfd) {
136         println('$this->assertInstanceOf(\'PelIfd\', $ifd0);');
137         ifdToTest('$ifd', 0, $ifd);
138     } else {
139         println('$this->assertNull($ifd0);');
140     }
141 }
142
143 function jpegContentToTest($name, PelJpegContent $content)
144 {
145     if ($content instanceof PelExif) {
146         println('$this->assertInstanceOf(\'PelExif\', %s);', $name);
147         $tiff = $content->getTiff();
148         println();
149         println('$tiff = %s->getTiff();', $name);
150         if ($tiff instanceof PelTiff) {
151             println('$this->assertInstanceOf(\'PelTiff\', $tiff);');
152             tiffToTest('$tiff', $tiff);
153         }
154     }
155 }
156
157 function jpegToTest($name, PelJpeg $jpeg)
158 {
159     $exif = $jpeg->getExif();
160     println('$exif = %s->getExif();', $name);
161     if ($exif == null) {
162         println('$this->assertNull($exif);');
163     } else {
164         jpegContentToTest('$exif', $exif);
165     }
166 }
167
168 /**
169  * convert a binary string to a sequence of hexadecimals
170  */
171 function binstrencode($field)
172 {
173     $field = bin2hex($field);
174     $field = chunk_split($field, 2, "\\x");
175     return str_replace('\x00', '\0', "\\x" . substr($field, 0, - 2));
176 }
177
178 /*
179  * All output is buffered so that we can dump it in $test_filename at
180  * the end.
181  */
182 ob_start();
183
184 println('<?php
185
186 /*  PEL: PHP Exif Library.  A library with support for reading and
187  *  writing all Exif headers in JPEG and TIFF images using PHP.
188  *
189  *  Copyright (C) 2005, 2006  Martin Geisler.
190  *
191  *  This program is free software; you can redistribute it and/or modify
192  *  it under the terms of the GNU General Public License as published by
193  *  the Free Software Foundation; either version 2 of the License, or
194  *  (at your option) any later version.
195  *
196  *  This program is distributed in the hope that it will be useful,
197  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
198  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
199  *  GNU General Public License for more details.
200  *
201  *  You should have received a copy of the GNU General Public License
202  *  along with this program in the file COPYING; if not, write to the
203  *  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
204  *  Boston, MA 02110-1301 USA
205  */
206
207
208 /* Autogenerated by the make-image-test.php script */
209
210 use lsolesen\pel\PelJpeg;
211
212 class %s extends \PHPUnit_Framework_TestCase
213 {
214
215   function testRead()
216   {
217     Pel::clearExceptions();
218     Pel::setStrictParsing(false);
219     $jpeg = new PelJpeg(dirname(__FILE__) . \'/%s\');
220 ', $test_name, $image_filename, $image_filename);
221
222 require_once(dirname(__FILE__) . '/../../src/PelJpeg.php');
223 $jpeg = new PelJpeg($image_filename);
224
225 $indent = 2;
226 jpegToTest('$jpeg', $jpeg);
227
228 println();
229
230 $exceptions = Pel::getExceptions();
231
232 if (count($exceptions) == 0) {
233     println('$this->assertTrue(count(Pel::getExceptions()) == 0);');
234 } else {
235     println('$exceptions = Pel::getExceptions();');
236     for ($i = 0; $i < count($exceptions); $i ++) {
237         println('$this->assertInstanceOf(\'%s\', $exceptions[%d]);', $i, get_class($exceptions[$i]));
238
239         println('$this->assertEquals($exceptions[%d]->getMessage(),', $i);
240         println('                   \'%s\');', quote($exceptions[$i]->getMessage()));
241     }
242 }
243
244 println('
245   }
246 }
247 ');
248
249 /* The test case is finished -- now dump the output as a PHP file. */
250 file_put_contents($test_filename, ob_get_clean());