Bitmap_Alpha.cpp
Go to the documentation of this file.00001 //*** Bitmap_Alpha.cpp *** 00002 00003 #include "Bitmap_Alpha.h" 00004 #include "ColorHelper.h" 00005 #include "Image.h" 00006 #include "Asset.h" 00007 00008 00009 //*** GetType *** 00010 00011 StringId Bitmap_Alpha::GetType() 00012 { 00013 static StringId idBitmap_Alpha("Bitmap_Alpha"); 00014 return idBitmap_Alpha; 00015 } 00016 00017 00018 //*** Constructor *** 00019 00020 Bitmap_Alpha::Bitmap_Alpha() 00021 { 00022 } 00023 00024 00025 //*** Constructor *** 00026 00027 Bitmap_Alpha::Bitmap_Alpha(const Asset& asset) 00028 { 00029 Load(asset); 00030 } 00031 00032 00033 //*** Constructor *** 00034 00035 Bitmap_Alpha::Bitmap_Alpha(int width, int height) 00036 { 00037 width_=width; 00038 height_=height; 00039 hPitch_=width; 00040 vPitch_=height; 00041 alpha_=static_cast<unsigned char*>(Malloc(sizeof(unsigned char)*width_*height_)); 00042 } 00043 00044 00045 //*** Constructor *** 00046 00047 Bitmap_Alpha::Bitmap_Alpha(const Image& image, bool useGrayscaleInsteadOfAlpha) 00048 { 00049 width_=image.GetWidth(); 00050 height_=image.GetHeight(); 00051 hPitch_=width_; 00052 vPitch_=height_; 00053 alpha_=static_cast<unsigned char*>(Malloc(sizeof(unsigned char)*width_*height_)); 00054 00055 if (!useGrayscaleInsteadOfAlpha) 00056 { 00057 for (int y=0; y<height_; y++) 00058 { 00059 for (int x=0; x<width_; x++) 00060 { 00061 unsigned int c=image.GetPixel(x,y); 00062 unsigned char a=(unsigned char)(c>>24); 00063 alpha_[x+y*width_]=a; 00064 } 00065 } 00066 } 00067 else 00068 { 00069 for (int y=0; y<height_; y++) 00070 { 00071 for (int x=0; x<width_; x++) 00072 { 00073 unsigned int c=image.GetPixel(x,y); 00074 unsigned int acc=(unsigned char)((c&0xff0000)>>16); 00075 acc+=(unsigned char)((c&0xff00)>>8); 00076 acc+=(unsigned char)((c&0xff)); 00077 acc/=3; 00078 alpha_[x+y*width_]=(unsigned char)acc; 00079 } 00080 } 00081 } 00082 } 00083 00084 00085 //*** Destructor *** 00086 00087 Bitmap_Alpha::~Bitmap_Alpha() 00088 { 00089 if (alpha_) 00090 { 00091 Free(alpha_); 00092 } 00093 } 00094 00095 00096 //*** Save *** 00097 00098 void Bitmap_Alpha::Save(Asset& asset) const 00099 { 00100 if (asset.Create()) 00101 { 00102 asset.Write("PIXALPHA",8); 00103 int version=0; 00104 asset.Write(&version); 00105 int celCount=0; 00106 asset.Write(&celCount); 00107 WriteToAsset(&asset); 00108 asset.Close(); 00109 } 00110 } 00111 00112 00113 //*** Load *** 00114 00115 void Bitmap_Alpha::Load(const Asset& asset) 00116 { 00117 if (asset.Open()) 00118 { 00119 char header[8]; 00120 asset.Read(header,8); 00121 00122 if (StrNCmp(header,"PIXALPHA",8)==0) 00123 { 00124 int version=0; 00125 asset.Read(&version); 00126 int celCount=0; 00127 asset.Read(&celCount); 00128 if (version==0) 00129 { 00130 ReadFromAsset(&asset); 00131 } 00132 } 00133 00134 // Check for old format too 00135 else if (StrNCmp(header,"PIXIE_AM",8)==0) 00136 { 00137 int version=0; 00138 asset.Read(&version); 00139 if (version==0) 00140 { 00141 ReadFromAsset(&asset); 00142 } 00143 } 00144 00145 else 00146 { 00147 Assert(false,"Invalid header"); 00148 } 00149 00150 asset.Close(); 00151 } 00152 // Report missing file 00153 #ifdef _DEBUG 00154 else 00155 { 00156 const char* filename=asset.GetFilename().GetString(); 00157 if (filename) 00158 { 00159 char errorMessage[1024]; 00160 SNPrintF(errorMessage,1024,"File not found: %s",filename); 00161 Assert(false,errorMessage); 00162 } 00163 else 00164 { 00165 Assert(false,"An asset could not be accessed."); 00166 } 00167 } 00168 #endif 00169 } 00170 00171 00172 //*** ReadFromAsset *** 00173 00174 void Bitmap_Alpha::ReadFromAsset(const Asset* asset) 00175 { 00176 if (alpha_) 00177 { 00178 Free(alpha_); 00179 alpha_=0; 00180 } 00181 00182 asset->Read(&hPitch_); 00183 asset->Read(&vPitch_); 00184 width_=hPitch_; 00185 height_=vPitch_; 00186 if (hPitch_*vPitch_>0) 00187 { 00188 alpha_=static_cast<unsigned char*>(Malloc(sizeof(unsigned char)*hPitch_*vPitch_)); 00189 asset->Read(alpha_,hPitch_*vPitch_); 00190 } 00191 } 00192 00193 00194 //*** WriteToAsset *** 00195 00196 void Bitmap_Alpha::WriteToAsset(Asset* asset) const 00197 { 00198 asset->Write(&hPitch_); 00199 asset->Write(&vPitch_); 00200 if (hPitch_*vPitch_>0) 00201 { 00202 asset->Write(alpha_,hPitch_*vPitch_); 00203 } 00204 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
