2740387bcf7316549effc480a2116a425318989a
[yaffs-website] / test / image-tests / ReadWriteTest.php
1 <?php
2
3 /*
4  * PEL: PHP Exif Library. A library with support for reading and
5  * writing all Exif headers in JPEG and TIFF images using PHP.
6  *
7  * Copyright (C) 2004, 2006, 2007 Martin Geisler.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program in the file COPYING; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301 USA
23  */
24
25 use lsolesen\pel\Pel;
26 use lsolesen\pel\PelEntryByte;
27 use lsolesen\pel\PelIfd;
28 use lsolesen\pel\PelTiff;
29 use lsolesen\pel\PelExif;
30 use lsolesen\pel\PelJpeg;
31 use lsolesen\pel\PelEntrySByte;
32 use lsolesen\pel\PelEntryShort;
33 use lsolesen\pel\PelEntrySShort;
34 use lsolesen\pel\PelEntryLong;
35 use lsolesen\pel\PelEntrySLong;
36 use lsolesen\pel\PelEntryAscii;
37
38 abstract class WriteEntryTestCase extends \PHPUnit_Framework_TestCase
39 {
40     protected $entries = array();
41
42     public function testWriteRead()
43     {
44         Pel::setStrictParsing(true);
45
46         $ifd = new PelIfd(PelIfd::IFD0);
47         $this->assertTrue($ifd->isLastIfd());
48
49         foreach ($this->entries as $entry) {
50             $ifd->addEntry($entry);
51         }
52
53         $tiff = new PelTiff();
54         $this->assertNull($tiff->getIfd());
55         $tiff->setIfd($ifd);
56         $this->assertNotNull($tiff->getIfd());
57
58         $exif = new PelExif();
59         $this->assertNull($exif->getTiff());
60         $exif->setTiff($tiff);
61         $this->assertNotNull($exif->getTiff());
62
63         $jpeg = new PelJpeg(dirname(__FILE__) . '/no-exif.jpg');
64         $this->assertNull($jpeg->getExif());
65         $jpeg->setExif($exif);
66         $this->assertNotNull($jpeg->getExif());
67
68         $jpeg->saveFile('test-output.jpg');
69         $this->assertTrue(file_exists('test-output.jpg'));
70         $this->assertTrue(filesize('test-output.jpg') > 0);
71
72         /* Now read the file and see if the entries are still there. */
73         $jpeg = new PelJpeg('test-output.jpg');
74
75         $exif = $jpeg->getExif();
76         $this->assertInstanceOf('lsolesen\pel\PelExif', $exif);
77
78         $tiff = $exif->getTiff();
79         $this->assertInstanceOf('lsolesen\pel\PelTiff', $tiff);
80
81         $ifd = $tiff->getIfd();
82         $this->assertInstanceOf('lsolesen\pel\PelIfd', $ifd);
83
84         $this->assertEquals($ifd->getType(), PelIfd::IFD0);
85         $this->assertTrue($ifd->isLastIfd());
86
87         foreach ($this->entries as $entry) {
88             $this->assertEquals($ifd->getEntry($entry->getTag())
89                 ->getValue(), $entry->getValue());
90         }
91
92         unlink('test-output.jpg');
93     }
94 }
95
96 class WriteByteTestCase extends WriteEntryTestCase
97 {
98     public function __construct()
99     {
100         $this->entries[] = new PelEntryByte(0xF001, 0);
101         $this->entries[] = new PelEntryByte(0xF002, 1);
102         $this->entries[] = new PelEntryByte(0xF003, 2);
103         $this->entries[] = new PelEntryByte(0xF004, 253);
104         $this->entries[] = new PelEntryByte(0xF005, 254);
105         $this->entries[] = new PelEntryByte(0xF006, 255);
106
107         $this->entries[] = new PelEntryByte(0xF007, 0, 1, 2, 253, 254, 255);
108         $this->entries[] = new PelEntryByte(0xF008);
109
110         parent::__construct('PEL Byte Read/Write Tests');
111     }
112 }
113
114 class WriteSByteTestCase extends WriteEntryTestCase
115 {
116     public function __construct()
117     {
118         $this->entries[] = new PelEntrySByte(0xF101, - 128);
119         $this->entries[] = new PelEntrySByte(0xF102, - 127);
120         $this->entries[] = new PelEntrySByte(0xF103, - 1);
121         $this->entries[] = new PelEntrySByte(0xF104, 0);
122         $this->entries[] = new PelEntrySByte(0xF105, 1);
123         $this->entries[] = new PelEntrySByte(0xF106, 126);
124         $this->entries[] = new PelEntrySByte(0xF107, 127);
125
126         $this->entries[] = new PelEntrySByte(0xF108, - 128, - 1, 0, 1, 127);
127         $this->entries[] = new PelEntrySByte(0xF109);
128
129         parent::__construct('PEL SByte Read/Write Tests');
130     }
131 }
132
133 class WriteShortTestCase extends WriteEntryTestCase
134 {
135     public function __construct()
136     {
137         $this->entries[] = new PelEntryShort(0xF201, 0);
138         $this->entries[] = new PelEntryShort(0xF202, 1);
139         $this->entries[] = new PelEntryShort(0xF203, 2);
140         $this->entries[] = new PelEntryShort(0xF204, 65533);
141         $this->entries[] = new PelEntryShort(0xF205, 65534);
142         $this->entries[] = new PelEntryShort(0xF206, 65535);
143
144         $this->entries[] = new PelEntryShort(0xF208, 0, 1, 65534, 65535);
145         $this->entries[] = new PelEntryShort(0xF209);
146
147         parent::__construct('PEL Short Read/Write Tests');
148     }
149 }
150
151 class WriteSShortTestCase extends WriteEntryTestCase
152 {
153     public function __construct()
154     {
155         $this->entries[] = new PelEntrySShort(0xF301, - 32768);
156         $this->entries[] = new PelEntrySShort(0xF302, - 32767);
157         $this->entries[] = new PelEntrySShort(0xF303, - 1);
158         $this->entries[] = new PelEntrySShort(0xF304, 0);
159         $this->entries[] = new PelEntrySShort(0xF305, 1);
160         $this->entries[] = new PelEntrySShort(0xF306, 32766);
161         $this->entries[] = new PelEntrySShort(0xF307, 32767);
162
163         $this->entries[] = new PelEntrySShort(0xF308, - 32768, - 1, 0, 1, 32767);
164         $this->entries[] = new PelEntrySShort(0xF309);
165
166         parent::__construct('PEL SShort Read/Write Tests');
167     }
168 }
169
170 class WriteLongTestCase extends WriteEntryTestCase
171 {
172     public function __construct()
173     {
174         $this->entries[] = new PelEntryLong(0xF401, 0);
175         $this->entries[] = new PelEntryLong(0xF402, 1);
176         $this->entries[] = new PelEntryLong(0xF403, 2);
177         $this->entries[] = new PelEntryLong(0xF404, 4294967293);
178         $this->entries[] = new PelEntryLong(0xF405, 4294967294);
179         $this->entries[] = new PelEntryLong(0xF406, 4294967295);
180
181         $this->entries[] = new PelEntryLong(0xF408, 0, 1, 4294967295);
182         $this->entries[] = new PelEntryLong(0xF409);
183
184         parent::__construct('PEL Long Read/Write Tests');
185     }
186 }
187
188 class WriteSLongTestCase extends WriteEntryTestCase
189 {
190     public function __construct()
191     {
192         $this->entries[] = new PelEntrySLong(0xF501, - 2147483648);
193         $this->entries[] = new PelEntrySLong(0xF502, - 2147483647);
194         $this->entries[] = new PelEntrySLong(0xF503, - 1);
195         $this->entries[] = new PelEntrySLong(0xF504, 0);
196         $this->entries[] = new PelEntrySLong(0xF505, 1);
197         $this->entries[] = new PelEntrySLong(0xF506, 2147483646);
198         $this->entries[] = new PelEntrySLong(0xF507, 2147483647);
199
200         $this->entries[] = new PelEntrySLong(0xF508, - 2147483648, 0, 2147483647);
201         $this->entries[] = new PelEntrySLong(0xF509);
202
203         parent::__construct('PEL SLong Read/Write Tests');
204     }
205 }
206
207 class WriteAsciiTestCase extends WriteEntryTestCase
208 {
209     public function __construct()
210     {
211         $this->entries[] = new PelEntryAscii(0xF601);
212         $this->entries[] = new PelEntryAscii(0xF602, '');
213         $this->entries[] = new PelEntryAscii(0xF603, 'Hello World!');
214         $this->entries[] = new PelEntryAscii(0xF604, "\x00\x01\x02...\xFD\xFE\xFF");
215
216         parent::__construct('PEL Ascii Read/Write Tests');
217     }
218 }