From 1a4ba7eebef8750912335cbab53080f9c1539e2d Mon Sep 17 00:00:00 2001 From: danblooomberg Date: Mon, 26 Aug 2024 16:23:32 -0600 Subject: [PATCH] Return error instead of first image plus warning if given animated gif input * this follows Amit's suggestion * also added a blending test to blend4_reg --- prog/blend4_reg.c | 64 ++++++++++++++++++++++++++++++++++++++++++++-- src/gifio.c | 16 +++++++++--- version-notes.html | 4 +-- 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/prog/blend4_reg.c b/prog/blend4_reg.c index d923f72cc..43a340440 100644 --- a/prog/blend4_reg.c +++ b/prog/blend4_reg.c @@ -29,11 +29,16 @@ * * Regression test for this function: * pixAddAlphaToBlend() - * * Blending is done using pixBlendWithGrayMask() + * + * Also, show blending of two color images using an alpha mask that + * varies linearly with radius from the center (which is transparent). */ #include "allheaders.h" +#include + +static PIX *AlphaRectangle(l_int32 w, l_int32 h, l_float32 fract); static const char *blenders[] = {"feyn-word.tif", "weasel4.16c.png", "karen8.jpg"}; @@ -42,7 +47,7 @@ int main(int argc, char **argv) { l_int32 i, w, h; -PIX *pix0, *pix1, *pix2, *pix3, *pix4, *pix5; +PIX *pix0, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7; PIXA *pixa; L_REGPARAMS *rp; @@ -89,6 +94,31 @@ L_REGPARAMS *rp; pixDestroy(&pix3); pixDestroy(&pix4); } + pixDestroy(&pix0); + pixDestroy(&pix1); + + /* Blending of two color images using special mask */ + pix1 = pixRead("fish24.jpg"); + pix2 = pixRead("wyom.jpg"); + pixGetDimensions(pix2, &w, &h, NULL); + pix3 = pixRotateOrth(pix1, 1); + pix4 = pixScaleToSize(pix3, w, h); /* same size as wyom.jpg */ + pix5 = AlphaRectangle(w, h, 1.0); + pix6 = pixBlendWithGrayMask(pix4, pix2, pix5, 0, 0); + pix7 = pixBlendWithGrayMask(pix2, pix4, pix5, 0, 0); + pixDisplayWithTitle(pix6, 1000, 0, NULL, rp->display); + pixDisplayWithTitle(pix7, 1000, 500, NULL, rp->display); + regTestWritePixAndCheck(rp, pix4, IFF_JFIF_JPEG); /* 5 */ + regTestWritePixAndCheck(rp, pix5, IFF_JFIF_JPEG); /* 6 */ + regTestWritePixAndCheck(rp, pix6, IFF_JFIF_JPEG); /* 7 */ + regTestWritePixAndCheck(rp, pix7, IFF_JFIF_JPEG); /* 8 */ + pixaAddPix(pixa, pix2, L_INSERT); + pixaAddPix(pixa, pix4, L_INSERT); + pixaAddPix(pixa, pix5, L_INSERT); + pixaAddPix(pixa, pix6, L_INSERT); + pixaAddPix(pixa, pix7, L_INSERT); + pixDestroy(&pix1); + pixDestroy(&pix3); pixaConvertToPdf(pixa, 100, 1.0, L_JPEG_ENCODE, 0, "Blendings: blend4_reg", "/tmp/lept/regout/blend.pdf"); @@ -100,3 +130,33 @@ L_REGPARAMS *rp; return regTestCleanup(rp); } + + /* Rectangular mask: opaque at center, linear change towards + * transparency with distance from the center */ +PIX * +AlphaRectangle(l_int32 w, l_int32 h, l_float32 fract) +{ +l_int32 i, j, wpl, w2, h2, val; +l_float32 frdist; +l_uint32 *data, *line; +PIX *pixd; + + pixd = pixCreate(w, h, 8); + data = pixGetData(pixd); + wpl = pixGetWpl(pixd); + w2 = w / 2; + h2 = h / 2; + for (i = 0; i < h; i++) { + line = data + i * wpl; + for (j = 0; j < w; j++) { + frdist = sqrt((h2 - i) * (h2 - i) + (w2 - j) * (w2 - j)) / + sqrt(w2 * w2 + h2 * h2); + val = (l_int32)(255. * (1.0 - frdist * fract)); + SET_DATA_BYTE(line, j, val); + } + } + + return pixd; + +} + diff --git a/src/gifio.c b/src/gifio.c index 446a651c1..9acb77a97 100644 --- a/src/gifio.c +++ b/src/gifio.c @@ -63,6 +63,12 @@ * successfully read gif files that it writes with this version; * DGifSlurp() gets an internal error from an uninitialized array * and returns failure. The problem was fixed in 5.1.3. + * + * Limitations: + * + * (1) We do not support animated gif. If the gif has more then one image, + * an error message is returned. + * * */ @@ -214,6 +220,7 @@ l_int32 bytesRead; * (1) This decodes the pix from the compressed gif stream and * closes the stream. * (2) It is static so that the stream is not exposed to clients. + * (3) Leptonica does not support gifanim (more than 1 image in the file). * */ static PIX * @@ -240,9 +247,12 @@ int giferr; } nimages = gif->ImageCount; - if (nimages > 1) - L_WARNING("There are %d images in the file; we only read the first\n", - __func__, nimages); + if (nimages > 1) { + DGifCloseFile(gif, &giferr); + L_ERROR("There are %d images in the file; gifanim is not supported\n", + __func__, nimages); + return NULL; + } si = gif->SavedImages[0]; w = si.ImageDesc.Width; diff --git a/version-notes.html b/version-notes.html index 69143f2f0..5c172466c 100644 --- a/version-notes.html +++ b/version-notes.html @@ -109,7 +109,7 @@

* Modified cleanpdf.c to use l_pdfRenderFiles(). * Source files changed: adaptmap.c affinecompose.c, bmpio.c, boxbasic.c, colormap.c, compare.c, - dewarp1.c, dnabasic.c, fpix1.c, + dewarp1.c, dnabasic.c, fpix1.c, gifio.c, gplot.c, grayquant.c, jbclass.c, jp2kheader.c, jp2kheaderstub.c, jp2kio.c, jpegio.c, libversions.c, morphseq.c, numabasic.c, pageseg.c partify.c, pdfapp.c, @@ -119,7 +119,7 @@

sarray1.c, sel1.c, utils2.c, writefile.c, allheaders.h, environ.h * Prog files changed: alltests_reg.c, binmorph3_reg.c, - blend2_reg.c, cleanpdf.c, compressedpdf.c, croppdf.c, + blend2_reg.c, blend4_reg.c cleanpdf.c, compressedpdf.c, croppdf.c, findpattern_reg.c, findpattern1_reg.c findpattern2_reg.c, htmlviewer.c, jp2kio_reg.c, libre_makefigs.c, misctest1.c, misctest2.c,