Permalink
Browse files

Update resource optimization scripts to use ImageMagick and OpenJPEG …

…to handle JP2 compression.

This fixes compatibility issues with JP2 files on macOS 10.12.2 (and issues with JP2 files created on 10.12.2 with all previous macOS versions).

Note that we need take a step via BMP to generate the JP2 because OpenJPEG won’t deal with the color profile embedded in the PNGs. ImageMagick can convert PNGs to BMPs with an alpha channel, while the sips tool cannot.
  • Loading branch information...
eelco committed Dec 8, 2016
1 parent e9d7ba1 commit 957efc8f24e1654be55d5d0efea98a67688c9759
Showing with 20 additions and 8 deletions.
  1. +9 −5 scripts/jp2-convert.sh
  2. +11 −3 scripts/resources-optimize.sh
View
@@ -2,17 +2,21 @@
INPUT=$1
OUTPUT=$2
-STANDARD_QUALITY=$3
+STANDARD_COMPRESSION=$3
SMALL_PIXEL_LIMIT=$4
-SMALL_QUALITY=$5
+SMALL_COMPRESSION=$5
# ---
XPATH_SCRIPT="number(//key[text()='pixelWidth']/following-sibling::integer[1]) * number(//key[text()='pixelHeight']/following-sibling::integer[1]) < $SMALL_PIXEL_LIMIT"
-QUALITY=$STANDARD_QUALITY
+COMPRESSION=$STANDARD_COMPRESSION
if [ -n "`sips -g allxml "$INPUT" | xmllint --xpath "$XPATH_SCRIPT" - | grep true`" ]; then
- QUALITY=$SMALL_QUALITY
+ COMPRESSION=$SMALL_COMPRESSION
fi
-sips -s format jp2 "$INPUT" -s formatOptions $QUALITY --out "$OUTPUT"
+SEMIOUTPUT=$OUTPUT.bmp
+
+convert "$INPUT" "$SEMIOUTPUT"
+opj_compress -i "$SEMIOUTPUT" -o "$OUTPUT" -r $COMPRESSION
+rm "$SEMIOUTPUT"
@@ -10,15 +10,23 @@ if [ -z "`which cwebp`" ]; then
brew install webp
fi
+if [ -z "`which convert`" ]; then
+ brew install imagemagick
+fi
+
+if [ -z "`which opj_compress`" ]; then
+ brew install openjpeg
+fi
+
SCRIPT_PATH="$( cd "$( dirname "$0" )" && pwd )"
cd "extras"
SOURCE_PATH=DeviceResources
TARGET_PATH=resources.framerjs.com/static/DeviceResources
-JP2_QUALITY=16
-JP2_QUALITY_SMALL=100
+JP2_COMPRESSION=100
+JP2_COMPRESSION_SMALL=10
WEBP_QUALITY=90
SMALL_PIXEL_LIMIT=400000
@@ -48,7 +56,7 @@ webp: \$(WEBP_FILES)
optipng -clobber \$< -out \$@
\$(JP2_FILES): %.jp2: %.unoptim.png
- $SCRIPT_PATH/jp2-convert.sh \$< \$@ $JP2_QUALITY $SMALL_PIXEL_LIMIT $JP2_QUALITY_SMALL
+ $SCRIPT_PATH/jp2-convert.sh \$< \$@ $JP2_COMPRESSION $SMALL_PIXEL_LIMIT $JP2_COMPRESSION_SMALL
\$(WEBP_FILES): %.webp: %.unoptim.png
cwebp -q $WEBP_QUALITY \$< -o \$@

0 comments on commit 957efc8

Please sign in to comment.