Skip to content

Commit

Permalink
FEAT: codecs/jpeg/size? function for resolving jpeg image size with…
Browse files Browse the repository at this point in the history
…out need of decoding
  • Loading branch information
Oldes committed Aug 30, 2021
1 parent 9e0f242 commit 3846021
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/mezz/codec-image-ext.reb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]
]

Expand Down Expand Up @@ -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
]
]
10 changes: 10 additions & 0 deletions src/tests/units/codecs-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file added src/tests/units/files/flower-from-photoshop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/tests/units/files/flower-tiny.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/tests/units/files/flower.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3846021

Please sign in to comment.