/usr/include/iulib/imgbitptr.h is in libiulib-dev 0.4+is+0.3-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | // Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
// or its licensors, as applicable.
// Copyright 1992-2007 Thomas M. Breuel
//
// You may not use this file except under the terms of the accompanying license.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Project: imgbits
// File: imgbitptr.h
// Purpose:
// Responsible: tmb
// Reviewer:
// Primary Repository:
// Web Sites: www.iupr.org, www.dfki.de, www.ocropus.org
// -*- C++ -*-
/* Copyright (c) Thomas M. Breuel */
#ifndef imgbitptr_h__
#define imgbitptr_h__
#include "colib/checks.h"
namespace imgbits {
typedef unsigned int word32;
////////////////////////////////////////////////////////////////
// a simple "dynamic array pointer" smart pointer, used by the blit
////////////////////////////////////////////////////////////////
template <class T>
struct dyna {
T *p;
operator T*() {return p;}
dyna(T *p=0):p(p) {}
~dyna() {if(p) delete [] p; p = 0;}
void operator=(T *other) {if(p) delete p; p = other;}
T &operator[](int i) { CHECK_CONDITION(p); return p[i]; }
T *steal() { T *temp = p; p = 0; return temp; }
private:
dyna(dyna<T> &);
void operator=(dyna<T> &);
T *operator&();
operator T&();
};
////////////////////////////////////////////////////////////////
// bit pointer data structures; allows us to step through the
// bits of an array of words relatively painlessly.
////////////////////////////////////////////////////////////////
struct BitSrc {
word32 *p;
int nbits;
word32 cur;
int ncur;
// Initialize the BitSrc to point to nbits bits starting at an
// offset of skip inside p.
BitSrc(word32 *p_,int nbits_,int skip=0):p(p_),nbits(nbits_) {
//#ifndef UNSAFE
// shut up bogus GNU C++ warnings
cur = 0;
ncur = 0;
//#endif
if(skip<0) throw "negative skip not allowed";
if(skip>=nbits) {nbits = 0; return;}
while(skip>=32) {
skip-=32; nbits-=32; p++;
}
//cur = 0;
//ncur = 0;
if(skip>0) getbits(skip);
}
// Check whether there are any bits left.
bool has_bits() {
return nbits>0;
}
// Check whether there are any words left.
bool has_words() {
return nbits>=32;
}
// Check whether the pointer is word-aligned.
bool is_wordaligned() {
ASSERT(ncur<32);
return ncur==0;
}
// Make sure there are bits left to be used.
void reload() {
if(ncur==0) {
cur = *p++;
ncur = 32;
}
}
// Number of bits that can be requested without moving to a new
// word.
int bits_left_in_word() {
return ncur<nbits ? ncur : nbits;
}
// Get one bit from the bit sequence.
word32 getbit() {
ASSERT(nbits>0 && ncur<32);
reload();
word32 result = !!(cur & 0x80000000);
cur <<= 1;
ncur--;
nbits--;
return result;
}
word32 peekbit() {
ASSERT(nbits>0 && ncur<32);
if(nbits==0) {
return !!(p[1] & 0x80000000);
} else {
return !!(cur & 0x80000000);
}
}
void skipbit() {
reload();
cur <<= 1;
ncur--;
nbits--;
}
bool get_run(int &start,int &end) {
if(nbits<1) return false;
start = 0;
word32 bit = 0;
while(nbits>0) {
bit = getbit();
if(bit) break;
if(is_wordaligned()) {
while(nbits>=32 && peekword_aligned()==0) {
skipword_aligned();
start += 32;
}
} else {
start++;
}
}
end = start;
while(nbits>0) {
bit = peekbit();
if(!bit) break;
skipbit();
if(is_wordaligned()) {
while(nbits>=32 && peekword_aligned()==0xffffffff) {
skipword_aligned();
end += 32;
}
} else {
end++;
}
}
return true;
}
// Get 16 bits at a time; the word must be aligned.
word32 get16() {
ASSERT(nbits>0 && ncur<32);
reload();
if(ncur==32) {
ncur = 16;
word32 result = cur>>16;
cur <<= 16;
return result;
} else if(ncur==16) {
ncur = 0;
word32 result = cur>>16;
return result;
} else {
throw "not 16bit aligned";
}
return 0;
}
// Get 32 bits from the bit sequence, assuming the bit pointer
// is 32 bit aligned. This is the fast case for inner loops.
word32 getword_aligned() {
ASSERT(nbits>=32 && ncur==0);
nbits -= 32;
return *p++;
}
word32 peekword_aligned() {
ASSERT(nbits>=32 && ncur==0);
return *p;
}
void skipword_aligned() {
ASSERT(nbits>=32 && ncur==0);
nbits -= 32;
p++;
}
// Get 32 bits from the bit sequence; the sequence need not be aligned.
// This is the second performance critical case for the inner loop.
word32 getword() {
ASSERT(nbits>=32 && ncur<32);
#if 1
if(ncur==0) return getword_aligned();
word32 result = cur; // already shifted
word32 temp = *p++;
result |= (temp >> ncur);
cur = (temp << (32-ncur));
nbits -= 32;
return result;
#else
// slow code for testing
word32 result = 0;
for(int i=0;i<32;i++)
result = (result<<1) | getbit();
return result;
#endif
}
// Get multiple bits (<=32).
word32 getbits(int n) {
ASSERT(nbits>=n && n<=32 && n>0);
reload();
if(n<=ncur) {
word32 result = (cur >> (32 - n));
cur <<= n;
ncur -= n;
nbits -= n;
return result;
}
word32 result = (cur >> (32 - n));
int remaining = n-ncur;
cur = *p++;
ncur = 32;
result |= getbits(remaining);
nbits -= n;
return result;
}
};
struct BitSnk {
word32 *p;
int nbits;
word32 cur;
int ncur;
static inline word32 rightmask(int nbits) {
return (1<<nbits)-1;
}
static inline word32 leftmask(int nbits) {
return ~rightmask(32-nbits);
}
// Create a bit pointer for writing. This starts skip bits into
// the sequence of 32 bit words pointed to by p and contains exactly
// nbits bits.
BitSnk(word32 *p_,int nbits_,int skip=0):p(p_),nbits(nbits_) {
//#ifndef UNSAFE
// shut up bogus GNU C++ warnings
cur = 0;
ncur = 0;
//#endif
if(skip<0) throw "negative skip not allowed";
if(skip>=nbits) {nbits = 0; return;}
while(skip>=32) { skip-=32; nbits-=32; p++; }
if(skip>0) {
cur = *p;
ncur = skip;
cur >>= 32-skip;
nbits -= skip;
// } else {
// ncur = 0;
// cur = 0;
}
}
~BitSnk() {
if(p) close();
}
// Write out any remaining bits.
void close() {
flush();
if(ncur==0) return;
*p = (cur << (32-ncur)) | (*p & rightmask(32-ncur));
ncur = 0;
p = 0;
}
// Check whether there is room to write any more bits.
bool has_bits() {
return nbits>0;
}
// Check whether there is room to write any more complete 32bit words.
bool has_words() {
return nbits>=32;
}
// Check whether the bit pointer is currently word aligned.
bool is_wordaligned() {
return ncur==0 || ncur==32;
}
// Flush a complete 32bit word if any (does nothing if the word
// isn't complete).
void flush() {
if(ncur==32) {
*p++ = cur;
ncur = 0;
}
}
// Check how many bits we can still write in the current word.
int bits_left_in_word() {
return ncur<nbits ? ncur : nbits;
}
// Put a single bit into the sequence.
void putbit(word32 bit) {
ASSERT(nbits>0 && unsigned(bit)<=1);
cur = (cur<<1) | bit;
ncur++;
flush();
nbits--;
}
void put_run(int size,int bit) {
CHECK_ARG(bit==0 || bit==1);
while(size>0 && !is_wordaligned()) { putbit(bit); size--; }
while(size>=32) {
putword_aligned(bit?0xffffffff:0);
size-=32;
}
while(size>0) { putbit(bit); size--; }
}
// Put a 32bit word into the sequence, assuming the sequence is
// aligned. This is the fast inner loop case.
void putword_aligned(word32 word) {
ASSERT(nbits>=32 && ncur==0);
nbits -= 32;
*p++ = word;
}
// Put a 32bit word into the sequence, even if it's not aligned.
// This is the second fast inner loop case.
void putword(word32 word) {
ASSERT(nbits>=32);
flush();
nbits -= 32;
if(ncur==0) {
*p++ = word;
return;
}
#if 1
*p++ = (cur << (32-ncur)) | (word >> ncur);
//cur = word & rightmask(ncur); // no need to clear high bits
cur = word;
#else
// slow code for testing
for(int i=0;i<32;i++) {
putbit(!!(word&0x80000000));
word <<= 1;
}
#endif
}
};
}
#endif
|