Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GRAY & CMYK Jpegs, fix rounding bug #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ void read_jpeg(FILE *in, struct jpeg *jpeg) {
jpeg->h = d.image_height;
jpeg->w = d.image_width;

if(d.num_components != 3) { die("only 3 component jpegs are supported"); }
jpeg->c = d.num_components;
if(d.num_components < 1 || d.num_components > 4)
{ die("only jpegs with 1 to 4 components are supported (gray, rgb/yuv or cmyk)"); }

for(int c = 0; c < d.num_components; c++) {
unsigned i = d.comp_info[c].quant_tbl_no;
Expand All @@ -46,6 +48,8 @@ void read_jpeg(FILE *in, struct jpeg *jpeg) {
memcpy(&(jpeg->coefs[c].quant_table), t->quantval, sizeof(uint16_t) * 64);
}

#define UPDIV(x,y) ((x + ((y) - 1)) / (y))

jvirt_barray_ptr *coefs = jpeg_read_coefficients(&d);
for(int c = 0; c < d.num_components; c++) {
jpeg_component_info *i = &d.comp_info[c];
Expand All @@ -56,10 +60,11 @@ void read_jpeg(FILE *in, struct jpeg *jpeg) {
coef->h = h;
coef->w_samp = d.max_h_samp_factor / i->h_samp_factor;
coef->h_samp = d.max_v_samp_factor / i->v_samp_factor;
if(coef->h / 8 != (jpeg->h / coef->h_samp + 7) / 8) {
if(coef->h / 8 != UPDIV(UPDIV(jpeg->h, coef->h_samp), 8)) {
die("jpeg invalid coef h size");
}
if(coef->w / 8 != (jpeg->w / coef->w_samp + 7) / 8) {
if(coef->w / 8 != UPDIV(UPDIV(jpeg->w, coef->w_samp), 8)) {
printf("coeg->w = %d, jpeg->w = %d, coef->w_samp = %d\n", coef->w, jpeg->w, coef->w_samp);
die("jpeg invalid coef w size");
}
if(SIZE_MAX / coef->h / coef->w / coef->h_samp / coef->w_samp < 6) {
Expand Down
3 changes: 2 additions & 1 deletion jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include "jpeg2png.h"

struct jpeg {
unsigned c;
unsigned h;
unsigned w;
struct coef coefs[3];
struct coef coefs[4];
};

void read_jpeg(FILE *in, struct jpeg *jpeg);
Expand Down
15 changes: 9 additions & 6 deletions jpeg2png.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ void decode_file(const char* infile, const char *outfile, unsigned iterations[3]
struct jpeg jpeg;
read_jpeg(in, &jpeg);
fclose(in);
for(unsigned c = 0; c < 3; c++) {
for(unsigned c = 0; c < jpeg.c; c++) {
struct coef *coef = &jpeg.coefs[c];
decode_coefficients(coef);
}
for(unsigned i = 0; i < 3; i++) {
for(unsigned i = 0; i < jpeg.c; i++) {
struct coef *coef = &jpeg.coefs[i];
float *temp = alloc_simd(sizeof(float) * coef->h * coef->w);

Expand All @@ -139,13 +139,13 @@ void decode_file(const char* infile, const char *outfile, unsigned iterations[3]
}

// smooth
if(all_together) {
if(all_together && jpeg.c == 3) {
plog->channel = 3;
compute(3, jpeg.coefs, plog, pb, weights[0], pweights, iterations[0]);
} else {
struct logger log = *plog;
OPENMP(parallel for schedule(dynamic) firstprivate(log))
for(unsigned i = 0; i < 3; i++) {
for(unsigned i = 0; i < jpeg.c; i++) {
log.channel = i;
struct coef *coef = &jpeg.coefs[i];
compute(1, coef, &log, pb, weights[i], &pweights[i], iterations[i]);
Expand All @@ -161,11 +161,14 @@ void decode_file(const char* infile, const char *outfile, unsigned iterations[3]
// write png
FILE *out = fopen(outfile, "wb");
if(!out) { die_perror("could not open output file `%s`", outfile); }
write_png(out, jpeg.w, jpeg.h, png_bits, &jpeg.coefs[0], &jpeg.coefs[1], &jpeg.coefs[2]);
if (jpeg.c == 3)
write_png(out, jpeg.w, jpeg.h, png_bits, &jpeg.coefs[0], &jpeg.coefs[1], &jpeg.coefs[2]);
else
write_png(out, jpeg.w, jpeg.h, png_bits, &jpeg.coefs[0], NULL, NULL);
fclose(out);

// clean up
for(unsigned i = 0; i < 3; i++) {
for(unsigned i = 0; i < jpeg.c; i++) {
free_simd(jpeg.coefs[i].fdata);
free(jpeg.coefs[i].data);
}
Expand Down
4 changes: 2 additions & 2 deletions png.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void write_png(FILE *out, unsigned w, unsigned h, unsigned bits, struct coef *y,
for(unsigned i = 0; i < h; i++) {
for(unsigned j = 0; j < w; j++) {
float yi = *p(y->fdata, j, i, y->w, y->h);
float cbi = *p(cb->fdata, j, i, cb->w, cb->h);
float cri = *p(cr->fdata, j, i, cr->w, cr->h);
float cbi = cb ? *p(cb->fdata, j, i, cb->w, cb->h) : 0.0;
float cri = cr ? *p(cr->fdata, j, i, cr->w, cr->h) : 0.0;

// YCbCr -> RGB
float bitfactor = (1 << bits) / 256.;
Expand Down