Leptonica 1.68
C Image Processing Library
|
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 * blend2_reg.c 00018 * 00019 * Regression test for this function: 00020 * pixBlendWithGrayMask() 00021 */ 00022 00023 #include <stdio.h> 00024 #include <stdlib.h> 00025 #include "allheaders.h" 00026 00027 00028 main(int argc, 00029 char **argv) 00030 { 00031 l_int32 i, j, w1, h1, w2, h2, w, h, same; 00032 BOX *box1, *box2; 00033 PIX *pixs, *pixs1, *pixs2, *pix1, *pix2; 00034 PIX *pixg, *pixg1, *pixg2, *pixc2, *pixbl, *pixd; 00035 PIXA *pixa; 00036 static char mainName[] = "blend2_reg"; 00037 00038 /* --- Set up the 8 bpp blending image --- */ 00039 pixg = pixCreate(660, 500, 8); 00040 for (i = 0; i < 500; i++) 00041 for (j = 0; j < 660; j++) 00042 pixSetPixel(pixg, j, i, (l_int32)(0.775 * j) % 256); 00043 00044 /* --- Set up the initial color images to be blended together --- */ 00045 pixs1 = pixRead("wyom.jpg"); 00046 pixs2 = pixRead("fish24.jpg"); 00047 pixGetDimensions(pixs1, &w1, &h1, NULL); 00048 pixGetDimensions(pixs2, &w2, &h2, NULL); 00049 h = L_MIN(h1, h2); 00050 w = L_MIN(w1, w2); 00051 box1 = boxCreate(0, 0, w1, h1); 00052 box2 = boxCreate(0, 300, 660, 500); 00053 pix1 = pixClipRectangle(pixs1, box1, NULL); 00054 pix2 = pixClipRectangle(pixs2, box2, NULL); 00055 pixDestroy(&pixs1); 00056 pixDestroy(&pixs2); 00057 boxDestroy(&box1); 00058 boxDestroy(&box2); 00059 00060 /* --- Blend 2 rgb images --- */ 00061 pixa = pixaCreate(0); 00062 pixSaveTiled(pixg, pixa, 1, 1, 40, 32); 00063 pixd = pixBlendWithGrayMask(pix1, pix2, pixg, 50, 50); 00064 pixSaveTiled(pix1, pixa, 1, 1, 40, 32); 00065 pixSaveTiled(pix2, pixa, 1, 0, 40, 32); 00066 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00067 pixDestroy(&pixd); 00068 00069 /* --- Blend 2 grayscale images --- */ 00070 pixg1 = pixConvertRGBToLuminance(pix1); 00071 pixg2 = pixConvertRGBToLuminance(pix2); 00072 pixd = pixBlendWithGrayMask(pixg1, pixg2, pixg, 50, 50); 00073 pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); 00074 pixSaveTiled(pixg2, pixa, 1, 0, 40, 32); 00075 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00076 pixDestroy(&pixg1); 00077 pixDestroy(&pixg2); 00078 pixDestroy(&pixd); 00079 00080 /* --- Blend a colormap image and an rgb image --- */ 00081 pixc2 = pixFixedOctcubeQuantGenRGB(pix2, 2); 00082 pixd = pixBlendWithGrayMask(pix1, pixc2, pixg, 50, 50); 00083 pixSaveTiled(pix1, pixa, 1, 1, 40, 32); 00084 pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); 00085 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00086 pixDestroy(&pixc2); 00087 pixDestroy(&pixd); 00088 00089 /* --- Blend a colormap image and a grayscale image --- */ 00090 pixg1 = pixConvertRGBToLuminance(pix1); 00091 pixc2 = pixFixedOctcubeQuantGenRGB(pix2, 2); 00092 pixd = pixBlendWithGrayMask(pixg1, pixc2, pixg, 50, 50); 00093 pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); 00094 pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); 00095 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00096 pixDestroy(&pixd); 00097 pixd = pixBlendWithGrayMask(pixg1, pixc2, pixg, -100, -100); 00098 pixSaveTiled(pixg1, pixa, 1, 1, 40, 32); 00099 pixSaveTiled(pixc2, pixa, 1, 0, 40, 32); 00100 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00101 pixDestroy(&pixd); 00102 pixDestroy(&pixg1); 00103 pixDestroy(&pixc2); 00104 00105 /* --- Test png read/write with alpha channel --- */ 00106 /* First make pixs1, using pixg as the alpha channel */ 00107 pixs = pixRead("fish24.jpg"); 00108 box1 = boxCreate(0, 300, 660, 500); 00109 pixs1 = pixClipRectangle(pixs, box1, NULL); 00110 pixSaveTiled(pixs1, pixa, 1, 1, 40, 32); 00111 pixSetRGBComponent(pixs1, pixg, L_ALPHA_CHANNEL); 00112 /* To see the alpha channel, blend with a black image */ 00113 pixbl = pixCreate(660, 500, 32); 00114 pixd = pixBlendWithGrayMask(pixbl, pixs1, NULL, 0, 0); 00115 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00116 pixDestroy(&pixd); 00117 /* Write out the RGBA image and read it back */ 00118 l_pngSetWriteAlpha(1); 00119 pixWrite("/tmp/junkpixs1.png", pixs1, IFF_PNG); 00120 l_pngSetStripAlpha(0); 00121 pixs2 = pixRead("/tmp/junkpixs1.png"); 00122 /* Make sure that the alpha channel image hasn't changed */ 00123 pixg2 = pixGetRGBComponent(pixs2, L_ALPHA_CHANNEL); 00124 pixEqual(pixg, pixg2, &same); 00125 if (same) 00126 fprintf(stderr, "PNG with alpha read/write OK\n"); 00127 else 00128 fprintf(stderr, "PNG with alpha read/write failed\n"); 00129 /* Blend again with a black image */ 00130 pixd = pixBlendWithGrayMask(pixbl, pixs2, NULL, 0, 0); 00131 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00132 pixDestroy(&pixd); 00133 /* Blend with a white image */ 00134 pixSetAll(pixbl); 00135 pixd = pixBlendWithGrayMask(pixbl, pixs2, NULL, 0, 0); 00136 pixSaveTiled(pixd, pixa, 1, 0, 40, 32); 00137 pixDestroy(&pixd); 00138 l_pngSetWriteAlpha(0); /* reset to default */ 00139 l_pngSetStripAlpha(1); /* reset to default */ 00140 pixDestroy(&pixbl); 00141 pixDestroy(&pixs); 00142 pixDestroy(&pixs1); 00143 pixDestroy(&pixs2); 00144 pixDestroy(&pixg2); 00145 boxDestroy(&box1); 00146 00147 /* --- Display results --- */ 00148 pixd = pixaDisplay(pixa, 0, 0); 00149 pixDisplay(pixd, 100, 100); 00150 pixWrite("/tmp/junkblend2.jpg", pixd, IFF_JFIF_JPEG); 00151 pixDestroy(&pixd); 00152 pixaDestroy(&pixa); 00153 00154 pixDestroy(&pixg); 00155 pixDestroy(&pix1); 00156 pixDestroy(&pix2); 00157 return 0; 00158 } 00159