00001 /* 00002 Copyright (c) 2002 Xavier Trochu 00003 00004 This software is provided 'as-is', without any express or implied warranty. In no event 00005 will the authors be held liable for any damages arising from the use of this software. 00006 00007 Permission is granted to anyone to use this software for any purpose, including commercial 00008 applications, and to alter it and redistribute it freely, subject to the following 00009 restrictions: 00010 00011 1. The origin of this software must not be misrepresented; you must not claim that you 00012 wrote the original software. If you use this software in a product, an acknowledgment 00013 in the product documentation would be appreciated but is not required. 00014 00015 2. Altered source versions must be plainly marked as such, and must not be misrepresented 00016 as being the original software. 00017 00018 3. This notice may not be removed or altered from any source distribution. 00019 */ 00020 #include "codec1.h" 00021 #include "chunck.h" 00022 #include "blitter.h" 00023 00024 #ifdef DEBUG 00025 #include <iostream> 00026 #endif 00027 00028 using namespace std; 00029 00030 codec1_decoder::~codec1_decoder() throw() { 00031 } 00032 00033 /* 00034 bool codec1_decoder::decode(blitter & dst, chunck & src) throw(exception) { 00035 #ifdef DEBUG 00036 cout << "codec1_decoder::decode()" << endl; 00037 try { 00038 #endif 00039 int width = get_width(); 00040 int height = get_height(); 00041 #ifdef DEBUG_CODEC1 00042 int decoded_size = 0; 00043 int coded_size = 0; 00044 #endif 00045 int size = width * height; 00046 int coded_length = src.get_size() - 14; 00047 00048 while(coded_length > 1 && size > 0) { 00049 int code = src.get_byte(); 00050 int length = (code >> 1) + 1; 00051 size -= length; 00052 #ifdef DEBUG_CODEC1 00053 decoded_size += length; 00054 coded_size += 1 + ((code & 1) ? 1 : length); 00055 cout << "decodeCodec1() : code == " << code 00056 << " : length == " << length 00057 << " : decoded_size == " << decoded_size 00058 << " : coded_size == " << coded_size 00059 << " : seek - header == " << src.tell() - 15 00060 << " : size == " << size + decoded_size 00061 << " : coded_length == " << coded_length 00062 << endl; 00063 #endif 00064 if(code & 1) { 00065 coded_length -= 2; 00066 char color = src.get_char(); 00067 dst.put(color, length); 00068 } else { 00069 if(length + 1 > coded_length) length = coded_length - 1; 00070 coded_length -= 1 + length; 00071 while(length--) { 00072 dst.put(src.get_char()); 00073 } 00074 } 00075 } 00076 #ifdef DEBUG 00077 } catch(const exception & e) { 00078 cout << "Exception occured during decodeCodec1() : " << e.what() << endl; 00079 } 00080 #endif 00081 } 00082 00083 */ 00084 00085 bool codec1_decoder::decode(blitter & dst, chunck & src) throw(exception) { 00086 int val; 00087 int size_line; 00088 int code, length; 00089 int h, height = get_rect().height(); 00090 00091 for(h = 0; h < height; h++) 00092 { 00093 size_line = src.get_word(); // size of compressed line ! 00094 #ifdef DEBUG_CODEC1 00095 cout << "codec1 : h == " << h << " : size_line == " << size_line << endl; 00096 #endif 00097 while(size_line > 0) { 00098 code = src.get_byte(); 00099 size_line --; 00100 length = (code >> 1) + 1; 00101 #ifdef DEBUG_CODEC1 00102 cout << "codec1 : length == " << length << endl; 00103 #endif 00104 if(code & 1) { 00105 val = src.get_byte(); 00106 size_line --; 00107 if(val) dst.put(val, length); 00108 else dst.advance(length); 00109 #ifdef DEBUG_CODEC1 00110 cout << "codec1 : blitting " << length << " times " << val << endl; 00111 #endif 00112 } else { 00113 size_line -= length; 00114 #ifdef DEBUG_CODEC1 00115 cout << "codec1 : blitting " << length << " entries" << endl; 00116 #endif 00117 while(length--) { 00118 val = src.get_byte(); 00119 if(val) dst.put(val); 00120 else dst.advance(); 00121 } 00122 } 00123 } 00124 } 00125 #ifdef DEBUG_CODEC1 00126 if(!src.eof()) { 00127 int len = src.get_size() - src.tell(); 00128 cout << "codec1: remaining length after decode == " << len << endl; 00129 } 00130 #endif 00131 return true; 00132 }