Bitmap_RLE16.cpp
Go to the documentation of this file.00001 //*** Bitmap_RLE16.cpp *** 00002 00003 #include "Bitmap_RLE16.h" 00004 #include "ColorHelper.h" 00005 #include "Image.h" 00006 #include "Asset.h" 00007 #include "GenerateRLE16.h" 00008 00009 00010 00011 //*** GetType *** 00012 00013 StringId Bitmap_RLE16::GetType() 00014 { 00015 static StringId idBitmap_RLE16("Bitmap_RLE16"); 00016 return idBitmap_RLE16; 00017 } 00018 00019 00020 //*** Constructor *** 00021 00022 Bitmap_RLE16::Bitmap_RLE16(): 00023 opaqueSize_(0), 00024 opaqueData_(0), 00025 alphaSize_(0), 00026 alphaData_(0) 00027 { 00028 } 00029 00030 00031 //*** Constructor *** 00032 00033 Bitmap_RLE16::Bitmap_RLE16(const Asset& asset): 00034 opaqueSize_(0), 00035 opaqueData_(0), 00036 alphaSize_(0), 00037 alphaData_(0) 00038 { 00039 Load(asset); 00040 } 00041 00042 00043 //*** Constructor *** 00044 00045 Bitmap_RLE16::Bitmap_RLE16(const Image& image, bool dither): 00046 opaqueSize_(0), 00047 opaqueData_(0), 00048 alphaSize_(0), 00049 alphaData_(0) 00050 { 00051 width_=image.GetWidth(); 00052 height_=image.GetHeight(); 00053 GenerateRLE16 generate(image,this,dither); 00054 } 00055 00056 00057 //*** Destructor *** 00058 00059 Bitmap_RLE16::~Bitmap_RLE16() 00060 { 00061 if (opaqueData_) 00062 { 00063 Free(opaqueData_); 00064 } 00065 00066 if (alphaData_) 00067 { 00068 Free(alphaData_); 00069 } 00070 } 00071 00072 00073 //*** Save *** 00074 00075 void Bitmap_RLE16::Save(Asset& asset) const 00076 { 00077 if (asset.Create()) 00078 { 00079 asset.Write("PIXRLE16",8); 00080 int version=0; 00081 asset.Write(&version); 00082 int celCount=0; 00083 asset.Write(&celCount); 00084 WriteToAsset(&asset); 00085 asset.Close(); 00086 } 00087 00088 } 00089 00090 00091 //*** Load *** 00092 00093 void Bitmap_RLE16::Load(const Asset& asset) 00094 { 00095 if (asset.Open()) 00096 { 00097 char header[8]; 00098 asset.Read(header,8); 00099 00100 if (StrNCmp(header,"PIXRLE16",8)==0) 00101 { 00102 int version=0; 00103 asset.Read(&version); 00104 if (version==0) 00105 { 00106 int celCount=0; 00107 asset.Read(&celCount); 00108 ReadFromAsset(&asset); 00109 } 00110 } 00111 00112 // Check for old format too 00113 else if (StrNCmp(header,"PIXIE_RL",8)==0) 00114 { 00115 // Read the extra header bytes (BM) 00116 char c[3]; 00117 asset.Read(c,3); 00118 Assert(StrNCmp(c,"E16",3)==0,"Invalid header"); 00119 if (StrNCmp(c,"E16",3)!=0) 00120 { 00121 asset.Close(); 00122 return; 00123 } 00124 00125 int version=0; 00126 asset.Read(&version); 00127 if (version==0) 00128 { 00129 ReadFromAsset(&asset); 00130 } 00131 } 00132 00133 else 00134 { 00135 Assert(false,"Invalid header"); 00136 } 00137 00138 asset.Close(); 00139 } 00140 // Report missing file 00141 #ifdef _DEBUG 00142 else 00143 { 00144 const char* filename=asset.GetFilename().GetString(); 00145 if (filename) 00146 { 00147 char errorMessage[1024]; 00148 SNPrintF(errorMessage,1024,"File not found: %s",filename); 00149 Assert(false,errorMessage); 00150 } 00151 else 00152 { 00153 Assert(false,"An asset could not be accessed."); 00154 } 00155 } 00156 #endif 00157 } 00158 00159 00160 //*** ReadFromAsset *** 00161 00162 void Bitmap_RLE16::ReadFromAsset(const Asset* asset) 00163 { 00164 asset->Read(&width_); 00165 asset->Read(&height_); 00166 asset->Read(&hPitch_); 00167 asset->Read(&vPitch_); 00168 asset->Read(&opaqueSize_); 00169 if (opaqueSize_>0) 00170 { 00171 opaqueData_=static_cast<unsigned char*>(Malloc(opaqueSize_)); 00172 asset->Read(opaqueData_,opaqueSize_); 00173 } 00174 asset->Read(&alphaSize_); 00175 if (alphaSize_>0) 00176 { 00177 alphaData_=static_cast<unsigned char*>(Malloc(alphaSize_)); 00178 asset->Read(alphaData_,alphaSize_); 00179 } 00180 asset->Read(&hOffset_,1); 00181 asset->Read(&vOffset_,1); 00182 } 00183 00184 00185 //*** WriteToAsset *** 00186 00187 void Bitmap_RLE16::WriteToAsset(Asset* asset) const 00188 { 00189 asset->Write(&width_); 00190 asset->Write(&height_); 00191 asset->Write(&hPitch_); 00192 asset->Write(&vPitch_); 00193 asset->Write(&opaqueSize_); 00194 if (opaqueSize_>0 && opaqueData_) 00195 { 00196 asset->Write(opaqueData_,opaqueSize_); 00197 } 00198 asset->Write(&alphaSize_); 00199 if (alphaSize_>0 && alphaData_) 00200 { 00201 asset->Write(alphaData_,alphaSize_); 00202 } 00203 asset->Write(&hOffset_); 00204 asset->Write(&vOffset_); 00205 } 00206 00207 00208 //*** GetWidth *** 00209 00210 int Bitmap_RLE16::GetWidth(Transformation transformation) const 00211 { 00212 switch (transformation) 00213 { 00214 case NoTransformation: 00215 case Rotate_180: 00216 case Mirror_X: 00217 case Mirror_Y: 00218 { 00219 return width_; 00220 } break; 00221 00222 case Rotate_90: 00223 case Rotate_270: 00224 { 00225 return height_; 00226 } break; 00227 } 00228 00229 return width_; 00230 } 00231 00232 00233 //*** GetHeight *** 00234 00235 int Bitmap_RLE16::GetHeight(Transformation transformation) const 00236 { 00237 switch (transformation) 00238 { 00239 case NoTransformation: 00240 case Rotate_180: 00241 case Mirror_X: 00242 case Mirror_Y: 00243 { 00244 return height_; 00245 } break; 00246 00247 case Rotate_90: 00248 case Rotate_270: 00249 { 00250 return width_; 00251 } break; 00252 } 00253 00254 return height_; 00255 } 00256 00257 00258 //*** GetColorData *** 00259 00260 unsigned short* Bitmap_RLE16::GetColorData() 00261 { 00262 return 0; 00263 } 00264 00265 00266 //*** GetAlphaData *** 00267 00268 unsigned char* Bitmap_RLE16::GetAlphaData() 00269 { 00270 return 0; 00271 } 00272 00273 00274 //*** Clear *** 00275 00276 void Bitmap_RLE16::Clear() 00277 { 00278 } 00279 00280 00281 //*** Fill *** 00282 00283 void Bitmap_RLE16::Fill(int x1, int y1, int x2, int y2, unsigned short color, unsigned char alpha) 00284 { 00285 } 00286 00287 00288 //*** Fill *** 00289 00290 void Bitmap_RLE16::Fill(unsigned short color, unsigned char alpha) 00291 { 00292 } 00293 00294 00295 00296 //*** SetPixelColor *** 00297 00298 void Bitmap_RLE16::SetPixelColor(int x, int y,unsigned short color, Transformation transformation) 00299 { 00300 } 00301 00302 00303 //*** GetPixelColor *** 00304 00305 unsigned short Bitmap_RLE16::GetPixelColor(int x, int y, Transformation transformation) const 00306 { 00307 // if (x>=0 && x<GetWidth(transformation) && y>=0 && y<GetHeight(transformation)) 00308 // { 00309 // TransformCoordinates(x,y,transformation); 00310 // return RLEGetPixelColor(x,y); 00311 // } 00312 00313 return 0; 00314 } 00315 00316 00317 //*** SetPixelAlpha *** 00318 00319 void Bitmap_RLE16::SetPixelAlpha(int x, int y,unsigned char alpha, Transformation transformation) 00320 { 00321 } 00322 00323 00324 //*** GetPixelAlpha *** 00325 00326 unsigned char Bitmap_RLE16::GetPixelAlpha(int x, int y, Transformation transformation) const 00327 { 00328 // if (x>=0 && x<GetWidth(transformation) && y>=0 && y<GetHeight(transformation)) 00329 // { 00330 // TransformCoordinates(x,y,transformation); 00331 // return RLEGetPixelAlpha(x,y); 00332 // } 00333 00334 return 0; 00335 } 00336 00337 00338 //*** BlendPixel *** 00339 00340 void Bitmap_RLE16::BlendPixel(int x, int y,unsigned short color, unsigned char alpha, Transformation transformation) 00341 { 00342 } 00343 00344 00345 //*** Blit *** 00346 00347 void Bitmap_RLE16::Blit(Bitmap& target, int x, int y, unsigned short modulate, unsigned char alpha, Transformation transformation) const 00348 { 00349 Blit(0, 0, width_-1, height_-1, target, x, y, modulate, alpha, transformation); 00350 } 00351 00352 00353 //*** Blit *** 00354 00355 void Bitmap_RLE16::Blit(int x1, int y1, int x2, int y2, Bitmap& target, int x, int y, unsigned short modulate, unsigned char alpha, Transformation transformation) const 00356 { 00357 BlitRLE(target,x,y); 00358 } 00359 00360 00361 //*** Copy *** 00362 00363 void Bitmap_RLE16::Copy(Bitmap& target, int x, int y, unsigned short modulate, Transformation transformation) const 00364 { 00365 } 00366 00367 00368 //*** Copy *** 00369 00370 void Bitmap_RLE16::Copy(int x1, int y1, int x2, int y2, Bitmap& target, int x, int y, unsigned short modulate, Transformation transformation) const 00371 { 00372 } 00373 00374 00375 //*** Blit *** 00376 00377 void Bitmap_RLE16::BlitRLE(Bitmap& target, int xp, int yp) const 00378 { 00379 unsigned short* data=reinterpret_cast<unsigned short*>(opaqueData_); 00380 int size=opaqueSize_; 00381 unsigned short* dest=target.GetColorData(); 00382 dest+=hOffset_+target.GetWidth()*(vOffset_+yp)+xp; 00383 int xdelta=target.GetWidth()-hPitch_; 00384 int x=0; 00385 while (size>0) 00386 { 00387 unsigned short blank=*data; 00388 data++; 00389 size-=2; 00390 dest+=blank; 00391 x+=blank; 00392 if (x>=hPitch_) 00393 { 00394 x-=hPitch_; 00395 dest+=xdelta; 00396 } 00397 00398 unsigned short nonblank=*data; 00399 data++; 00400 size-=2; 00401 while (nonblank>0) 00402 { 00403 nonblank--; 00404 unsigned short col=*data; 00405 data++; 00406 size-=2; 00407 *dest=col; 00408 dest++; 00409 x++; 00410 if (x>=hPitch_) 00411 { 00412 x-=hPitch_; 00413 dest+=xdelta; 00414 } 00415 } 00416 } 00417 00418 unsigned char* alphadata=alphaData_; 00419 size=alphaSize_; 00420 dest=target.GetColorData(); 00421 dest+=hOffset_+target.GetWidth()*(vOffset_+yp)+xp; 00422 x=0; 00423 while (size>0) 00424 { 00425 unsigned short blank=*reinterpret_cast<unsigned short*>(alphadata); 00426 alphadata+=2; 00427 size-=2; 00428 dest+=blank; 00429 x+=blank; 00430 if (x>=hPitch_) 00431 { 00432 x-=hPitch_; 00433 dest+=xdelta; 00434 } 00435 00436 unsigned short nonblank=*reinterpret_cast<unsigned short*>(alphadata); 00437 alphadata+=2; 00438 size-=2; 00439 while (nonblank>0) 00440 { 00441 nonblank--; 00442 unsigned short col=*reinterpret_cast<unsigned short*>(alphadata); 00443 alphadata+=2; 00444 size-=2; 00445 unsigned char alpha=*static_cast<unsigned char*>(alphadata); 00446 alphadata+=1; 00447 size-=1; 00448 *dest=AlphaBlend16(*dest,col,alpha); 00449 dest++; 00450 x++; 00451 if (x>=hPitch_) 00452 { 00453 x-=hPitch_; 00454 dest+=xdelta; 00455 } 00456 } 00457 } 00458 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
