| 1 | /* |
| 2 | * |
| 3 | * Softweave calculator for Gutenprint. |
| 4 | * |
| 5 | * Copyright 2000 Charles Briscoe-Smith <[email protected]> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the Free |
| 9 | * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This file must include only standard C header files. The core code must |
| 23 | * compile on generic platforms that don't support glib, gimp, gtk, etc. |
| 24 | */ |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_H |
| 27 | #include <config.h> |
| 28 | #endif |
| 29 | #include <string.h> |
| 30 | #include <gutenprint/gutenprint.h> |
| 31 | #include "gutenprint-internal.h" |
| 32 | #include <gutenprint/gutenprint-intl-internal.h> |
| 33 | #ifdef HAVE_LIMITS_H |
| 34 | #include <limits.h> |
| 35 | #endif |
| 36 | |
| 37 | #ifdef __GNUC__ |
| 38 | #define inline __inline__ |
| 39 | #define NOINLINE __attribute__ ((noinline)) |
| 40 | #else |
| 41 | $define NOINLINE |
| 42 | #endif |
| 43 | |
| 44 | void |
| 45 | stp_fold(const unsigned char *line, |
| 46 | int single_length, |
| 47 | unsigned char *outbuf) |
| 48 | { |
| 49 | int i; |
| 50 | memset(outbuf, 0, single_length * 2); |
| 51 | for (i = 0; i < single_length; i++) |
| 52 | { |
| 53 | unsigned char l0 = line[0]; |
| 54 | unsigned char l1 = line[single_length]; |
| 55 | if (l0 || l1) |
| 56 | { |
| 57 | outbuf[0] = /* B7 A7 B6 A6 B5 A5 B4 A4 */ |
| 58 | ((l0 & (1 << 7)) >> 1) + |
| 59 | ((l0 & (1 << 6)) >> 2) + |
| 60 | ((l0 & (1 << 5)) >> 3) + |
| 61 | ((l0 & (1 << 4)) >> 4) + |
| 62 | ((l1 & (1 << 7)) >> 0) + |
| 63 | ((l1 & (1 << 6)) >> 1) + |
| 64 | ((l1 & (1 << 5)) >> 2) + |
| 65 | ((l1 & (1 << 4)) >> 3); |
| 66 | outbuf[1] = /* B3 A3 B2 A2 B1 A1 B0 A0 */ |
| 67 | ((l0 & (1 << 3)) << 3) + |
| 68 | ((l0 & (1 << 2)) << 2) + |
| 69 | ((l0 & (1 << 1)) << 1) + |
| 70 | ((l0 & (1 << 0)) << 0) + |
| 71 | ((l1 & (1 << 3)) << 4) + |
| 72 | ((l1 & (1 << 2)) << 3) + |
| 73 | ((l1 & (1 << 1)) << 2) + |
| 74 | ((l1 & (1 << 0)) << 1); |
| 75 | } |
| 76 | line++; |
| 77 | outbuf += 2; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | stp_fold_3bit(const unsigned char *line, |
| 83 | int single_length, |
| 84 | unsigned char *outbuf) |
| 85 | { |
| 86 | int i; |
| 87 | memset(outbuf, 0, single_length * 3); |
| 88 | for (i = 0; i < single_length; i++) |
| 89 | { |
| 90 | unsigned char l0 = line[0]; |
| 91 | unsigned char l1 = line[single_length]; |
| 92 | unsigned char l2 = line[single_length * 2]; |
| 93 | if (l0 || l1 || l2) |
| 94 | { |
| 95 | outbuf[0] = /* C7 B7 A7 C6 B6 A6 C5 B5 */ |
| 96 | ((l0 & (1 << 7)) >> 2) | |
| 97 | ((l0 & (1 << 6)) >> 4) | |
| 98 | ((l1 & (1 << 7)) >> 1) | |
| 99 | ((l1 & (1 << 6)) >> 3) | |
| 100 | ((l1 & (1 << 5)) >> 5) | |
| 101 | ((l2 & (1 << 7)) << 0) | |
| 102 | ((l2 & (1 << 6)) >> 2) | |
| 103 | ((l2 & (1 << 5)) >> 4); |
| 104 | outbuf[1] = /* A5 C4 B4 A4 C3 B3 A3 C2 */ |
| 105 | ((l0 & (1 << 5)) << 2) | |
| 106 | ((l0 & (1 << 4)) << 0) | |
| 107 | ((l0 & (1 << 3)) >> 2) | |
| 108 | ((l1 & (1 << 4)) << 1) | |
| 109 | ((l1 & (1 << 3)) >> 1) | |
| 110 | ((l2 & (1 << 4)) << 2) | |
| 111 | ((l2 & (1 << 3)) << 0) | |
| 112 | ((l2 & (1 << 2)) >> 2); |
| 113 | outbuf[2] = /* B2 A2 C1 B1 A1 C0 B0 A0 */ |
| 114 | ((l0 & (1 << 2)) << 4) | |
| 115 | ((l0 & (1 << 1)) << 2) | |
| 116 | ((l0 & (1 << 0)) << 0) | |
| 117 | ((l1 & (1 << 2)) << 5) | |
| 118 | ((l1 & (1 << 1)) << 3) | |
| 119 | ((l1 & (1 << 0)) << 1) | |
| 120 | ((l2 & (1 << 1)) << 4) | |
| 121 | ((l2 & (1 << 0)) << 2); |
| 122 | } |
| 123 | line++; |
| 124 | outbuf += 3; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | stp_fold_3bit_323(const unsigned char *line, |
| 130 | int single_length, |
| 131 | unsigned char *outbuf) |
| 132 | { |
| 133 | const unsigned char *last= line + single_length; |
| 134 | memset(outbuf, 0, single_length * 3); |
| 135 | for (; line < last; line += 3) |
| 136 | { |
| 137 | unsigned char A0 = line[0]; |
| 138 | unsigned char B0 = line[single_length]; |
| 139 | unsigned char C0 = line[2*single_length]; |
| 140 | unsigned char A1 = (line < last - 2) ? line[1] : 0; |
| 141 | unsigned char B1 = (line < last - 2) ? line[single_length + 1] : 0; |
| 142 | unsigned char C1 = (line < last - 2) ? line[(single_length * 2) + 1] : 0; |
| 143 | unsigned char A2 = (line < last - 1) ? line[2] : 0; |
| 144 | unsigned char B2 = (line < last - 1) ? line[single_length + 2] : 0; |
| 145 | unsigned char C2 = (line < last - 1) ? line[(single_length * 2) + 2] : 0; |
| 146 | |
| 147 | if (A0 || A1 || A2 || B0 || B1 || B2 || C0 || C1 || C2) |
| 148 | { |
| 149 | /* Missing: C0_6, C0_3, C0_0, C1_5, C1_2, C2_7, C2_4, C2_1 ??? */ |
| 150 | outbuf[0] = /* C0_7 B0_7 A0_7 B0_6 A0_6 C0_5 B0_5 A0_5 */ |
| 151 | ((C0 & (1 << 7)) >> 0) | |
| 152 | ((B0 & (1 << 7)) >> 1) | |
| 153 | ((A0 & (1 << 7)) >> 2) | |
| 154 | ((B0 & (1 << 6)) >> 2) | |
| 155 | ((A0 & (1 << 6)) >> 3) | |
| 156 | ((C0 & (1 << 5)) >> 3) | |
| 157 | ((B0 & (1 << 5)) >> 4) | |
| 158 | ((A0 & (1 << 5)) >> 5); |
| 159 | outbuf[1] = /* C0_4 B0_4 A0_4 B0_3 A0_3 C0_2 B0_2 A0_2 */ |
| 160 | ((C0 & (1 << 4)) << 3) | |
| 161 | ((B0 & (1 << 4)) << 2) | |
| 162 | ((A0 & (1 << 4)) << 1) | |
| 163 | ((B0 & (1 << 3)) << 1) | |
| 164 | ((A0 & (1 << 3)) << 0) | |
| 165 | ((C0 & (1 << 2)) >> 0) | |
| 166 | ((B0 & (1 << 2)) >> 1) | |
| 167 | ((A0 & (1 << 2)) >> 2); |
| 168 | outbuf[2] = /* C0_1 B0_1 A0_1 B0_0 A0_0 C1_7 B1_7 A1_7 */ |
| 169 | ((C0 & (1 << 1)) << 6) | |
| 170 | ((B0 & (1 << 1)) << 5) | |
| 171 | ((A0 & (1 << 1)) << 4) | |
| 172 | ((B0 & (1 << 0)) << 4) | |
| 173 | ((A0 & (1 << 0)) << 3) | |
| 174 | ((C1 & (1 << 7)) >> 5) | |
| 175 | ((B1 & (1 << 7)) >> 6) | |
| 176 | ((A1 & (1 << 7)) >> 7); |
| 177 | outbuf[3] = /* C1_6 B1_6 A1_6 B1_5 A1_5 C1_4 B1_4 A1_4 */ |
| 178 | ((C1 & (1 << 6)) << 1) | |
| 179 | ((B1 & (1 << 6)) << 0) | |
| 180 | ((A1 & (1 << 6)) >> 1) | |
| 181 | ((B1 & (1 << 5)) >> 1) | |
| 182 | ((A1 & (1 << 5)) >> 2) | |
| 183 | ((C1 & (1 << 4)) >> 2) | |
| 184 | ((B1 & (1 << 4)) >> 3) | |
| 185 | ((A1 & (1 << 4)) >> 4); |
| 186 | outbuf[4] = /* C1_3 B1_3 A1_3 B1_2 A1_2 C1_1 B1_1 A1_1 */ |
| 187 | ((C1 & (1 << 3)) << 4) | |
| 188 | ((B1 & (1 << 3)) << 3) | |
| 189 | ((A1 & (1 << 3)) << 2) | |
| 190 | ((B1 & (1 << 2)) << 2) | |
| 191 | ((A1 & (1 << 2)) << 1) | |
| 192 | ((C1 & (1 << 1)) << 1) | |
| 193 | ((B1 & (1 << 1)) >> 0) | |
| 194 | ((A1 & (1 << 1)) >> 1); |
| 195 | outbuf[5] = /* C1_0 B1_0 A1_0 B2_7 A2_7 C2_6 B2_6 A2_6 */ |
| 196 | ((C1 & (1 << 0)) << 7) | |
| 197 | ((B1 & (1 << 0)) << 6) | |
| 198 | ((A1 & (1 << 0)) << 5) | |
| 199 | ((B2 & (1 << 7)) >> 3) | |
| 200 | ((A2 & (1 << 7)) >> 4) | |
| 201 | ((C2 & (1 << 6)) >> 4) | |
| 202 | ((B2 & (1 << 6)) >> 5) | |
| 203 | ((A2 & (1 << 6)) >> 6); |
| 204 | outbuf[6] = /* C2_5 B2_5 A2_5 B2_4 A2_4 C2_3 B2_3 A2_3 */ |
| 205 | ((C2 & (1 << 5)) << 2) | |
| 206 | ((B2 & (1 << 5)) << 1) | |
| 207 | ((A2 & (1 << 5)) << 0) | |
| 208 | ((B2 & (1 << 4)) >> 0) | |
| 209 | ((A2 & (1 << 4)) >> 1) | |
| 210 | ((C2 & (1 << 3)) >> 1) | |
| 211 | ((B2 & (1 << 3)) >> 2) | |
| 212 | ((A2 & (1 << 3)) >> 3); |
| 213 | outbuf[7] = /* C2_2 B2_2 A2_2 B2_1 A2_1 C2_0 B2_0 A2_0 */ |
| 214 | ((C2 & (1 << 2)) << 5) | |
| 215 | ((B2 & (1 << 2)) << 4) | |
| 216 | ((A2 & (1 << 2)) << 3) | |
| 217 | ((B2 & (1 << 1)) << 3) | |
| 218 | ((A2 & (1 << 1)) << 2) | |
| 219 | ((C2 & (1 << 0)) << 2) | |
| 220 | ((B2 & (1 << 0)) << 1) | |
| 221 | ((A2 & (1 << 0)) << 0); |
| 222 | } |
| 223 | outbuf += 8; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | stp_fold_4bit(const unsigned char *line, |
| 229 | int single_length, |
| 230 | unsigned char *outbuf) |
| 231 | { |
| 232 | int i; |
| 233 | memset(outbuf, 0, single_length * 4); |
| 234 | for (i = 0; i < single_length; i++) |
| 235 | { |
| 236 | unsigned char l0 = line[0]; |
| 237 | unsigned char l1 = line[single_length]; |
| 238 | unsigned char l2 = line[single_length*2]; |
| 239 | unsigned char l3 = line[single_length*3]; |
| 240 | if (l0 || l1 || l2 || l3) |
| 241 | { |
| 242 | outbuf[0] = /* D7 C7 B7 A7 D6 C6 B6 A6 */ |
| 243 | ((l3 & (1 << 7)) >> 0)| |
| 244 | ((l2 & (1 << 7)) >> 1)| |
| 245 | ((l1 & (1 << 7)) >> 2)| |
| 246 | ((l0 & (1 << 7)) >> 3)| |
| 247 | ((l3 & (1 << 6)) >> 3)| |
| 248 | ((l2 & (1 << 6)) >> 4)| |
| 249 | ((l1 & (1 << 6)) >> 5)| |
| 250 | ((l0 & (1 << 6)) >> 6); |
| 251 | |
| 252 | outbuf[1] = /* D5 C5 B5 A5 D4 C4 B4 A4 */ |
| 253 | ((l3 & (1 << 5)) << 2)| |
| 254 | ((l2 & (1 << 5)) << 1)| |
| 255 | ((l1 & (1 << 5)) << 0)| |
| 256 | ((l0 & (1 << 5)) >> 1)| |
| 257 | ((l3 & (1 << 4)) >> 1)| |
| 258 | ((l2 & (1 << 4)) >> 2)| |
| 259 | ((l1 & (1 << 4)) >> 3)| |
| 260 | ((l0 & (1 << 4)) >> 4); |
| 261 | |
| 262 | outbuf[2] = /* D3 C3 B3 A3 D2 C2 B2 A2 */ |
| 263 | ((l3 & (1 << 3)) << 4)| |
| 264 | ((l2 & (1 << 3)) << 3)| |
| 265 | ((l1 & (1 << 3)) << 2)| |
| 266 | ((l0 & (1 << 3)) << 1)| |
| 267 | ((l3 & (1 << 2)) << 1)| |
| 268 | ((l2 & (1 << 2)) << 0)| |
| 269 | ((l1 & (1 << 2)) >> 1)| |
| 270 | ((l0 & (1 << 2)) >> 2); |
| 271 | |
| 272 | outbuf[3] = /* D1 C1 B1 A1 D0 C0 B0 A0 */ |
| 273 | ((l3 & (1 << 1)) << 6)| |
| 274 | ((l2 & (1 << 1)) << 5)| |
| 275 | ((l1 & (1 << 1)) << 4)| |
| 276 | ((l0 & (1 << 1)) << 3)| |
| 277 | ((l3 & (1 << 0)) << 3)| |
| 278 | ((l2 & (1 << 0)) << 2)| |
| 279 | ((l1 & (1 << 0)) << 1)| |
| 280 | ((l0 & (1 << 0)) << 0); |
| 281 | } |
| 282 | line++; |
| 283 | outbuf += 4; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | stp_fold_8bit(const unsigned char *line, |
| 289 | int single_length, |
| 290 | unsigned char *outbuf) |
| 291 | { |
| 292 | int i; |
| 293 | memset(outbuf, 0, single_length * 8); |
| 294 | for (i = 0; i < single_length; i++) |
| 295 | { |
| 296 | unsigned char l0 = line[0]; |
| 297 | unsigned char l1 = line[single_length]; |
| 298 | unsigned char l2 = line[single_length*2]; |
| 299 | unsigned char l3 = line[single_length*3]; |
| 300 | unsigned char l4 = line[single_length*4]; |
| 301 | unsigned char l5 = line[single_length*5]; |
| 302 | unsigned char l6 = line[single_length*6]; |
| 303 | unsigned char l7 = line[single_length*7]; |
| 304 | if (l0 || l1 || l2 || l3 || l4 || l5 || l6 || l7) |
| 305 | { |
| 306 | outbuf[0] = /* H7 G7 F7 E7 D7 C7 B7 A7 */ |
| 307 | ((l7 & (1 << 7)) >> 0)| |
| 308 | ((l6 & (1 << 7)) >> 1)| |
| 309 | ((l5 & (1 << 7)) >> 2)| |
| 310 | ((l4 & (1 << 7)) >> 3)| |
| 311 | ((l3 & (1 << 7)) >> 4)| |
| 312 | ((l2 & (1 << 7)) >> 5)| |
| 313 | ((l1 & (1 << 7)) >> 6)| |
| 314 | ((l0 & (1 << 7)) >> 7); |
| 315 | |
| 316 | outbuf[1] = /* H6 G6 F6 E6 D6 C6 B6 A6 */ |
| 317 | ((l7 & (1 << 6)) << 1)| |
| 318 | ((l6 & (1 << 6)) >> 0)| |
| 319 | ((l5 & (1 << 6)) >> 1)| |
| 320 | ((l4 & (1 << 6)) >> 2)| |
| 321 | ((l3 & (1 << 6)) >> 3)| |
| 322 | ((l2 & (1 << 6)) >> 4)| |
| 323 | ((l1 & (1 << 6)) >> 5)| |
| 324 | ((l0 & (1 << 6)) >> 6); |
| 325 | |
| 326 | outbuf[2] = /* H5 G5 F5 E5 D5 C5 B5 A5 */ |
| 327 | ((l7 & (1 << 5)) << 2)| |
| 328 | ((l6 & (1 << 5)) << 1)| |
| 329 | ((l5 & (1 << 5)) >> 0)| |
| 330 | ((l4 & (1 << 5)) >> 1)| |
| 331 | ((l3 & (1 << 5)) >> 2)| |
| 332 | ((l2 & (1 << 5)) >> 3)| |
| 333 | ((l1 & (1 << 5)) >> 4)| |
| 334 | ((l0 & (1 << 5)) >> 5); |
| 335 | |
| 336 | outbuf[3] = /* H4 G4 F4 E4 D4 C4 B4 A4 */ |
| 337 | ((l7 & (1 << 4)) << 3)| |
| 338 | ((l6 & (1 << 4)) << 2)| |
| 339 | ((l5 & (1 << 4)) << 1)| |
| 340 | ((l4 & (1 << 4)) >> 0)| |
| 341 | ((l3 & (1 << 4)) >> 1)| |
| 342 | ((l2 & (1 << 4)) >> 2)| |
| 343 | ((l1 & (1 << 4)) >> 3)| |
| 344 | ((l0 & (1 << 4)) >> 4); |
| 345 | outbuf[4] = /* H3 G3 F3 E3 D3 C3 B3 A3 */ |
| 346 | ((l7 & (1 << 3)) << 4)| |
| 347 | ((l6 & (1 << 3)) << 3)| |
| 348 | ((l5 & (1 << 3)) << 2)| |
| 349 | ((l4 & (1 << 3)) << 1)| |
| 350 | ((l3 & (1 << 3)) >> 0)| |
| 351 | ((l2 & (1 << 3)) >> 1)| |
| 352 | ((l1 & (1 << 3)) >> 2)| |
| 353 | ((l0 & (1 << 3)) >> 3); |
| 354 | |
| 355 | outbuf[5] = /* H2 G2 F2 E2 D2 C2 B2 A2 */ |
| 356 | ((l7 & (1 << 2)) << 5)| |
| 357 | ((l6 & (1 << 2)) << 4)| |
| 358 | ((l5 & (1 << 2)) << 3)| |
| 359 | ((l4 & (1 << 2)) << 2)| |
| 360 | ((l3 & (1 << 2)) << 1)| |
| 361 | ((l2 & (1 << 2)) >> 0)| |
| 362 | ((l1 & (1 << 2)) >> 1)| |
| 363 | ((l0 & (1 << 2)) >> 2); |
| 364 | |
| 365 | outbuf[6] = /* H1 G1 F1 E1 D1 C1 B1 A1 */ |
| 366 | ((l7 & (1 << 1)) << 6)| |
| 367 | ((l6 & (1 << 1)) << 5)| |
| 368 | ((l5 & (1 << 1)) << 4)| |
| 369 | ((l4 & (1 << 1)) << 3)| |
| 370 | ((l3 & (1 << 1)) << 2)| |
| 371 | ((l2 & (1 << 1)) << 1)| |
| 372 | ((l1 & (1 << 1)) >> 0)| |
| 373 | ((l0 & (1 << 1)) >> 1); |
| 374 | |
| 375 | outbuf[7] = /* H0 G0 F0 E0 D0 C0 B0 A0 */ |
| 376 | ((l7 & (1 << 0)) << 7)| |
| 377 | ((l6 & (1 << 0)) << 6)| |
| 378 | ((l5 & (1 << 0)) << 5)| |
| 379 | ((l4 & (1 << 0)) << 4)| |
| 380 | ((l3 & (1 << 0)) << 3)| |
| 381 | ((l2 & (1 << 0)) << 2)| |
| 382 | ((l1 & (1 << 0)) << 1)| |
| 383 | ((l0 & (1 << 0)) >> 0); |
| 384 | } |
| 385 | line++; |
| 386 | outbuf += 8; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | #define SPLIT_MASK(k, b) (((1 << (b)) - 1) << ((k) * (b))) |
| 391 | |
| 392 | #define SPLIT_STEP(k, b, i, o, in, r, inc, rl) \ |
| 393 | do \ |
| 394 | { \ |
| 395 | if (in & SPLIT_MASK(k, b)) \ |
| 396 | { \ |
| 397 | o[r][i] |= SPLIT_MASK(k, b) & in; \ |
| 398 | r += inc; \ |
| 399 | if (r >= rl) \ |
| 400 | r = 0; \ |
| 401 | } \ |
| 402 | } while (0) |
| 403 | |
| 404 | void |
| 405 | stp_split(int length, |
| 406 | int bits, |
| 407 | int n, |
| 408 | const unsigned char *in, |
| 409 | int increment, |
| 410 | unsigned char **outs) |
| 411 | { |
| 412 | int row = 0; |
| 413 | int limit = length * bits; |
| 414 | int rlimit = n * increment; |
| 415 | int i; |
| 416 | for (i = 1; i < n; i++) |
| 417 | memset(outs[i * increment], 0, limit); |
| 418 | |
| 419 | if (bits == 1) |
| 420 | { |
| 421 | for (i = 0; i < limit; i++) |
| 422 | { |
| 423 | unsigned char inbyte = in[i]; |
| 424 | outs[0][i] = 0; |
| 425 | if (inbyte == 0) |
| 426 | continue; |
| 427 | /* For some reason gcc isn't unrolling this, even with -funroll-loops */ |
| 428 | SPLIT_STEP(0, 1, i, outs, inbyte, row, increment, rlimit); |
| 429 | SPLIT_STEP(1, 1, i, outs, inbyte, row, increment, rlimit); |
| 430 | SPLIT_STEP(2, 1, i, outs, inbyte, row, increment, rlimit); |
| 431 | SPLIT_STEP(3, 1, i, outs, inbyte, row, increment, rlimit); |
| 432 | SPLIT_STEP(4, 1, i, outs, inbyte, row, increment, rlimit); |
| 433 | SPLIT_STEP(5, 1, i, outs, inbyte, row, increment, rlimit); |
| 434 | SPLIT_STEP(6, 1, i, outs, inbyte, row, increment, rlimit); |
| 435 | SPLIT_STEP(7, 1, i, outs, inbyte, row, increment, rlimit); |
| 436 | } |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | for (i = 0; i < limit; i++) |
| 441 | { |
| 442 | unsigned char inbyte = in[i]; |
| 443 | outs[0][i] = 0; |
| 444 | if (inbyte == 0) |
| 445 | continue; |
| 446 | /* For some reason gcc isn't unrolling this, even with -funroll-loops */ |
| 447 | SPLIT_STEP(0, 2, i, outs, inbyte, row, increment, rlimit); |
| 448 | SPLIT_STEP(1, 2, i, outs, inbyte, row, increment, rlimit); |
| 449 | SPLIT_STEP(2, 2, i, outs, inbyte, row, increment, rlimit); |
| 450 | SPLIT_STEP(3, 2, i, outs, inbyte, row, increment, rlimit); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void |
| 456 | stp_split_2(int length, |
| 457 | int bits, |
| 458 | const unsigned char *in, |
| 459 | unsigned char *outhi, |
| 460 | unsigned char *outlo) |
| 461 | { |
| 462 | unsigned char *outs[2]; |
| 463 | outs[0] = outhi; |
| 464 | outs[1] = outlo; |
| 465 | stp_split(length, bits, 2, in, 1, outs); |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | stp_split_4(int length, |
| 470 | int bits, |
| 471 | const unsigned char *in, |
| 472 | unsigned char *out0, |
| 473 | unsigned char *out1, |
| 474 | unsigned char *out2, |
| 475 | unsigned char *out3) |
| 476 | { |
| 477 | unsigned char *outs[4]; |
| 478 | outs[0] = out0; |
| 479 | outs[1] = out1; |
| 480 | outs[2] = out2; |
| 481 | outs[3] = out3; |
| 482 | stp_split(length, bits, 4, in, 1, outs); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | static void NOINLINE |
| 487 | stpi_unpack_2_1(int length, |
| 488 | const unsigned char *in, |
| 489 | unsigned char **outs) |
| 490 | { |
| 491 | unsigned char tempin, bit, temp0, temp1; |
| 492 | |
| 493 | if (length <= 0) |
| 494 | return; |
| 495 | for (bit = 128, temp0 = 0, temp1 = 0; |
| 496 | length > 0; |
| 497 | length --) |
| 498 | { |
| 499 | tempin = *in++; |
| 500 | |
| 501 | if (tempin & 128) |
| 502 | temp0 |= bit; |
| 503 | if (tempin & 64) |
| 504 | temp1 |= bit; |
| 505 | bit >>= 1; |
| 506 | if (tempin & 32) |
| 507 | temp0 |= bit; |
| 508 | if (tempin & 16) |
| 509 | temp1 |= bit; |
| 510 | bit >>= 1; |
| 511 | if (tempin & 8) |
| 512 | temp0 |= bit; |
| 513 | if (tempin & 4) |
| 514 | temp1 |= bit; |
| 515 | bit >>= 1; |
| 516 | if (tempin & 2) |
| 517 | temp0 |= bit; |
| 518 | if (tempin & 1) |
| 519 | temp1 |= bit; |
| 520 | |
| 521 | if (bit > 1) |
| 522 | bit >>= 1; |
| 523 | else |
| 524 | { |
| 525 | bit = 128; |
| 526 | *outs[0]++ = temp0; |
| 527 | *outs[1]++ = temp1; |
| 528 | |
| 529 | temp0 = 0; |
| 530 | temp1 = 0; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (bit < 128) |
| 535 | { |
| 536 | *outs[0]++ = temp0; |
| 537 | *outs[1]++ = temp1; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static void NOINLINE |
| 542 | stpi_unpack_2_2(int length, |
| 543 | const unsigned char *in, |
| 544 | unsigned char **outs) |
| 545 | { |
| 546 | if (length <= 0) |
| 547 | return; |
| 548 | |
| 549 | for (;length;length --) |
| 550 | { |
| 551 | unsigned char ti0, ti1; |
| 552 | ti0 = in[0]; |
| 553 | ti1 = in[1]; |
| 554 | |
| 555 | *outs[0]++ = (ti0 & 0xc0) << 0 |
| 556 | | (ti0 & 0x0c) << 2 |
| 557 | | (ti1 & 0xc0) >> 4 |
| 558 | | (ti1 & 0x0c) >> 2; |
| 559 | *outs[1]++ = (ti0 & 0x30) << 2 |
| 560 | | (ti0 & 0x03) << 4 |
| 561 | | (ti1 & 0x30) >> 2 |
| 562 | | (ti1 & 0x03) >> 0; |
| 563 | in += 2; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | static void NOINLINE |
| 568 | stpi_unpack_4_1(int length, |
| 569 | const unsigned char *in, |
| 570 | unsigned char **outs) |
| 571 | { |
| 572 | unsigned char tempin, bit, temp0, temp1, temp2, temp3; |
| 573 | |
| 574 | if (length <= 0) |
| 575 | return; |
| 576 | for (bit = 128, temp0 = 0, temp1 = 0, temp2 = 0, temp3 = 0; |
| 577 | length > 0; |
| 578 | length --) |
| 579 | { |
| 580 | tempin = *in++; |
| 581 | |
| 582 | if (tempin & 128) |
| 583 | temp0 |= bit; |
| 584 | if (tempin & 64) |
| 585 | temp1 |= bit; |
| 586 | if (tempin & 32) |
| 587 | temp2 |= bit; |
| 588 | if (tempin & 16) |
| 589 | temp3 |= bit; |
| 590 | bit >>= 1; |
| 591 | if (tempin & 8) |
| 592 | temp0 |= bit; |
| 593 | if (tempin & 4) |
| 594 | temp1 |= bit; |
| 595 | if (tempin & 2) |
| 596 | temp2 |= bit; |
| 597 | if (tempin & 1) |
| 598 | temp3 |= bit; |
| 599 | |
| 600 | if (bit > 1) |
| 601 | bit >>= 1; |
| 602 | else |
| 603 | { |
| 604 | bit = 128; |
| 605 | *outs[0]++ = temp0; |
| 606 | *outs[1]++ = temp1; |
| 607 | *outs[2]++ = temp2; |
| 608 | *outs[3]++ = temp3; |
| 609 | |
| 610 | temp0 = 0; |
| 611 | temp1 = 0; |
| 612 | temp2 = 0; |
| 613 | temp3 = 0; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (bit < 128) |
| 618 | { |
| 619 | *outs[0]++ = temp0; |
| 620 | *outs[1]++ = temp1; |
| 621 | *outs[2]++ = temp2; |
| 622 | *outs[3]++ = temp3; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static void NOINLINE |
| 627 | stpi_unpack_4_2(int length, |
| 628 | const unsigned char *in, |
| 629 | unsigned char **outs) |
| 630 | { |
| 631 | unsigned char tempin, |
| 632 | shift, |
| 633 | temp0, |
| 634 | temp1, |
| 635 | temp2, |
| 636 | temp3; |
| 637 | |
| 638 | length *= 2; |
| 639 | |
| 640 | for (shift = 0, temp0 = 0, temp1 = 0, temp2 = 0, temp3 = 0; |
| 641 | length > 0; |
| 642 | length --) |
| 643 | { |
| 644 | /* |
| 645 | * Note - we can't use (tempin & N) >> (shift - M) since negative |
| 646 | * right-shifts are not always implemented. |
| 647 | */ |
| 648 | |
| 649 | tempin = *in++; |
| 650 | |
| 651 | if (tempin & 192) |
| 652 | temp0 |= (tempin & 192) >> shift; |
| 653 | if (tempin & 48) |
| 654 | temp1 |= ((tempin & 48) << 2) >> shift; |
| 655 | if (tempin & 12) |
| 656 | temp2 |= ((tempin & 12) << 4) >> shift; |
| 657 | if (tempin & 3) |
| 658 | temp3 |= ((tempin & 3) << 6) >> shift; |
| 659 | |
| 660 | if (shift < 6) |
| 661 | shift += 2; |
| 662 | else |
| 663 | { |
| 664 | shift = 0; |
| 665 | *outs[0]++ = temp0; |
| 666 | *outs[1]++ = temp1; |
| 667 | *outs[2]++ = temp2; |
| 668 | *outs[3]++ = temp3; |
| 669 | |
| 670 | temp0 = 0; |
| 671 | temp1 = 0; |
| 672 | temp2 = 0; |
| 673 | temp3 = 0; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | if (shift) |
| 678 | { |
| 679 | *outs[0]++ = temp0; |
| 680 | *outs[1]++ = temp1; |
| 681 | *outs[2]++ = temp2; |
| 682 | *outs[3]++ = temp3; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | static void NOINLINE |
| 687 | stpi_unpack_8_1(int length, |
| 688 | const unsigned char *in, |
| 689 | unsigned char **outs) |
| 690 | { |
| 691 | unsigned char tempin, bit, temp0, temp1, temp2, temp3, temp4, temp5, temp6, |
| 692 | temp7; |
| 693 | |
| 694 | if (length <= 0) |
| 695 | return; |
| 696 | |
| 697 | for (bit = 128, temp0 = 0, temp1 = 0, temp2 = 0, |
| 698 | temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0, temp7 = 0; |
| 699 | length > 0; |
| 700 | length --) |
| 701 | { |
| 702 | tempin = *in++; |
| 703 | |
| 704 | if (tempin & 128) |
| 705 | temp0 |= bit; |
| 706 | if (tempin & 64) |
| 707 | temp1 |= bit; |
| 708 | if (tempin & 32) |
| 709 | temp2 |= bit; |
| 710 | if (tempin & 16) |
| 711 | temp3 |= bit; |
| 712 | if (tempin & 8) |
| 713 | temp4 |= bit; |
| 714 | if (tempin & 4) |
| 715 | temp5 |= bit; |
| 716 | if (tempin & 2) |
| 717 | temp6 |= bit; |
| 718 | if (tempin & 1) |
| 719 | temp7 |= bit; |
| 720 | |
| 721 | if (bit > 1) |
| 722 | bit >>= 1; |
| 723 | else |
| 724 | { |
| 725 | bit = 128; |
| 726 | *outs[0]++ = temp0; |
| 727 | *outs[1]++ = temp1; |
| 728 | *outs[2]++ = temp2; |
| 729 | *outs[3]++ = temp3; |
| 730 | *outs[4]++ = temp4; |
| 731 | *outs[5]++ = temp5; |
| 732 | *outs[6]++ = temp6; |
| 733 | *outs[7]++ = temp7; |
| 734 | |
| 735 | temp0 = 0; |
| 736 | temp1 = 0; |
| 737 | temp2 = 0; |
| 738 | temp3 = 0; |
| 739 | temp4 = 0; |
| 740 | temp5 = 0; |
| 741 | temp6 = 0; |
| 742 | temp7 = 0; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (bit < 128) |
| 747 | { |
| 748 | *outs[0]++ = temp0; |
| 749 | *outs[1]++ = temp1; |
| 750 | *outs[2]++ = temp2; |
| 751 | *outs[3]++ = temp3; |
| 752 | *outs[4]++ = temp4; |
| 753 | *outs[5]++ = temp5; |
| 754 | *outs[6]++ = temp6; |
| 755 | *outs[7]++ = temp7; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static void NOINLINE |
| 760 | stpi_unpack_8_2(int length, |
| 761 | const unsigned char *in, |
| 762 | unsigned char **outs) |
| 763 | { |
| 764 | unsigned char tempin, |
| 765 | shift, |
| 766 | temp0, |
| 767 | temp1, |
| 768 | temp2, |
| 769 | temp3, |
| 770 | temp4, |
| 771 | temp5, |
| 772 | temp6, |
| 773 | temp7; |
| 774 | |
| 775 | |
| 776 | for (shift = 0, temp0 = 0, temp1 = 0, |
| 777 | temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0, temp7 = 0; |
| 778 | length > 0; |
| 779 | length --) |
| 780 | { |
| 781 | /* |
| 782 | * Note - we can't use (tempin & N) >> (shift - M) since negative |
| 783 | * right-shifts are not always implemented. |
| 784 | */ |
| 785 | |
| 786 | tempin = *in++; |
| 787 | |
| 788 | if (tempin & 192) |
| 789 | temp0 |= (tempin & 192) >> shift; |
| 790 | if (tempin & 48) |
| 791 | temp1 |= ((tempin & 48) << 2) >> shift; |
| 792 | if (tempin & 12) |
| 793 | temp2 |= ((tempin & 12) << 4) >> shift; |
| 794 | if (tempin & 3) |
| 795 | temp3 |= ((tempin & 3) << 6) >> shift; |
| 796 | |
| 797 | tempin = *in++; |
| 798 | |
| 799 | if (tempin & 192) |
| 800 | temp4 |= (tempin & 192) >> shift; |
| 801 | if (tempin & 48) |
| 802 | temp5 |= ((tempin & 48) << 2) >> shift; |
| 803 | if (tempin & 12) |
| 804 | temp6 |= ((tempin & 12) << 4) >> shift; |
| 805 | if (tempin & 3) |
| 806 | temp7 |= ((tempin & 3) << 6) >> shift; |
| 807 | |
| 808 | if (shift < 6) |
| 809 | shift += 2; |
| 810 | else |
| 811 | { |
| 812 | shift = 0; |
| 813 | *outs[0]++ = temp0; |
| 814 | *outs[1]++ = temp1; |
| 815 | *outs[2]++ = temp2; |
| 816 | *outs[3]++ = temp3; |
| 817 | *outs[4]++ = temp4; |
| 818 | *outs[5]++ = temp5; |
| 819 | *outs[6]++ = temp6; |
| 820 | *outs[7]++ = temp7; |
| 821 | |
| 822 | temp0 = 0; |
| 823 | temp1 = 0; |
| 824 | temp2 = 0; |
| 825 | temp3 = 0; |
| 826 | temp4 = 0; |
| 827 | temp5 = 0; |
| 828 | temp6 = 0; |
| 829 | temp7 = 0; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (shift) |
| 834 | { |
| 835 | *outs[0]++ = temp0; |
| 836 | *outs[1]++ = temp1; |
| 837 | *outs[2]++ = temp2; |
| 838 | *outs[3]++ = temp3; |
| 839 | *outs[4]++ = temp4; |
| 840 | *outs[5]++ = temp5; |
| 841 | *outs[6]++ = temp6; |
| 842 | *outs[7]++ = temp7; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | static void NOINLINE |
| 847 | stpi_unpack_16_1(int length, |
| 848 | const unsigned char *in, |
| 849 | unsigned char **outs) |
| 850 | { |
| 851 | unsigned char tempin, bit; |
| 852 | unsigned char temp[16]; |
| 853 | int j; |
| 854 | |
| 855 | if (length <= 0) |
| 856 | return; |
| 857 | |
| 858 | memset(temp, 0, 16); |
| 859 | |
| 860 | for (bit = 128; length > 0; length--) |
| 861 | { |
| 862 | tempin = *in++; |
| 863 | |
| 864 | if (tempin & 128) |
| 865 | temp[0] |= bit; |
| 866 | if (tempin & 64) |
| 867 | temp[1] |= bit; |
| 868 | if (tempin & 32) |
| 869 | temp[2] |= bit; |
| 870 | if (tempin & 16) |
| 871 | temp[3] |= bit; |
| 872 | if (tempin & 8) |
| 873 | temp[4] |= bit; |
| 874 | if (tempin & 4) |
| 875 | temp[5] |= bit; |
| 876 | if (tempin & 2) |
| 877 | temp[6] |= bit; |
| 878 | if (tempin & 1) |
| 879 | temp[7] |= bit; |
| 880 | |
| 881 | tempin = *in++; |
| 882 | |
| 883 | if (tempin & 128) |
| 884 | temp[8] |= bit; |
| 885 | if (tempin & 64) |
| 886 | temp[9] |= bit; |
| 887 | if (tempin & 32) |
| 888 | temp[10] |= bit; |
| 889 | if (tempin & 16) |
| 890 | temp[11] |= bit; |
| 891 | if (tempin & 8) |
| 892 | temp[12] |= bit; |
| 893 | if (tempin & 4) |
| 894 | temp[13] |= bit; |
| 895 | if (tempin & 2) |
| 896 | temp[14] |= bit; |
| 897 | if (tempin & 1) |
| 898 | temp[15] |= bit; |
| 899 | |
| 900 | if (bit > 1) |
| 901 | bit >>= 1; |
| 902 | else |
| 903 | { |
| 904 | bit = 128; |
| 905 | for (j = 0; j < 16; j++) |
| 906 | *outs[j]++ = temp[j]; |
| 907 | |
| 908 | memset(temp, 0, 16); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (bit < 128) |
| 913 | for (j = 0; j < 16; j++) |
| 914 | *outs[j]++ = temp[j]; |
| 915 | } |
| 916 | |
| 917 | static void NOINLINE |
| 918 | stpi_unpack_16_2(int length, |
| 919 | const unsigned char *in, |
| 920 | unsigned char **outs) |
| 921 | { |
| 922 | unsigned char tempin, shift; |
| 923 | unsigned char temp[16]; |
| 924 | int j; |
| 925 | |
| 926 | if (length <= 0) |
| 927 | return; |
| 928 | |
| 929 | memset(temp, 0, 16); |
| 930 | |
| 931 | for (shift = 0; length > 0; length--) |
| 932 | { |
| 933 | /* |
| 934 | * Note - we can't use (tempin & N) >> (shift - M) since negative |
| 935 | * right-shifts are not always implemented. |
| 936 | */ |
| 937 | |
| 938 | tempin = *in++; |
| 939 | |
| 940 | if (tempin & 192) |
| 941 | temp[0] |= (tempin & 192) >> shift; |
| 942 | if (tempin & 48) |
| 943 | temp[1] |= ((tempin & 48) << 2) >> shift; |
| 944 | if (tempin & 12) |
| 945 | temp[2] |= ((tempin & 12) << 4) >> shift; |
| 946 | if (tempin & 3) |
| 947 | temp[3] |= ((tempin & 3) << 6) >> shift; |
| 948 | |
| 949 | tempin = *in++; |
| 950 | |
| 951 | if (tempin & 192) |
| 952 | temp[4] |= (tempin & 192) >> shift; |
| 953 | if (tempin & 48) |
| 954 | temp[5] |= ((tempin & 48) << 2) >> shift; |
| 955 | if (tempin & 12) |
| 956 | temp[6] |= ((tempin & 12) << 4) >> shift; |
| 957 | if (tempin & 3) |
| 958 | temp[7] |= ((tempin & 3) << 6) >> shift; |
| 959 | |
| 960 | if (length-- > 0) |
| 961 | { |
| 962 | tempin = *in++; |
| 963 | |
| 964 | if (tempin & 192) |
| 965 | temp[8] |= (tempin & 192) >> shift; |
| 966 | if (tempin & 48) |
| 967 | temp[9] |= ((tempin & 48) << 2) >> shift; |
| 968 | if (tempin & 12) |
| 969 | temp[10] |= ((tempin & 12) << 4) >> shift; |
| 970 | if (tempin & 3) |
| 971 | temp[11] |= ((tempin & 3) << 6) >> shift; |
| 972 | |
| 973 | tempin = *in++; |
| 974 | |
| 975 | if (tempin & 192) |
| 976 | temp[12] |= (tempin & 192) >> shift; |
| 977 | if (tempin & 48) |
| 978 | temp[13] |= ((tempin & 48) << 2) >> shift; |
| 979 | if (tempin & 12) |
| 980 | temp[14] |= ((tempin & 12) << 4) >> shift; |
| 981 | if (tempin & 3) |
| 982 | temp[15] |= ((tempin & 3) << 6) >> shift; |
| 983 | } |
| 984 | |
| 985 | if (shift < 6) |
| 986 | shift += 2; |
| 987 | else |
| 988 | { |
| 989 | shift = 0; |
| 990 | for (j = 0; j < 16; j++) |
| 991 | *outs[j]++ = temp[j]; |
| 992 | |
| 993 | memset(temp, 0, 16); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | if (shift) |
| 998 | for (j = 0; j < 16; j++) |
| 999 | *outs[j]++ = temp[j]; |
| 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | stp_unpack(int length, |
| 1004 | int bits, |
| 1005 | int n, |
| 1006 | const unsigned char *in, |
| 1007 | unsigned char **outs) |
| 1008 | { |
| 1009 | unsigned char **touts; |
| 1010 | int i; |
| 1011 | if (n < 2) |
| 1012 | return; |
| 1013 | touts = stp_malloc(sizeof(unsigned char *) * n); |
| 1014 | for (i = 0; i < n; i++) |
| 1015 | touts[i] = outs[i]; |
| 1016 | if (bits == 1) |
| 1017 | switch (n) |
| 1018 | { |
| 1019 | case 2: |
| 1020 | stpi_unpack_2_1(length, in, touts); |
| 1021 | break; |
| 1022 | case 4: |
| 1023 | stpi_unpack_4_1(length, in, touts); |
| 1024 | break; |
| 1025 | case 8: |
| 1026 | stpi_unpack_8_1(length, in, touts); |
| 1027 | break; |
| 1028 | case 16: |
| 1029 | stpi_unpack_16_1(length, in, touts); |
| 1030 | break; |
| 1031 | } |
| 1032 | else |
| 1033 | switch (n) |
| 1034 | { |
| 1035 | case 2: |
| 1036 | stpi_unpack_2_2(length, in, touts); |
| 1037 | break; |
| 1038 | case 4: |
| 1039 | stpi_unpack_4_2(length, in, touts); |
| 1040 | break; |
| 1041 | case 8: |
| 1042 | stpi_unpack_8_2(length, in, touts); |
| 1043 | break; |
| 1044 | case 16: |
| 1045 | stpi_unpack_16_2(length, in, touts); |
| 1046 | break; |
| 1047 | } |
| 1048 | stp_free(touts); |
| 1049 | } |
| 1050 | |
| 1051 | void |
| 1052 | stp_unpack_2(int length, |
| 1053 | int bits, |
| 1054 | const unsigned char *in, |
| 1055 | unsigned char *outhi, |
| 1056 | unsigned char *outlo) |
| 1057 | { |
| 1058 | unsigned char *outs[2]; |
| 1059 | outs[0] = outhi; |
| 1060 | outs[1] = outlo; |
| 1061 | stp_unpack(length, bits, 2, in, outs); |
| 1062 | } |
| 1063 | |
| 1064 | void |
| 1065 | stp_unpack_4(int length, |
| 1066 | int bits, |
| 1067 | const unsigned char *in, |
| 1068 | unsigned char *out0, |
| 1069 | unsigned char *out1, |
| 1070 | unsigned char *out2, |
| 1071 | unsigned char *out3) |
| 1072 | { |
| 1073 | unsigned char *outs[4]; |
| 1074 | outs[0] = out0; |
| 1075 | outs[1] = out1; |
| 1076 | outs[2] = out2; |
| 1077 | outs[3] = out3; |
| 1078 | stp_unpack(length, bits, 4, in, outs); |
| 1079 | } |
| 1080 | |
| 1081 | void |
| 1082 | stp_unpack_8(int length, |
| 1083 | int bits, |
| 1084 | const unsigned char *in, |
| 1085 | unsigned char *out0, |
| 1086 | unsigned char *out1, |
| 1087 | unsigned char *out2, |
| 1088 | unsigned char *out3, |
| 1089 | unsigned char *out4, |
| 1090 | unsigned char *out5, |
| 1091 | unsigned char *out6, |
| 1092 | unsigned char *out7) |
| 1093 | { |
| 1094 | unsigned char *outs[8]; |
| 1095 | outs[0] = out0; |
| 1096 | outs[1] = out1; |
| 1097 | outs[2] = out2; |
| 1098 | outs[3] = out3; |
| 1099 | outs[4] = out4; |
| 1100 | outs[5] = out5; |
| 1101 | outs[6] = out6; |
| 1102 | outs[7] = out7; |
| 1103 | stp_unpack(length, bits, 8, in, outs); |
| 1104 | } |
| 1105 | |
| 1106 | void |
| 1107 | stp_unpack_16(int length, |
| 1108 | int bits, |
| 1109 | const unsigned char *in, |
| 1110 | unsigned char *out0, |
| 1111 | unsigned char *out1, |
| 1112 | unsigned char *out2, |
| 1113 | unsigned char *out3, |
| 1114 | unsigned char *out4, |
| 1115 | unsigned char *out5, |
| 1116 | unsigned char *out6, |
| 1117 | unsigned char *out7, |
| 1118 | unsigned char *out8, |
| 1119 | unsigned char *out9, |
| 1120 | unsigned char *out10, |
| 1121 | unsigned char *out11, |
| 1122 | unsigned char *out12, |
| 1123 | unsigned char *out13, |
| 1124 | unsigned char *out14, |
| 1125 | unsigned char *out15) |
| 1126 | { |
| 1127 | unsigned char *outs[16]; |
| 1128 | outs[0] = out0; |
| 1129 | outs[1] = out1; |
| 1130 | outs[2] = out2; |
| 1131 | outs[3] = out3; |
| 1132 | outs[4] = out4; |
| 1133 | outs[5] = out5; |
| 1134 | outs[6] = out6; |
| 1135 | outs[7] = out7; |
| 1136 | outs[8] = out8; |
| 1137 | outs[9] = out9; |
| 1138 | outs[10] = out10; |
| 1139 | outs[11] = out11; |
| 1140 | outs[12] = out12; |
| 1141 | outs[13] = out13; |
| 1142 | outs[14] = out14; |
| 1143 | outs[15] = out15; |
| 1144 | stp_unpack(length, bits, 16, in, outs); |
| 1145 | } |
| 1146 | |
| 1147 | static void NOINLINE |
| 1148 | find_first_and_last(const unsigned char *line, int length, |
| 1149 | int *first, int *last) |
| 1150 | { |
| 1151 | int found_first = 0; |
| 1152 | int f = 0; |
| 1153 | int l = 0; |
| 1154 | for (f = 0; f < length; f++) |
| 1155 | { |
| 1156 | if (line[f]) |
| 1157 | { |
| 1158 | found_first = 1; |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | *first = f; |
| 1163 | if (!found_first) |
| 1164 | { |
| 1165 | *last = 0; |
| 1166 | return; |
| 1167 | } |
| 1168 | for (l = length - 1; l >= f; l--) |
| 1169 | if (line[l]) |
| 1170 | break; |
| 1171 | ; |
| 1172 | *last = l; |
| 1173 | } |
| 1174 | |
| 1175 | int |
| 1176 | stp_pack_uncompressed(stp_vars_t *v, |
| 1177 | const unsigned char *line, |
| 1178 | int length, |
| 1179 | unsigned char *comp_buf, |
| 1180 | unsigned char **comp_ptr, |
| 1181 | int *first, |
| 1182 | int *last) |
| 1183 | { |
| 1184 | (void)v; |
| 1185 | find_first_and_last(line, length, first, last); |
| 1186 | memcpy(comp_buf, line, length); |
| 1187 | *comp_ptr = comp_buf + length; |
| 1188 | if (first && last && *first > *last) |
| 1189 | return 0; |
| 1190 | else |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 1194 | int |
| 1195 | stp_pack_tiff(stp_vars_t *v, |
| 1196 | const unsigned char *line, |
| 1197 | int length, |
| 1198 | unsigned char *comp_buf, |
| 1199 | unsigned char **comp_ptr, |
| 1200 | int *first, |
| 1201 | int *last) |
| 1202 | { |
| 1203 | unsigned char *comp_pti = comp_buf; |
| 1204 | (void)v; |
| 1205 | if (first && last) |
| 1206 | find_first_and_last(line, length, first, last); |
| 1207 | |
| 1208 | /* |
| 1209 | * Compress using TIFF "packbits" run-length encoding... |
| 1210 | */ |
| 1211 | |
| 1212 | while (length > 0) |
| 1213 | { |
| 1214 | const unsigned char *start = line; /* Start of compressed data */ |
| 1215 | unsigned char repeat; /* Repeating char */ |
| 1216 | int count; /* Count of compressed bytes */ |
| 1217 | /* |
| 1218 | * Get a run of at least 3 non-repeated chars... |
| 1219 | */ |
| 1220 | |
| 1221 | line += 2; |
| 1222 | length -= 2; |
| 1223 | |
| 1224 | while (length > 0 && (line[-2] != line[-1] || line[-1] != line[0])) |
| 1225 | { |
| 1226 | line ++; |
| 1227 | length --; |
| 1228 | } |
| 1229 | |
| 1230 | line -= 2; |
| 1231 | length += 2; |
| 1232 | |
| 1233 | /* |
| 1234 | * Output the non-repeated sequences (max 128 at a time). |
| 1235 | */ |
| 1236 | |
| 1237 | count = line - start; |
| 1238 | while (count > 0) |
| 1239 | { |
| 1240 | int tcount = count > 128 ? 128 : count; |
| 1241 | |
| 1242 | comp_pti[0] = tcount - 1; |
| 1243 | memcpy(comp_pti + 1, start, tcount); |
| 1244 | |
| 1245 | comp_pti += tcount + 1; |
| 1246 | start += tcount; |
| 1247 | count -= tcount; |
| 1248 | } |
| 1249 | |
| 1250 | if (length <= 0) |
| 1251 | break; |
| 1252 | |
| 1253 | /* |
| 1254 | * Find the repeated sequences... |
| 1255 | */ |
| 1256 | |
| 1257 | start = line; |
| 1258 | repeat = line[0]; |
| 1259 | |
| 1260 | line ++; |
| 1261 | length --; |
| 1262 | |
| 1263 | while (length > 0 && *line == repeat) |
| 1264 | { |
| 1265 | line++; |
| 1266 | length--; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * Output the repeated sequences (max 128 at a time). |
| 1271 | */ |
| 1272 | |
| 1273 | count = line - start; |
| 1274 | while (count > 0) |
| 1275 | { |
| 1276 | int tcount = count > 128 ? 128 : count; |
| 1277 | |
| 1278 | comp_pti[0] = 1 - tcount; |
| 1279 | comp_pti[1] = repeat; |
| 1280 | |
| 1281 | comp_pti += 2; |
| 1282 | count -= tcount; |
| 1283 | } |
| 1284 | } |
| 1285 | (*comp_ptr) = comp_pti; |
| 1286 | |
| 1287 | if (first && last && *first > *last) |
| 1288 | return 0; |
| 1289 | else |
| 1290 | return 1; |
| 1291 | } |