Skip to content

Commit

Permalink
Add jph support
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhuai committed Aug 5, 2024
1 parent 195c052 commit d010e59
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ imageinfo has been restructured, if you are using old version, please check `v1`
* [x] heic (heif)
* [x] icns
* [x] ico
* [x] j2k
* [x] jp2
* [x] jpeg2000 code stream (j2k, j2c, jpc, jhc)
* [x] jpeg2000 (jp2, jpx, jph)
* [x] jpeg (jpg)
* [x] jpx
* [x] ktx
* [x] png
* [x] psd
Expand Down
5 changes: 2 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ imageinfo 已经重构,如果你使用的是旧版本,请查看 `v1` 分支
* [x] heic (heif)
* [x] icns
* [x] ico
* [x] j2k
* [x] jp2
* [x] jpeg2000 code stream (j2k, j2c, jpc, jhc)
* [x] jpeg2000 (jp2, jpx, jph)
* [x] jpeg (jpg)
* [x] jpx
* [x] ktx
* [x] png
* [x] psd
Expand Down
Binary file added images/valid/jph/byte.jph
Binary file not shown.
57 changes: 32 additions & 25 deletions include/imageinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ enum Format {
kFormatIco,
kFormatJ2k,
kFormatJp2,
kFormatJpeg,
kFormatJph,
kFormatJpx,
kFormatJpeg,
kFormatKtx,
kFormatPng,
kFormatPsd,
Expand Down Expand Up @@ -924,7 +925,7 @@ inline bool try_icns(ReadInterface &ri, size_t length, ImageInfo &info) {

// https://docs.fileformat.com/image/jp2/
// https://docs.fileformat.com/image/jpx/
inline bool try_j2k(ReadInterface &ri, size_t length, ImageInfo &info) {
inline bool try_jpeg2000_code_stream(ReadInterface &ri, size_t length, ImageInfo &info) {
if (length < 16) {
return false;
}
Expand All @@ -951,7 +952,7 @@ inline bool try_j2k(ReadInterface &ri, size_t length, ImageInfo &info) {

// https://docs.fileformat.com/image/jp2/
// https://docs.fileformat.com/image/jpx/
inline bool try_jp2_jpx(ReadInterface &ri, size_t length, ImageInfo &info) {
inline bool try_jpeg2000(ReadInterface &ri, size_t length, ImageInfo &info) {
if (length < 8) {
return false;
}
Expand Down Expand Up @@ -982,6 +983,11 @@ inline bool try_jp2_jpx(ReadInterface &ri, size_t length, ImageInfo &info) {
ext = "jp2";
full_ext = "jp2";
mimetype = "image/jp2";
} else if (buffer.cmp(8, 4, "jph ")) {
format = kFormatJph;
ext = "jph";
full_ext = "jph";
mimetype = "image/jph";
} else if (buffer.cmp(8, 4, "jpx ")) {
format = kFormatJpx;
ext = "jpx";
Expand Down Expand Up @@ -1352,8 +1358,8 @@ enum DetectorIndex {
kDetectorIndexGif,
kDetectorIndexHdr,
kDetectorIndexIcns,
kDetectorIndexJ2k,
kDetectorIndexJp2Jpx,
kDetectorIndexJpeg2000CodeStream,
kDetectorIndexJpeg2000,
kDetectorIndexJpg,
kDetectorIndexKtx,
kDetectorIndexPng,
Expand Down Expand Up @@ -1403,26 +1409,27 @@ inline ImageInfo parse(ReadInterface &ri, //
size_t length = ri.length();

constexpr DetectorInfo dl[] = {
{kFormatAvif, kDetectorIndexAvifHeic, try_avif_heic},
{ kFormatBmp, kDetectorIndexBmp, try_bmp},
{ kFormatCur, kDetectorIndexCurIco, try_cur_ico},
{ kFormatDds, kDetectorIndexDds, try_dds},
{ kFormatGif, kDetectorIndexGif, try_gif},
{ kFormatHdr, kDetectorIndexHdr, try_hdr},
{kFormatHeic, kDetectorIndexAvifHeic, try_avif_heic},
{kFormatIcns, kDetectorIndexIcns, try_icns},
{ kFormatIco, kDetectorIndexCurIco, try_cur_ico},
{ kFormatJ2k, kDetectorIndexJ2k, try_j2k},
{ kFormatJp2, kDetectorIndexJp2Jpx, try_jp2_jpx},
{kFormatJpeg, kDetectorIndexJpg, try_jpg},
{ kFormatJpx, kDetectorIndexJp2Jpx, try_jp2_jpx},
{ kFormatKtx, kDetectorIndexKtx, try_ktx},
{ kFormatPng, kDetectorIndexPng, try_png},
{ kFormatPsd, kDetectorIndexPsd, try_psd},
{ kFormatQoi, kDetectorIndexQoi, try_qoi},
{kFormatTiff, kDetectorIndexTiff, try_tiff},
{kFormatWebp, kDetectorIndexWebp, try_webp},
{ kFormatTga, kDetectorIndexTga, try_tga},
{kFormatAvif, kDetectorIndexAvifHeic, try_avif_heic},
{ kFormatBmp, kDetectorIndexBmp, try_bmp},
{ kFormatCur, kDetectorIndexCurIco, try_cur_ico},
{ kFormatDds, kDetectorIndexDds, try_dds},
{ kFormatGif, kDetectorIndexGif, try_gif},
{ kFormatHdr, kDetectorIndexHdr, try_hdr},
{kFormatHeic, kDetectorIndexAvifHeic, try_avif_heic},
{kFormatIcns, kDetectorIndexIcns, try_icns},
{ kFormatIco, kDetectorIndexCurIco, try_cur_ico},
{ kFormatJ2k, kDetectorIndexJpeg2000CodeStream, try_jpeg2000_code_stream},
{ kFormatJp2, kDetectorIndexJpeg2000, try_jpeg2000},
{ kFormatJph, kDetectorIndexJpeg2000, try_jpeg2000},
{ kFormatJpx, kDetectorIndexJpeg2000, try_jpeg2000},
{kFormatJpeg, kDetectorIndexJpg, try_jpg},
{ kFormatKtx, kDetectorIndexKtx, try_ktx},
{ kFormatPng, kDetectorIndexPng, try_png},
{ kFormatPsd, kDetectorIndexPsd, try_psd},
{ kFormatQoi, kDetectorIndexQoi, try_qoi},
{kFormatTiff, kDetectorIndexTiff, try_tiff},
{kFormatWebp, kDetectorIndexWebp, try_webp},
{ kFormatTga, kDetectorIndexTga, try_tga},
};
static_assert(FORMAT_COUNT == countof(dl), "FORMAT_COUNT != countof(dl)");
static_assert(check_format_order(dl), "Format order is incorrect");
Expand Down
2 changes: 2 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ int main() {
ASSET_II(IMAGES_DIR "valid/jp2/jpx_disguised_as_jp2.jp2", kNoError, kFormatJp2, 2717l, 3701l);
}

{ ASSET_II(IMAGES_DIR "valid/jph/byte.jph", kNoError, kFormatJph, 20l, 20l); }

{ ASSET_II(IMAGES_DIR "valid/jpx/sample.jpx", kNoError, kFormatJpx, 2717l, 3701l); }

{
Expand Down

0 comments on commit d010e59

Please sign in to comment.