Leptonica 1.68
C Image Processing Library

alphaclean_reg.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * alphaclean_reg.c
00018  *
00019  *    Tests transparency and cleaning under alpha.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include "allheaders.h"
00025 
00026 static const l_int32   SHOW = 0;
00027 
00028 main(int    argc,
00029      char **argv)
00030 {
00031 l_int32      w, h, n1, n2;
00032 PIX         *pixs, *pixb, *pixg, *pixc, *pixd;
00033 PIX         *pixg2, *pixcs1, *pixcs2, *pixd1, *pixd2, *pixt1, *pixt2;
00034 PIXA        *pixa;
00035 static char  mainName[] = "alphaclean_reg";
00036 
00037     if (argc != 1)
00038         exit(ERROR_INT(" Syntax:  alphaclean_reg", mainName, 1));
00039 
00040         /* Make the transparency (alpha) layer.
00041          * pixs is the mask.  We turn it into a transparency (alpha)
00042          * layer by converting to 8 bpp.  A small convolution fuzzes
00043          * the mask edges so that you don't see the pixels. */
00044     pixs = pixRead("feyn-fract.tif");
00045     pixGetDimensions(pixs, &w, &h, NULL);
00046     pixg = pixConvert1To8(NULL, pixs, 0, 255);
00047     pixg2 = pixBlockconvGray(pixg, NULL, 1, 1);
00048     pixDisplayWithTitle(pixg2, 300, 0, "alpha", SHOW);
00049 
00050         /* Make the viewable image.
00051          * pixc is the image that we see where the alpha layer is
00052          * opaque -- i.e., greater than 0.  Scale it to the same
00053          * size as the mask.  To visualize what this will look like
00054          * when displayed over a black background, create the black
00055          * background image, pixb, and do the blending with pixcs1
00056          * explicitly using the alpha layer pixg2. */
00057     pixc = pixRead("tetons.jpg");
00058     pixcs1 = pixScaleToSize(pixc, w, h);
00059     pixDisplayWithTitle(pixcs1, 100, 200, "viewable", SHOW);
00060     pixb = pixCreateTemplate(pixcs1);  /* black */
00061     pixd1 = pixBlendWithGrayMask(pixb, pixcs1, pixg2, 0, 0);
00062     pixDisplayWithTitle(pixd1, 100, 500, "alpha-blended 1", SHOW);
00063 
00064         /* Embed the alpha layer pixg2 into the color image pixc.
00065          * Write it out as is.  Then clean pixcs1 (to 0) under the fully
00066          * transparent part of the alpha layer, and write that result
00067          * out as well. */
00068     pixSetRGBComponent(pixcs1, pixg2, L_ALPHA_CHANNEL);
00069     pixWriteRGBAPng("/tmp/junkpixcs1.png", pixcs1);
00070     pixcs2 = pixSetUnderTransparency(pixcs1, 0, 0);
00071     pixWriteRGBAPng("/tmp/junkpixcs2.png", pixcs2);
00072 
00073         /* What will this look like over a black background?
00074          * Do the blending explicitly and display.  It should
00075          * look identical to the blended result pixd1 before cleaning. */
00076     pixd2 = pixBlendWithGrayMask(pixb, pixcs2, pixg2, 0, 0);
00077     pixDisplayWithTitle(pixd2, 600, 500, "alpha blended 2", SHOW);
00078 
00079         /* Read the two images back, ignoring the transparency layer.
00080          * The uncleaned image will come back identical to pixcs1.
00081          * However, the cleaned image will be black wherever
00082          * the alpha layer was fully transparent.  It will 
00083          * look the same when viewed through the alpha layer,
00084          * but have much better compression. */
00085     pixt1 = pixRead("/tmp/junkpixcs1.png");  /* just pixcs1 */
00086     pixt2 = pixRead("/tmp/junkpixcs2.png");  /* cleaned out under transparent */
00087     n1 = nbytesInFile("/tmp/junkpixcs1.png");
00088     n2 = nbytesInFile("/tmp/junkpixcs2.png");
00089     fprintf(stderr, " Original: %d bytes\n Cleaned: %d bytes\n", n1, n2);
00090     pixDisplayWithTitle(pixt1, 600, 200, "without alpha", SHOW);
00091     pixDisplayWithTitle(pixt2, 300, 800, "cleaned under transparent", SHOW);
00092 
00093     pixa = pixaCreate(0);
00094     pixSaveTiled(pixg2, pixa, 1, 1, 20, 32);
00095     pixSaveTiled(pixcs1, pixa, 1, 1, 20, 0);
00096     pixSaveTiled(pixt1, pixa, 1, 0, 20, 0);
00097     pixSaveTiled(pixd1, pixa, 1, 1, 20, 0);
00098     pixSaveTiled(pixd2, pixa, 1, 0, 20, 0);
00099     pixSaveTiled(pixt2, pixa, 1, 1, 20, 0);
00100     pixd = pixaDisplay(pixa, 0, 0);
00101     pixDisplay(pixd, 100, 100);
00102     pixWrite("/tmp/junkalpha.png", pixd, IFF_JFIF_JPEG);
00103     pixDestroy(&pixd);
00104     pixaDestroy(&pixa);
00105 
00106     pixDestroy(&pixs);
00107     pixDestroy(&pixb);
00108     pixDestroy(&pixg);
00109     pixDestroy(&pixg2);
00110     pixDestroy(&pixc);
00111     pixDestroy(&pixcs1);
00112     pixDestroy(&pixcs2);
00113     pixDestroy(&pixd);
00114     pixDestroy(&pixd1);
00115     pixDestroy(&pixd2);
00116     pixDestroy(&pixt1);
00117     pixDestroy(&pixt2);
00118     return 0;
00119 }
00120 
00121 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines