Skip to content

Commit

Permalink
Return error instead of first image plus warning if given animated gi…
Browse files Browse the repository at this point in the history
…f input

* this follows Amit's suggestion
* also added a blending test to blend4_reg
  • Loading branch information
DanBloomberg committed Aug 26, 2024
1 parent 6551f38 commit 1a4ba7e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
64 changes: 62 additions & 2 deletions prog/blend4_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <math.h>

static PIX *AlphaRectangle(l_int32 w, l_int32 h, l_float32 fract);

static const char *blenders[] =
{"feyn-word.tif", "weasel4.16c.png", "karen8.jpg"};
Expand All @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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;

}

16 changes: 13 additions & 3 deletions src/gifio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,

This comment has been minimized.

Copy link
@amitdo

amitdo Aug 26, 2024

Contributor

'more then' -> 'more than'

This comment has been minimized.

Copy link
@DanBloomberg

DanBloomberg Aug 27, 2024

Author Owner

:-)

I'll fix it with the next commit.

* an error message is returned.
*
* </pre>
*/

Expand Down Expand Up @@ -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).
* </pre>
*/
static PIX *
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions version-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h2 align=center> <IMG SRC="moller52.jpg" border=1 ALIGN_MIDDLE> </h2>
* 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,
Expand All @@ -119,7 +119,7 @@ <h2 align=center> <IMG SRC="moller52.jpg" border=1 ALIGN_MIDDLE> </h2>
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,
Expand Down

0 comments on commit 1a4ba7e

Please sign in to comment.