Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
Init Version
  • Loading branch information
zvezdochiot committed Jun 15, 2018
0 parents commit bf792bb
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 0 deletions.
13 changes: 13 additions & 0 deletions LICENSE
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.
59 changes: 59 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
PNAME=smallfry
CC = gcc
CFLAGS=-Wall
LDFLAGS=-s
LIBS=-lm
ARC = ar rcs
LN = @ln -fsv
RM = @rm -fv
RMDIR = @rm -frv
PLIB = lib$(PNAME)
VERSION_MAJOR = 0
VERSION_MINOR = 0
VERSION_RELEASE = 1
PREFIX = /usr/local
INCPREFIX = $(PREFIX)/include
LIBPREFIX = $(PREFIX)/lib
DOCPREFIX = $(PREFIX)/share/doc/$(PLIB)$(VERSION_MAJOR)
INSTALL = install

SRCDIR=./src
SRC= $(SRCDIR)/smallfry.c
LIBOBJ = $(SRC:.c=.o)

all: lib

$(PLIB).a: $(LIBOBJ)
$(ARC) $@ $(LIBOBJ)

$(PLIB).so: $(LIBOBJ)
$(CC) -Wl,-soname,$@.$(VERSION_MAJOR) -shared -o $@ $^ $(LDFLAGS) $(LIBS)

lib: $(PLIB).a $(PLIB).so

