Skip to content

Commit

Permalink
0.20230818: fix 'DCT coefficient out of range'
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Aug 18, 2023
1 parent 36578c8 commit 3bf373c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
APPNAME ?= jpegqs
MPFLAGS ?= -fopenmp
CFLAGS ?= -Wall -O2 $(MPFLAGS) -DAPPNAME=$(APPNAME)
LDFLAGS ?= -ljpeg -lm
CFLAGS ?= -Wall -O3 $(MPFLAGS) -DAPPNAME=$(APPNAME)
LDFLAGS ?= -ljpeg -lm -s
SRCS = src
OBJS = $(SRCS)/idct.o $(SRCS)/libjpegqs.o
OBJB = $(SRCS)/jpegqs.o

ifneq ($(shell uname -m), i386)
CFLAGS += -fPIC
Expand All @@ -16,16 +18,16 @@ PLIBS = lib$(APPNAME).a lib$(APPNAME).so.0
all: $(PLIBS) $(PROGS)

clean:
rm -rf $(PLIBS) $(PROGS) $(SRCS)/*.o
rm -rf $(PLIBS) $(PROGS) $(OBJS) $(OBJB)

lib$(APPNAME).a: $(SRCS)/idct.o $(SRCS)/libjpegqs.o
lib$(APPNAME).a: $(OBJS)
$(AR) rcs $@ $^

lib$(APPNAME).so.0: $(SRCS)/idct.o $(SRCS)/libjpegqs.o
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) -s
lib$(APPNAME).so.0: $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

$(APPNAME): $(SRCS)/jpegqs.o lib$(APPNAME).so.0
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) -s
$(APPNAME): $(OBJB) lib$(APPNAME).so.0
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

$(APPNAME)-static: $(SRCS)/jpegqs.c lib$(APPNAME).a
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) -s
$(APPNAME)-static: $(OBJB) lib$(APPNAME).a
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
26 changes: 26 additions & 0 deletions src/libjpegqs.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,32 @@ void quantsmooth_transform(j_decompress_ptr srcinfo, jvirt_barray_ptr *src_coef_
}
}
} // iter

// fix a rare JERR_BAD_DCT_COEF ("DCT coefficient out of range") error
// it doesn't take much time
#ifdef _OPENMP
# pragma omp parallel for schedule(dynamic)
#endif
for (blk_y = 0; blk_y < comp_height; blk_y++)
{
JDIMENSION blk_x;
JBLOCKARRAY buffer = (*srcinfo->mem->access_virt_barray)
((j_common_ptr)srcinfo, src_coef_arrays[ci], blk_y, 1, TRUE);

for (blk_x = 0; blk_x < comp_width; blk_x++)
{
JCOEFPTR coef = buffer[0][blk_x]; int i;
for (i = 0; i < DCTSIZE2; i++)
{
// MAX_COEF_BITS = BITS_IN_JSAMPLE + 2
int lim = (4 << BITS_IN_JSAMPLE) - 1;
int a = coef[i];
a = (a < -lim) ? -lim : (a < lim) ? a : lim;
coef[i] = a;
}
}
}

#undef IMAGEPTR
free(image - 7);
}
Expand Down

0 comments on commit 3bf373c

Please sign in to comment.