/* Returns 1 on error */ static l_int32 test_mem_png(const char *fname) { l_uint8 *data = NULL; l_int32 same; size_t size = 0; PIX *pixs; PIX *pixd = NULL; if ((pixs = pixRead(fname)) == NULL) { fprintf(stderr, "Failure to read %s\n", fname); return 1; } if (pixWriteMem(&data, &size, pixs, IFF_PNG)) { fprintf(stderr, "Mem write fail for png\n"); return 1; } if ((pixd = pixReadMem(data, size)) == NULL) { fprintf(stderr, "Mem read fail for png\n"); lept_free(data); return 1; } pixEqual(pixs, pixd, &same); if (!same) fprintf(stderr, "Mem write/read fail for file %s\n", fname); pixDestroy(&pixs); pixDestroy(&pixd); lept_free(data); return (!same); }
/* Returns 1 on error */ static l_int32 test_mem_gif(const char *fname, l_int32 index) { l_uint8 *data = NULL; l_int32 same; size_t size = 0; PIX *pixs; PIX *pixd = NULL; if ((pixs = pixRead(fname)) == NULL) { fprintf(stderr, "Failure to read gif file: %s\n", fname); return 1; } if (pixWriteMem(&data, &size, pixs, IFF_GIF)) { fprintf(stderr, "Mem gif write fail on image %d\n", index); return 1; } if ((pixd = pixReadMem(data, size)) == NULL) { fprintf(stderr, "Mem gif read fail on image %d\n", index); lept_free(data); return 1; } pixEqual(pixs, pixd, &same); pixDestroy(&pixs); pixDestroy(&pixd); lept_free(data); if (!same && index < 6) { fprintf(stderr, "Mem gif write/read fail for file %s\n", fname); return 1; } else return 0; }
// Saves the given Pix as a PNG-encoded string and destroys it. void ImageData::SetPixInternal(Pix* pix, GenericVector<char>* image_data) { l_uint8* data; size_t size; pixWriteMem(&data, &size, pix, IFF_PNG); pixDestroy(&pix); image_data->init_to_size(size, 0); memcpy(&(*image_data)[0], data, size); free(data); }
/* Returns 1 on error */ static l_int32 test_writemem(PIX *pixs, l_int32 format, char *psfile) { l_uint8 *data = NULL; l_int32 same; size_t size = 0; PIX *pixd = NULL; if (format == IFF_PS) { pixWriteMemPS(&data, &size, pixs, NULL, 0, 1.0); l_binaryWrite(psfile, "w", data, size); lept_free(data); return 0; } /* Fail silently if library is not available */ #if !HAVE_LIBJPEG if (format == IFF_JFIF_JPEG) return 0; #endif /* !HAVE_LIBJPEG */ #if !HAVE_LIBPNG if (format == IFF_PNG) return 0; #endif /* !HAVE_LIBPNG */ #if !HAVE_LIBTIFF if (format == IFF_TIFF) return 0; #endif /* !HAVE_LIBTIFF */ if (pixWriteMem(&data, &size, pixs, format)) { fprintf(stderr, "Mem write fail for format %d\n", format); return 1; } if ((pixd = pixReadMem(data, size)) == NULL) { fprintf(stderr, "Mem read fail for format %d\n", format); lept_free(data); return 1; } if (format == IFF_JFIF_JPEG) { fprintf(stderr, "jpeg size = %ld\n", size); pixDisplayWrite(pixd, 1); same = TRUE; } else { pixEqual(pixs, pixd, &same); if (!same) fprintf(stderr, "Mem write/read fail for format %d\n", format); } pixDestroy(&pixd); lept_free(data); return (!same); }
// Send an image of type Pix. void ScrollView::Image(struct Pix* image, int x_pos, int y_pos) { l_uint8* data; size_t size; pixWriteMem(&data, &size, image, IFF_PNG); int base64_len = (size + 2) / 3 * 4; y_pos = TranslateYCoordinate(y_pos); SendMsg("readImage(%d,%d,%d)", x_pos, y_pos, base64_len); // Base64 encode the data. const char kBase64Table[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', }; char* base64 = new char[base64_len + 1]; memset(base64, '=', base64_len); base64[base64_len] = '\0'; int remainder = 0; int bits_left = 0; int code_len = 0; for (size_t i = 0; i < size; ++i) { int code = (data[i] >> (bits_left + 2)) | remainder; base64[code_len++] = kBase64Table[code & 63]; bits_left += 2; remainder = data[i] << (6 - bits_left); if (bits_left == 6) { base64[code_len++] = kBase64Table[remainder & 63]; bits_left = 0; remainder = 0; } } if (bits_left > 0) base64[code_len++] = kBase64Table[remainder & 63]; SendRawMessage(base64); delete [] base64; lept_free(data); }
jbyteArray Java_com_googlecode_leptonica_android_WriteFile_nativeWriteMem(JNIEnv *env, jclass clazz, jlong nativePix, jint format) { PIX *pixs = (PIX *) nativePix; l_uint8 *data; size_t size; if (pixWriteMem(&data, &size, pixs, (l_uint32) format)) { LOGE("Failed to write pix data"); return NULL; } // TODO Can we just use the byte array directly? jbyteArray array = env->NewByteArray(size); env->SetByteArrayRegion(array, 0, size, (jbyte *) data); free(data); return array; }
main(int argc, char **argv) { char buf[256]; size_t nbytes; l_int32 i, w, h, success, display; l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; l_uint8 *data; l_uint32 *data32, *data32r; BOX *box; FILE *fp; PIX *pixs, *pixt, *pixt2, *pixd; if (regTestSetup(argc, argv, &fp, &display, &success, NULL)) return 1; /* Test basic serialization/deserialization */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); /* Serialize to memory */ pixSerializeToMemory(pixs, &data32, &nbytes); /* Just for fun, write and read back from file */ arrayWrite("/tmp/array", "w", data32, nbytes); data32r = (l_uint32 *)arrayRead("/tmp/array", (l_int32 *)(&nbytes)); /* Deserialize */ pixd = pixDeserializeFromMemory(data32r, nbytes); regTestComparePix(fp, argv, pixs, pixd, i, &success); pixDestroy(&pixd); pixDestroy(&pixs); FREE(data32); FREE(data32r); } /* Test read/write fileio interface */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixGetDimensions(pixs, &w, &h, NULL); box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); snprintf(buf, sizeof(buf), "/tmp/pixs.%d", i); pixWrite(buf, pixt, IFF_SPIX); regTestCheckFile(fp, argv, buf, i, &success); pixt2 = pixRead(buf); regTestComparePix(fp, argv, pixt, pixt2, nfiles + i, &success); pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixt2); } /* Test read header. Note that for rgb input, spp = 3, * but for 32 bpp spix, we set spp = 4. */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixWriteMem(&data, &nbytes, pixs, IFF_SPIX); pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); pixReadHeaderMem(data, nbytes, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); if (format2 != 16 || w != w2 || h != h2 || bps != bps2 || iscmap != iscmap2) { if (fp) fprintf(fp, "Failure comparing data"); else fprintf(stderr, "Failure comparing data"); success = FALSE; } pixDestroy(&pixs); FREE(data); } #if 0 /* Do timing */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); startTimer(); pixSerializeToMemory(pixs, &data32, &nbytes); pixd = pixDeserializeFromMemory(data32, nbytes); fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); FREE(data32); pixDestroy(&pixs); pixDestroy(&pixd); } #endif regTestCleanup(argc, argv, fp, success, NULL); return 0; }
int main(int argc, char **argv) { char buf[256]; size_t size; l_int32 i, w, h; l_int32 format, bps, spp, iscmap, format2, w2, h2, bps2, spp2, iscmap2; l_uint8 *data; l_uint32 *data32, *data32r; BOX *box; PIX *pixs, *pixt, *pixt2, *pixd; L_REGPARAMS *rp; if (regTestSetup(argc, argv, &rp)) return 1; /* Test basic serialization/deserialization */ data32 = NULL; for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); /* Serialize to memory */ pixSerializeToMemory(pixs, &data32, &size); /* Just for fun, write and read back from file */ l_binaryWrite("/tmp/regout/array", "w", data32, size); data32r = (l_uint32 *)l_binaryRead("/tmp/regout/array", &size); /* Deserialize */ pixd = pixDeserializeFromMemory(data32r, size); regTestComparePix(rp, pixs, pixd); /* i */ pixDestroy(&pixd); pixDestroy(&pixs); lept_free(data32); lept_free(data32r); } /* Test read/write fileio interface */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixGetDimensions(pixs, &w, &h, NULL); box = boxCreate(0, 0, L_MIN(150, w), L_MIN(150, h)); pixt = pixClipRectangle(pixs, box, NULL); boxDestroy(&box); snprintf(buf, sizeof(buf), "/tmp/regout/pixs.%d.spix", rp->index + 1); pixWrite(buf, pixt, IFF_SPIX); regTestCheckFile(rp, buf); /* nfiles + 2 * i */ pixt2 = pixRead(buf); regTestComparePix(rp, pixt, pixt2); /* nfiles + 2 * i + 1 */ pixDestroy(&pixs); pixDestroy(&pixt); pixDestroy(&pixt2); } /* Test read header. Note that for rgb input, spp = 3, * but for 32 bpp spix, we set spp = 4. */ data = NULL; for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); pixWriteMem(&data, &size, pixs, IFF_SPIX); pixReadHeader(filename[i], &format, &w, &h, &bps, &spp, &iscmap); pixReadHeaderMem(data, size, &format2, &w2, &h2, &bps2, &spp2, &iscmap2); if (format2 != IFF_SPIX || w != w2 || h != h2 || bps != bps2 || iscmap != iscmap2) { if (rp->fp) fprintf(rp->fp, "Failure comparing data"); else fprintf(stderr, "Failure comparing data"); } pixDestroy(&pixs); lept_free(data); } #if 0 /* Do timing */ for (i = 0; i < nfiles; i++) { pixs = pixRead(filename[i]); startTimer(); pixSerializeToMemory(pixs, &data32, &size); pixd = pixDeserializeFromMemory(data32, size); fprintf(stderr, "Time for %s: %7.3f sec\n", filename[i], stopTimer()); lept_free(data32); pixDestroy(&pixs); pixDestroy(&pixd); } #endif return regTestCleanup(rp); }