diff --git a/move_best b/move_best deleted file mode 100755 index c31480d4e..000000000 --- a/move_best +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -# For use in the pngzop project -# Copyright 2013 by Glenn Randers-Pehrson -# Released under the pngcrush license (equivalent to the libpng license) - -# Usage: -# move_best *.zdat -# assumes that for each *.zdat, *.idat.zlib exists -# -# moves the smaller of root.zdat or root.idat.zlib -# to root.best - -for file in $* -do -root=`echo $file | sed -e "s/.zdat$//"` -cp `picksmaller $root.zdat` $root.best -done diff --git a/pngidat b/pngidat deleted file mode 100644 index 3a9737dfe..000000000 --- a/pngidat +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -# For use in the pngzop project -# Copyright 2013 by Glenn Randers-Pehrson -# Released under the pngcrush license (equivalent to the libpng license) - -# Extracts and decompresses the data from the IDAT chunks in a set of -# PNG files. - -for x in $* -do - case $1 in - *.png) - root=`echo $1 | sed -e "s/.png$//"` - pngidat.exe < $1 | zpipe -d > $root.idat - ;; - *.zdat) - root=`echo $1 | sed -e "s/.zdat$//"` - zpipe -d < $1 > $root.idat - ;; - esac - shift -done diff --git a/pngzdat b/pngzdat deleted file mode 100755 index 60be5dd52..000000000 --- a/pngzdat +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh - -# For use in the pngzop project -# Copyright 2013 by Glenn Randers-Pehrson -# Released under the pngcrush license (equivalent to the libpng license) - -# Extracts the data from the IDAT chunks in a set of # PNG files. - -for x in $* -do - root=`echo $1 | sed -e s/.png$//` - pngidat.exe < $1 > $root.zdat - shift -done diff --git a/pngzop b/pngzop deleted file mode 100755 index 1da82a097..000000000 --- a/pngzop +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -# pngzop file.png > file_pz.png - -for f in 0 1 2 3 4 5 -do - rm -f pngzop-f$f-$$.png - pngcrush -q -m 1 -f $f -force $1 pngzop-f$f-$$.png - pngidat pngzop-f$f-$$.png - zopfli --i25 --zlib pngzop-f$f-$$.idat -done -cat `ls -S pngzop-f?-$$.idat.zlib | tail -1` -rm pngzop-f?-$$.png pngzop-f?-$$.idat pngzop-f?-$$.idat.zlib diff --git a/pngzop-try b/pngzop-try deleted file mode 100755 index 955c12ec4..000000000 --- a/pngzop-try +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh - -# pngzop-try - -# usage: pngzop-try dir ... - -# runs pngzop on each PNG file in a set of directories - -# For use in the pngzop project -# Copyright 2013 by Glenn Randers-Pehrson -# Released under the pngcrush license (equivalent to the libpng license) - -# From a PNG file, finds the smallest resulting compressed IDAT -# data from zopfli compression of filter types 0 through 4 and -# "adaptive" (f5) filtering. - -for dir in $* -do -( - cd $dir - for n in *.png - do - pngzop $n > $n.zop - pngcrush -brute -force $n $n.bf.png - pngzdat $n.bf.png - done -) -done diff --git a/README_pngzop.txt b/pngzop_README.txt similarity index 64% rename from README_pngzop.txt rename to pngzop_README.txt index e533b3987..f4d6a93f4 100644 --- a/README_pngzop.txt +++ b/pngzop_README.txt @@ -3,4 +3,7 @@ This is the pngzop branch of the "pmt" tree. Code for supporting the "zopfli" compression method in PNG files will appear here. + + Eventually this collection of scripts will be incorporated + into pngcrush. */ diff --git a/pngzop_brute.sh b/pngzop_brute.sh new file mode 100755 index 000000000..372a656ed --- /dev/null +++ b/pngzop_brute.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +# pngzop_brute.sh + +# For use in the pngzop project +# Copyright 2013 by Glenn Randers-Pehrson +# Released under the pngcrush license (equivalent to the libpng license) + +# Extracts the concatenated data from the IDAT chunks in a set of PNG files, +# leaving it compressed as found. + +# Usage: +# pngzop_brute *.png + +# Standard Input: *.png +# Output: *_zop.png + +for x in $* +do + root=`echo $1 | sed -e s/.png$//` + pngzop_get_ihdr.exe < $1 > ${root}_pngzop_brute.png + pngzop_brute_zopfli.sh $1 + pngzop_zlib_to_idat.exe < ${root}.zlib >> ${root}_pngzop_brute.png + rm ${root}.zlib + pngzop_get_iend.exe < $1 >> ${root}_pngzop_brute.png + shift +done diff --git a/pngzop_brute_zopfli.sh b/pngzop_brute_zopfli.sh new file mode 100755 index 000000000..8332f6400 --- /dev/null +++ b/pngzop_brute_zopfli.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# png_brute_zopfli.sh + +# For use in the pngzop project +# Copyright 2013 by Glenn Randers-Pehrson +# Released under the pngcrush license (equivalent to the libpng license) + +# Usage: +# png_brute_zopfli.sh file.png [...] + +# Input: *.png, a set of PNG files +# Output: *.zlib, zopfli-compressed IDAT data + +for x in $* +do +root=`echo $1 | sed -e "s/.png$//"` + +# Generate trial PNGs with filter none, 4 PNG filters, and adaptive filter +for f in 0 1 2 3 4 5 +do + pngcrush -q -m 1 -f $f -force $1 ${root}_f$f.png & +done +wait + +# Extract and decompress the zlib datastream from the concatenated IDAT chunks. +for f in 0 1 2 3 4 5 +do + pngzop_get_idat.exe < ${root}_f$f.png | zpipe -d > ${root}_f$f.idat & +done +wait +rm -f ${root}_f?.png + +# Recompress the IDAT data using zopfli +for f in 0 1 2 3 4 5 +do + zopfli --i25 --zlib ${root}_f$f.idat & +done +wait +rm -f ${root}_f?.idat + +# Copy the smallest result to file.zlib +cat `ls -S ${root}_f?.idat.zlib | tail -1` > ${root}.zlib +rm -f ${root}_f?.idat.zlib + +shift +done diff --git a/pngidat.c b/pngzop_get_idat.c similarity index 100% rename from pngidat.c rename to pngzop_get_idat.c diff --git a/pngzop_get_idat.sh b/pngzop_get_idat.sh new file mode 100644 index 000000000..6e7242ee9 --- /dev/null +++ b/pngzop_get_idat.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# png_get_idat.sh + +# For use in the pngzop project +# Copyright 2013 by Glenn Randers-Pehrson +# Released under the pngcrush license (equivalent to the libpng license) + +# Extracts and decompresses the data from the IDAT chunks in a set of +# PNG files. + +# Usage: +# png_get_idat.sh file.png + +# Output: file.idat + +for x in $* +do + root=`echo $1 | sed -e "s/.png$//"` + pngzop_get_idat.exe < $1 | zpipe -d > $root.idat + shift +done diff --git a/pngiend.c b/pngzop_get_iend.c similarity index 100% rename from pngiend.c rename to pngzop_get_iend.c diff --git a/pngihdr.c b/pngzop_get_ihdr.c similarity index 100% rename from pngihdr.c rename to pngzop_get_ihdr.c diff --git a/pngzop_get_zdat.sh b/pngzop_get_zdat.sh new file mode 100755 index 000000000..be6b8dd76 --- /dev/null +++ b/pngzop_get_zdat.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# pngzop_get_zdat.sh + +# For use in the pngzop project +# Copyright 2013 by Glenn Randers-Pehrson +# Released under the pngcrush license (equivalent to the libpng license) + +# Extracts the concatenated data from the IDAT chunks in a set of PNG files, +# leaving it compressed as found. + +# Usage: +# pngzop_get_zdat.sh file.png + +# Output: file.zdat + +for x in $* +do + root=`echo $1 | sed -e s/.png$//` + pngzop_get_idat.exe < $1 > $root.zdat + shift +done diff --git a/pngzop_pngcrush.sh b/pngzop_pngcrush.sh new file mode 100755 index 000000000..ea7659777 --- /dev/null +++ b/pngzop_pngcrush.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# pngzop_pngcrush.sh + +# For use in the pngzop project +# Copyright 2013 by Glenn Randers-Pehrson +# Released under the pngcrush license (equivalent to the libpng license) + +# Extracts the concatenated data from the IDAT chunks in a set of PNG files, +# leaving it compressed as found. + +# Usage: +# pngzop_pngcrush.sh *.png + +# Standard Input: *.png +# Output: *_zop.png + +for x in $* +do + root=`echo $1 | sed -e s/.png$//` + pngzop_get_ihdr.exe < $1 > ${root}_pngzop.png + pngzop_pngcrush_zopfli.sh $1 | pngzop_zlib_to_idat.exe >> ${root}_pngzop.png + pngzop_get_iend.exe < $1 >> ${root}_pngzop.png + shift +done diff --git a/pngzop_pngcrush_zopfli.sh b/pngzop_pngcrush_zopfli.sh new file mode 100755 index 000000000..30673eb7a --- /dev/null +++ b/pngzop_pngcrush_zopfli.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# For use in the pngzop project +# Copyright 2013 by Glenn Randers-Pehrson +# Released under the pngcrush license (equivalent to the libpng license) + +# Usage: +# pngzop_pngcrush_zopfli.sh file.png > file.zlib + +# file.png is a PNG file. +# Standard output is the uncompressed IDAT chunk data, recompressed as a zlib +# datastream. + +root=`echo $1 | sed -e "s/.png$//"` +rm -f ${root}_L9.png + +# Do pngcrush level 9 with none, 4 filters, and adaptive filtering, and +# select the smallest. +pngcrush -q -m 113 -m 114 -m 115 -m 116 -m 117 -m 118 \ + -force $1 ${root}_L9.png + +# Extract and decompress the zlib datastream from the concatenated IDAT chunks. +pngzop_get_idat.exe < ${root}_L9.png | zpipe -d > ${root}_L9.idat +rm ${root}_L9.png + +# Recompress with zopfli and write it on standard output. +zopfli --i25 --zlib -c ${root}_L9.idat + +rm ${root}_L9.idat diff --git a/pngzop-references.html b/pngzop_references.html similarity index 100% rename from pngzop-references.html rename to pngzop_references.html diff --git a/results.txt b/pngzop_results.txt similarity index 100% rename from results.txt rename to pngzop_results.txt diff --git a/pngzop_zlib_to_idat.c b/pngzop_zlib_to_idat.c new file mode 100644 index 000000000..597cacf8a --- /dev/null +++ b/pngzop_zlib_to_idat.c @@ -0,0 +1,95 @@ +/* pngzopz2i */ + +/* Copyright 2013 Glenn Randers-Pehrson + * Released under the pngcrush license (equivalent to the libpng license) + */ + +/* Usage: + * pngzop_zlib_to_idat < file.zlib > file_out.idat + * + * file.zlib is the zlib datastream from zopli --zlib file_in.idat + * file_out.idat is the zlib datastream enclosed in PNG IDAT chunks. + * This is a single file that may contain multiple IDAT chunks. + * + * To do: make the maximum IDAT chunk data length an option (currently fixed + * at 250000 bytes). + * + */ + +#include + +#include /* for crc32 */ + +void +put_uLong(uLong val) +{ + putchar(val >> 24); + putchar(val >> 16); + putchar(val >> 8); + putchar(val >> 0); +} + +void +put_chunk(const unsigned char *chunk, uInt length) +{ + uLong crc; + + put_uLong(length-4); /* Exclude the tag */ + fwrite(chunk, 1, length, stdout); + + crc = crc32(0, Z_NULL, 0); + put_uLong(crc32(crc, chunk, length)); +} + +int +main() +{ + unsigned char buf[250000]; + int c; + const unsigned char idat[] = { 73, 68, 65, 84 /* "IDAT" */ }; + int n=6; + + /* IDAT */ + buf[0]=idat[0]; + buf[1]=idat[1]; + buf[2]=idat[2]; + buf[3]=idat[3]; + + /* CMF */ + c=getchar(); + if (c != EOF) + buf[4]=(unsigned char) c; + + /* FLAGS */ + if (c != EOF) + { + c=getchar(); + if (c != EOF) + buf[5]=(unsigned char) c; + } + + if (c != EOF) + for(;;) + { + /* read up to n=250000 bytes */ + + c=getchar(); + + if (c != EOF) + { + buf[n]=(unsigned char) c; + n++; + } + + if (c != EOF && n < 250000) + continue; + + put_chunk(buf, n); + + if (c == EOF) + break; + + n=4; + } +} +