/** * Set thumbnail data. * * Use this to embed an arbitrary JPEG image within this IFD. The * data will be checked to ensure that it has a proper {@link * PelJpegMarker::EOI} at the end. If not, then the length is * adjusted until one if found. An {@link PelIfdException} might be * thrown (depending on {@link Pel::$strict}) this case. * * @param PelDataWindow the thumbnail data. */ function setThumbnail(PelDataWindow $d) { $size = $d->getSize(); /* Now move backwards until we find the EOI JPEG marker. */ while ($d->getByte($size - 2) != 0xff || $d->getByte($size - 1) != PelJpegMarker::EOI) { $size--; } if ($size != $d->getSize()) { Pel::maybeThrow(new PelIfdException('Decrementing thumbnail size ' . 'to %d bytes', $size)); } $this->thumb_data = $d->getClone(0, $size); }
/** * Test data to see if it could be a valid JPEG image. * * The function will only look at the first few bytes of the data, * and try to determine if it could be a valid JPEG image based on * those bytes. This means that the check is more like a heuristic * than a rigorous check. * * @param PelDataWindow the bytes that will be checked. * * @return boolean true if the bytes look like the beginning of a * JPEG image, false otherwise. * * @see PelTiff::isValid() */ static function isValid(PelDataWindow $d) { /* JPEG data is stored in big-endian format. */ $d->setByteOrder(PelConvert::BIG_ENDIAN); for ($i = 0; $i < 7; $i++) { if ($d->getByte($i) != 0xff) { break; } } return $d->getByte($i) == PelJpegMarker::SOI; }
/** * Load TIFF data. * * The data given will be parsed and an internal tree representation * will be built. If the data cannot be parsed correctly, a {@link * PelInvalidDataException} is thrown, explaining the problem. * * @param * d * PelDataWindow the data from which the object will be * constructed. This should be valid TIFF data, coming either * directly from a TIFF image or from the Exif data in a JPEG image. */ public function load(PelDataWindow $d) { Pel::debug('Parsing %d bytes of TIFF data...', $d->getSize()); /* * There must be at least 8 bytes available: 2 bytes for the byte * order, 2 bytes for the TIFF header, and 4 bytes for the offset * to the first IFD. */ if ($d->getSize() < 8) { throw new PelInvalidDataException('Expected at least 8 bytes of TIFF ' . 'data, found just %d bytes.', $d->getSize()); } /* Byte order */ if ($d->strcmp(0, 'II')) { Pel::debug('Found Intel byte order'); $d->setByteOrder(PelConvert::LITTLE_ENDIAN); } elseif ($d->strcmp(0, 'MM')) { Pel::debug('Found Motorola byte order'); $d->setByteOrder(PelConvert::BIG_ENDIAN); } else { throw new PelInvalidDataException('Unknown byte order found in TIFF ' . 'data: 0x%2X%2X', $d->getByte(0), $d->getByte(1)); } /* Verify the TIFF header */ if ($d->getShort(2) != self::TIFF_HEADER) { throw new PelInvalidDataException('Missing TIFF magic value.'); } /* IFD 0 offset */ $offset = $d->getLong(4); Pel::debug('First IFD at offset %d.', $offset); if ($offset > 0) { /* * Parse the first IFD, this will automatically parse the * following IFDs and any sub IFDs. */ $this->ifd = new PelIfd(PelIfd::IFD0); $this->ifd->load($d, $offset); } }
function testReadBigIntegers() { $window = new PelDataWindow("‰«Íï", PelConvert::BIG_ENDIAN); $this->assertEqual($window->getSize(), 4); $this->assertEqual($window->getBytes(), "‰«Íï"); $this->assertEqual($window->getByte(0), 0x89); $this->assertEqual($window->getByte(1), 0xab); $this->assertEqual($window->getByte(2), 0xcd); $this->assertEqual($window->getByte(3), 0xef); $this->assertEqual($window->getShort(0), 0x89ab); $this->assertEqual($window->getShort(1), 0xabcd); $this->assertEqual($window->getShort(2), 0xcdef); $this->assertEqual($window->getLong(0), 0x89abcdef); $window->setByteOrder(PelConvert::LITTLE_ENDIAN); $this->assertEqual($window->getSize(), 4); $this->assertEqual($window->getBytes(), "‰«Íï"); $this->assertEqual($window->getByte(0), 0x89); $this->assertEqual($window->getByte(1), 0xab); $this->assertEqual($window->getByte(2), 0xcd); $this->assertEqual($window->getByte(3), 0xef); $this->assertEqual($window->getShort(0), 0xab89); $this->assertEqual($window->getShort(1), 0xcdab); $this->assertEqual($window->getShort(2), 0xefcd); $this->assertEqual($window->getLong(0), 0xefcdab89); }