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 /* 00018 * binexpandlow.c 00019 * 00020 * Low level power-of-2 binary expansion 00021 * l_int32 expandBinaryPower2Low() 00022 * 00023 * Expansion tables 00024 * l_uint16 *makeExpandTab2x() 00025 * l_uint32 *makeExpandTab4x() 00026 * l_uint32 *makeExpandTab8x() 00027 */ 00028 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <string.h> 00032 #include "allheaders.h" 00033 00034 00035 static l_uint32 expandtab16[] = { 00036 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff}; 00037 00038 00039 /*-------------------------------------------------------------------* 00040 * Low level power-of-2 binary expansion * 00041 *-------------------------------------------------------------------*/ 00042 /*! 00043 * expandBinaryPower2Low() 00044 */ 00045 l_int32 00046 expandBinaryPower2Low(l_uint32 *datad, 00047 l_int32 wd, 00048 l_int32 hd, 00049 l_int32 wpld, 00050 l_uint32 *datas, 00051 l_int32 ws, 00052 l_int32 hs, 00053 l_int32 wpls, 00054 l_int32 factor) 00055 { 00056 l_int32 i, j, k, sdibits, sqbits, sbytes; 00057 l_uint8 sval; 00058 l_uint16 *tab2; 00059 l_uint32 *tab4, *tab8; 00060 l_uint32 *lines, *lined; 00061 00062 PROCNAME("expandBinaryPower2Low"); 00063 00064 switch (factor) 00065 { 00066 case 2: 00067 if ((tab2 = makeExpandTab2x()) == NULL) 00068 return ERROR_INT("tab2 not made", procName, 1); 00069 sbytes = (ws + 7) / 8; 00070 for (i = 0; i < hs; i++) { 00071 lines = datas + i * wpls; 00072 lined = datad + 2 * i * wpld; 00073 for (j = 0; j < sbytes; j++) { 00074 sval = GET_DATA_BYTE(lines, j); 00075 SET_DATA_TWO_BYTES(lined, j, tab2[sval]); 00076 } 00077 memcpy((char *)(lined + wpld), (char *)lined, 4 * wpld); 00078 } 00079 FREE(tab2); 00080 break; 00081 case 4: 00082 if ((tab4 = makeExpandTab4x()) == NULL) 00083 return ERROR_INT("tab4 not made", procName, 1); 00084 sbytes = (ws + 7) / 8; 00085 for (i = 0; i < hs; i++) { 00086 lines = datas + i * wpls; 00087 lined = datad + 4 * i * wpld; 00088 for (j = 0; j < sbytes; j++) { 00089 sval = GET_DATA_BYTE(lines, j); 00090 lined[j] = tab4[sval]; 00091 } 00092 for (k = 1; k < 4; k++) 00093 memcpy((char *)(lined + k * wpld), (char *)lined, 4 * wpld); 00094 } 00095 FREE(tab4); 00096 break; 00097 case 8: 00098 if ((tab8 = makeExpandTab8x()) == NULL) 00099 return ERROR_INT("tab8 not made", procName, 1); 00100 sqbits = (ws + 3) / 4; 00101 for (i = 0; i < hs; i++) { 00102 lines = datas + i * wpls; 00103 lined = datad + 8 * i * wpld; 00104 for (j = 0; j < sqbits; j++) { 00105 sval = GET_DATA_QBIT(lines, j); 00106 if (sval > 15) 00107 L_WARNING_INT("sval = %d; should be < 16", procName, sval); 00108 lined[j] = tab8[sval]; 00109 } 00110 for (k = 1; k < 8; k++) 00111 memcpy((char *)(lined + k * wpld), (char *)lined, 4 * wpld); 00112 } 00113 FREE(tab8); 00114 break; 00115 case 16: 00116 sdibits = (ws + 1) / 2; 00117 for (i = 0; i < hs; i++) { 00118 lines = datas + i * wpls; 00119 lined = datad + 16 * i * wpld; 00120 for (j = 0; j < sdibits; j++) { 00121 sval = GET_DATA_DIBIT(lines, j); 00122 lined[j] = expandtab16[sval]; 00123 } 00124 for (k = 1; k < 16; k++) 00125 memcpy((char *)(lined + k * wpld), (char *)lined, 4 * wpld); 00126 } 00127 break; 00128 default: 00129 return ERROR_INT("expansion factor not in {2,4,8,16}", procName, 1); 00130 } 00131 00132 return 0; 00133 } 00134 00135 00136 00137 /*-------------------------------------------------------------------* 00138 * Expansion tables for 2x, 4x and 8x expansion * 00139 *-------------------------------------------------------------------*/ 00140 l_uint16 * 00141 makeExpandTab2x(void) 00142 { 00143 l_uint16 *tab; 00144 l_int32 i; 00145 00146 PROCNAME("makeExpandTab2x"); 00147 00148 if ((tab = (l_uint16 *) CALLOC(256, sizeof(l_uint16))) == NULL) 00149 return (l_uint16 *)ERROR_PTR("tab not made", procName, NULL); 00150 00151 for (i = 0; i < 256; i++) { 00152 if (i & 0x01) 00153 tab[i] = 0x3; 00154 if (i & 0x02) 00155 tab[i] |= 0xc; 00156 if (i & 0x04) 00157 tab[i] |= 0x30; 00158 if (i & 0x08) 00159 tab[i] |= 0xc0; 00160 if (i & 0x10) 00161 tab[i] |= 0x300; 00162 if (i & 0x20) 00163 tab[i] |= 0xc00; 00164 if (i & 0x40) 00165 tab[i] |= 0x3000; 00166 if (i & 0x80) 00167 tab[i] |= 0xc000; 00168 } 00169 00170 return tab; 00171 } 00172 00173 00174 l_uint32 * 00175 makeExpandTab4x(void) 00176 { 00177 l_uint32 *tab; 00178 l_int32 i; 00179 00180 PROCNAME("makeExpandTab4x"); 00181 00182 if ((tab = (l_uint32 *) CALLOC(256, sizeof(l_uint32))) == NULL) 00183 return (l_uint32 *)ERROR_PTR("tab not made", procName, NULL); 00184 00185 for (i = 0; i < 256; i++) { 00186 if (i & 0x01) 00187 tab[i] = 0xf; 00188 if (i & 0x02) 00189 tab[i] |= 0xf0; 00190 if (i & 0x04) 00191 tab[i] |= 0xf00; 00192 if (i & 0x08) 00193 tab[i] |= 0xf000; 00194 if (i & 0x10) 00195 tab[i] |= 0xf0000; 00196 if (i & 0x20) 00197 tab[i] |= 0xf00000; 00198 if (i & 0x40) 00199 tab[i] |= 0xf000000; 00200 if (i & 0x80) 00201 tab[i] |= 0xf0000000; 00202 } 00203 00204 return tab; 00205 } 00206 00207 00208 l_uint32 * 00209 makeExpandTab8x(void) 00210 { 00211 l_uint32 *tab; 00212 l_int32 i; 00213 00214 PROCNAME("makeExpandTab8x"); 00215 00216 if ((tab = (l_uint32 *) CALLOC(16, sizeof(l_uint32))) == NULL) 00217 return (l_uint32 *)ERROR_PTR("tab not made", procName, NULL); 00218 00219 for (i = 0; i < 16; i++) { 00220 if (i & 0x01) 00221 tab[i] = 0xff; 00222 if (i & 0x02) 00223 tab[i] |= 0xff00; 00224 if (i & 0x04) 00225 tab[i] |= 0xff0000; 00226 if (i & 0x08) 00227 tab[i] |= 0xff000000; 00228 } 00229 00230 return tab; 00231 } 00232