Example #1
0
 function testReadBytes()
 {
     $window = new PelDataWindow('abcdefgh');
     $this->assertEqual($window->getSize(), 8);
     $this->assertEqual($window->getBytes(), 'abcdefgh');
     $this->assertEqual($window->getBytes(0), 'abcdefgh');
     $this->assertEqual($window->getBytes(1), 'bcdefgh');
     $this->assertEqual($window->getBytes(7), 'h');
     //$this->assertEqual($window->getBytes(8), '');
     $this->assertEqual($window->getBytes(-1), 'h');
     $this->assertEqual($window->getBytes(-2), 'gh');
     $this->assertEqual($window->getBytes(-7), 'bcdefgh');
     $this->assertEqual($window->getBytes(-8), 'abcdefgh');
     $clone = $window->getClone(2, 4);
     $this->assertEqual($clone->getSize(), 4);
     $this->assertEqual($clone->getBytes(), 'cdef');
     $this->assertEqual($clone->getBytes(0), 'cdef');
     $this->assertEqual($clone->getBytes(1), 'def');
     $this->assertEqual($clone->getBytes(3), 'f');
     //$this->assertEqual($clone->getBytes(4), '');
     $this->assertEqual($clone->getBytes(-1), 'f');
     $this->assertEqual($clone->getBytes(-2), 'ef');
     $this->assertEqual($clone->getBytes(-3), 'def');
     $this->assertEqual($clone->getBytes(-4), 'cdef');
     $caught = false;
     try {
         $clone->getBytes(0, 6);
     } catch (PelDataWindowOffsetException $e) {
         $caught = true;
     }
     $this->assertTrue($caught);
 }
Example #2
0
 /**
  * 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);
 }
Example #3
0
 /**
  * Load data into a JPEG object.
  *
  * The data supplied will be parsed and turned into an object
  * structure representing the image.  This structure can then be
  * manipulated and later turned back into an string of bytes.
  *
  * This methods can be called at any time after a JPEG object has
  * been constructed, also after the {@link appendSection()} has been
  * called to append custom sections.  Loading several JPEG images
  * into one object will accumulate the sections, but there will only
  * be one {@link PelJpegMarker::SOS} section at any given time.
  *
  * @param PelDataWindow the data that will be turned into JPEG
  * sections.
  */
 function load(PelDataWindow $d)
 {
     Pel::debug('Parsing %d bytes...', $d->getSize());
     /* JPEG data is stored in big-endian format. */
     $d->setByteOrder(PelConvert::BIG_ENDIAN);
     /* Run through the data to read the sections in the image.  After
      * each section is read, the start of the data window will be
      * moved forward, and after the last section we'll terminate with
      * no data left in the window. */
     while ($d->getSize() > 0) {
         /* JPEG sections start with 0xFF. The first byte that is not
          * 0xFF is a marker (hopefully).
          */
         for ($i = 0; $i < 7; $i++) {
             if ($d->getByte($i) != 0xff) {
                 break;
             }
         }
         $marker = $d->getByte($i);
         if (!PelJpegMarker::isValid($marker)) {
             throw new PelJpegInvalidMarkerException($marker, $i);
         }
         /* Move window so first byte becomes first byte in this
          * section. */
         $d->setWindowStart($i + 1);
         if ($marker == PelJpegMarker::SOI || $marker == PelJpegMarker::EOI) {
             $content = new PelJpegContent(new PelDataWindow());
             $this->appendSection($marker, $content);
         } else {
             /* Read the length of the section.  The length includes the
              * two bytes used to store the length. */
             $len = $d->getShort(0) - 2;
             Pel::debug('Found %s section of length %d', PelJpegMarker::getName($marker), $len);
             /* Skip past the length. */
             $d->setWindowStart(2);
             if ($marker == PelJpegMarker::APP1) {
                 try {
                     $content = new PelExif();
                     $content->load($d->getClone(0, $len));
                 } catch (PelInvalidDataException $e) {
                     /* We store the data as normal JPEG content if it could
                      * not be parsed as Exif data. */
                     $content = new PelJpegContent($d->getClone(0, $len));
                 }
                 $this->appendSection($marker, $content);
                 /* Skip past the data. */
                 $d->setWindowStart($len);
             } elseif ($marker == PelJpegMarker::COM) {
                 $content = new PelJpegComment();
                 $content->load($d->getClone(0, $len));
                 $this->appendSection($marker, $content);
                 $d->setWindowStart($len);
             } else {
                 $content = new PelJpegContent($d->getClone(0, $len));
                 $this->appendSection($marker, $content);
                 /* Skip past the data. */
                 $d->setWindowStart($len);
                 /* In case of SOS, image data will follow. */
                 if ($marker == PelJpegMarker::SOS) {
                     /* Some images have some trailing (garbage?) following the
                      * EOI marker.  To handle this we seek backwards until we
                      * find the EOI marker.  Any trailing content is stored as
                      * a PelJpegContent object. */
                     $length = $d->getSize();
                     while ($d->getByte($length - 2) != 0xff || $d->getByte($length - 1) != PelJpegMarker::EOI) {
                         $length--;
                     }
                     $this->jpeg_data = $d->getClone(0, $length - 2);
                     Pel::debug('JPEG data: ' . $this->jpeg_data->__toString());
                     /* Append the EOI. */
                     $this->appendSection(PelJpegMarker::EOI, new PelJpegContent(new PelDataWindow()));
                     /* Now check to see if there are any trailing data. */
                     if ($length != $d->getSize()) {
                         Pel::maybeThrow(new PelException('Found trailing content ' . 'after EOI: %d bytes', $d->getSize() - $length));
                         $content = new PelJpegContent($d->getClone($length));
                         /* We don't have a proper JPEG marker for trailing
                          * garbage, so we just use 0x00... */
                         $this->appendSection(0x0, $content);
                     }
                     /* Done with the loop. */
                     break;
                 }
             }
         }
     }
     /* while ($d->getSize() > 0) */
 }