Skip to content

Commit 3846021

Browse files
committed
FEAT: codecs/jpeg/size? function for resolving jpeg image size without need of decoding
1 parent 9e0f242 commit 3846021

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

src/mezz/codec-image-ext.reb

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ REBOL [
22
title: "REBOL 3 image codecs extensions"
33
name: 'codec-image-ext
44
author: "Oldes"
5-
version: 0.2.0
6-
date: 8-Mar-2021
5+
version: 0.3.0
6+
date: 30-Aug-2021
77
history: [
88
0.1.0 10-Nov-2020 "Oldes" {Extend native PNG codec with `size?` function}
99
0.2.0 08-Mar-2021 "Oldes" {Extend native PNG with `chunks` function}
10+
0.3.0 30-Aug-2021 "Oldes" {Extend native JPEG codec with `size?` function}
1011
]
1112
]
1213

@@ -113,3 +114,35 @@ if find codecs 'png [
113114
out
114115
]
115116
]
117+
118+
if find codecs 'jpeg [
119+
extend codecs/jpeg 'size? function ["Return JPEG image size or none" img [file! url! binary!]][
120+
unless binary? img [img: read/binary img]
121+
unless img: find/tail img #{FFD8} [return none]
122+
while [2 <= length? img][
123+
if img/1 <> 255 [break] ;invalid chunk
124+
switch img/2 [
125+
192 ;baseline
126+
193 ;baseline extended
127+
194 ;progressive
128+
195 ;lossless
129+
[
130+
binary/read img [
131+
skip 5 ; tag, length, bpp
132+
h: UI16
133+
w: UI16
134+
]
135+
return as-pair w h
136+
]
137+
217 [break] ;end of image
138+
218 0 [
139+
unless img: find img 255 [return none] ; error
140+
continue
141+
]
142+
]
143+
img: skip img 2 ; skip chunk name
144+
img: skip img binary/read img 'ui16
145+
]
146+
none
147+
]
148+
]

src/tests/units/codecs-test.r3

+10
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ if find codecs 'PNG [
404404
===end-group===
405405
]
406406

407+
if find codecs 'JPEG [
408+
===start-group=== "JPEG codec"
409+
--test-- "jpeg/size?"
410+
--assert 256x256 = codecs/jpeg/size? %units/files/flower.jpg
411+
--assert 256x256 = codecs/jpeg/size? %units/files/flower-from-photoshop.jpg
412+
--assert 256x256 = codecs/jpeg/size? %units/files/flower-tiny.jpg
413+
--assert none? codecs/jpeg/size? %units/files/test.aar
414+
===end-group===
415+
]
416+
407417
if find codecs 'XML [
408418
===start-group=== "XML codec"
409419
--test-- "XML decode test1"
38.1 KB
Loading

src/tests/units/files/flower-tiny.jpg

18.1 KB
Loading

src/tests/units/files/flower.jpg

15.1 KB
Loading

0 commit comments

Comments
 (0)