Changeset 124 for abuse/trunk/src/imlib/decoder.cpp
- Timestamp:
- Mar 18, 2008, 9:36:56 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
abuse/trunk/src/imlib/decoder.cpp
r112 r124 121 121 { 122 122 if (navail_bytes <= 0) 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 123 { 124 125 /* Out of bytes in current block, so read next block 126 */ 127 pbytes = byte_buff; 128 if ((navail_bytes = get_byte()) < 0) 129 return(navail_bytes); 130 else if (navail_bytes) 131 { 132 for (i = 0; i < navail_bytes; ++i) 133 { 134 if ((x = get_byte()) < 0) 135 return(x); 136 byte_buff[i] = x; 137 } 138 } 139 } 140 140 b1 = *pbytes++; 141 141 nbits_left = 8; … … 147 147 { 148 148 if (navail_bytes <= 0) 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 149 { 150 151 /* Out of bytes in current block, so read next block 152 */ 153 pbytes = byte_buff; 154 if ((navail_bytes = get_byte()) < 0) 155 return(navail_bytes); 156 else if (navail_bytes) 157 { 158 for (i = 0; i < navail_bytes; ++i) 159 { 160 if ((x = get_byte()) < 0) 161 return(x); 162 byte_buff[i] = x; 163 } 164 } 165 } 166 166 b1 = *pbytes++; 167 167 ret |= b1 << nbits_left; … … 252 252 */ 253 253 if (c < 0) 254 255 // 256 257 254 { 255 // free(buf); 256 return(0); 257 } 258 258 259 259 /* If the code is a clear code, reinitialize all necessary items. 260 260 */ 261 261 if (c == clear) 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 262 { 263 curr_size = size + 1; 264 slot = newcodes; 265 top_slot = 1 << curr_size; 266 267 /* Continue reading codes until we get a non-clear code 268 * (Another unlikely, but possible case...) 269 */ 270 while ((c = get_next_code()) == clear) 271 ; 272 273 /* If we get an ending code immediately after a clear code 274 * (Yet another unlikely case), then break out of the loop. 275 */ 276 if (c == ending) 277 break; 278 279 /* Finally, if the code is beyond the range of already set codes, 280 * (This one had better NOT happen... I have no idea what will 281 * result from this, but I doubt it will look good...) then set it 282 * to color zero. 283 */ 284 CONDITION(c<slot,"Error occurred while reading gif"); 285 if (c >= slot) 286 c = 0; 287 288 oc = fc = c; 289 290 /* And let us not forget to put the char into the buffer... And 291 * if, on the off chance, we were exactly one pixel from the end 292 * of the line, we have to send the buffer to the out_line() 293 * routine... 294 */ 295 *bufptr++ = c; 296 if (--bufcnt == 0) 297 { 298 299 // if ((ret = out_line(buf, linewidth)) < 0) 300 // { 301 // free(buf); 302 // return(ret); 303 // } 304 y++; 305 if (y<im->height()) 306 buf=im->scan_line(y); 307 bufptr = buf; 308 bufcnt = im->width()-1; 309 } 310 } 311 311 else 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 312 { 313 314 /* In this case, it's not a clear code or an ending code, so 315 * it must be a code code... So we can now decode the code into 316 * a stack of character codes. (Clear as mud, right?) 317 */ 318 code = c; 319 320 /* Here we go again with one of those off chances... If, on the 321 * off chance, the code we got is beyond the range of those already 322 * set up (Another thing which had better NOT happen...) we trick 323 * the decoder into thinking it actually got the last code read. 324 * (Hmmn... I'm not sure why this works... But it does...) 325 */ 326 327 if (code >= slot) 328 { 329 if (code > slot) 330 ++bad_code_count; 331 code = oc; 332 *sp++ = fc; 333 } 334 335 /* Here we scan back along the linked list of prefixes, pushing 336 * helpless characters (ie. suffixes) onto the stack as we do so. 337 */ 338 while (code >= newcodes) 339 { 340 *sp++ = suffix[code]; 341 code = prefix[code]; 342 } 343 344 /* Push the last character on the stack, and set up the new 345 * prefix and suffix, and if the required slot number is greater 346 * than that allowed by the current bit size, increase the bit 347 * size. (NOTE - If we are all full, we *don't* save the new 348 * suffix and prefix... I'm not certain if this is correct... 349 * it might be more proper to overwrite the last code... 350 */ 351 *sp++ = code; 352 if (slot < top_slot) 353 { 354 suffix[slot] = fc = code; 355 prefix[slot++] = oc; 356 oc = c; 357 } 358 if (slot >= top_slot) 359 if (curr_size < 12) 360 { 361 top_slot <<= 1; 362 ++curr_size; 363 } 364 365 /* Now that we've pushed the decoded string (in reverse order) 366 * onto the stack, lets pop it off and put it into our decode 367 * buffer... And when the decode buffer is full, write another 368 * line... 369 */ 370 while (sp > stack) 371 { 372 *bufptr++ = *(--sp); 373 if (--bufcnt == 0) 374 { 375 /* if ((ret = out_line(buf, linewidth)) < 0) 376 { 377 free(buf); 378 return(ret); 379 } */ 380 y++; 381 if (y<im->height()) 382 buf=im->scan_line(y); 383 384 bufptr = buf; 385 bufcnt = im->width()-1; 386 } 387 } 388 } 389 389 } 390 390 ret = 0;
Note: See TracChangeset
for help on using the changeset viewer.