Leptonica 1.68
C Image Processing Library

blend_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  * blend_reg.c
00018  *
00019  *   Regression test for these functions:
00020  *       pixBlendGray()
00021  *       pixBlendGrayAdapt()
00022  *       pixBlendColor()
00023  */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "allheaders.h"
00028 
00029 #define  DISPLAY   0
00030 
00031 void GrayBlend(PIX  *pixs, PIX  *pixb, l_int32 op, l_float32 fract);
00032 void AdaptiveGrayBlend(PIX  *pixs, PIX  *pixb, l_float32 fract);
00033 void ColorBlend(PIX  *pixs, PIX  *pixb, l_float32 fract);
00034 PIX *MakeGrayWash(l_int32 w, l_int32 h);
00035 PIX *MakeColorWash(l_int32 w, l_int32 h, l_int32 color);
00036 
00037 main(int    argc,
00038      char **argv)
00039 {
00040 PIX         *pixs, *pixg, *pixc, *pixt, *pixd;
00041 PIXA        *pixa;
00042 static char  mainName[] = "blend_reg";
00043 
00044     pixg = pixRead("blender8.png");
00045     pixt = pixRead("weasel4.11c.png");
00046     pixc = pixRemoveColormap(pixt, REMOVE_CMAP_TO_FULL_COLOR);
00047     pixDisplayWrite(NULL, -1);
00048     pixa = pixaCreate(0);
00049 
00050 #if 1
00051         /* Gray blend (straight) */
00052     pixs = pixRead("test24.jpg");
00053     GrayBlend(pixs, pixg, L_BLEND_GRAY, 0.3);
00054     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00055     pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY);
00056     pixDestroy(&pixs);
00057     pixs = pixRead("marge.jpg");
00058     GrayBlend(pixs, pixg, L_BLEND_GRAY, 0.2);
00059     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00060     pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY);
00061     pixDestroy(&pixs);
00062     pixs = pixRead("marge.jpg");
00063     pixt = pixConvertRGBToLuminance(pixs);
00064     GrayBlend(pixt, pixg, L_BLEND_GRAY, 0.2);
00065     pixSaveTiled(pixt, pixa, 1, 0, 20, 32);
00066     pixDisplayWithTitle(pixt, 100, 100, NULL, DISPLAY);
00067     pixDestroy(&pixs);
00068     pixDestroy(&pixt);
00069 #endif
00070 
00071 #if 1
00072         /* Gray blend (inverse) */
00073     pixs = pixRead("test24.jpg");
00074     GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6);
00075     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00076     pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY);
00077     pixDestroy(&pixs);
00078     pixs = pixRead("marge.jpg");
00079     GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6);
00080     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00081     pixDisplayWithTitle(pixs, 100, 100, NULL, DISPLAY);
00082     pixDestroy(&pixs);
00083     pixs = pixRead("marge.jpg");
00084     pixt = pixConvertRGBToLuminance(pixs);
00085     GrayBlend(pixt, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.6);
00086     pixSaveTiled(pixt, pixa, 1, 0, 20, 32);
00087     pixDisplayWithTitle(pixt, 100, 100, NULL, DISPLAY);
00088     pixDestroy(&pixs);
00089     pixDestroy(&pixt);
00090     pixs = MakeGrayWash(1000, 120);
00091     GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 0.3);
00092     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00093     pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY);
00094     pixDestroy(&pixs);
00095     pixs = MakeColorWash(1000, 120, COLOR_RED);
00096     GrayBlend(pixs, pixg, L_BLEND_GRAY_WITH_INVERSE, 1.0);
00097     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00098     pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY);
00099     pixDestroy(&pixs);
00100 #endif
00101 
00102 #if 1
00103         /* Adaptive gray blend */
00104     pixs = pixRead("test24.jpg");
00105     AdaptiveGrayBlend(pixs, pixg, 0.8);
00106     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00107     pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY);
00108     pixDestroy(&pixs);
00109     pixs = pixRead("marge.jpg");
00110     AdaptiveGrayBlend(pixs, pixg, 0.8);
00111     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00112     pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY);
00113     pixt = pixConvertRGBToLuminance(pixs);
00114     AdaptiveGrayBlend(pixt, pixg, 0.1);
00115     pixSaveTiled(pixt, pixa, 1, 0, 20, 32);
00116     pixDisplayWithTitle(pixt, 200, 100, NULL, DISPLAY);
00117     pixDestroy(&pixs);
00118     pixDestroy(&pixt);
00119     pixs = MakeGrayWash(1000, 120);
00120     AdaptiveGrayBlend(pixs, pixg, 0.3);
00121     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00122     pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY);
00123     pixDestroy(&pixs);
00124     pixs = MakeColorWash(1000, 120, COLOR_RED);
00125     AdaptiveGrayBlend(pixs, pixg, 0.5);
00126     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00127     pixDisplayWithTitle(pixs, 200, 100, NULL, DISPLAY);
00128     pixDestroy(&pixs);
00129 #endif
00130 
00131 #if 1
00132         /* Color blend */
00133     pixs = pixRead("test24.jpg");
00134     ColorBlend(pixs, pixc, 0.3);
00135     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00136     pixDisplayWithTitle(pixs, 300, 100, NULL, DISPLAY);
00137     pixDestroy(&pixs);
00138     pixs = pixRead("marge.jpg");
00139     ColorBlend(pixs, pixc, 0.30);
00140     pixSaveTiled(pixs, pixa, 1, 1, 20, 32);
00141     pixDisplayWithTitle(pixs, 300, 100, NULL, DISPLAY);
00142     pixDestroy(&pixs);
00143     pixs = pixRead("marge.jpg");
00144     ColorBlend(pixs, pixc, 0.15);
00145     pixSaveTiled(pixs, pixa, 1, 0, 20, 32);
00146     pixDisplayWithTitle(pixs, 300, 100, NULL, DISPLAY);
00147     pixDestroy(&pixs);
00148 #endif
00149 
00150         /* Display results */
00151     pixd = pixaDisplay(pixa, 0, 0);
00152     pixDisplay(pixd, 100, 100);
00153     pixWrite("/tmp/junkblend.jpg", pixd, IFF_JFIF_JPEG);
00154     pixDestroy(&pixd);
00155     pixaDestroy(&pixa);
00156 
00157     pixDisplayMultiple("/tmp/junk_write_display*");
00158 
00159     pixDestroy(&pixg);
00160     pixDestroy(&pixt);
00161     pixDestroy(&pixc);
00162     return 0;
00163 }
00164 
00165 
00166 void
00167 GrayBlend(PIX       *pixs,
00168           PIX       *pixb,
00169           l_int32    op,
00170           l_float32  fract)
00171 {
00172 l_int32   i, j, wb, hb, ws, hs, delx, dely, x, y;
00173 
00174     pixGetDimensions(pixs, &ws, &hs, NULL);
00175     pixGetDimensions(pixb, &wb, &hb, NULL);
00176     delx = wb + 30;
00177     dely = hb + 25;
00178     x = 200;
00179     y = 300;
00180     for (i = 0; i < 20; i++) {
00181         y = 20 + i * dely;
00182         if (y >= hs - hb)
00183             continue;
00184         for (j = 0; j < 20; j++) {
00185             x = 30 + j * delx;
00186             if (x >= ws - wb)
00187                 continue;
00188             pixBlendGray(pixs, pixs, pixb, x, y, fract, op, 1, 255);
00189         }
00190     }
00191     pixDisplayWriteFormat(pixs, 1, IFF_PNG);
00192 }
00193 
00194 
00195 void
00196 AdaptiveGrayBlend(PIX       *pixs,
00197                   PIX       *pixb,
00198                   l_float32  fract)
00199 {
00200 l_int32   i, j, wb, hb, ws, hs, delx, dely, x, y;
00201 
00202     pixGetDimensions(pixs, &ws, &hs, NULL);
00203     pixGetDimensions(pixb, &wb, &hb, NULL);
00204     delx = wb + 30;
00205     dely = hb + 25;
00206     x = 200;
00207     y = 300;
00208     for (i = 0; i < 20; i++) {
00209         y = 20 + i * dely;
00210         if (y >= hs - hb)
00211             continue;
00212         for (j = 0; j < 20; j++) {
00213             x = 30 + j * delx;
00214             if (x >= ws - wb)
00215                 continue;
00216             pixBlendGrayAdapt(pixs, pixs, pixb, x, y, fract, 80);
00217         }
00218     }
00219     pixDisplayWriteFormat(pixs, 1, IFF_PNG);
00220 }
00221 
00222 
00223 void
00224 ColorBlend(PIX       *pixs,
00225            PIX       *pixb,
00226            l_float32  fract)
00227 {
00228 l_int32   i, j, wb, hb, ws, hs, delx, dely, x, y;
00229 
00230     pixGetDimensions(pixs, &ws, &hs, NULL);
00231     pixGetDimensions(pixb, &wb, &hb, NULL);
00232     delx = wb + 30;
00233     dely = hb + 25;
00234     x = 200;
00235     y = 300;
00236     for (i = 0; i < 20; i++) {
00237         y = 20 + i * dely;
00238         if (y >= hs - hb)
00239             continue;
00240         for (j = 0; j < 20; j++) {
00241             x = 30 + j * delx;
00242             if (x >= ws - wb)
00243                 continue;
00244             pixBlendColor(pixs, pixs, pixb, x, y, fract, 1, 255);
00245         }
00246     }
00247     pixDisplayWriteFormat(pixs, 1, IFF_PNG);
00248 }
00249 
00250 
00251 PIX *
00252 MakeGrayWash(l_int32  w,
00253              l_int32  h)
00254 {
00255 l_int32    i, j, wpl, val;
00256 l_uint32  *data, *line;
00257 PIX       *pixd;
00258 
00259     pixd = pixCreate(w, h, 8);
00260     data = pixGetData(pixd);
00261     wpl = pixGetWpl(pixd);
00262     for (i = 0; i < h; i++) {
00263         line = data + i * wpl;
00264         for (j = 0; j < w; j++) {
00265             val = (j * 255) / w;
00266             SET_DATA_BYTE(line, j, val);
00267         }
00268     }
00269     return pixd;
00270 }
00271 
00272 
00273 PIX *
00274 MakeColorWash(l_int32  w,
00275               l_int32  h,
00276               l_int32  color)
00277 {
00278 l_int32    i, j, wpl;
00279 l_uint32   val;
00280 l_uint32  *data, *line;
00281 PIX       *pixd;
00282 
00283     pixd = pixCreate(w, h, 32);
00284     data = pixGetData(pixd);
00285     wpl = pixGetWpl(pixd);
00286     for (i = 0; i < h; i++) {
00287         line = data + i * wpl;
00288         for (j = 0; j < w; j++) {
00289             if (color == COLOR_RED)
00290                 val = ((j * 255) / w) << L_GREEN_SHIFT |
00291                       ((j * 255) / w) << L_BLUE_SHIFT |
00292                       255 << L_RED_SHIFT;
00293             else if (color == COLOR_GREEN)
00294                 val = ((j * 255) / w) << L_RED_SHIFT |
00295                       ((j * 255) / w) << L_BLUE_SHIFT |
00296                       255 << L_GREEN_SHIFT;
00297             else
00298                 val = ((j * 255) / w) << L_RED_SHIFT |
00299                       ((j * 255) / w) << L_GREEN_SHIFT |
00300                       255 << L_BLUE_SHIFT;
00301             line[j] = val;
00302         }
00303     }
00304     return pixd;
00305 }
00306 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines