Skip to content

Commit

Permalink
1.0: Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Jul 27, 2022
1 parent d0fcc9c commit fecf5fb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 35 deletions.
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC ?= cc
CFLAGS ?= -Wall
LIBS = -lpng -lm
LDFLAGS ?= -s
RM = rm -f
TARGET = xpng

all: $(TARGET)

$(TARGET): $(TARGET).o
$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LIBS) -o $@

$(TARGET).o: $(TARGET).c
$(CC) $(CFLAGS) -c $< -o $@

clean:
$(RM) $(TARGET) $(TARGET).o
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
`ORG.IPEP:`
![GitHub release (latest by date)](https://img.shields.io/github/v/release/ImageProcessing-ElectronicPublications/xpng)
![GitHub Release Date](https://img.shields.io/github/release-date/ImageProcessing-ElectronicPublications/xpng)
![GitHub repo size](https://img.shields.io/github/repo-size/ImageProcessing-ElectronicPublications/xpng)
![GitHub all releases](https://img.shields.io/github/downloads/ImageProcessing-ElectronicPublications/xpng/total)
![GitHub](https://img.shields.io/github/license/ImageProcessing-ElectronicPublications/xpng)

# XPNG
Program to lossily compress PNG images using adaptive quantization.

Expand All @@ -16,7 +23,7 @@ Apply a png optimizer afterwards for full compression.
## Compiling
Requires libpng and zlib.

Compile with `gcc main.c -o xpng -lpng`
Compile with `make` or `gcc xpng.c -o xpng -lpng -lm`

## Sample Images
Here is a diverse suite of test images fromm the [Kodak Image Suite](http://r0k.us/graphics/kodak/). Both original and compressed versions were run through PNGGauntlet for an optimized apples to apples comparison. A comprssion level of 6 was used. The compressed image follows the original.
Expand Down
70 changes: 36 additions & 34 deletions main.c → xpng.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* gcc main.c -o xpng -lpng */
/* gcc xpng.c -o xpng -lpng -lm*/
/* Requires libpng and zlib */

#include <stddef.h>
Expand All @@ -24,7 +24,8 @@ png_bytep pix(int32_t i, int32_t j, int c, png_bytep data)
/* Returns the difference between two bytes for error calculation */
uint8_t err(png_byte x1, png_byte x2)
{
uint8_t a1 = x1; uint8_t a2 = x2;
uint8_t a1 = x1;
uint8_t a2 = x2;
return a1 > a2 ? a1 - a2 : a2 - a1;
}

Expand All @@ -33,7 +34,7 @@ png_byte avg(int32_t i, int32_t j, int c, png_bytep data)
{
png_byte left = 0;
png_byte up = 0;

if (i > 0) up = *pix(i-1,j,c,data);
if (j > 0) left = *pix(i,j-1,c,data);

Expand Down Expand Up @@ -63,7 +64,7 @@ png_byte paeth(int32_t i, int32_t j, int c, png_bytep data)
png_byte left = 0;
png_byte up = 0;
png_byte topleft = 0;

if (i > 0) up = *pix(i-1,j,c,data);
if (j > 0) left = *pix(i,j-1,c,data);
if (i > 0 && j > 0) topleft = *pix(i-1,j-1,c,data);
Expand All @@ -73,7 +74,7 @@ png_byte paeth(int32_t i, int32_t j, int c, png_bytep data)
p -= topleft;
if (p < 0) p = 0;
if (p > 255) p = 255;

uint8_t dist = 0;
png_byte base;

Expand Down Expand Up @@ -109,7 +110,7 @@ png_byte calc_noise(int32_t i, int32_t j, int32_t c, png_bytep data)
{
X += *pix(ii, jj, c, data);
X2 += *pix(ii, jj, c, data) *
*pix(ii, jj, c, data);
*pix(ii, jj, c, data);
N += 1;
}
}
Expand All @@ -120,25 +121,25 @@ png_byte calc_noise(int32_t i, int32_t j, int32_t c, png_bytep data)
int main(int argc, const char **argv)
{
if (argc != 4)
{
fprintf(stderr, "xpng: usage: xpng inputfile outputfile level\n");
exit(1);
}
unsigned int clevel = atoi(argv[3]);
png_image image; /* The control structure used by libpng */

/* Initialize png_image structure */
memset(&image, 0, (sizeof image));
image.version = PNG_IMAGE_VERSION;

/* The first argument is the input file */
if (png_image_begin_read_from_file(&image, argv[1]) == 0)
{
{
fprintf(stderr, "xpng: usage: xpng inputfile outputfile level\n");
exit(1);
}

unsigned int clevel = atoi(argv[3]);
png_image image; /* The control structure used by libpng */

/* Initialize png_image structure */
memset(&image, 0, (sizeof image));
image.version = PNG_IMAGE_VERSION;

/* The first argument is the input file */
if (png_image_begin_read_from_file(&image, argv[1]) == 0)
{
fprintf(stderr, "xpng: error: %s\n", image.message);
exit (1);
}

png_bytep buffer; /* Buffer for original image */
png_bytep buffer2; /* Output image buffer */
png_bytep diff; /* Residuals from predictor */
Expand All @@ -150,25 +151,25 @@ int main(int argc, const char **argv)
buffer2 = malloc(PNG_IMAGE_SIZE(image));
diff = malloc(PNG_IMAGE_SIZE(image));
noise = malloc(PNG_IMAGE_SIZE(image));


if (buffer == NULL || buffer2 == NULL || diff == NULL || noise == NULL)
{
fprintf(stderr, "xpng: error: insufficient memory\n");
exit(1);
}

memset(buffer, 0, PNG_IMAGE_SIZE(image));
memset(buffer2, 0, PNG_IMAGE_SIZE(image));

if (buffer == NULL ||
png_image_finish_read(&image, NULL/*bg*/, buffer,
0/*row_stride*/, NULL/*colormap*/) == 0)
png_image_finish_read(&image, NULL/*bg*/, buffer,
0/*row_stride*/, NULL/*colormap*/) == 0)
{
fprintf(stderr, "xpng: error: %s\n", image.message);
exit (1);
}

h = image.height;
w = image.width;

Expand All @@ -193,7 +194,7 @@ int main(int argc, const char **argv)
freq[(uint8_t)i] = jumpsize;
}
}

/* Go through image and adaptively quantize for noise masking */
for (int32_t i = 0; i < h; i++)
{
Expand All @@ -204,7 +205,7 @@ int main(int argc, const char **argv)
{
png_byte target; /* Target value */
png_byte base; /* Output from predictor */

/* Left predictor for first row, avg. for others */
if (i == 0 && j > 0)
{
Expand All @@ -219,7 +220,7 @@ int main(int argc, const char **argv)

/* Calculate tolerance using comp. level and local noise */
uint8_t delta = clevel
+ (uint16_t) *pix(i,j,c,noise)*clevel/(10+clevel);
+ (uint16_t) *pix(i,j,c,noise)*clevel/(10+clevel);

/* Calculate interval based on tolerance */
png_byte ltarget = 0;
Expand All @@ -240,7 +241,8 @@ int main(int argc, const char **argv)
approx = a;
f = freq[d];
}
} while (a++ != rtarget);
}
while (a++ != rtarget);

*pix(i,j,c,diff) = approx-base;
/*freq[*pix(i,j,c,diff)]++;*/
Expand All @@ -254,10 +256,10 @@ int main(int argc, const char **argv)
*pix(i,j,c,buffer2) = base + *pix(i,j,c,diff);
continue;
}
for (size_t rd=1; rd<=j*3+c && rd<=4;rd++)
for (size_t rd=1; rd<=j*3+c && rd<=4; rd++)
{
if (err(target,base+*(pix(i,j,c,diff)-rd))
< delta)
< delta)
{
refdist = rd;
*pix(i,j,c,diff) = *(pix(i,j,c,diff)-rd);
Expand All @@ -269,7 +271,7 @@ int main(int argc, const char **argv)
}
}
if (png_image_write_to_file(&image, argv[2], 0/*already_8bit*/,
buffer2, 0/*row_stride*/, NULL/*colormap*/) == 0)
buffer2, 0/*row_stride*/, NULL/*colormap*/) == 0)
{
fprintf(stderr, "xpng: error: %s\n", image.message);
exit (1);
Expand Down

0 comments on commit fecf5fb

Please sign in to comment.