l_int32 main(int argc, char **argv) { l_int32 size, i, rval, gval, bval, yval, uval, vval; l_float32 *a[3], b[3]; L_BMF *bmf; PIX *pixd; PIXA *pixa; /* Explore the range of rgb --> yuv transforms. All rgb * values transform to a valid value of yuv, so when transforming * back we get the same rgb values that we started with. */ pixa = pixaCreate(0); bmf = bmfCreate("fonts", 6); for (gval = 0; gval <= 255; gval += 20) AddTransformsRGB(pixa, bmf, gval); pixd = pixaDisplayTiledAndScaled(pixa, 32, 755, 1, 0, 20, 2); pixDisplay(pixd, 100, 0); pixWrite("/tmp/yuv1.png", pixd, IFF_PNG); pixDestroy(&pixd); pixaDestroy(&pixa); /* Now start with all "valid" yuv values, not all of which are * related to a valid rgb value. Our yuv --> rgb transform * clips the rgb components to [0 ... 255], so when transforming * back we get different values whenever the initial yuv * value is out of the rgb gamut. */ pixa = pixaCreate(0); for (yval = 16; yval <= 235; yval += 16) AddTransformsYUV(pixa, bmf, yval); pixd = pixaDisplayTiledAndScaled(pixa, 32, 755, 1, 0, 20, 2); pixDisplay(pixd, 600, 0); pixWrite("/tmp/yuv2.png", pixd, IFF_PNG); pixDestroy(&pixd); pixaDestroy(&pixa); bmfDestroy(&bmf); /* --------- Try out a special case by hand, and show that --------- * * ------- the transform matrices we are using are inverses ---------*/ /* First, use our functions for the transform */ fprintf(stderr, "Start with: yval = 143, uval = 79, vval = 103\n"); convertYUVToRGB(143, 79, 103, &rval, &gval, &bval); fprintf(stderr, " ==> rval = %d, gval = %d, bval = %d\n", rval, gval, bval); convertRGBToYUV(rval, gval, bval, &yval, &uval, &vval); fprintf(stderr, " ==> yval = %d, uval = %d, vval = %d\n", yval, uval, vval); /* Next, convert yuv --> rbg by solving for rgb --> yuv transform. * [ a00 a01 a02 ] r = b0 (y - 16) * [ a10 a11 a12 ] * g = b1 (u - 128) * [ a20 a21 a22 ] b = b2 (v - 128) */ b[0] = 143.0 - 16.0; /* y - 16 */ b[1] = 79.0 - 128.0; /* u - 128 */ b[2] = 103.0 - 128.0; /* v - 128 */ for (i = 0; i < 3; i++) a[i] = (l_float32 *)lept_calloc(3, sizeof(l_float32)); a[0][0] = 65.738 / 256.0; a[0][1] = 129.057 / 256.0; a[0][2] = 25.064 / 256.0; a[1][0] = -37.945 / 256.0; a[1][1] = -74.494 / 256.0; a[1][2] = 112.439 / 256.0; a[2][0] = 112.439 / 256.0; a[2][1] = -94.154 / 256.0; a[2][2] = -18.285 / 256.0; fprintf(stderr, "Here's the original matrix: yuv --> rgb:\n"); for (i = 0; i < 3; i++) fprintf(stderr, " %7.3f %7.3f %7.3f\n", 256.0 * a[i][0], 256.0 * a[i][1], 256.0 * a[i][2]); gaussjordan(a, b, 3); fprintf(stderr, "\nInput (yuv) = (143,79,103); solve for rgb:\n" "rval = %7.3f, gval = %7.3f, bval = %7.3f\n", b[0], b[1], b[2]); fprintf(stderr, "Here's the inverse matrix: rgb --> yuv:\n"); for (i = 0; i < 3; i++) fprintf(stderr, " %7.3f %7.3f %7.3f\n", 256.0 * a[i][0], 256.0 * a[i][1], 256.0 * a[i][2]); /* Now, convert back: rgb --> yuv; * Do this by solving for yuv --> rgb transform. * Use the b[] found previously (the rgb values), and * the a[][] which now holds the rgb --> yuv transform. */ gaussjordan(a, b, 3); fprintf(stderr, "\nInput rgb; solve for yuv:\n" "yval = %7.3f, uval = %7.3f, vval = %7.3f\n", b[0] + 16.0, b[1] + 128.0, b[2] + 128.0); fprintf(stderr, "Inverting the matrix again: yuv --> rgb:\n"); for (i = 0; i < 3; i++) fprintf(stderr, " %7.3f %7.3f %7.3f\n", 256.0 * a[i][0], 256.0 * a[i][1], 256.0 * a[i][2]); for (i = 0; i < 3; i++) lept_free(a[i]); return 0; }
main(int argc, char **argv) { char *filein, *fileout; l_uint8 *array1, *array2, *dataout, *dataout2; l_int32 i, blocksize; size_t nbytes, nout, nout2; BBUFFER *bb, *bb2; FILE *fp; static char mainName[] = "buffertest"; if (argc != 3) exit(ERROR_INT(" Syntax: buffertest filein fileout", mainName, 1)); filein = argv[1]; fileout = argv[2]; if ((array1 = l_binaryRead(filein, &nbytes)) == NULL) exit(ERROR_INT("array not made", mainName, 1)); fprintf(stderr, " Bytes read from file: %ld\n", nbytes); /* Application of byte buffer ops: compress/decompress in memory */ #if 1 dataout = zlibCompress(array1, nbytes, &nout); l_binaryWrite(fileout, "w", dataout, nout); dataout2 = zlibUncompress(dataout, nout, &nout2); l_binaryWrite("/tmp/junktest", "w", dataout2, nout2); fprintf(stderr, "nbytes in = %ld, nbytes comp = %ld, nbytes uncomp = %ld\n", nbytes, nout, nout2); lept_free(dataout); lept_free(dataout2); #endif /* Low-level byte buffer read/write test */ #if 0 bb = bbufferCreate(array1, nbytes); bbufferRead(bb, array1, nbytes); array2 = (l_uint8 *)lept_calloc(2 * nbytes, sizeof(l_uint8)); fprintf(stderr, " Bytes initially in buffer: %d\n", bb->n); blocksize = (2 * nbytes) / NBLOCKS; for (i = 0; i <= NBLOCKS; i++) { bbufferWrite(bb, array2, blocksize, &nout); fprintf(stderr, " block %d: wrote %d bytes\n", i + 1, nout); } fprintf(stderr, " Bytes left in buffer: %d\n", bb->n); bb2 = bbufferCreate(NULL, 0); bbufferRead(bb2, array1, nbytes); fp = lept_fopen(fileout, "wb"); bbufferWriteStream(bb2, fp, nbytes, &nout); fprintf(stderr, " bytes written out to fileout: %d\n", nout); bbufferDestroy(&bb); bbufferDestroy(&bb2); lept_free(array2); #endif lept_free(array1); return 0; }