aboutsummaryrefslogtreecommitdiffstats
path: root/tjunittest.c
diff options
context:
space:
mode:
Diffstat (limited to 'tjunittest.c')
-rw-r--r--tjunittest.c193
1 files changed, 115 insertions, 78 deletions
diff --git a/tjunittest.c b/tjunittest.c
index ae72e836..c92fea79 100644
--- a/tjunittest.c
+++ b/tjunittest.c
@@ -59,16 +59,16 @@ void usage(char *progName)
}
-#define _throwtj() { \
+#define THROW_TJ() { \
printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \
- bailout() \
+ BAILOUT() \
}
-#define _tj(f) { if ((f) == -1) _throwtj(); }
-#define _throw(m) { printf("ERROR: %s\n", m); bailout() }
-#define _throwmd5(filename, md5sum, ref) { \
+#define TRY_TJ(f) { if ((f) == -1) THROW_TJ(); }
+#define THROW(m) { printf("ERROR: %s\n", m); BAILOUT() }
+#define THROW_MD5(filename, md5sum, ref) { \
printf("\n%s has an MD5 sum of %s.\n Should be %s.\n", filename, md5sum, \
ref); \
- bailout() \
+ BAILOUT() \
}
const char *subNameLong[TJ_NUMSAMP] = {
@@ -93,7 +93,7 @@ const int _onlyRGB[] = { TJPF_RGB };
int doYUV = 0, alloc = 0, pad = 4;
int exitStatus = 0;
-#define bailout() { exitStatus = -1; goto bailout; }
+#define BAILOUT() { exitStatus = -1; goto bailout; }
void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
@@ -151,7 +151,7 @@ void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
}
-#define checkval(v, cv) { \
+#define CHECKVAL(v, cv) { \
if (v < cv - 1 || v > cv + 1) { \
printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, row, col, cv, \
v); \
@@ -159,14 +159,14 @@ void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
} \
}
-#define checkval0(v) { \
+#define CHECKVAL0(v) { \
if (v > 1) { \
printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \
retval = 0; exitStatus = -1; goto bailout; \
} \
}
-#define checkval255(v) { \
+#define CHECKVAL255(v) { \
if (v < 254) { \
printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \
retval = 0; exitStatus = -1; goto bailout; \
@@ -200,13 +200,13 @@ int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
y = buf[index * ps + 2];
k = buf[index * ps + 3];
if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
- checkval255(c); checkval255(m); checkval255(y);
- if (row < halfway) checkval255(k)
- else checkval0(k)
+ CHECKVAL255(c); CHECKVAL255(m); CHECKVAL255(y);
+ if (row < halfway) CHECKVAL255(k)
+ else CHECKVAL0(k)
} else {
- checkval255(c); checkval0(y); checkval255(k);
- if (row < halfway) checkval0(m)
- else checkval255(m)
+ CHECKVAL255(c); CHECKVAL0(y); CHECKVAL255(k);
+ if (row < halfway) CHECKVAL0(m)
+ else CHECKVAL255(m)
}
}
}
@@ -225,26 +225,26 @@ int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp,
a = aoffset >= 0 ? buf[index * ps + aoffset] : 0xFF;
if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
if (row < halfway) {
- checkval255(r); checkval255(g); checkval255(b);
+ CHECKVAL255(r); CHECKVAL255(g); CHECKVAL255(b);
} else {
- checkval0(r); checkval0(g); checkval0(b);
+ CHECKVAL0(r); CHECKVAL0(g); CHECKVAL0(b);
}
} else {
if (subsamp == TJSAMP_GRAY) {
if (row < halfway) {
- checkval(r, 76); checkval(g, 76); checkval(b, 76);
+ CHECKVAL(r, 76); CHECKVAL(g, 76); CHECKVAL(b, 76);
} else {
- checkval(r, 226); checkval(g, 226); checkval(b, 226);
+ CHECKVAL(r, 226); CHECKVAL(g, 226); CHECKVAL(b, 226);
}
} else {
if (row < halfway) {
- checkval255(r); checkval0(g); checkval0(b);
+ CHECKVAL255(r); CHECKVAL0(g); CHECKVAL0(b);
} else {
- checkval255(r); checkval255(g); checkval0(b);
+ CHECKVAL255(r); CHECKVAL255(g); CHECKVAL0(b);
}
}
}
- checkval255(a);
+ CHECKVAL255(a);
}
}
@@ -287,11 +287,11 @@ int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
unsigned char y = buf[ypitch * row + col];
if (((row / blocksize) + (col / blocksize)) % 2 == 0) {
- if (row < halfway) checkval255(y)
- else checkval0(y);
+ if (row < halfway) CHECKVAL255(y)
+ else CHECKVAL0(y);
} else {
- if (row < halfway) checkval(y, 76)
- else checkval(y, 226);
+ if (row < halfway) CHECKVAL(y, 76)
+ else CHECKVAL(y, 226);
}
}
}
@@ -304,12 +304,12 @@ int checkBufYUV(unsigned char *buf, int w, int h, int subsamp,
v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
if (((row * vsf / blocksize) + (col * hsf / blocksize)) % 2 == 0) {
- checkval(u, 128); checkval(v, 128);
+ CHECKVAL(u, 128); CHECKVAL(v, 128);
} else {
if (row < halfway) {
- checkval(u, 85); checkval255(v);
+ CHECKVAL(u, 85); CHECKVAL255(v);
} else {
- checkval0(u); checkval(v, 149);
+ CHECKVAL0(u); CHECKVAL(v, 149);
}
}
}
@@ -348,7 +348,7 @@ void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename)
if (!file || fwrite(jpegBuf, jpegSize, 1, file) != 1) {
printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno));
- bailout()
+ BAILOUT()
}
bailout:
@@ -368,7 +368,7 @@ void compTest(tjhandle handle, unsigned char **dstBuf, unsigned long *dstSize,
const char *buStr = (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD";
if ((srcBuf = (unsigned char *)malloc(w * h * tjPixelSize[pf])) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
initBuf(srcBuf, w, h, pf, flags);
if (*dstBuf && *dstSize > 0) memset(*dstBuf, 0, *dstSize);
@@ -379,28 +379,28 @@ void compTest(tjhandle handle, unsigned char **dstBuf, unsigned long *dstSize,
tjscalingfactor sf = { 1, 1 };
tjhandle handle2 = tjInitCompress();
- if (!handle2) _throwtj();
+ if (!handle2) THROW_TJ();
if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
memset(yuvBuf, 0, yuvSize);
printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]);
- _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
- flags));
+ TRY_TJ(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp,
+ flags));
tjDestroy(handle2);
if (checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n");
else printf("FAILED!\n");
printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong,
jpegQual);
- _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf, dstSize,
- jpegQual, flags));
+ TRY_TJ(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf,
+ dstSize, jpegQual, flags));
} else {
printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp],
jpegQual);
- _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
- jpegQual, flags));
+ TRY_TJ(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp,
+ jpegQual, flags));
}
snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr,
@@ -424,14 +424,14 @@ void _decompTest(tjhandle handle, unsigned char *jpegBuf,
int scaledHeight = TJSCALED(h, sf);
unsigned long dstSize = 0;
- _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
- &_hdrsubsamp));
+ TRY_TJ(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh,
+ &_hdrsubsamp));
if (_hdrw != w || _hdrh != h || _hdrsubsamp != subsamp)
- _throw("Incorrect JPEG header");
+ THROW("Incorrect JPEG header");
dstSize = scaledWidth * scaledHeight * tjPixelSize[pf];
if ((dstBuf = (unsigned char *)malloc(dstSize)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
memset(dstBuf, 0, dstSize);
if (doYUV) {
@@ -439,26 +439,26 @@ void _decompTest(tjhandle handle, unsigned char *jpegBuf,
subsamp);
tjhandle handle2 = tjInitDecompress();
- if (!handle2) _throwtj();
+ if (!handle2) THROW_TJ();
if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
memset(yuvBuf, 0, yuvSize);
printf("JPEG -> YUV %s ", subNameLong[subsamp]);
if (sf.num != 1 || sf.denom != 1)
printf("%d/%d ... ", sf.num, sf.denom);
else printf("... ");
- _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth, pad,
- scaledHeight, flags));
+ TRY_TJ(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth,
+ pad, scaledHeight, flags));
if (checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf))
printf("Passed.\n");
else printf("FAILED!\n");
printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf],
(flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down ");
- _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
- scaledHeight, pf, flags));
+ TRY_TJ(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0,
+ scaledHeight, pf, flags));
tjDestroy(handle2);
} else {
printf("JPEG -> %s %s ", pixFormatStr[pf],
@@ -466,8 +466,8 @@ void _decompTest(tjhandle handle, unsigned char *jpegBuf,
if (sf.num != 1 || sf.denom != 1)
printf("%d/%d ... ", sf.num, sf.denom);
else printf("... ");
- _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
- scaledHeight, pf, flags));
+ TRY_TJ(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0,
+ scaledHeight, pf, flags));
}
if (checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags))
@@ -488,7 +488,7 @@ void decompTest(tjhandle handle, unsigned char *jpegBuf,
int i, n = 0;
tjscalingfactor *sf = tjGetScalingFactors(&n);
- if (!sf || !n) _throwtj();
+ if (!sf || !n) THROW_TJ();
for (i = 0; i < n; i++) {
if (subsamp == TJSAMP_444 || subsamp == TJSAMP_GRAY ||
@@ -517,11 +517,11 @@ void doTest(int w, int h, const int *formats, int nformats, int subsamp,
size = tjBufSize(w, h, subsamp);
if (size != 0)
if ((dstBuf = (unsigned char *)tjAlloc(size)) == NULL)
- _throw("Memory allocation failure.");
+ THROW("Memory allocation failure.");
if ((chandle = tjInitCompress()) == NULL ||
(dhandle = tjInitDecompress()) == NULL)
- _throwtj();
+ THROW_TJ();
for (pfi = 0; pfi < nformats; pfi++) {
for (i = 0; i < 2; i++) {
@@ -552,14 +552,50 @@ bailout:
}
-void bufSizeTest(void)
+#if SIZEOF_SIZE_T == 8
+#define CHECKSIZE(function) { \
+ if ((unsigned long long)size < (unsigned long long)0xFFFFFFFF) \
+ THROW(#function " overflow"); \
+}
+#else
+#define CHECKSIZE(function) { \
+ if (size != (unsigned long)(-1) || \
+ !strcmp(tjGetErrorStr2(NULL), "No error")) \
+ THROW(#function " overflow"); \
+}
+#endif
+
+static void overflowTest(void)
+{
+ /* Ensure that the various buffer size functions don't overflow */
+ unsigned long size;
+
+ size = tjBufSize(26755, 26755, TJSAMP_444);
+ CHECKSIZE(tjBufSize());
+ size = TJBUFSIZE(26755, 26755);
+ CHECKSIZE(TJBUFSIZE());
+ size = tjBufSizeYUV2(37838, 1, 37838, TJSAMP_444);
+ CHECKSIZE(tjBufSizeYUV2());
+ size = TJBUFSIZEYUV(37838, 37838, TJSAMP_444);
+ CHECKSIZE(TJBUFSIZEYUV());
+ size = tjBufSizeYUV(37838, 37838, TJSAMP_444);
+ CHECKSIZE(tjBufSizeYUV());
+ size = tjPlaneSizeYUV(0, 65536, 0, 65536, TJSAMP_444);
+ CHECKSIZE(tjPlaneSizeYUV());
+
+bailout:
+ return;
+}
+
+
+static void bufSizeTest(void)
{
int w, h, i, subsamp;
unsigned char *srcBuf = NULL, *dstBuf = NULL;
tjhandle handle = NULL;
unsigned long dstSize = 0;
- if ((handle = tjInitCompress()) == NULL) _throwtj();
+ if ((handle = tjInitCompress()) == NULL) THROW_TJ();
printf("Buffer size regression test\n");
for (subsamp = 0; subsamp < TJ_NUMSAMP; subsamp++) {
@@ -569,12 +605,12 @@ void bufSizeTest(void)
for (h = 1; h < maxh; h++) {
if (h % 100 == 0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
if ((srcBuf = (unsigned char *)malloc(w * h * 4)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
if (!alloc || doYUV) {
if (doYUV) dstSize = tjBufSizeYUV2(w, pad, h, subsamp);
else dstSize = tjBufSize(w, h, subsamp);
if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
}
for (i = 0; i < w * h * 4; i++) {
@@ -583,12 +619,12 @@ void bufSizeTest(void)
}
if (doYUV) {
- _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
- subsamp, 0));
+ TRY_TJ(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad,
+ subsamp, 0));
} else {
- _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
- &dstSize, subsamp, 100,
- alloc ? 0 : TJFLAG_NOREALLOC));
+ TRY_TJ(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf,
+ &dstSize, subsamp, 100,
+ alloc ? 0 : TJFLAG_NOREALLOC));
}
free(srcBuf); srcBuf = NULL;
if (!alloc || doYUV) {
@@ -596,12 +632,12 @@ void bufSizeTest(void)
}
if ((srcBuf = (unsigned char *)malloc(h * w * 4)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
if (!alloc || doYUV) {
if (doYUV) dstSize = tjBufSizeYUV2(h, pad, w, subsamp);
else dstSize = tjBufSize(h, w, subsamp);
if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL)
- _throw("Memory allocation failure");
+ THROW("Memory allocation failure");
}
for (i = 0; i < h * w * 4; i++) {
@@ -610,12 +646,12 @@ void bufSizeTest(void)
}
if (doYUV) {
- _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
- subsamp, 0));
+ TRY_TJ(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad,
+ subsamp, 0));
} else {
- _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
- &dstSize, subsamp, 100,
- alloc ? 0 : TJFLAG_NOREALLOC));
+ TRY_TJ(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf,
+ &dstSize, subsamp, 100,
+ alloc ? 0 : TJFLAG_NOREALLOC));
}
free(srcBuf); srcBuf = NULL;
if (!alloc || doYUV) {
@@ -736,20 +772,20 @@ int doBmpTest(const char *ext, int width, int align, int height, int pf,
}
if ((buf = (unsigned char *)tjAlloc(pitch * height)) == NULL)
- _throw("Could not allocate memory");
+ THROW("Could not allocate memory");
initBitmap(buf, width, pitch, height, pf, flags);
snprintf(filename, 80, "test_bmp_%s_%d_%s.%s", pixFormatStr[pf], align,
(flags & TJFLAG_BOTTOMUP) ? "bu" : "td", ext);
- _tj(tjSaveImage(filename, buf, width, pitch, height, pf, flags));
+ TRY_TJ(tjSaveImage(filename, buf, width, pitch, height, pf, flags));
md5sum = MD5File(filename, md5buf);
if (strcasecmp(md5sum, md5ref))
- _throwmd5(filename, md5sum, md5ref);
+ THROW_MD5(filename, md5sum, md5ref);
tjFree(buf); buf = NULL;
if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
flags)) == NULL)
- _throwtj();
+ THROW_TJ();
if (width != loadWidth || height != loadHeight) {
printf("\n Image dimensions of %s are bogus\n", filename);
retval = -1; goto bailout;
@@ -763,7 +799,7 @@ int doBmpTest(const char *ext, int width, int align, int height, int pf,
pf = TJPF_XBGR;
if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
flags)) == NULL)
- _throwtj();
+ THROW_TJ();
pitch = PAD(width * tjPixelSize[pf], align);
if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
printf("\n Converting %s to RGB failed\n", filename);
@@ -774,7 +810,7 @@ int doBmpTest(const char *ext, int width, int align, int height, int pf,
pf = TJPF_CMYK;
if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf,
flags)) == NULL)
- _throwtj();
+ THROW_TJ();
pitch = PAD(width * tjPixelSize[pf], align);
if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) {
printf("\n Converting %s to CMYK failed\n", filename);
@@ -788,7 +824,7 @@ int doBmpTest(const char *ext, int width, int align, int height, int pf,
pixelFormat = TJPF_UNKNOWN;
if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight,
&pixelFormat, flags)) == NULL)
- _throwtj();
+ THROW_TJ();
if ((pf == TJPF_GRAY && pixelFormat != TJPF_GRAY) ||
(pf != TJPF_GRAY && !strcasecmp(ext, "bmp") &&
pixelFormat != TJPF_BGR) ||
@@ -863,6 +899,7 @@ int main(int argc, char *argv[])
}
if (alloc) printf("Testing automatic buffer allocation\n");
if (doYUV) num4bf = 4;
+ overflowTest();
doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test");
doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test");
doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test");