diff --git a/src/mezz/codec-image-ext.reb b/src/mezz/codec-image-ext.reb index a2bba0758e..ee604d0488 100644 --- a/src/mezz/codec-image-ext.reb +++ b/src/mezz/codec-image-ext.reb @@ -2,11 +2,12 @@ REBOL [ title: "REBOL 3 image codecs extensions" name: 'codec-image-ext author: "Oldes" - version: 0.2.0 - date: 8-Mar-2021 + version: 0.3.0 + date: 30-Aug-2021 history: [ 0.1.0 10-Nov-2020 "Oldes" {Extend native PNG codec with `size?` function} 0.2.0 08-Mar-2021 "Oldes" {Extend native PNG with `chunks` function} + 0.3.0 30-Aug-2021 "Oldes" {Extend native JPEG codec with `size?` function} ] ] @@ -113,3 +114,35 @@ if find codecs 'png [ out ] ] + +if find codecs 'jpeg [ + extend codecs/jpeg 'size? function ["Return JPEG image size or none" img [file! url! binary!]][ + unless binary? img [img: read/binary img] + unless img: find/tail img #{FFD8} [return none] + while [2 <= length? img][ + if img/1 <> 255 [break] ;invalid chunk + switch img/2 [ + 192 ;baseline + 193 ;baseline extended + 194 ;progressive + 195 ;lossless + [ + binary/read img [ + skip 5 ; tag, length, bpp + h: UI16 + w: UI16 + ] + return as-pair w h + ] + 217 [break] ;end of image + 218 0 [ + unless img: find img 255 [return none] ; error + continue + ] + ] + img: skip img 2 ; skip chunk name + img: skip img binary/read img 'ui16 + ] + none + ] +] diff --git a/src/tests/units/codecs-test.r3 b/src/tests/units/codecs-test.r3 index 30661d9e7a..d0ed0cf88b 100644 --- a/src/tests/units/codecs-test.r3 +++ b/src/tests/units/codecs-test.r3 @@ -404,6 +404,16 @@ if find codecs 'PNG [ ===end-group=== ] +if find codecs 'JPEG [ + ===start-group=== "JPEG codec" + --test-- "jpeg/size?" + --assert 256x256 = codecs/jpeg/size? %units/files/flower.jpg + --assert 256x256 = codecs/jpeg/size? %units/files/flower-from-photoshop.jpg + --assert 256x256 = codecs/jpeg/size? %units/files/flower-tiny.jpg + --assert none? codecs/jpeg/size? %units/files/test.aar + ===end-group=== +] + if find codecs 'XML [ ===start-group=== "XML codec" --test-- "XML decode test1" diff --git a/src/tests/units/files/flower-from-photoshop.jpg b/src/tests/units/files/flower-from-photoshop.jpg new file mode 100644 index 0000000000..98a5e878b5 Binary files /dev/null and b/src/tests/units/files/flower-from-photoshop.jpg differ diff --git a/src/tests/units/files/flower-tiny.jpg b/src/tests/units/files/flower-tiny.jpg new file mode 100644 index 0000000000..ab1cfbd792 Binary files /dev/null and b/src/tests/units/files/flower-tiny.jpg differ diff --git a/src/tests/units/files/flower.jpg b/src/tests/units/files/flower.jpg new file mode 100644 index 0000000000..6d8f879354 Binary files /dev/null and b/src/tests/units/files/flower.jpg differ