BitmapStrip.cpp
Go to the documentation of this file.00001 //*** BitmapStrip.cpp ** 00002 00003 #include "BitmapStrip.h" 00004 #include "Image.h" 00005 #include "StandardLibrary.h" 00006 #include "Bitmap_RLE8.h" 00007 #include "Bitmap_RLE16.h" 00008 #include "Bitmap_16Bit.h" 00009 #include "Bitmap_16BitAlpha.h" 00010 #include "Bitmap_16BitAlphaCrop.h" 00011 #include "Bitmap_Alpha.h" 00012 #include "ColorHelper.h" 00013 #include "Asset.h" 00014 00015 00016 //*** Constructor *** 00017 00018 BitmapStrip::BitmapStrip() 00019 { 00020 } 00021 00022 00023 //*** Constructor *** 00024 00025 BitmapStrip::BitmapStrip(const Asset& asset) 00026 { 00027 if (asset.Open()) 00028 { 00029 char header[8]; 00030 asset.Read(header,8); 00031 00032 // Is this an old-style pixie RLE file? 00033 if (StrNCmp(header,"PIXIE_RL",8)==0) 00034 { 00035 // Yes, so read the extra header byte 00036 char c; 00037 asset.Read(&c); 00038 Assert(c=='E',"Invalid header"); 00039 if (c!='E') 00040 { 00041 asset.Close(); 00042 return; 00043 } 00044 00045 // Read the data 00046 int version=0; 00047 asset.Read(&version); 00048 if (version==0) 00049 { 00050 int celCount=0; 00051 asset.Read(&celCount); 00052 // Create and load a bitmap for each cel 00053 for (int i=0; i<Max(1,celCount); i++) 00054 { 00055 Bitmap* bitmap=new Bitmap_RLE8(); 00056 bitmap->ReadFromAsset(&asset); 00057 cels_.Add(bitmap); 00058 } 00059 } 00060 } 00061 00062 // Is this an old-style pixie ABM file? 00063 else if (StrNCmp(header,"PIXIE_AB",8)==0) 00064 { 00065 // Yes, so read the extra header byte 00066 char c; 00067 asset.Read(&c); 00068 Assert(c=='M',"Invalid header"); 00069 if (c!='M') 00070 { 00071 asset.Close(); 00072 return; 00073 } 00074 00075 // Read the data 00076 int version=0; 00077 asset.Read(&version); 00078 if (version==0) 00079 { 00080 Bitmap* bitmap=new Bitmap_16bitAlpha(); 00081 bitmap->ReadFromAsset(&asset); 00082 cels_.Add(bitmap); 00083 } 00084 } 00085 00086 // Is this an old-style pixie BM file? 00087 else if (StrNCmp(header,"PIXIE_BM",8)==0) 00088 { 00089 // Read the data 00090 int version=0; 00091 asset.Read(&version); 00092 if (version==0) 00093 { 00094 Bitmap* bitmap=new Bitmap_16bit(); 00095 bitmap->ReadFromAsset(&asset); 00096 cels_.Add(bitmap); 00097 } 00098 } 00099 00100 // Is this an old-style pixie AM file? 00101 else if (StrNCmp(header,"PIXIE_AM",8)==0) 00102 { 00103 // Read the data 00104 int version=0; 00105 asset.Read(&version); 00106 if (version==0) 00107 { 00108 Bitmap* bitmap=new Bitmap_Alpha(); 00109 bitmap->ReadFromAsset(&asset); 00110 cels_.Add(bitmap); 00111 } 00112 } 00113 00114 // Is this a Bitmap_RLE8 file? 00115 else if (StrNCmp(header,"PIXRLE8B",8)==0) 00116 { 00117 // Read the data 00118 int version=0; 00119 asset.Read(&version); 00120 if (version==0) 00121 { 00122 int celCount=0; 00123 asset.Read(&celCount); 00124 // Create and load a bitmap for each cel 00125 for (int i=0; i<Max(1,celCount); i++) 00126 { 00127 Bitmap* bitmap=new Bitmap_RLE8(); 00128 bitmap->ReadFromAsset(&asset); 00129 cels_.Add(bitmap); 00130 } 00131 } 00132 } 00133 00134 // Is this a Bitmap_RLE16 file? 00135 else if (StrNCmp(header,"PIXRLE16",8)==0) 00136 { 00137 // Read the data 00138 int version=0; 00139 asset.Read(&version); 00140 if (version==0) 00141 { 00142 int celCount=0; 00143 asset.Read(&celCount); 00144 // Create and load a bitmap for each cel 00145 for (int i=0; i<Max(1,celCount); i++) 00146 { 00147 Bitmap* bitmap=new Bitmap_RLE16(); 00148 bitmap->ReadFromAsset(&asset); 00149 cels_.Add(bitmap); 00150 } 00151 } 00152 } 00153 00154 // Is this a Bitmap_16bit file? 00155 else if (StrNCmp(header,"PIX16BIT",8)==0) 00156 { 00157 int version=0; 00158 asset.Read(&version); 00159 if (version==0) 00160 { 00161 int celCount=0; 00162 asset.Read(&celCount); 00163 // Create and load a bitmap for each cel 00164 for (int i=0; i<Max(1,celCount); i++) 00165 { 00166 Bitmap* bitmap=new Bitmap_16bit(); 00167 bitmap->ReadFromAsset(&asset); 00168 cels_.Add(bitmap); 00169 } 00170 } 00171 } 00172 00173 // Is this a Bitmap_16bitAlpha file? 00174 else if (StrNCmp(header,"PIX16BAL",8)==0) 00175 { 00176 int version=0; 00177 asset.Read(&version); 00178 if (version==0) 00179 { 00180 int celCount=0; 00181 asset.Read(&celCount); 00182 // Create and load a bitmap for each cel 00183 for (int i=0; i<Max(1,celCount); i++) 00184 { 00185 Bitmap* bitmap=new Bitmap_16bitAlpha(); 00186 bitmap->ReadFromAsset(&asset); 00187 cels_.Add(bitmap); 00188 } 00189 } 00190 } 00191 00192 // Is this a Bitmap_16bitAlphaCrop file? 00193 else if (StrNCmp(header,"PIX16BAC",8)==0) 00194 { 00195 int version=0; 00196 asset.Read(&version); 00197 if (version==0) 00198 { 00199 int celCount=0; 00200 asset.Read(&celCount); 00201 // Create and load a bitmap for each cel 00202 for (int i=0; i<Max(1,celCount); i++) 00203 { 00204 Bitmap* bitmap=new Bitmap_16bitAlphaCrop(); 00205 bitmap->ReadFromAsset(&asset); 00206 cels_.Add(bitmap); 00207 } 00208 } 00209 } 00210 00211 // Is this a Bitmap_Alpha file? 00212 else if (StrNCmp(header,"PIXALPHA",8)==0) 00213 { 00214 int version=0; 00215 asset.Read(&version); 00216 if (version==0) 00217 { 00218 int celCount=0; 00219 asset.Read(&celCount); 00220 // Create and load a bitmap for each cel 00221 for (int i=0; i<Max(1,celCount); i++) 00222 { 00223 Bitmap* bitmap=new Bitmap_Alpha(); 00224 bitmap->ReadFromAsset(&asset); 00225 cels_.Add(bitmap); 00226 } 00227 } 00228 } 00229 00230 // Unknown file type 00231 else 00232 { 00233 Assert(false,"Invalid header"); 00234 } 00235 00236 asset.Close(); 00237 } 00238 // Report missing file 00239 #ifdef _DEBUG 00240 else 00241 { 00242 const char* filename=asset.GetFilename().GetString(); 00243 if (filename) 00244 { 00245 char errorMessage[1024]; 00246 SNPrintF(errorMessage,1024,"File not found: %s",filename); 00247 Assert(false,errorMessage); 00248 } 00249 else 00250 { 00251 Assert(false,"An asset could not be accessed."); 00252 } 00253 } 00254 #endif 00255 } 00256 00257 00258 //*** Constructor *** 00259 00260 BitmapStrip::BitmapStrip(const Image& image) 00261 { 00262 int width=image.GetWidth(); 00263 int height=image.GetHeight(); 00264 Image celImage(width,height); 00265 for (int i=0; i<image.GetCelCount(); i++) 00266 { 00267 00268 for (int y=0; y<height; y++) 00269 { 00270 for (int x=0; x<width; x++) 00271 { 00272 celImage.SetPixel(x,y,image.GetPixel(i,x,y)); 00273 } 00274 } 00275 00276 Bitmap* cel=new Bitmap_16bitAlphaCrop(celImage); 00277 cels_.Add(cel); 00278 } 00279 } 00280 00281 00282 //*** Constructor *** 00283 00284 BitmapStrip::BitmapStrip(const Bitmap* bitmap) 00285 { 00286 cels_.Add(bitmap); 00287 } 00288 00289 00290 //*** Destructor *** 00291 00292 BitmapStrip::~BitmapStrip() 00293 { 00294 for (int i=0; i<cels_.GetItemCount(); i++) 00295 { 00296 const Bitmap* cel=cels_.Get(i); 00297 delete cel; 00298 } 00299 } 00300 00301 00302 //*** GetCel *** 00303 00304 const Bitmap& BitmapStrip::GetCel(int index) const 00305 { 00306 Assert(cels_.GetItemCount()!=0,"Bitmap strip is empty"); 00307 if (cels_.GetItemCount()==0) 00308 { 00309 static Bitmap_16bit defaultValue; 00310 return defaultValue; 00311 } 00312 00313 int cel=index%cels_.GetItemCount(); 00314 if (cel<0) 00315 { 00316 cel+=cels_.GetItemCount(); 00317 } 00318 return *cels_.Get(cel); 00319 } 00320 00321 00322 //*** GetCelCount *** 00323 00324 int BitmapStrip::GetCelCount() const 00325 { 00326 return cels_.GetItemCount(); 00327 } 00328 00329 00330 //*** operator Bitmap *** 00331 00332 BitmapStrip::operator const Bitmap&() const 00333 { 00334 Assert(cels_.GetItemCount()!=0,"Bitmap strip is empty"); 00335 if (cels_.GetItemCount()==0) 00336 { 00337 static Bitmap_16bit defaultValue; 00338 return defaultValue; 00339 } 00340 00341 return *cels_.Get(0); 00342 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
