Merge branch 'stbiw_update_jpeg' of https://github.com/DanielGibson/stb into work2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents 1586318, 1551619 · Browse files · 1 file·+75 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+75 added−32 removed
- other:+75 added−32 removed
Provenance
- Commit
454da53- Tree
b86a545- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent 1
1586318- Parent 2
1551619
1 file changed+75 added−32 removed
modified:stb_image_write.h+75 added-32 removed
1269 unmodified lines1270 }1271 bits[0] = val & ((1<<bits[1])-1);1272 }1273 1274-static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {1274+static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {1275 const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };1276 const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };1277- int dataOff, i, diff, end0pos;1277+ int dataOff, i, j, n, diff, end0pos, x, y;1278 int DU[64];1279 1280 // DCT rows1281- for(dataOff=0; dataOff<64; dataOff+=8) {1281+ for(dataOff=0, n=du_stride*8; dataOff<n; dataOff+=du_stride) {1282 stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+1], &CDU[dataOff+2], &CDU[dataOff+3], &CDU[dataOff+4], &CDU[dataOff+5], &CDU[dataOff+6], &CDU[dataOff+7]);1283 }1284 // DCT columns1285 for(dataOff=0; dataOff<8; ++dataOff) {1286- stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+8], &CDU[dataOff+16], &CDU[dataOff+24], &CDU[dataOff+32], &CDU[dataOff+40], &CDU[dataOff+48], &CDU[dataOff+56]);1286+ stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+du_stride], &CDU[dataOff+du_stride*2], &CDU[dataOff+du_stride*3], &CDU[dataOff+du_stride*4],1287+ &CDU[dataOff+du_stride*5], &CDU[dataOff+du_stride*6], &CDU[dataOff+du_stride*7]);1288 }1289 // Quantize/descale/zigzag the coefficients1290+ for(y = 0, j=0; y < 8; ++y) {1291+ for(x = 0; x < 8; ++x,++j) {1289- for(i=0; i<64; ++i) {1290- float v = CDU[i]*fdtbl[i];1291- // DU[stbiw__jpg_ZigZag[i]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f));1292- // ceilf() and floorf() are C99, not C89, but I /think/ they're not needed here anyway?1293- DU[stbiw__jpg_ZigZag[i]] = (int)(v < 0 ? v - 0.5f : v + 0.5f);1292+ i = y*du_stride+x;1293+ float v = CDU[i]*fdtbl[j];1294+ // DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f));1295+ // ceilf() and floorf() are C99, not C89, but I /think/ they're not needed here anyway?1296+ DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? v - 0.5f : v + 0.5f);1297+ }1298 }1299 1300 // Encode DC1301 diff = DU[0] - DC;109 unmodified lines1411 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99};1412 static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 1413 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f };1414 1411- int row, col, i, k;1415+ int row, col, i, k, subsample;1416 float fdtbl_Y[64], fdtbl_UV[64];1417 unsigned char YTable[64], UVTable[64];1418 1419 if(!data || !width || !height || comp > 4 || comp < 1) {1420 return 0;1421 }1422 1423 quality = quality ? quality : 90;1424+ subsample = quality <= 90 ? 1 : 0;1425 quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;1426 quality = quality < 50 ? 5000 / quality : 200 - quality * 2;1427 1428 for(i = 0; i < 64; ++i) {14 unmodified lines1443 {1444 static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 };1445 static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 };1446 const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width),1442- 3,1,0x11,0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 };1447+ 3,1,(unsigned char)(subsample?0x22:0x11),0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 };1448 s->func(s->context, (void*)head0, sizeof(head0));1449 s->func(s->context, (void*)YTable, sizeof(YTable));1450 stbiw__putc(s, 1);1451 s->func(s->context, UVTable, sizeof(UVTable));14 unmodified lines1466 1467 // Encode 8x8 macroblocks1468 {1469 static const unsigned short fillBits[] = {0x7F, 7};1465- const unsigned char *imageData = (const unsigned char *)data;1470 int DCY=0, DCU=0, DCV=0;1471 int bitBuf=0, bitCnt=0;1472 // comp == 2 is grey+alpha (alpha is ignored)1473 int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;1474+ const unsigned char *dataR = (const unsigned char *)data;1475+ const unsigned char *dataG = dataR + ofsG;1476+ const unsigned char *dataB = dataR + ofsB;1477 int x, y, pos;1471- for(y = 0; y < height; y += 8) {1472- for(x = 0; x < width; x += 8) {1473- float YDU[64], UDU[64], VDU[64];1474- for(row = y, pos = 0; row < y+8; ++row) {1475- // row >= height => use last input row1476- int clamped_row = (row < height) ? row : height - 1;1477- int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;1478- for(col = x; col < x+8; ++col, ++pos) {1479- float r, g, b;1480- // if col >= width => use pixel from last input column1481- int p = base_p + ((col < width) ? col : (width-1))*comp;1478+ if(subsample) {1479+ for(y = 0; y < height; y += 16) {1480+ for(x = 0; x < width; x += 16) {1481+ float Y[256], U[256], V[256];1482+ for(row = y, pos = 0; row < y+16; ++row) {1483+ // row >= height => use last input row1484+ int clamped_row = (row < height) ? row : height - 1;1485+ int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;1486+ for(col = x; col < x+16; ++col, ++pos) {1487+ // if col >= width => use pixel from last input column1488+ int p = base_p + ((col < width) ? col : (width-1))*comp;1489+ float r = dataR[p], g = dataG[p], b = dataB[p];1490+ Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;1491+ U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;1492+ V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b;1493+ }1494+ }1495+ DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+0, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);1496+ DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+8, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);1497+ DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+128, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);1498+ DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+136, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);1499 1483- r = imageData[p+0];1484- g = imageData[p+ofsG];1485- b = imageData[p+ofsB];1486- YDU[pos]=+0.29900f*r+0.58700f*g+0.11400f*b-128;1487- UDU[pos]=-0.16874f*r-0.33126f*g+0.50000f*b;1488- VDU[pos]=+0.50000f*r-0.41869f*g-0.08131f*b;1500+ // subsample U,V1501+ {1502+ float subU[64], subV[64];1503+ int yy, xx;1504+ for(yy = 0, pos = 0; yy < 8; ++yy) {1505+ for(xx = 0; xx < 8; ++xx, ++pos) {1506+ int j = yy*32+xx*2;1507+ subU[pos] = (U[j+0] + U[j+1] + U[j+16] + U[j+17]) * 0.25f;1508+ subV[pos] = (V[j+0] + V[j+1] + V[j+16] + V[j+17]) * 0.25f;1509+ }1510+ }1511+ DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subU, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);1512+ DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subV, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);1513 }1514 }1515+ }1516+ } else {1517+ for(y = 0; y < height; y += 8) {1518+ for(x = 0; x < width; x += 8) {1519+ float Y[64], U[64], V[64];1520+ for(row = y, pos = 0; row < y+8; ++row) {1521+ // row >= height => use last input row1522+ int clamped_row = (row < height) ? row : height - 1;1523+ int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;1524+ for(col = x; col < x+8; ++col, ++pos) {1525+ // if col >= width => use pixel from last input column1526+ int p = base_p + ((col < width) ? col : (width-1))*comp;1527+ float r = dataR[p], g = dataG[p], b = dataB[p];1528+ Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;1529+ U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;1530+ V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b;1531+ }1532+ }1533 1492- DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);1493- DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);1494- DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);1534+ DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y, 8, fdtbl_Y, DCY, YDC_HT, YAC_HT);1535+ DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, U, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);1536+ DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, V, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);1537+ }1538 }1539 }1540 1541 // Do the bit alignment of the EOI marker121 unmodified lines