-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Derek Buitenhuis <[email protected]>
- Loading branch information
0 parents
commit a2ae62b
Showing
15 changed files
with
829 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright (c) 2014, Derek Buitenhuis | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
OBJ = ijgdec.o \ | ||
ijgenc.o \ | ||
metric.o \ | ||
api.o \ | ||
main.o | ||
|
||
LIBOBJ = ijgdec.lo \ | ||
ijgenc.lo \ | ||
metric.lo \ | ||
api.lo | ||
|
||
PREFIX=/usr/local | ||
CFLAGS=-Wall | ||
LDFLAGS=-s | ||
LIBS=-ljpeg -lm | ||
|
||
all: smallfry lib | ||
|
||
smallfry: $(OBJ) | ||
$(CC) -o $@ $^ $(LIBS) | ||
|
||
$(LIBOBJ): | ||
$(CC) -fPIC $(CFLAGS) -c $(@:.lo=.c) -o $@ | ||
|
||
libsmallfry.so: $(LIBOBJ) | ||
$(CC) -Wl,--version-script,smallfry.ver -shared -o $@ $^ $(LIBS) | ||
|
||
lib: libsmallfry.so | ||
|
||
clean: | ||
@rm -vf ./*.o | ||
@rm -vf ./smallfry | ||
@rm -vf ./*.lo | ||
@rm -vf ./libsmallfry.so | ||
|
||
install: smallfry | ||
@cp -vf ./smallfry $(PREFIX)/bin/smallfry | ||
|
||
uninstall: | ||
@rm -vf $(PREFIX)/bin/smallfry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## SmallFry ## | ||
|
||
SmallFry is a small proof-of-concept JPEG "optimizer" with a permissive license. What it does is try and find the most you can compress a JPEG without suffering obvious perceived quality loss. It works similarly to how [JPEGmini](http://www.jpegmini.com) works, with some borrowed metrics from their SPIE papers. If you want a nice GUI and easier batch automation, take a look at JPEGmini. | ||
|
||
**Background** | ||
|
||
I wrote this hack-of-a-program in early 2013, and largely ignored it since. Now that [mozjpeg](https://github.com/mozilla/mozjpeg) has gain some small mindshared, I thought it might be useful to others, since now it can take advantage of trellis quantization and scan optimization provided by it. | ||
|
||
**Usability** | ||
|
||
A small CLI util is provided, along with a (crappy) API and library. The code is just C89, and can be compiled with pretty much any compiler. Binaries are provided for Windows in the releases section. | ||
|
||
The code is very sub-optimal and slow. Pull requests are welcome. I am lazy. | ||
|
||
Currently only JPEG input is supported, but it is absolutely trivial to add other input via [ImageMagick](http://www.imagemagick.org) or [libavcodec](http://ffmpeg.org) (maybe via [FFMS2](https://github.com/FFMS/ffms2)). The current code is just what I had hacked together previously; it is not optimal. | ||
|
||
**Disclaimer** | ||
|
||
This code is entirely mine, and unrelated to my employer in any way, and is not used by my employer. | ||
|
||
Users of this code may want to look into any patents they may require to deploy it. I am not a lawyer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright (c) 2014, Derek Buitenhuis | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include <errno.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "smallfry.h" | ||
#include "ijgdec.h" | ||
#include "ijgenc.h" | ||
#include "metric.h" | ||
|
||
void smallfryfreectx(smallfryctx **in) | ||
{ | ||
smallfryctx *ctx = *in; | ||
|
||
if (!ctx) | ||
return; | ||
|
||
if (ctx->outbuf) | ||
free(ctx->outbuf); | ||
|
||
if (ctx->decinput) | ||
free(ctx->decinput); | ||
|
||
if (ctx->decoutput) | ||
free(ctx->decoutput); | ||
|
||
if (ctx->inplanar) | ||
free(ctx->inplanar); | ||
|
||
if (ctx->outplanar) | ||
free(ctx->outplanar); | ||
|
||
free(ctx); | ||
|
||
*in = NULL; | ||
} | ||
|
||
smallfryctx *smallfryallocctx(uint8_t *inbuf, uint64_t size) | ||
{ | ||
smallfryctx *ret; | ||
int bufsize; | ||
|
||
ret = calloc(sizeof(smallfryctx), 1); | ||
if (!ret) | ||
goto allocfail; | ||
|
||
ret->inbuf = inbuf; | ||
ret->isize = size; | ||
ret->osize = 0; | ||
|
||
ret->res = jpeg_get_res(ret->inbuf, ret->isize); | ||
if (!ret->res.width) | ||
goto allocfail; | ||
|
||
bufsize = ret->res.width * ret->res.height * 3; | ||
|
||
ret->outbuf = malloc(bufsize); | ||
ret->decinput = malloc(bufsize); | ||
ret->decoutput = malloc(bufsize); | ||
ret->inplanar = malloc(bufsize / 3); | ||
ret->outplanar = malloc(bufsize / 3); | ||
if (!ret->outbuf || !ret->decinput || !ret->decoutput || !ret->inplanar || !ret->outplanar) | ||
goto allocfail; | ||
|
||
return ret; | ||
|
||
allocfail: | ||
smallfryfreectx(&ret); | ||
return NULL; | ||
} | ||
|
||
static void process_planar(uint8_t *interleaved, uint8_t *planar, jinfo res) | ||
{ | ||
int i; | ||
int j = 0; | ||
uint8_t *out = planar; | ||
|
||
for (i = 0; i < res.width * res.height * 3; i += 3) { | ||
out[j] = interleaved[i]; | ||
j++; | ||
} | ||
} | ||
|
||
int smallfryoptimize(smallfryctx *ctx) | ||
{ | ||
const double threshold = 99.91 + 4.13 / 2; | ||
|
||
int ret; | ||
int qmin = 0, qmax = 100; | ||
double cur = threshold + 1; | ||
|
||
ret = jpeg_decode(ctx->inbuf, ctx->decinput, ctx->isize); | ||
if (ret < 0) | ||
return EINVAL; | ||
|
||
process_planar(ctx->decinput, ctx->inplanar, ctx->res); | ||
|
||
while (qmin <= qmax) { | ||
int size; | ||
int quality = (qmin + qmax) / 2; | ||
|
||
size = jpeg_encode(ctx->decinput, ctx->outbuf, ctx->res, quality); | ||
|
||
jpeg_decode(ctx->outbuf, ctx->decoutput, size); | ||
|
||
process_planar(ctx->decoutput, ctx->outplanar, ctx->res); | ||
|
||
cur = getmetric(ctx->inplanar, ctx->outplanar, ctx->res.width, ctx->res.height); | ||
|
||
if (cur < threshold) | ||
qmin = quality + 1; | ||
else | ||
qmax = quality - 1; | ||
} | ||
|
||
printf("Using quality = %d.\n", qmax + 1); | ||
|
||
ctx->osize = jpeg_encode(ctx->decinput, ctx->outbuf, ctx->res, qmax + 1); | ||
|
||
return 0; | ||
} | ||
|
||
uint64_t smallfrygetsize(smallfryctx *ctx) | ||
{ | ||
return ctx->osize; | ||
} | ||
|
||
uint8_t *smallfrygetbuffer(smallfryctx *ctx) | ||
{ | ||
return ctx->outbuf; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@echo off | ||
cl /MT /GL /c /I. *.c | ||
link /LTCG /out:smallfry.exe *.obj jpeg-static.lib | ||
link /LTCG /dll /def:smallfry.def /out:smallfry.dll *.obj jpeg-static.lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright (c) 2014, Derek Buitenhuis | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <jerror.h> | ||
#include <jpeglib.h> | ||
|
||
#include "ijgdec.h" | ||
|
||
static const JOCTET EOI_BUFFER[1] = { JPEG_EOI }; | ||
|
||
static void init(j_decompress_ptr cinfo) | ||
{ | ||
} | ||
|
||
static boolean input(j_decompress_ptr cinfo) | ||
{ | ||
cinfo->src->next_input_byte = EOI_BUFFER; | ||
cinfo->src->bytes_in_buffer = 1; | ||
|
||
return TRUE; | ||
} | ||
|
||
static void skip(j_decompress_ptr cinfo, long num_bytes) | ||
{ | ||
if (cinfo->src->bytes_in_buffer < num_bytes) { | ||
cinfo->src->next_input_byte = EOI_BUFFER; | ||
cinfo->src->bytes_in_buffer = 1; | ||
} else { | ||
cinfo->src->next_input_byte += num_bytes; | ||
cinfo->src->bytes_in_buffer -= num_bytes; | ||
} | ||
} | ||
|
||
static void term(j_decompress_ptr cinfo) | ||
{ | ||
} | ||
|
||
jinfo jpeg_get_res(uint8_t *inb, uint64_t insize) | ||
{ | ||
struct jpeg_decompress_struct cinfo; | ||
struct jpeg_source_mgr s; | ||
struct jpeg_error_mgr jerr; | ||
jinfo ret; | ||
|
||
jpeg_create_decompress(&cinfo); | ||
|
||
cinfo.err = jpeg_std_error(&jerr); | ||
cinfo.src = &s; | ||
|
||
cinfo.src->init_source = init; | ||
cinfo.src->fill_input_buffer = input; | ||
cinfo.src->skip_input_data = skip; | ||
cinfo.src->resync_to_restart = jpeg_resync_to_restart; | ||
cinfo.src->term_source = term; | ||
cinfo.src->bytes_in_buffer = insize; | ||
cinfo.src->next_input_byte = (const JOCTET *) inb; | ||
|
||
jpeg_read_header(&cinfo, TRUE); | ||
jpeg_start_decompress(&cinfo); | ||
|
||
ret.width = cinfo.output_width; | ||
ret.height = cinfo.output_height; | ||
ret.vsamp = cinfo.comp_info[0].v_samp_factor; | ||
ret.hsamp = cinfo.comp_info[0].h_samp_factor; | ||
|
||
jpeg_destroy_decompress(&cinfo); | ||
|
||
return ret; | ||
} | ||
|
||
int jpeg_decode(uint8_t *inb, uint8_t *o, uint64_t insize) | ||
{ | ||
JSAMPARRAY outb; | ||
struct jpeg_decompress_struct cinfo; | ||
struct jpeg_source_mgr s; | ||
struct jpeg_error_mgr jerr; | ||
|
||
jpeg_create_decompress(&cinfo); | ||
|
||
cinfo.err = jpeg_std_error(&jerr); | ||
cinfo.src = &s; | ||
|
||
cinfo.src->init_source = init; | ||
cinfo.src->fill_input_buffer = input; | ||
cinfo.src->skip_input_data = skip; | ||
cinfo.src->resync_to_restart = jpeg_resync_to_restart; | ||
cinfo.src->term_source = term; | ||
cinfo.src->bytes_in_buffer = insize; | ||
cinfo.src->next_input_byte = (const JOCTET *) inb; | ||
|
||
jpeg_read_header(&cinfo, TRUE); | ||
|
||
cinfo.out_color_space = JCS_YCbCr; | ||
|
||
jpeg_start_decompress(&cinfo); | ||
|
||
outb = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, | ||
cinfo.output_width * cinfo.output_components, 1); | ||
if (!outb) | ||
return -1; | ||
|
||
while (cinfo.output_scanline < cinfo.output_height) { | ||
jpeg_read_scanlines(&cinfo, outb, 1); | ||
memcpy(o + cinfo.output_width * cinfo.output_components * (cinfo.output_scanline - 1), | ||
outb[0], cinfo.output_width * cinfo.output_components); | ||
} | ||
|
||
jpeg_finish_decompress(&cinfo); | ||
|
||
jpeg_destroy_decompress(&cinfo); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2014, Derek Buitenhuis | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#ifndef IJGDEC_H | ||
#define IJGDEC_H | ||
|
||
typedef struct jinfo { | ||
int width; | ||
int height; | ||
int vsamp; | ||
int hsamp; | ||
} jinfo; | ||
|
||
jinfo jpeg_get_res(uint8_t *inb, uint64_t insize); | ||
int jpeg_decode(uint8_t *inb, uint8_t *o, uint64_t insize); | ||
|
||
#endif |
Oops, something went wrong.