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