Yritän luoda c++:lla bittikartan tyhjästä. Koodi toimii tällä hetkellä vain 100x100 kokoisille kuville. Katsoin speksejä Wikipediasta.
void WriteInt(fstream &file, int _int)
{
char temp[4];
temp[3] = _int >> 24;
temp[2] = _int >> 16;
temp[1] = _int >> 8;
temp[0] = _int << 24 >> 24;
file.write(temp, 4);
}
void WriteShort(fstream &file, short _short)
{
char temp[2];
temp[1] = _short >> 8;
temp[0] = _short;
file.write(temp, 2);
}
void CreateBitmap(std::string filename, int width, int height, short bits_per_channel, unsigned char* pixeldata)
{
fstream myfile;
myfile.open (filename, std::ios::binary | std::ios::out);
short bits = bits_per_channel * 3;
int header_size = 14;
int dib_size = 40;
int filesize = header_size+dib_size+width*height*3;
// bitmap file header
myfile << 'B' << 'M'; // bmp header
WriteInt(myfile, filesize); // file size in bytes
WriteInt(myfile, 0); // 2x 2-byte reserved
WriteInt(myfile, header_size+dib_size); // pixel data offset
// DIB header
WriteInt(myfile, dib_size); // size of header in bytes
WriteInt(myfile, width); // width in pixels
WriteInt(myfile, height); // height in pixels
WriteShort(myfile, 1); // has to be 1
WriteShort(myfile, bits); // bits per pixel
WriteInt(myfile, 0); // compression method (0 = none)
WriteInt(myfile, width*height*3); // size of raw bitmap data
WriteInt(myfile, 100); // horizontal resolution (pixel per meter)
WriteInt(myfile, 100); // vertical resolution (pixel per meter)
WriteInt(myfile, 0); // number of colors in palette
WriteInt(myfile, 0); // number of important colors used
// pixel data
for(int i=0;i<width*height*3;i+=3)
{
myfile << pixeldata[i+2]; // b
myfile << pixeldata[i+1]; // g
myfile << pixeldata[i]; // r
}
myfile.close();
}
int main()
{
srand(time(NULL)); // random seed
int w = 100;
int h = 100;
unsigned char* yeah = new unsigned char[w*h*3];
for(int i=0;i<w*h*3;i+=3)
{
yeah[i] = 255; // r
yeah[i+1] = 255; // g
yeah[i+2] = 255; // b
}
remove("media/yeah_buddy.bmp");
CreateBitmap("media/yeah_buddy.bmp", w, h, 8, yeah);
for(int i=0;i<w*h*3;i+=3)
{
yeah[i] = rand() % 255;
yeah[i+1] = rand() % 255;
yeah[i+2] = rand() % 255;
}
remove("media/yeah_buddy_2.bmp");
CreateBitmap("media/yeah_buddy_2.bmp", w, h, 8, yeah);
delete[] yeah;
}WriteIntin vähitenmerkitsevän tavun muodostamiskoodi näyttää aika erikoiselta, joskin tuskin vaikuttaa mihinkään. Se on myöskin epäyhteneväinen WriteShortin kanssa.
En suuremmin alkanut tutkia, mutta jos haluat pystyä tallentamaan muitakin kuin 4 jaollisen levyisiä bittikarttoja tuossa formaatissa, niin rivin leveydestä täytyisi tehdä kuitenkin 4:llä tavulla jaollinen, kuten mainitsemasi Wikipedia-artikkelin kappaleessa Pixel storage kerrotaankin.
Tarkoittanet tuota bittisiftausta eestaas WriteIntissä, joku aivopieru päässyt koodiin. Pitänee lukea vähän tarkemmin noita speksejä.
punppis kirjoitti:
Tarkoittanet tuota bittisiftausta eestaas WriteIntissä, joku aivopieru päässyt koodiin..
Itse varmaan laiskuuttani antaisin kääntäjän tehdä laskemisen käyttämällä unionia liittämään integerin tavut oikeisiin komponentteihin ja kirjoittaisin nuo sitten yksinkertaisesti tavu kerrallaan oikeassa järjestyksessä...
Aihe on jo aika vanha, joten et voi enää vastata siihen.