clean:
$(RM) $(SRCDIR)/*.o
$(RM) ./$(PLIB).a
$(RM) ./$(PLIB).so

install: lib
$(INSTALL) -d $(LIBPREFIX)
$(INSTALL) -m 0644 $(PLIB).a $(LIBPREFIX)/$(PLIB).a
$(INSTALL) -m 0644 $(PLIB).so $(LIBPREFIX)/$(PLIB).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_RELEASE)
$(LN) $(PLIB).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_RELEASE) $(LIBPREFIX)/$(PLIB).so.$(VERSION_MAJOR)
$(LN) $(PLIB).so.$(VERSION_MAJOR) $(LIBPREFIX)/$(PLIB).so
$(INSTALL) -d $(INCPREFIX)
$(INSTALL) -m 0644 $(SRCDIR)/$(PNAME).h $(INCPREFIX)
$(INSTALL) -d $(LIBPREFIX)/pkgconfig
$(INSTALL) -m 0644 $(PNAME).pc $(LIBPREFIX)/pkgconfig
$(INSTALL) -d $(DOCPREFIX)
$(INSTALL) -m 0644 LICENSE README.md $(DOCPREFIX)

uninstall:
$(RM) $(LIBPREFIX)/$(PLIB).a
$(RM) $(LIBPREFIX)/$(PLIB).so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_RELEASE)
$(RM) $(LIBPREFIX)/$(PLIB).so.$(VERSION_MAJOR)
$(RM) $(LIBPREFIX)/$(PLIB).so
$(RM) $(LIBPREFIX)/pkgconfig/$(PNAME).pc
$(RM) $(INCPREFIX)/$(PNAME).h
$(RMDIR) $(DOCPREFIX)
21 changes: 21 additions & 0 deletions README.md
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. It does however required mozjpeg to build currently, and in lieu of a proper configure script, it can be built with vanilla libjpeg by simply commenting out the offending lines in `ijgenc.c` that set mozjpeg-only struct members (sorry!).

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.
9 changes: 9 additions & 0 deletions smallfry.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prefix=/usr/local
libdir=${prefix}/lib
includedir=${prefix}/include

Name: smallfry
Description: Lib Smallfry metric for JPEG Optimization
Version: 0.0.1
Libs: -L${libdir} -lsmallfry -lm
Cflags: -I${includedir}
156 changes: 156 additions & 0 deletions src/smallfry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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 <math.h>
#include <stdint.h>
#include <stdlib.h>

#include "smallfry.h"

#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)

static double psnr_factor(uint8_t *orig, uint8_t *cmp, int orig_stride,
int cmp_stride, int width, int height, uint8_t max)
{
uint8_t *old, *new;
double ret;
int sum;
int i, j;

sum = 0;
old = orig;
new = cmp;

for (i = 0; i < height; i++) {
for (j = 0; j < width; j++)
sum += (old[j] - new[j]) * (old[j] - new[j]);

old += orig_stride;
new += cmp_stride;
}

ret = (double) sum / (double) (width * height);
ret = 10.0 * log10(65025.0 / ret);

if (max > 128)
ret /= 50.0;
else
ret /= (0.0016 * (double) (max * max)) - (0.38 * (double) max + 72.5);

return MAX(MIN(ret, 1.0), 0.0);
}

#define DVAL(i) (abs(old[i] - new[i]))
#define HDVAL(i,j) (abs(old[i + j * orig_stride] - new[i + j * cmp_stride]))
static double aae_factor(uint8_t *orig, uint8_t *cmp, int orig_stride,
int cmp_stride, int width, int height, uint8_t max)
{
uint8_t *old, *new;
double ret;
double sum;
double cfmax, cf;
int i, j;
int cnt;

sum = 0.0;
cnt = 0;
old = orig;
new = cmp;

for (i = 0; i < height; i++) {
for (j = 7; j < width - 1; j += 8) {
double calc;

cnt++;

calc = abs(DVAL(j) - DVAL(j + 1));
calc /= (abs(DVAL(j - 1) - DVAL(j)) + abs(DVAL(j + 1) - DVAL(j + 2)) + 0.0001) / 2.0;

if (calc > 5.0)
sum += 1.0;
else if (calc > 2.0)
sum += (calc - 2.0) / (5.0 - 2.0);
}

old += orig_stride;
new += cmp_stride;
}

old = orig + 7 * orig_stride;
new = cmp + 7 * cmp_stride;

for (i = 7; i < height - 1; i += 8) {
for (j = 0; j < width; j++) {
double calc;

cnt++;

calc = abs(DVAL(j) - HDVAL(j, 1));
calc /= (abs(HDVAL(j, -1) - DVAL(j)) + abs(HDVAL(j, 1) - HDVAL(j, 2)) + 0.0001) / 2.0;

if (calc > 5.0)
sum += 1.0;
else if (calc > 2.0)
sum += (calc - 2.0) / (5.0 - 2.0);
}

old += 8 * orig_stride;
new += 8 * cmp_stride;
}

ret = 1 - (sum / (double) cnt);

if (max > 128)
cfmax = 0.65;
else
cfmax = 0.65 + 0.35 * ((128.0 - (double) max) / 128.0);

cf = MAX(cfmax, MIN(1, 0.25 + (1000.0 * (double) cnt) / sum));

return ret * cf;
}

static uint8_t maxluma(uint8_t *buf, int stride, int width, int height)
{
uint8_t *in = buf;
uint8_t max = 0;
int i, j;

for (i = 0; i < height; i++) {
for (j = 0; j < width; j++)
max = MAX(in[j], max);

in += stride;
}

return max;
}

double smallfry_metric(uint8_t *inbuf, uint8_t *outbuf, int width, int height)
{
double p, a, b;
uint8_t max;

max = maxluma(inbuf, width, width, height);

p = psnr_factor(inbuf, outbuf, width, width, width, height, max);
a = aae_factor(inbuf, outbuf, width, width, width, height, max);

b = p * 37.1891885161239 + a * 78.5328607296973;

return b;
}
22 changes: 22 additions & 0 deletions src/smallfry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 SMALLFLY_METRIC_H
#define SMALLFLY_METRIC_H

double smallfry_metric(uint8_t *inbuf, uint8_t *outbuf, int width, int height);

#endif SMALLFLY_METRIC_H

0 comments on commit bf792bb

Please sign in to comment.