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
This commit is contained in:
DRC
2011-07-12 06:22:06 +00:00
parent 9b49f0e4c7
commit 724c56b46a
2 changed files with 69 additions and 89 deletions

View File

@@ -94,18 +94,6 @@ public class TJUnitTest {
return (double)System.nanoTime() / 1.0e9; 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, private static void initBuf(byte[] buf, int w, int pitch, int h, int pf,
int flags) throws Exception { int flags) throws Exception {
int roffset = TJ.getRedOffset(pf); int roffset = TJ.getRedOffset(pf);
@@ -712,41 +700,39 @@ public class TJUnitTest {
if(tjd != null) tjd.close(); if(tjd != null) tjd.close();
} }
private static void doTest1() throws Exception { private static void bufSizeTest() throws Exception {
int w, h, i; int w, h, i, subsamp;
byte[] srcBuf, jpegBuf; byte[] srcBuf, jpegBuf;
TJCompressor tjc = null; TJCompressor tjc = null;
Random r = new Random();
try { try {
tjc = new TJCompressor(); tjc = new TJCompressor();
System.out.println("Buffer size regression test"); System.out.println("Buffer size regression test");
for(w = 1; w < 48; w++) { for(subsamp = 0; subsamp < TJ.NUMSAMP; subsamp++) {
int maxh = (w == 1) ? 2048 : 48; for(w = 1; w < 48; w++) {
for(h = 1; h < maxh; h++) { int maxh = (w == 1) ? 2048 : 48;
if(h % 100 == 0) for(h = 1; h < maxh; h++) {
System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h); if(h % 100 == 0)
srcBuf = new byte[w * h * 4]; System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h);
jpegBuf = new byte[TJ.bufSize(w, h, TJ.SAMP_444)]; srcBuf = new byte[w * h * 4];
Arrays.fill(srcBuf, (byte)0); jpegBuf = new byte[TJ.bufSize(w, h, subsamp)];
for(i = 0; i < w * h; i++) { for(i = 0; i < w * h * 4; i++) {
srcBuf[i * 4] = pixels[i % 9][0]; srcBuf[i] = (byte)(r.nextInt(2) * 255);
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(subsamp);
tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX); tjc.setJPEGQuality(100);
tjc.setSubsamp(TJ.SAMP_444); tjc.compress(jpegBuf, 0);
tjc.setJPEGQuality(100);
tjc.compress(jpegBuf, 0);
srcBuf = new byte[h * w * 4]; srcBuf = new byte[h * w * 4];
jpegBuf = new byte[TJ.bufSize(h, w, TJ.SAMP_444)]; jpegBuf = new byte[TJ.bufSize(h, w, subsamp)];
for(i = 0; i < h * w; i++) { for(i = 0; i < h * w * 4; i++) {
if(i % 2 == 0) srcBuf[i * 4] = srcBuf[i] = (byte)(r.nextInt(2) * 255);
srcBuf[i * 4 + 1] = srcBuf[i * 4 + 2] = (byte)0xFF; }
else srcBuf[i * 4] = srcBuf[i * 4 + 1] = srcBuf[i * 4 + 2] = 0; 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. "); System.out.println("Done. ");
@@ -794,7 +780,7 @@ public class TJUnitTest {
testName); testName);
doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_GRAY, doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_GRAY,
testName); testName);
if(!doyuv && !bi) doTest1(); if(!doyuv && !bi) bufSizeTest();
if(doyuv && !bi) { if(doyuv && !bi) {
yuv = YUVDECODE; yuv = YUVDECODE;
doTest(48, 48, onlyRGB, TJ.SAMP_444, "javatest_yuv0"); doTest(48, 48, onlyRGB, TJ.SAMP_444, "javatest_yuv0");

View File

@@ -75,19 +75,6 @@ int yuv=0, alloc=0;
int exitStatus=0; int exitStatus=0;
#define bailout() {exitStatus=-1; goto bailout;} #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) void initBuf(unsigned char *buf, int w, int h, int pf, int flags)
{ {
@@ -526,9 +513,9 @@ void doTest(int w, int h, const int *formats, int nformats, int subsamp,
} }
void doTest1(void) void bufSizeTest(void)
{ {
int w, h, i; int w, h, i, subsamp;
unsigned char *srcBuf=NULL, *jpegBuf=NULL; unsigned char *srcBuf=NULL, *jpegBuf=NULL;
tjhandle handle=NULL; tjhandle handle=NULL;
unsigned long jpegSize=0; unsigned long jpegSize=0;
@@ -536,49 +523,56 @@ void doTest1(void)
if((handle=tjInitCompress())==NULL) _throwtj(); if((handle=tjInitCompress())==NULL) _throwtj();
printf("Buffer size regression test\n"); 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(w=1; w<48; w++)
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); int maxh=(w==1)? 2048:48;
if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL) for(h=1; h<maxh; h++)
_throw("Memory allocation failure");
if(!alloc)
{ {
if((jpegBuf=(unsigned char *)tjAlloc(tjBufSize(w, h, TJSAMP_444))) if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h);
==NULL) if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL)
_throw("Memory allocation failure"); _throw("Memory allocation failure");
jpegSize=tjBufSize(w, h, TJSAMP_444); if(!alloc)
} {
memset(srcBuf, 0, w*h*4); 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, _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &jpegBuf,
TJSAMP_444, 100, alloc? 0:TJFLAG_NOREALLOC)); &jpegSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC));
free(srcBuf); srcBuf=NULL; free(srcBuf); srcBuf=NULL;
tjFree(jpegBuf); jpegBuf=NULL; tjFree(jpegBuf); jpegBuf=NULL;
if((srcBuf=(unsigned char *)malloc(h*w*4))==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)
_throw("Memory allocation failure"); _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++) for(i=0; i<h*w*4; i++)
{ {
if(i%2==0) srcBuf[i*4]=srcBuf[i*4+1]=srcBuf[i*4+2]=0xFF; if(random()<RAND_MAX/2) srcBuf[i]=0;
else srcBuf[i*4]=srcBuf[i*4+1]=srcBuf[i*4+2]=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"); printf("Done. \n");
@@ -619,7 +613,7 @@ int main(int argc, char *argv[])
doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test"); doTest(35, 39, _onlyGray, 1, TJSAMP_GRAY, "test");
doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test"); doTest(39, 41, _3byteFormats, 2, TJSAMP_GRAY, "test");
doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test"); doTest(41, 35, _4byteFormats, 4, TJSAMP_GRAY, "test");
if(!doyuv) doTest1(); if(!doyuv) bufSizeTest();
if(doyuv) if(doyuv)
{ {
yuv=YUVDECODE; yuv=YUVDECODE;