Leptonica 1.68
C Image Processing Library

binexpand.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  *  binexpand.c
00018  *
00019  *      Replicated expansion (integer scaling)
00020  *         PIX     *pixExpandBinaryReplicate()
00021  *
00022  *      Special case: power of 2 replicated expansion
00023  *         PIX     *pixExpandBinaryPower2()
00024  */
00025 
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <stdlib.h>
00029 #include "allheaders.h"
00030 
00031 
00032 /*------------------------------------------------------------------*
00033  *              Replicated expansion (integer scaling)              *
00034  *------------------------------------------------------------------*/
00035 /*!
00036  *  pixExpandBinaryReplicate()
00037  *
00038  *      Input:  pixs (1 bpp)
00039  *              factor (integer scale factor for replicative expansion)
00040  *      Return: pixd (scaled up), or null on error
00041  */
00042 PIX *
00043 pixExpandBinaryReplicate(PIX     *pixs,
00044                          l_int32  factor)
00045 {
00046 l_int32    w, h, d, wd, hd, wpls, wpld, i, j, k, start;
00047 l_uint32  *datas, *datad, *lines, *lined;
00048 PIX       *pixd;
00049 
00050     PROCNAME("pixExpandBinaryReplicate");
00051 
00052     if (!pixs)
00053         return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
00054     pixGetDimensions(pixs, &w, &h, &d);
00055     if (d != 1)
00056         return (PIX *)ERROR_PTR("pixs not binary", procName, NULL);
00057     if (factor <= 0)
00058         return (PIX *)ERROR_PTR("factor <= 0; invalid", procName, NULL);
00059 
00060     if (factor == 1)
00061         return pixCopy(NULL, pixs);
00062     if (factor == 2 || factor == 4 || factor == 8 || factor == 16)
00063         return pixExpandBinaryPower2(pixs, factor);
00064 
00065     wpls = pixGetWpl(pixs);
00066     datas = pixGetData(pixs);
00067     wd = factor * w;
00068     hd = factor * h;
00069     if ((pixd = pixCreate(wd, hd, 1)) == NULL)
00070         return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
00071     pixCopyResolution(pixd, pixs);
00072     pixScaleResolution(pixd, (l_float32)factor, (l_float32)factor);
00073     wpld = pixGetWpl(pixd);
00074     datad = pixGetData(pixd);
00075 
00076     for (i = 0; i < h; i++) {
00077         lines = datas + i * wpls;
00078         lined = datad + factor * i * wpld;
00079         for (j = 0; j < w; j++) {
00080             if (GET_DATA_BIT(lines, j)) {
00081                 start = factor * j;
00082                 for (k = 0; k < factor; k++)
00083                     SET_DATA_BIT(lined, start + k);
00084             }
00085         }
00086         for (k = 1; k < factor; k++)
00087             memcpy(lined + k * wpld, lined, 4 * wpld);
00088     }
00089 
00090     return pixd;
00091 }
00092 
00093 
00094 /*------------------------------------------------------------------*
00095  *                      Power of 2 expansion                        *
00096  *------------------------------------------------------------------*/
00097 /*!
00098  *  pixExpandBinaryPower2()
00099  *
00100  *      Input:  pixs (1 bpp)
00101  *              factor (expansion factor: 1, 2, 4, 8, 16)
00102  *      Return: pixd (expanded 1 bpp by replication), or null on error
00103  */
00104 PIX *
00105 pixExpandBinaryPower2(PIX     *pixs,
00106                       l_int32  factor)
00107 {
00108 l_int32    w, h, d, wd, hd, wpls, wpld;
00109 l_uint32  *datas, *datad;
00110 PIX       *pixd;
00111 
00112     PROCNAME("pixExpandBinaryPower2");
00113 
00114     if (!pixs)
00115         return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
00116     pixGetDimensions(pixs, &w, &h, &d);
00117     if (d != 1)
00118         return (PIX *)ERROR_PTR("pixs not binary", procName, NULL);
00119     if (factor == 1)
00120         return pixCopy(NULL, pixs);
00121     if (factor != 2 && factor != 4 && factor != 8 && factor != 16)
00122         return (PIX *)ERROR_PTR("factor must be in {2,4,8,16}", procName, NULL);
00123 
00124     wpls = pixGetWpl(pixs);
00125     datas = pixGetData(pixs);
00126     wd = factor * w;
00127     hd = factor * h;
00128     if ((pixd = pixCreate(wd, hd, 1)) == NULL)
00129         return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
00130     pixCopyResolution(pixd, pixs);
00131     pixScaleResolution(pixd, (l_float32)factor, (l_float32)factor);
00132     wpld = pixGetWpl(pixd);
00133     datad = pixGetData(pixd);
00134 
00135     expandBinaryPower2Low(datad, wd, hd, wpld, datas, w, h, wpls, factor);
00136 
00137     return pixd;
00138 }
00139 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines