Image.cpp
Go to the documentation of this file.00001 //*** Image.cpp *** 00002 00003 #include "Image.h" 00004 #include "Debug.h" 00005 #include "StandardLibrary.h" 00006 #include "ImageFormat.h" 00007 #include "Bitmap.h" 00008 #include "ColorHelper.h" 00009 00010 00011 //*** Constructor *** 00012 00013 Image::Image(): 00014 width_(0), 00015 height_(0), 00016 data_(0), 00017 celCount_(0), 00018 orientation_(Rotate_0) 00019 { 00020 } 00021 00022 00023 //*** Constructor *** 00024 00025 Image::Image(const Asset& asset, Orientation orientation): 00026 width_(0), 00027 height_(0), 00028 data_(0), 00029 celCount_(1), 00030 orientation_(orientation) 00031 { 00032 ImageFormat* format=ImageFormat::CreateImageFormat(asset); 00033 00034 Assert(format,"Unknown image format"); 00035 00036 if (format) 00037 { 00038 width_=format->GetWidth(); 00039 height_=format->GetHeight(); 00040 celCount_=format->GetCelCount(); 00041 data_=new unsigned int[width_*height_*celCount_]; 00042 format->CopyPixels(data_); 00043 for (int i=0; i<format->GetCelCount(); i++) 00044 { 00045 float delay=format->GetCelDelay(i); 00046 celDelay_.Add(delay); 00047 } 00048 delete format; 00049 } 00050 } 00051 00052 00053 //*** Constructor *** 00054 00055 Image::Image(const Asset& asset, int celCount, Orientation orientation): 00056 width_(0), 00057 height_(0), 00058 data_(0), 00059 celCount_(celCount), 00060 orientation_(orientation) 00061 { 00062 ImageFormat* format=ImageFormat::CreateImageFormat(asset); 00063 00064 Assert(format,"Unknown image format"); 00065 00066 if (format) 00067 { 00068 if (celCount_==1) 00069 { 00070 celCount_=format->GetCelCount(); 00071 } 00072 00073 width_=format->GetWidth(); 00074 height_=format->GetHeight()/celCount_; 00075 data_=new unsigned int[width_*height_*celCount_]; 00076 format->CopyPixels(data_); 00077 delete format; 00078 } 00079 } 00080 00081 00082 //*** Constructor *** 00083 00084 Image::Image(int width, int height): 00085 width_(width), 00086 height_(height), 00087 data_(0), 00088 celCount_(1), 00089 orientation_(Rotate_0) 00090 00091 { 00092 data_=new unsigned int[width_*height_]; 00093 MemSet(data_,0,width_*height_*sizeof(unsigned int)); 00094 } 00095 00096 00097 //*** Constructor *** 00098 00099 Image::Image(int width, int height, int celCount): 00100 width_(width), 00101 height_(height), 00102 data_(0), 00103 celCount_(celCount), 00104 orientation_(Rotate_0) 00105 { 00106 Assert(celCount>0, "Invalid cel count"); 00107 if (celCount==0) 00108 { 00109 return; 00110 } 00111 data_=new unsigned int[width_*height_*celCount_]; 00112 MemSet(data_,0,width_*height_*celCount_*sizeof(unsigned int)); 00113 } 00114 00115 00116 //*** Constructor *** 00117 00118 Image::Image(const Image& image): 00119 width_(image.width_), 00120 height_(image.height_), 00121 data_(0), 00122 celCount_(image.celCount_), 00123 orientation_(image.orientation_) 00124 { 00125 if (width_*height_*celCount_>0) 00126 { 00127 data_=new unsigned int[width_*height_*celCount_]; 00128 MemCpy(data_,image.data_,width_*height_*celCount_*sizeof(unsigned int)); 00129 for (int i=0; i<image.celDelay_.GetItemCount(); i++) 00130 { 00131 celDelay_.Add(image.celDelay_.Get(i)); 00132 } 00133 } 00134 } 00135 00136 00137 //*** Constructor *** 00138 00139 Image::Image(const Bitmap& bitmap): 00140 width_(bitmap.GetWidth()), 00141 height_(bitmap.GetHeight()), 00142 data_(0), 00143 celCount_(1), 00144 orientation_(Rotate_0) 00145 { 00146 if (width_*height_*celCount_>0) 00147 { 00148 data_=new unsigned int[width_*height_*celCount_]; 00149 for (int y=0; y<height_; y++) 00150 { 00151 for (int x=0; x<width_; x++) 00152 { 00153 data_[x+y*width_]=RGB16TO32(bitmap.GetPixelColor(x,y),bitmap.GetPixelAlpha(x,y)); 00154 } 00155 } 00156 } 00157 } 00158 00159 00160 //*** Destructor *** 00161 00162 Image::~Image() 00163 { 00164 if (data_) 00165 { 00166 delete data_; 00167 } 00168 } 00169 00170 00171 //*** Copy operator *** 00172 00173 const Image& Image::operator=(const Image& image) 00174 { 00175 if (data_) 00176 { 00177 delete data_; 00178 } 00179 00180 width_=image.width_; 00181 height_=image.height_; 00182 data_=0; 00183 celCount_=image.celCount_; 00184 orientation_=image.orientation_; 00185 00186 if (width_*height_*celCount_>0) 00187 { 00188 data_=new unsigned int[width_*height_*celCount_]; 00189 MemCpy(data_,image.data_,width_*height_*celCount_*sizeof(unsigned int)); 00190 for (int i=0; i<image.celDelay_.GetItemCount(); i++) 00191 { 00192 celDelay_.Add(image.celDelay_.Get(i)); 00193 } 00194 } 00195 00196 return *this; 00197 } 00198 00199 //*** SetOrientation *** 00200 00201 void Image::SetOrientation(Orientation orientation) 00202 { 00203 orientation_=orientation; 00204 } 00205 00206 //*** GetWidth *** 00207 00208 int Image::GetWidth() const 00209 { 00210 switch (orientation_) 00211 { 00212 case Rotate_0: 00213 case Rotate_180: 00214 case Mirror_X: 00215 case Mirror_Y: 00216 { 00217 return width_; 00218 } break; 00219 00220 case Rotate_90: 00221 case Rotate_270: 00222 { 00223 return height_; 00224 } break; 00225 } 00226 00227 return width_; 00228 } 00229 00230 00231 //*** GetHeight *** 00232 00233 int Image::GetHeight() const 00234 { 00235 switch (orientation_) 00236 { 00237 case Rotate_0: 00238 case Rotate_180: 00239 case Mirror_X: 00240 case Mirror_Y: 00241 { 00242 return height_; 00243 } break; 00244 00245 case Rotate_90: 00246 case Rotate_270: 00247 { 00248 return width_; 00249 } break; 00250 } 00251 00252 return height_; 00253 } 00254 00255 00256 00257 00258 //*** GetPixel *** 00259 00260 unsigned int Image::GetPixel(int x, int y) const 00261 { 00262 Assert(x>=0 && x<GetWidth() && y>=0 && y<GetHeight(),"Coordinates out of range"); 00263 if (!(x>=0 && x<GetWidth() && y>=0 && y<GetHeight())) 00264 { 00265 return 0; 00266 } 00267 00268 int xp=x; 00269 int yp=y; 00270 00271 switch (orientation_) 00272 { 00273 case Rotate_0: 00274 { 00275 } break; 00276 00277 case Rotate_90: 00278 { 00279 xp=y; 00280 yp=(height_-1)-x; 00281 } break; 00282 00283 case Rotate_180: 00284 { 00285 xp=(width_-1)-x; 00286 yp=(height_-1)-y; 00287 } break; 00288 00289 case Rotate_270: 00290 { 00291 xp=(width_-1)-y; 00292 yp=x; 00293 } break; 00294 00295 case Mirror_X: 00296 { 00297 xp=(width_-1)-x; 00298 } break; 00299 00300 case Mirror_Y: 00301 { 00302 yp=(height_-1)-y; 00303 } break; 00304 00305 } 00306 00307 00308 00309 if (!data_) 00310 { 00311 return 0; 00312 } 00313 00314 return data_[xp+yp*width_]; 00315 } 00316 00317 00318 //*** SetPixel *** 00319 00320 void Image::SetPixel(int x, int y, unsigned int color) 00321 { 00322 Assert(x>=0 && x<GetWidth() && y>=0 && y<GetHeight(),"Coordinates out of range"); 00323 if (!(x>=0 && x<GetWidth() && y>=0 && y<GetHeight())) 00324 { 00325 return; 00326 } 00327 00328 int xp=x; 00329 int yp=y; 00330 00331 switch (orientation_) 00332 { 00333 case Rotate_0: 00334 { 00335 } break; 00336 00337 case Rotate_90: 00338 { 00339 xp=y; 00340 yp=(height_-1)-x; 00341 } break; 00342 00343 case Rotate_180: 00344 { 00345 xp=(width_-1)-x; 00346 yp=(height_-1)-y; 00347 } break; 00348 00349 case Rotate_270: 00350 { 00351 xp=(width_-1)-y; 00352 yp=x; 00353 } break; 00354 00355 case Mirror_X: 00356 { 00357 xp=(width_-1)-x; 00358 } break; 00359 00360 case Mirror_Y: 00361 { 00362 yp=(height_-1)-y; 00363 } break; 00364 00365 } 00366 00367 if (data_) 00368 { 00369 data_[xp+yp*width_]=color; 00370 } 00371 } 00372 00373 00374 //*** GetPixel *** 00375 00376 unsigned int Image::GetPixel(int cel, int x, int y) const 00377 { 00378 Assert(x>=0 && x<GetWidth() && y>=0 && y<GetHeight(),"Coordinates out of range"); 00379 if (!(x>=0 && x<GetWidth() && y>=0 && y<GetHeight())) 00380 { 00381 return 0; 00382 } 00383 00384 int xp=x; 00385 int yp=y; 00386 00387 switch (orientation_) 00388 { 00389 case Rotate_0: 00390 { 00391 } break; 00392 00393 case Rotate_90: 00394 { 00395 xp=y; 00396 yp=(height_-1)-x; 00397 } break; 00398 00399 case Rotate_180: 00400 { 00401 xp=(width_-1)-x; 00402 yp=(height_-1)-y; 00403 } break; 00404 00405 case Rotate_270: 00406 { 00407 xp=(width_-1)-y; 00408 yp=x; 00409 } break; 00410 00411 case Mirror_X: 00412 { 00413 xp=(width_-1)-x; 00414 } break; 00415 00416 case Mirror_Y: 00417 { 00418 yp=(height_-1)-y; 00419 } break; 00420 00421 } 00422 00423 if (!data_) 00424 { 00425 return 0; 00426 } 00427 00428 return data_[width_*height_*cel+xp+yp*width_]; 00429 } 00430 00431 00432 //*** SetPixel *** 00433 void Image::SetPixel(int cel, int x, int y, unsigned int color) 00434 { 00435 Assert(x>=0 && x<GetWidth() && y>=0 && y<GetHeight(),"Coordinates out of range"); 00436 if (!(x>=0 && x<GetWidth() && y>=0 && y<GetHeight())) 00437 { 00438 return; 00439 } 00440 00441 int xp=x; 00442 int yp=y; 00443 00444 switch (orientation_) 00445 { 00446 case Rotate_0: 00447 { 00448 } break; 00449 00450 case Rotate_90: 00451 { 00452 xp=y; 00453 yp=(height_-1)-x; 00454 } break; 00455 00456 case Rotate_180: 00457 { 00458 xp=(width_-1)-x; 00459 yp=(height_-1)-y; 00460 } break; 00461 00462 case Rotate_270: 00463 { 00464 xp=(width_-1)-y; 00465 yp=x; 00466 } break; 00467 00468 case Mirror_X: 00469 { 00470 xp=(width_-1)-x; 00471 } break; 00472 00473 case Mirror_Y: 00474 { 00475 yp=(height_-1)-y; 00476 } break; 00477 00478 } 00479 if (data_) 00480 { 00481 data_[width_*height_*cel+xp+yp*width_]=color; 00482 } 00483 } 00484 00485 00486 //*** GetCelCount **** 00487 00488 int Image::GetCelCount() const 00489 { 00490 return celCount_; 00491 } 00492 00493 00494 //*** GetCelDelay **** 00495 00496 float Image::GetCelDelay(int cel) const 00497 { 00498 Assert(cel>=0 && cel<celCount_ ,"Cel index out of range"); 00499 if (cel<0 || cel>=celCount_) 00500 { 00501 return 0.0f; 00502 } 00503 00504 return celDelay_.Get(cel); 00505 } 00506 00507 00508 //*** AdjustBrightness **** 00509 00510 void Image::AdjustBrightness(float redMultiplier, float greenMultiplier, float blueMultiplier) 00511 { 00512 for (int cel=0; cel<celCount_; cel++) 00513 { 00514 for (int y=0; y<height_; y++) 00515 { 00516 for (int x=0; x<width_; x++) 00517 { 00518 unsigned int c=GetPixel(cel,x,y); 00519 if (c&0xff000000) 00520 { 00521 unsigned int r=(c&0x00ff0000)>>16; 00522 unsigned int g=(c&0x0000ff00)>>8; 00523 unsigned int b=(c&0x000000ff); 00524 r=Clamp((int)(r*redMultiplier),0,0xff); 00525 g=Clamp((int)(g*greenMultiplier),0,0xff); 00526 b=Clamp((int)(b*blueMultiplier),0,0xff); 00527 c&=0xff000000; 00528 c|=(r<<16) | (g<<8) | b; 00529 SetPixel(cel,x,y,c); 00530 } 00531 } 00532 } 00533 } 00534 } 00535 00536 00537 //*** GetPointer *** 00538 00539 unsigned int* Image::GetPointer() const 00540 { 00541 return data_; 00542 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
