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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
|
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2009 Red Hat, Inc. and/or its affiliates.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
This file incorporates work covered by the following copyright and
permission notice:
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <config.h>
#include "lz.h"
#define HASH_LOG 13
#define HASH_SIZE (1 << HASH_LOG)
#define HASH_MASK (HASH_SIZE - 1)
/* Maximum image size, mainly to avoid possible integer overflows */
#define SPICE_MAX_IMAGE_SIZE (1024 * 1024 * 1024 - 1)
typedef struct LzImageSegment LzImageSegment;
struct LzImageSegment {
uint8_t *lines;
uint8_t *lines_end;
unsigned int size_delta; // total size of the previous segments in units of
// pixels for rgb and bytes for plt.
LzImageSegment *next;
};
// TODO: pack?
typedef struct HashEntry {
LzImageSegment *image_seg;
uint8_t *ref;
} HashEntry;
typedef struct Encoder {
LzUsrContext *usr;
LzImageType type;
const SpicePalette *palette; // for decoding images with palettes to rgb
int stride; // stride is in bytes. For rgb must be equal to
// width*bytes_per_pix.
// For palettes stride can be bigger than width/pixels_per_byte by 1 only if
// width%pixels_per_byte != 0.
int height;
int width; // the original width (in pixels)
LzImageSegment *head_image_segs;
LzImageSegment *tail_image_segs;
LzImageSegment *free_image_segs;
// the dictionary hash table is composed (1) a pointer to the segment the word was found in
// (2) a pointer to the first byte in the segment that matches the word
HashEntry htab[HASH_SIZE];
uint8_t *io_now;
uint8_t *io_end;
size_t io_bytes_count;
uint8_t *io_last_copy; // pointer to the last byte in which copy count was written
} Encoder;
/****************************************************/
/* functions for managing the pool of image segments*/
/****************************************************/
static inline LzImageSegment *lz_alloc_image_seg(Encoder *encoder);
static void lz_reset_image_seg(Encoder *encoder);
static int lz_read_image_segments(Encoder *encoder, uint8_t *first_lines,
unsigned int num_first_lines);
// return a free image segment if one exists. Make allocation if needed. adds it to the
// tail of the image segments lists
static inline LzImageSegment *lz_alloc_image_seg(Encoder *encoder)
{
LzImageSegment *ret;
if (encoder->free_image_segs) {
ret = encoder->free_image_segs;
encoder->free_image_segs = ret->next;
} else {
if (!(ret = (LzImageSegment *)encoder->usr->malloc(encoder->usr, sizeof(*ret)))) {
return NULL;
}
}
ret->next = NULL;
if (encoder->tail_image_segs) {
encoder->tail_image_segs->next = ret;
}
encoder->tail_image_segs = ret;
if (!encoder->head_image_segs) {
encoder->head_image_segs = ret;
}
return ret;
}
// adding seg to the head of free segments (lz_reset_image_seg removes it from used ones)
static inline void __lz_free_image_seg(Encoder *encoder, LzImageSegment *seg)
{
seg->next = encoder->free_image_segs;
encoder->free_image_segs = seg;
}
// moves all the used image segments to the free pool
static void lz_reset_image_seg(Encoder *encoder)
{
while (encoder->head_image_segs) {
LzImageSegment *seg = encoder->head_image_segs;
encoder->head_image_segs = seg->next;
__lz_free_image_seg(encoder, seg);
}
encoder->tail_image_segs = NULL;
}
static void lz_dealloc_free_segments(Encoder *encoder)
{
while (encoder->free_image_segs) {
LzImageSegment *seg = encoder->free_image_segs;
encoder->free_image_segs = seg->next;
encoder->usr->free(encoder->usr, seg);
}
}
// return FALSE when operation fails (due to failure in allocation)
static int lz_read_image_segments(Encoder *encoder, uint8_t *first_lines,
unsigned int num_first_lines)
{
LzImageSegment *image_seg;
uint32_t size_delta = 0;
unsigned int num_lines = num_first_lines;
uint8_t* lines = first_lines;
int row;
spice_return_val_if_fail(!encoder->head_image_segs, FALSE);
image_seg = lz_alloc_image_seg(encoder);
if (!image_seg) {
goto error_1;
}
image_seg->lines = lines;
image_seg->lines_end = lines + num_lines * encoder->stride;
image_seg->size_delta = size_delta;
size_delta += num_lines * encoder->stride / RGB_BYTES_PER_PIXEL[encoder->type];
for (row = num_first_lines; row < encoder->height; row += num_lines) {
num_lines = encoder->usr->more_lines(encoder->usr, &lines);
if (num_lines <= 0) {
encoder->usr->error(encoder->usr, "more lines failed\n");
}
image_seg = lz_alloc_image_seg(encoder);
if (!image_seg) {
goto error_1;
}
image_seg->lines = lines;
image_seg->lines_end = lines + num_lines * encoder->stride;
image_seg->size_delta = size_delta;
size_delta += num_lines * encoder->stride / RGB_BYTES_PER_PIXEL[encoder->type];
}
return TRUE;
error_1:
lz_reset_image_seg(encoder);
return FALSE;
}
/**************************************************************************
* Handling encoding and decoding of a byte
***************************************************************************/
static inline int more_io_bytes(Encoder *encoder)
{
uint8_t *io_ptr;
int num_io_bytes = encoder->usr->more_space(encoder->usr, &io_ptr);
encoder->io_bytes_count += num_io_bytes;
encoder->io_now = io_ptr;
encoder->io_end = encoder->io_now + num_io_bytes;
return num_io_bytes;
}
static inline void encode(Encoder *encoder, uint8_t byte)
{
if (encoder->io_now == encoder->io_end) {
if (more_io_bytes(encoder) <= 0) {
encoder->usr->error(encoder->usr, "%s: no more bytes\n", __FUNCTION__);
}
spice_return_if_fail(encoder->io_now);
}
spice_return_if_fail(encoder->io_now < encoder->io_end);
*(encoder->io_now++) = byte;
}
static inline void encode_32(Encoder *encoder, unsigned int word)
{
encode(encoder, (uint8_t)(word >> 24));
encode(encoder, (uint8_t)(word >> 16) & 0x0000ff);
encode(encoder, (uint8_t)(word >> 8) & 0x0000ff);
encode(encoder, (uint8_t)(word & 0x0000ff));
}
static inline void encode_copy_count(Encoder *encoder, uint8_t copy_count)
{
encode(encoder, copy_count);
encoder->io_last_copy = encoder->io_now - 1; // io_now cannot be the first byte of the buffer
}
static inline void update_copy_count(Encoder *encoder, uint8_t copy_count)
{
spice_return_if_fail(encoder->io_last_copy);
*(encoder->io_last_copy) = copy_count;
}
// decrease the io ptr by 1
static inline void compress_output_prev(Encoder *encoder)
{
// io_now cannot be the first byte of the buffer
encoder->io_now--;
// the function should be called only when copy count is written unnecessarily by lz_compress
spice_return_if_fail(encoder->io_now == encoder->io_last_copy);
}
static int encoder_reset(Encoder *encoder, uint8_t *io_ptr, uint8_t *io_ptr_end)
{
spice_return_val_if_fail(io_ptr <= io_ptr_end, FALSE);
encoder->io_bytes_count = io_ptr_end - io_ptr;
encoder->io_now = io_ptr;
encoder->io_end = io_ptr_end;
encoder->io_last_copy = NULL;
return TRUE;
}
static inline uint8_t decode(Encoder *encoder)
{
if (encoder->io_now == encoder->io_end) {
int num_io_bytes = more_io_bytes(encoder);
if (num_io_bytes <= 0) {
encoder->usr->error(encoder->usr, "%s: no more bytes\n", __FUNCTION__);
}
spice_assert(encoder->io_now);
}
spice_assert(encoder->io_now < encoder->io_end);
return *(encoder->io_now++);
}
static inline uint32_t decode_32(Encoder *encoder)
{
uint32_t word = 0;
word |= decode(encoder);
word <<= 8;
word |= decode(encoder);
word <<= 8;
word |= decode(encoder);
word <<= 8;
word |= decode(encoder);
return word;
}
static inline int is_io_to_decode_end(Encoder *encoder)
{
if (encoder->io_now != encoder->io_end) {
return FALSE;
} else {
int num_io_bytes = more_io_bytes(encoder); //disable inline optimizations
return (num_io_bytes <= 0);
}
}
/*******************************************************************
* intialization and finalization of lz
********************************************************************/
static int init_encoder(Encoder *encoder, LzUsrContext *usr)
{
encoder->usr = usr;
encoder->free_image_segs = NULL;
encoder->head_image_segs = NULL;
encoder->tail_image_segs = NULL;
return TRUE;
}
LzContext *lz_create(LzUsrContext *usr)
{
Encoder *encoder;
if (!usr || !usr->error || !usr->warn || !usr->info || !usr->malloc ||
!usr->free || !usr->more_space || !usr->more_lines) {
return NULL;
}
if (!(encoder = (Encoder *)usr->malloc(usr, sizeof(Encoder)))) {
return NULL;
}
if (!init_encoder(encoder, usr)) {
usr->free(usr, encoder);
return NULL;
}
return (LzContext *)encoder;
}
void lz_destroy(LzContext *lz)
{
Encoder *encoder = (Encoder *)lz;
if (!lz) {
return;
}
if (encoder->head_image_segs) {
encoder->usr->error(encoder->usr, "%s: used_image_segments not empty\n", __FUNCTION__);
lz_reset_image_seg(encoder);
}
lz_dealloc_free_segments(encoder);
encoder->usr->free(encoder->usr, encoder);
}
/*******************************************************************
* encoding and decoding the image
********************************************************************/
/*
* Give hints to the compiler for branch prediction optimization.
*/
#if defined(__GNUC__) && (__GNUC__ > 2)
#define LZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
#define LZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
#else
#define LZ_EXPECT_CONDITIONAL(c) (c)
#define LZ_UNEXPECT_CONDITIONAL(c) (c)
#endif
#include <spice/start-packed.h>
/* the palette images will be treated as one byte pixels. Their width should be transformed
accordingly.
*/
typedef struct SPICE_ATTR_PACKED one_byte_pixel_t {
uint8_t a;
} one_byte_pixel_t;
typedef struct SPICE_ATTR_PACKED rgb32_pixel_t {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t pad;
} rgb32_pixel_t;
typedef struct SPICE_ATTR_PACKED rgb24_pixel_t {
uint8_t b;
uint8_t g;
uint8_t r;
} rgb24_pixel_t;
typedef uint16_t rgb16_pixel_t;
#include <spice/end-packed.h>
#define MAX_COPY 32
#define MAX_LEN 264 /* 256 + 8 */
#define BOUND_OFFSET 2
#define LIMIT_OFFSET 6
#define MIN_FILE_SIZE 4
#define COMP_LEVEL_SIZE_LIMIT 65536
// TODO: implemented lz2. should lz1 be an option (no RLE + distance limitation of MAX_DISTANCE)
// TODO: I think MAX_FARDISTANCE can be changed easily to 2^29
// (and maybe even more when pixel > byte).
// i.e. we can support 512M Bytes/Pixels distance instead of only ~68K.
#define MAX_DISTANCE 8191 // 2^13
#define MAX_FARDISTANCE (65535 + MAX_DISTANCE - 1) // ~2^16+2^13
#define LZ_PLT
#include "lz_compress_tmpl.c"
#define LZ_PLT
#include "lz_decompress_tmpl.c"
#define LZ_PLT
#define PLT8
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_PLT
#define PLT4_BE
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_PLT
#define PLT4_LE
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_PLT
#define PLT1_BE
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_PLT
#define PLT1_LE
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_A8
#include "lz_compress_tmpl.c"
#define LZ_A8
#include "lz_decompress_tmpl.c"
#define LZ_A8
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_RGB16
#include "lz_compress_tmpl.c"
#define LZ_RGB16
#include "lz_decompress_tmpl.c"
#define LZ_RGB16
#define TO_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_RGB24
#include "lz_compress_tmpl.c"
#define LZ_RGB24
#include "lz_decompress_tmpl.c"
#define LZ_RGB32
#include "lz_compress_tmpl.c"
#define LZ_RGB32
#include "lz_decompress_tmpl.c"
#define LZ_RGB_ALPHA
#include "lz_compress_tmpl.c"
#define LZ_RGB_ALPHA
#include "lz_decompress_tmpl.c"
#undef LZ_UNEXPECT_CONDITIONAL
#undef LZ_EXPECT_CONDITIONAL
static void lz_set_sizes(Encoder *encoder, int type, int width, int height, int stride)
{
if (width < 0) {
encoder->usr->error(encoder->usr, "invalid lz width %d\n", width);
}
if (height < 0) {
encoder->usr->error(encoder->usr, "invalid lz height %d\n", height);
}
if (stride < 0) {
encoder->usr->error(encoder->usr, "invalid lz stride %d\n", stride);
}
if (IS_IMAGE_TYPE_PLT[type]) {
if (stride > (width / PLT_PIXELS_PER_BYTE[type])) {
if (((width % PLT_PIXELS_PER_BYTE[type]) == 0) || (
(stride - (width / PLT_PIXELS_PER_BYTE[type])) > 1)) {
encoder->usr->error(encoder->usr, "stride overflows (plt)\n");
}
}
} else {
if (stride != width * RGB_BYTES_PER_PIXEL[type]) {
encoder->usr->error(encoder->usr, "stride != width*bytes_per_pixel (rgb) %d != %d * %d (%d)\n",
stride, width, RGB_BYTES_PER_PIXEL[type],
type);
}
}
// avoid too big images
if ((uint64_t) stride * height > SPICE_MAX_IMAGE_SIZE) {
encoder->usr->error(encoder->usr, "image too large\n");
}
encoder->type = type;
encoder->width = width;
encoder->height = height;
encoder->stride = stride;
}
int lz_encode(LzContext *lz, LzImageType type, int width, int height, int top_down,
uint8_t *lines, unsigned int num_lines, int stride,
uint8_t *io_ptr, unsigned int num_io_bytes)
{
Encoder *encoder = (Encoder *)lz;
uint8_t *io_ptr_end = io_ptr + num_io_bytes;
lz_set_sizes(encoder, type, width, height, stride);
// assign the output buffer
if (!encoder_reset(encoder, io_ptr, io_ptr_end)) {
encoder->usr->error(encoder->usr, "lz encoder io reset failed\n");
}
// first read the list of the image segments
if (!lz_read_image_segments(encoder, lines, num_lines)) {
encoder->usr->error(encoder->usr, "lz encoder reading image segments failed\n");
}
encode_32(encoder, LZ_MAGIC);
encode_32(encoder, LZ_VERSION);
encode_32(encoder, type);
encode_32(encoder, width);
encode_32(encoder, height);
encode_32(encoder, stride);
encode_32(encoder, top_down); // TODO: maybe compress type and top_down to one byte
switch (encoder->type) {
case LZ_IMAGE_TYPE_PLT1_BE:
case LZ_IMAGE_TYPE_PLT1_LE:
case LZ_IMAGE_TYPE_PLT4_BE:
case LZ_IMAGE_TYPE_PLT4_LE:
case LZ_IMAGE_TYPE_PLT8:
lz_plt_compress(encoder);
break;
case LZ_IMAGE_TYPE_RGB16:
lz_rgb16_compress(encoder);
break;
case LZ_IMAGE_TYPE_RGB24:
lz_rgb24_compress(encoder);
break;
case LZ_IMAGE_TYPE_RGB32:
lz_rgb32_compress(encoder);
break;
case LZ_IMAGE_TYPE_RGBA:
lz_rgb32_compress(encoder);
lz_rgb_alpha_compress(encoder);
break;
case LZ_IMAGE_TYPE_XXXA:
lz_rgb_alpha_compress(encoder);
break;
case LZ_IMAGE_TYPE_A8:
lz_a8_compress(encoder);
break;
case LZ_IMAGE_TYPE_INVALID:
default:
encoder->usr->error(encoder->usr, "bad image type\n");
}
// move all the used segments to the free ones
lz_reset_image_seg(encoder);
encoder->io_bytes_count -= (encoder->io_end - encoder->io_now);
return encoder->io_bytes_count;
}
/*
initialize and read lz magic
*/
void lz_decode_begin(LzContext *lz, uint8_t *io_ptr, unsigned int num_io_bytes,
LzImageType *out_type, int *out_width, int *out_height,
int *out_n_pixels, int *out_top_down, const SpicePalette *palette)
{
Encoder *encoder = (Encoder *)lz;
uint8_t *io_ptr_end = io_ptr + num_io_bytes;
uint32_t magic;
uint32_t version;
if (!encoder_reset(encoder, io_ptr, io_ptr_end)) {
encoder->usr->error(encoder->usr, "io reset failed");
}
magic = decode_32(encoder);
if (magic != LZ_MAGIC) {
encoder->usr->error(encoder->usr, "bad magic\n");
}
version = decode_32(encoder);
if (version != LZ_VERSION) {
encoder->usr->error(encoder->usr, "bad version\n");
}
int type = decode_32(encoder);
if (type <= LZ_IMAGE_TYPE_INVALID || type > LZ_IMAGE_TYPE_A8) {
encoder->usr->error(encoder->usr, "invalid lz type %d\n", type);
}
int width = decode_32(encoder);
int height = decode_32(encoder);
int stride = decode_32(encoder);
lz_set_sizes(encoder, type, width, height, stride);
*out_top_down = decode_32(encoder);
*out_width = encoder->width;
*out_height = encoder->height;
// *out_stride = encoder->stride;
*out_type = encoder->type;
// TODO: maybe instead of stride we can encode out_n_pixels
// (if stride is not necessary in decoding).
if (IS_IMAGE_TYPE_PLT[encoder->type]) {
encoder->palette = palette;
*out_n_pixels = encoder->stride * PLT_PIXELS_PER_BYTE[encoder->type] * encoder->height;
} else {
*out_n_pixels = encoder->width * encoder->height;
}
}
void lz_decode(LzContext *lz, LzImageType to_type, uint8_t *buf)
{
Encoder *encoder = (Encoder *)lz;
size_t out_size = 0;
size_t alpha_size = 0;
size_t size = 0;
if (IS_IMAGE_TYPE_PLT[encoder->type]) {
if (to_type == encoder->type) {
size = encoder->height * encoder->stride;
out_size = lz_plt_decompress(encoder, (one_byte_pixel_t *)buf, size);
} else if (to_type == LZ_IMAGE_TYPE_RGB32) {
size = encoder->height * encoder->stride * PLT_PIXELS_PER_BYTE[encoder->type];
if (!encoder->palette) {
encoder->usr->error(encoder->usr,
"a palette is missing (for bpp to rgb decoding)\n");
return;
}
switch (encoder->type) {
case LZ_IMAGE_TYPE_PLT1_BE:
out_size = lz_plt1_be_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
break;
case LZ_IMAGE_TYPE_PLT1_LE:
out_size = lz_plt1_le_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
break;
case LZ_IMAGE_TYPE_PLT4_BE:
out_size = lz_plt4_be_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
break;
case LZ_IMAGE_TYPE_PLT4_LE:
out_size = lz_plt4_le_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
break;
case LZ_IMAGE_TYPE_PLT8:
out_size = lz_plt8_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
break;
case LZ_IMAGE_TYPE_RGB16:
case LZ_IMAGE_TYPE_RGB24:
case LZ_IMAGE_TYPE_RGB32:
case LZ_IMAGE_TYPE_RGBA:
case LZ_IMAGE_TYPE_XXXA:
case LZ_IMAGE_TYPE_INVALID:
default:
encoder->usr->error(encoder->usr, "bad image type\n");
}
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
} else {
size = encoder->height * encoder->width;
switch (encoder->type) {
case LZ_IMAGE_TYPE_RGB16:
if (encoder->type == to_type) {
out_size = lz_rgb16_decompress(encoder, (rgb16_pixel_t *)buf, size);
} else if (to_type == LZ_IMAGE_TYPE_RGB32) {
out_size = lz_rgb16_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
break;
case LZ_IMAGE_TYPE_RGB24:
if (encoder->type == to_type) {
out_size = lz_rgb24_decompress(encoder, (rgb24_pixel_t *)buf, size);
} else if (to_type == LZ_IMAGE_TYPE_RGB32) {
out_size = lz_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
break;
case LZ_IMAGE_TYPE_RGB32:
if (encoder->type == to_type) {
out_size = lz_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
break;
case LZ_IMAGE_TYPE_RGBA:
if (encoder->type == to_type) {
out_size = lz_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
alpha_size = lz_rgb_alpha_decompress(encoder, (rgb32_pixel_t *)buf, size);
spice_assert(alpha_size == size);
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
break;
case LZ_IMAGE_TYPE_XXXA:
if (encoder->type == to_type) {
alpha_size = lz_rgb_alpha_decompress(encoder, (rgb32_pixel_t *)buf, size);
out_size = alpha_size;
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
break;
case LZ_IMAGE_TYPE_A8:
if (encoder->type == to_type) {
alpha_size = lz_a8_decompress(encoder, (one_byte_pixel_t *)buf, size);
out_size = alpha_size;
} else if (to_type == LZ_IMAGE_TYPE_RGB32) {
alpha_size = lz_a8_to_rgb32_decompress(encoder, (rgb32_pixel_t *)buf, size);
out_size = alpha_size;
} else {
encoder->usr->error(encoder->usr, "unsupported output format\n");
}
break;
case LZ_IMAGE_TYPE_PLT1_LE:
case LZ_IMAGE_TYPE_PLT1_BE:
case LZ_IMAGE_TYPE_PLT4_LE:
case LZ_IMAGE_TYPE_PLT4_BE:
case LZ_IMAGE_TYPE_PLT8:
case LZ_IMAGE_TYPE_INVALID:
default:
encoder->usr->error(encoder->usr, "bad image type\n");
}
}
spice_assert(is_io_to_decode_end(encoder));
spice_assert(out_size == size);
if (out_size != size) {
encoder->usr->error(encoder->usr, "bad decode size\n");
}
}
|