Use random noise to ensure that the JPEG image generated in the buffer size test exceeds the size of the uncompressed source image.
git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@669 632fc199-4ca6-4c93-a231-07263d6284db
diff --git a/java/TJUnitTest.java b/java/TJUnitTest.java
index b0c44ad..37e0831 100644
--- a/java/TJUnitTest.java
+++ b/java/TJUnitTest.java
@@ -94,18 +94,6 @@
return (double)System.nanoTime() / 1.0e9;
}
- private final static byte pixels[][] = {
- {0, (byte)255, 0},
- {(byte)255, 0, (byte)255},
- {0, (byte)255, (byte)255},
- {(byte)255, 0, 0},
- {(byte)255, (byte)255, 0},
- {0, 0, (byte)255},
- {(byte)255, (byte)255, (byte)255},
- {0, 0, 0},
- {0, 0, (byte)255}
- };
-
private static void initBuf(byte[] buf, int w, int pitch, int h, int pf,
int flags) throws Exception {
int roffset = TJ.getRedOffset(pf);
@@ -712,41 +700,39 @@
if(tjd != null) tjd.close();
}
- private static void doTest1() throws Exception {
- int w, h, i;
+ private static void bufSizeTest() throws Exception {
+ int w, h, i, subsamp;
byte[] srcBuf, jpegBuf;
TJCompressor tjc = null;
+ Random r = new Random();
try {
tjc = new TJCompressor();
System.out.println("Buffer size regression test");
- for(w = 1; w < 48; w++) {
- int maxh = (w == 1) ? 2048 : 48;
- for(h = 1; h < maxh; h++) {
- if(h % 100 == 0)
- System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h);
- srcBuf = new byte[w * h * 4];
- jpegBuf = new byte[TJ.bufSize(w, h, TJ.SAMP_444)];
- Arrays.fill(srcBuf, (byte)0);
- for(i = 0; i < w * h; i++) {
- srcBuf[i * 4] = pixels[i % 9][0];
- srcBuf[i * 4 + 1] = pixels[i % 9][1];
- srcBuf[i * 4 + 2] = pixels[i % 9][2];
- }
- tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX);
- tjc.setSubsamp(TJ.SAMP_444);
- tjc.setJPEGQuality(100);
- tjc.compress(jpegBuf, 0);
+ for(subsamp = 0; subsamp < TJ.NUMSAMP; subsamp++) {
+ for(w = 1; w < 48; w++) {
+ int maxh = (w == 1) ? 2048 : 48;
+ for(h = 1; h < maxh; h++) {
+ if(h % 100 == 0)
+ System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h);
+ srcBuf = new byte[w * h * 4];
+ jpegBuf = new byte[TJ.bufSize(w, h, subsamp)];
+ for(i = 0; i < w * h * 4; i++) {
+ srcBuf[i] = (byte)(r.nextInt(2) * 255);
+ }
+ tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX);
+ tjc.setSubsamp(subsamp);
+ tjc.setJPEGQuality(100);
+ tjc.compress(jpegBuf, 0);
- srcBuf = new byte[h * w * 4];
- jpegBuf = new byte[TJ.bufSize(h, w, TJ.SAMP_444)];
- for(i = 0; i < h * w; i++) {
- if(i % 2 == 0) srcBuf[i * 4] =
- srcBuf[i * 4 + 1] = srcBuf[i * 4 + 2] = (byte)0xFF;
- else srcBuf[i * 4] = srcBuf[i * 4 + 1] = srcBuf[i * 4 + 2] = 0;
+ srcBuf = new byte[h * w * 4];
+ jpegBuf = new byte[TJ.bufSize(h, w, subsamp)];
+ for(i = 0; i < h * w * 4; i++) {
+ srcBuf[i] = (byte)(r.nextInt(2) * 255);
+ }
+ tjc.setSourceImage(srcBuf, h, 0, w, TJ.PF_BGRX);
+ tjc.compress(jpegBuf, 0);
}
- tjc.setSourceImage(srcBuf, h, 0, w, TJ.PF_BGRX);
- tjc.compress(jpegBuf, 0);
}
}
System.out.println("Done. ");
@@ -794,7 +780,7 @@
testName);
doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_GRAY,
testName);
- if(!doyuv && !bi) doTest1();
+ if(!doyuv && !bi) bufSizeTest();
if(doyuv && !bi) {
yuv = YUVDECODE;
doTest(48, 48, onlyRGB, TJ.SAMP_444, "javatest_yuv0");
diff --git a/tjunittest.c b/tjunittest.c
index 9acab0a..e30a91a 100644
--- a/tjunittest.c
+++ b/tjunittest.c
@@ -75,19 +75,6 @@
int exitStatus=0;
#define bailout() {exitStatus=-1; goto bailout;}
-int pixels[9][3]=
-{
- {0, 255, 0},
- {255, 0, 255},
- {0, 255, 255},
- {255, 0, 0},
- {255, 255, 0},
- {0, 0, 255},
- {255, 255, 255},
- {0, 0, 0},
- {0, 0, 255}
-};
-
void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
{
@@ -526,9 +513,9 @@
}
-void doTest1(void)
+void bufSizeTest(void)
{
- int w, h, i;
+ int w, h, i, subsamp;
unsigned char *srcBuf=NULL, *jpegBuf=NULL;
tjhandle handle=NULL;
unsigned long jpegSize=0;
@@ -536,49 +523,56 @@
if((handle=tjInitCompress())==NULL) _throwtj();
printf("Buffer size regression test\n");
- for(w=1; w<48; w++)
+ for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++)
{
- int maxh=(w==1)? 2048:48;
- for(h=1; h<maxh; h++)
+ for(w=1; w<48; w++)
{
- 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");
- if(!alloc)
+ int maxh=(w==1)? 2048:48;
+ for(h=1; h<maxh; h++)
{
- if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, TJSAMP_444)))
- ==NULL)
+ 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");
- jpegSize=tjBufSize(w, h, TJSAMP_444);
- }
- memset(srcBuf, 0, w*h*4);
+ if(!alloc)
+ {
+ if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, subsamp)))
+ ==NULL)
+ _throw("Memory allocation failure");
+ jpegSize=tjBufSize(w, h, subsamp);
+ }
- for(i=0; i<w*h; i++) memcpy(srcBuf, &pixels[i%9], 3);
+ for(i=0; i<w*h*4; i++)
+ {
+ if(random()<RAND_MAX/2) srcBuf[i]=0;
+ else srcBuf[i]=255;
+ }
- _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf, &jpegSize,
- TJSAMP_444, 100, alloc? 0:TJFLAG_NOREALLOC));
- free(srcBuf); srcBuf=NULL;
- tjFree(jpegBuf); jpegBuf=NULL;
+ _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf,
+ &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
+ free(srcBuf); srcBuf=NULL;
+ tjFree(jpegBuf); jpegBuf=NULL;
- if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
- _throw("Memory allocation failure");
- if(!alloc)
- {
- if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, TJSAMP_444)))
- ==NULL)
+ if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL)
_throw("Memory allocation failure");
- jpegSize=tjBufSize(h, w, TJSAMP_444);
- }
+ if(!alloc)
+ {
+ if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(h, w, subsamp)))
+ ==NULL)
+ _throw("Memory allocation failure");
+ jpegSize=tjBufSize(h, w, subsamp);
+ }
- for(i=0; i<h*w; i++)
- {
- if(i%2==0) srcBuf[i*4]=srcBuf[i*4+1]=srcBuf[i*4+2]=0xFF;
- else srcBuf[i*4]=srcBuf[i*4+1]=srcBuf[i*4+2]=0;
+ for(i=0; i<h*w*4; i++)
+ {
+ if(random()<RAND_MAX/2) srcBuf[i]=0;
+ else srcBuf[i]=255;
+ }
+
+ _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf,
+ &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
+ free(srcBuf); srcBuf=NULL;
+ tjFree(jpegBuf); jpegBuf=NULL;
}
- _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &jpegBuf, &jpegSize,
- TJSAMP_444, 100, alloc? 0:TJFLAG_NOREALLOC));
- free(srcBuf); srcBuf=NULL;
- tjFree(jpegBuf); jpegBuf=NULL;
}
}
printf("Done. \n");
@@ -619,7 +613,7 @@
doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test");
doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test");
doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test");
- if(!doyuv) doTest1();
+ if(!doyuv) bufSizeTest();
if(doyuv)
{
yuv=YUVDECODE;