Bitmap.h
Go to the documentation of this file.00001 00022 #ifndef __Bitmap_H__ 00023 #define __Bitmap_H__ 00024 00025 // Includes 00026 #include "StringId.h" 00027 00028 // Forward declares 00029 class Asset; 00030 00031 // Bitmap 00032 class Bitmap 00033 { 00034 public: 00035 Bitmap(); 00036 00044 virtual StringId GetType() = 0; 00045 00046 00051 virtual ~Bitmap() { }; 00052 00053 00061 virtual unsigned short* GetColorData() const; 00062 00070 virtual unsigned char* GetAlphaData() const; 00071 00072 virtual int GetHPitch() const; 00073 00074 virtual int GetVPitch() const; 00075 00076 virtual int GetHOffset() const; 00077 00078 virtual int GetVOffset() const; 00079 00085 virtual void Clear(); 00086 00087 00093 virtual void Fill( 00094 unsigned short color, 00095 unsigned char alpha = 255 00096 ); 00097 00098 00108 virtual void Fill( 00109 int x1, 00110 int y1, 00111 int x2, 00112 int y2, 00113 unsigned short color, 00114 unsigned char alpha = 255 00115 ); 00116 00117 00118 virtual void FillAlpha( 00119 unsigned char alpha 00120 ); 00121 00122 virtual void FillAlpha( 00123 int x1, 00124 int y1, 00125 int x2, 00126 int y2, 00127 unsigned char alpha 00128 ); 00129 00139 enum Transformation 00140 { 00141 NoTransformation, 00142 Rotate_90, 00143 Rotate_180, 00144 Rotate_270, 00145 Mirror_X, 00146 Mirror_Y, 00147 Transformations_Count 00148 }; 00149 00150 00156 virtual int GetWidth( 00157 Transformation transformation = NoTransformation 00158 ) const; 00159 00160 00166 virtual int GetHeight( 00167 Transformation transformation = NoTransformation 00168 ) const; 00169 00170 00178 virtual unsigned short GetPixelColor( 00179 int x, 00180 int y, 00181 Transformation transformation = NoTransformation 00182 ) const; 00183 00184 virtual unsigned char GetPixelAlpha( 00185 int x, 00186 int y, 00187 Transformation transformation = NoTransformation 00188 ) const; 00189 00195 virtual void SetPixelColor( 00196 int x, 00197 int y, 00198 unsigned short color, 00199 Transformation transformation = NoTransformation 00200 ); 00201 00202 virtual void SetPixelAlpha( 00203 int x, 00204 int y, 00205 unsigned char alpha, 00206 Transformation transformation = NoTransformation 00207 ); 00208 00209 00216 virtual void BlendPixel( 00217 int x, 00218 int y, 00219 unsigned short color, 00220 unsigned char alpha, 00221 Transformation transformation = NoTransformation 00222 ); 00223 00228 virtual void Blit( 00229 Bitmap& target, 00230 int x, 00231 int y, 00232 unsigned short modulate = 0xffff, 00233 unsigned char alpha = 255, 00234 Transformation transformation = NoTransformation 00235 ) const; 00236 00242 virtual void Blit( 00243 int x1, 00244 int y1, 00245 int x2, 00246 int y2, 00247 Bitmap& target, 00248 int x, 00249 int y, 00250 unsigned short modulate = 0xffff, 00251 unsigned char alpha = 255, 00252 Transformation transformation = NoTransformation 00253 ) const; 00254 00255 virtual void Copy( 00256 Bitmap& target, 00257 int x, 00258 int y, 00259 unsigned short modulate = 0xffff, 00260 Transformation transformation = NoTransformation 00261 ) const; 00262 00263 virtual void Copy( 00264 int x1, 00265 int y1, 00266 int x2, 00267 int y2, 00268 Bitmap& target, 00269 int x, 00270 int y, 00271 unsigned short modulate = 0xffff, 00272 Transformation transformation = NoTransformation 00273 ) const; 00274 00275 virtual void Save(Asset& asset) const = 0; 00276 virtual void Load(const Asset& asset) = 0; 00277 00278 virtual void WriteToAsset(Asset* asset) const = 0; 00279 virtual void ReadFromAsset(const Asset* asset) = 0; 00280 00281 protected: 00285 inline void TransformCoordinates( 00286 int& x, 00287 int& y, 00288 Transformation transformation 00289 ) const 00290 { 00291 int xp=x; 00292 int yp=y; 00293 switch (transformation) 00294 { 00295 case NoTransformation: 00296 { 00297 return; 00298 } break; 00299 00300 case Rotate_90: 00301 { 00302 xp=y; 00303 yp=(GetHeight()-1)-x; 00304 } break; 00305 00306 case Rotate_180: 00307 { 00308 xp=(GetWidth()-1)-x; 00309 yp=(GetHeight()-1)-y; 00310 } break; 00311 00312 case Rotate_270: 00313 { 00314 xp=(GetWidth()-1)-y; 00315 yp=x; 00316 } break; 00317 00318 case Mirror_X: 00319 { 00320 xp=(GetWidth()-1)-x; 00321 } break; 00322 00323 case Mirror_Y: 00324 { 00325 yp=(GetHeight()-1)-y; 00326 } break; 00327 00328 } 00329 x=xp; 00330 y=yp; 00331 } 00332 00333 00334 bool Clip(int& sourceX, int& sourceY, int& sourceWidth, int& sourceHeight, int& targetX, int& targetY, int targetWidth, int targetHeight, Transformation transformation ) const 00335 { 00336 00337 // First, make sure source is in valid range 00338 00339 // Is left side negative? 00340 if (sourceX<0) 00341 { 00342 targetX+=-sourceX; // Compensate destination position 00343 sourceWidth-=-sourceX; // Compensate width 00344 sourceX=0; // Clamp left side 00345 } 00346 00347 // Is top side negative? 00348 if (sourceY<0) 00349 { 00350 targetY+=-sourceY; // Compensate destination position 00351 sourceHeight-=-sourceY; // Compensate height 00352 sourceY=0; // Clamp top side 00353 } 00354 00355 // Is right side out of range? 00356 if (sourceX+sourceWidth>GetWidth(transformation)) 00357 { 00358 sourceWidth-=(sourceX+sourceWidth)-GetWidth(transformation); // Truncate width 00359 } 00360 00361 // Is bottom side out of range? 00362 if (sourceY+sourceHeight>GetHeight(transformation)) 00363 { 00364 sourceHeight-=(sourceY+sourceHeight)-GetHeight(transformation); // Truncate height 00365 } 00366 00367 // Next, make sure target is in valid range 00368 00369 // Is left side negative? 00370 if (targetX<0) 00371 { 00372 sourceX+=-targetX; // Compensate source position 00373 sourceWidth-=-targetX; // Compensate width 00374 targetX=0; // Clamp left side 00375 } 00376 00377 // Is top side negative? 00378 if (targetY<0) 00379 { 00380 sourceY+=-targetY; // Compensate source position 00381 sourceHeight-=-targetY; // Compensate width 00382 targetY=0; // Clamp left side 00383 } 00384 00385 // Is right side out of range? 00386 if (targetX+sourceWidth>targetWidth) 00387 { 00388 sourceWidth-=(targetX+sourceWidth)-targetWidth; // Truncate width 00389 } 00390 00391 // Is bottom side out of range? 00392 if (targetY+sourceHeight>targetHeight) 00393 { 00394 sourceHeight-=(targetY+sourceHeight)-targetHeight; // Truncate height 00395 } 00396 00397 // Check if there's something left to draw 00398 if (sourceWidth<=0 || sourceHeight<=0) 00399 { 00400 return false; 00401 } 00402 00403 return true; 00404 } 00405 00406 protected: 00407 unsigned short* color_; 00408 unsigned char* alpha_; 00409 int width_; 00410 int height_; 00411 int hPitch_; 00412 int vPitch_; 00413 int hOffset_; 00414 int vOffset_; 00415 }; 00416 00417 00418 00419 00420 #endif /* __Bitmap16_H__ */ 00421
Reproduction/republishing of any material on this site without permission is strictly prohibited.
