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