Repository navigation
Usage
タグの意味は 設計、メソッドとプロパティは 機能 にあります。
例の test.jxr は 94 バイトの JPEG XR です。ヘッダの直後に PixelFormat の GUID、その後に IFD (PixelFormat、幅 640、高さ 480、画像データの位置とバイト数の 5 タグ) があり、最後に画像データの代わりの 4 バイト (01 02 03 04) が続きます。
broken.jxr は test.jxr の先頭 40 バイト、notjxr.bin は XXXXXXXX の 8 バイトです。
composer で入れた場合は require_once 'IO/JPEGXR.php'; の代わりに require 'vendor/autoload.php'; と書きます。
バージョン番号、IFD の名前、幅・高さ、PixelFormat を表示する例です。
require_once 'IO/JPEGXR.php';
$jxr = new IO_JPEGXR();
$jxr->parse(file_get_contents('test.jxr'));
printf("version:0x%04X IFDs:%s\n", $jxr->tiffVersion, implode(',', array_keys($jxr->IFDs)));
$t = $jxr->IFDs['0th']->tagTable;
echo "size: ", $t[0xBC80]->data[0], "x", $t[0xBC81]->data[0], "\n"; // ImageWidth, ImageHeight
$g = $t[0xBC01]->data; // PixelFormat (GUID, 16 bytes)
vprintf("PixelFormat: %02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
array_merge(array_reverse(array_slice($g, 0, 4)), array_reverse(array_slice($g, 4, 2)),
array_reverse(array_slice($g, 6, 2)), array_slice($g, 8)));version:0x01BC IFDs:0th
size: 640x480
PixelFormat: 6FDDC324-4E03-4BFE-B185-3D77768DC90C
ImageOffset (0xBCC0) と ImageByteCount (0xBCC1) の値は tiffData (JPEG XR ではファイル全体) の中の位置と長さです。中身のビットストリームは解釈しないので、取り出したバイト列を他のデコーダに渡して使います。
require_once 'IO/JPEGXR.php';
$jxr = new IO_JPEGXR();
$jxr->parse(file_get_contents('test.jxr'));
$t = $jxr->IFDs['0th']->tagTable;
$offset = $t[0xBCC0]->data[0]; // ImageOffset
$length = $t[0xBCC1]->data[0]; // ImageByteCount
$image = substr($jxr->tiffData, $offset, $length);
echo "offset:$offset length:$length ", bin2hex($image), "\n";
echo isset($t[0xBCC2]) ? "with alpha\n" : "no alpha\n"; // AlphaOffsetoffset:90 length:4 01020304
no alpha
読めないデータでは parse() が例外を投げます。IO_Bit_Exception も Exception の子クラスなので、まとめて受けられます。
require_once 'IO/JPEGXR.php';
foreach (array('broken.jxr', 'notjxr.bin') as $file) {
$jxr = new IO_JPEGXR();
try {
$jxr->parse(file_get_contents($file));
} catch (Exception $e) { // IO_Bit_Exception too
echo get_class($e).": ".$e->getMessage()."\n";
}
}IO_Bit_Exception: getUI16LE: 40 < 40 + 2
Exception: Unknown head 6 byte: XXXXXX
継承した IO_TIFF の build() で書き出せます。バージョン番号 (0x01BC) はそのまま、画像データ (0xBCC0 / 0xBCC1、0xBCC2 / 0xBCC3) は引き継がれ、配置は詰め直されます (IFD が先頭に来て、GUID はその後ろに移ります)。
JPEG XR のデコーダで読めるかは確かめていません。
require_once 'IO/JPEGXR.php';
$jxr = new IO_JPEGXR();
$jxr->parse(file_get_contents('test.jxr'));
$jxr->IFDs['0th']->tagTable[0xBC80]->data = array(320); // ImageWidth
$out = $jxr->build();
$jxr2 = new IO_JPEGXR();
$jxr2->parse($out);
$t = $jxr2->IFDs['0th']->tagTable;
printf("version:0x%04X size:%d width:%d image:%s\n", $jxr2->tiffVersion, strlen($out),
$t[0xBC80]->data[0], bin2hex(substr($out, $t[0xBCC0]->data[0], $t[0xBCC1]->data[0])));version:0x01BC size:94 width:320 image:01020304
sample/jpegxrdump.php は parse() して dump() を呼ぶスクリプトです。タグ名は出ません。
% php vendor/yoya/io_jpegxr/sample/jpegxrdump.php -f test.jxr
ByteOrder:II(LittleEndian)
TIFFVersion:0x01BC
IFD:0th
BaseOffset:24 BaseSize:62
ExtendOffset:8 ExtendSize:16
TagTable:(count=5)
0xBC01: Type:BYTE Count:16 Offset:8 Data: [0]36 [1]195 [2]221 [3]111 [4]3 [5]78 [6]254 [7]75 [8]177 [9]133 [10]61 [11]119 [12]118 [13]141 [14]201 [15]12
0xBC80: Type:LONG Count:1 Data: [0]640
0xBC81: Type:LONG Count:1 Data: [0]480
0xBCC0: Type:LONG Count:1 Data: [0]90
0xBCC1: Type:LONG Count:1 Data: [0]4
-h を付けると 16 進ダンプが付きます。この例のように IFD の外の値 (GUID) が IFD より前にあると、IFD 本体は (useless scape) の後に出ます。
% php vendor/yoya/io_jpegxr/sample/jpegxrdump.php -h -f test.jxr
ByteOrder:II(LittleEndian)
TIFFVersion:0x01BC
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
0x00000000 49 49 bc 01 18 00 00 00 II
IFD:0th
BaseOffset:24 BaseSize:62
ExtendOffset:8 ExtendSize:16
TagTable:(count=5)
0xBC01: Type:BYTE Count:16 Offset:8 Data: [0]36 [1]195 [2]221 [3]111 [4]3 [5]78 [6]254 [7]75 [8]177 [9]133 [10]61 [11]119 [12]118 [13]141 [14]201 [15]12
0xBC80: Type:LONG Count:1 Data: [0]640
0xBC81: Type:LONG Count:1 Data: [0]480
0xBCC0: Type:LONG Count:1 Data: [0]90
0xBCC1: Type:LONG Count:1 Data: [0]4
(useless scape)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
0x00000000 24 c3 dd 6f 03 4e fe 4b $ o N K
0x00000010 b1 85 3d 77 76 8d c9 0c =wv
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
0x00000010
(useless scape)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
0x00000010 05 00 01 bc 01 00 10 00
0x00000020 00 00 08 00 00 00 80 bc 04 00 01 00 00 00 80 02
0x00000030 00 00 81 bc 04 00 01 00 00 00 e0 01 00 00 c0 bc
0x00000040 04 00 01 00 00 00 5a 00 00 00 c1 bc 04 00 01 00 Z
0x00000050 00 00 04 00 00 00 00 00 00 00 01 02 03 04
ファイルを指定しないと使い方を STDOUT に、読めないデータではエラーのメッセージを STDERR に出し、終了コード 1 で終わります。
% php vendor/yoya/io_jpegxr/sample/jpegxrdump.php
Usage: php jpegxrdump.php [-h] [-D] [-S] -f <jpegxrfile>
% php vendor/yoya/io_jpegxr/sample/jpegxrdump.php -f broken.jxr
getUI16LE: 40 < 40 + 2