Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
mondrake committed Jan 24, 2024
1 parent 46a54b8 commit 2afb0fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
2 changes: 2 additions & 0 deletions specs/Jpeg/SegmentSos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ name: SOS
title: 'JPEG Start of scan'
payload: scan
class: FileEye\MediaProbe\Block\Jpeg\SegmentSos
parser: FileEye\MediaProbe\Parser\Jpeg\SegmentSos
writer: FileEye\MediaProbe\Writer\Jpeg\SegmentSos
DOMNode: jpegSegment
items: {}
49 changes: 0 additions & 49 deletions src/Block/Jpeg/SegmentSos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

namespace FileEye\MediaProbe\Block\Jpeg;

use FileEye\MediaProbe\Block\RawData;
use FileEye\MediaProbe\Collection\CollectionFactory;
use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Data\DataWindow;
use FileEye\MediaProbe\Entry\Core\Undefined;
use FileEye\MediaProbe\ItemDefinition;
use FileEye\MediaProbe\Data\DataFormat;

/**
* Class representing a JPEG SOS segment.
*/
Expand All @@ -19,45 +11,4 @@ class SegmentSos extends SegmentBase
* JPEG EOI marker.
*/
const JPEG_EOI = 0xD9;

/**
* {@inheritdoc}
*/
protected function doParseData(DataElement $data): void
{
assert($this->debugInfo(['dataElement' => $data]));
// This segment is last before End Of Image, and its length needs to be
// determined by finding the EOI marker backwards from the end of data.
// Some images have some trailing (garbage?) following the EOI marker,
// which we store in a RawData object.
$scan_size = $data->getSize();
while ($data->getByte($scan_size - 2) !== Jpeg::JPEG_DELIMITER || $data->getByte($scan_size - 1) != self::JPEG_EOI) {
$scan_size --;
}
$scan_size -= 2;

// Load data in an Undefined entry.
$data_window = new DataWindow($data, 0, $scan_size);
new Undefined($this, $data_window);

// Append the EOI.
$end_offset = $scan_size;
$eoi = new ItemDefinition(
$this->getParentElement()->getCollection()->getItemCollection(self::JPEG_EOI)
);
$this->getParentElement()->addBlock($eoi)->parseData($data, $end_offset, 2);
$end_offset += 2;

// Now check to see if there are any trailing data.
if ($end_offset < $data->getSize()) {
$raw_size = $data->getSize() - $end_offset;
$this->warning('Found trailing content after EOI: {size} bytes', ['size' => $raw_size]);
// There is no JPEG marker for trailing garbage, so we just collect
// the data in a RawData object.
$trail_definition = new ItemDefinition(CollectionFactory::get('RawData'), DataFormat::BYTE, $raw_size);
$trail_data_window = new DataWindow($data, $end_offset, $raw_size);
$trail = new RawData($trail_definition, $this->getParentElement());
$trail->parseData($trail_data_window);
}
}
}
2 changes: 2 additions & 0 deletions src/Collection/Jpeg/SegmentSos.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SegmentSos extends CollectionBase {
'title' => 'JPEG Start of scan',
'payload' => 'scan',
'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\SegmentSos',
'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\SegmentSos',
'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\SegmentSos',
'DOMNode' => 'jpegSegment',
'id' => 'Jpeg\\SegmentSos',
);
Expand Down
58 changes: 58 additions & 0 deletions src/Parser/Jpeg/SegmentSos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace FileEye\MediaProbe\Parser\Jpeg;

use FileEye\MediaProbe\Block\Jpeg\Jpeg as JpegBlock;
use FileEye\MediaProbe\Block\Jpeg\SegmentSos as SegmentSosBlock;
use FileEye\MediaProbe\Block\RawData;
use FileEye\MediaProbe\Collection\CollectionFactory;
use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Data\DataWindow;
use FileEye\MediaProbe\Entry\Core\Undefined;
use FileEye\MediaProbe\ItemDefinition;
use FileEye\MediaProbe\Parser\ParserBase;
use FileEye\MediaProbe\Data\DataFormat;

/**
* Class representing a JPEG SOS segment.
*/
class SegmentSos extends ParserBase
{
public function parseData(DataElement $data): void
{
assert($this->block->debugInfo(['dataElement' => $data]));
// This segment is last before End Of Image, and its length needs to be
// determined by finding the EOI marker backwards from the end of data.
// Some images have some trailing (garbage?) following the EOI marker,
// which we store in a RawData object.
$scan_size = $data->getSize();
while ($data->getByte($scan_size - 2) !== JpegBlock::JPEG_DELIMITER || $data->getByte($scan_size - 1) != SegmentSosBlock::JPEG_EOI) {
$scan_size --;
}
$scan_size -= 2;

// Load data in an Undefined entry.
$data_window = new DataWindow($data, 0, $scan_size);
new Undefined($this->block, $data_window);

// Append the EOI.
$end_offset = $scan_size;
$eoi = new ItemDefinition(
$this->block->getParentElement()->getCollection()->getItemCollection(SegmentSosBlock::JPEG_EOI)
);
$this->block->getParentElement()->addBlock($eoi)->parseData($data, $end_offset, 2);
$end_offset += 2;

// Now check to see if there are any trailing data.
if ($end_offset < $data->getSize()) {
$raw_size = $data->getSize() - $end_offset;
$this->block->warning('Found trailing content after EOI: {size} bytes', ['size' => $raw_size]);
// There is no JPEG marker for trailing garbage, so we just collect
// the data in a RawData object.
$trail_definition = new ItemDefinition(CollectionFactory::get('RawData'), DataFormat::BYTE, $raw_size);
$trail_data_window = new DataWindow($data, $end_offset, $raw_size);
$trail = new RawData($trail_definition, $this->block->getParentElement());
$trail->parseData($trail_data_window);
}
}
}

0 comments on commit 2afb0fa

Please sign in to comment.