CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2015
    Posts
    48

    Smile write a c++ bitmap image file

    hi i am trying to write a bitmap image with a circle in it in c++ i don't no where to start it then i found some code in stackoverflow i like to use this code and write a circle or a rectangle in and save this file as a .bmp file format i have some complicated data need to write into this bitmap file to do that i need a full working solution i need help to complete this code and i don't understand this line ( long red = lround(255.0 * waterfall[x][y]); )
    thanks

    Code:
    #include <iostream>
    #include <ostream>
    #include <fstream>
    
    using namespace std;
    
    unsigned char file[14] = {
    	'B','M', // magic
    	0,0,0,0, // size in bytes
    	0,0, // app data
    	0,0, // app data
    	40 + 14,0,0,0 // start of data offset
    };
    unsigned char info[40] = {
    	40,0,0,0, // info hd size
    	0,0,0,0, // width
    	0,0,0,0, // heigth
    	1,0, // number color planes
    	24,0, // bits per pixel
    	0,0,0,0, // compression is none
    	0,0,0,0, // image bits size
    	0x13,0x0B,0,0, // horz resoluition in pixel / m
    	0x13,0x0B,0,0, // vert resolutions (0x03C3 = 96 dpi, 0x0B13 = 72 dpi)
    	0,0,0,0, // #colors in pallete
    	0,0,0,0, // #important colors
    };
    
    
    
    int main() {
    
    	ofstream stream;
    
    	// mimeType = "image/bmp";
    	unsigned int const waterfallWidth = 256;
    	unsigned int const waterfallHeight = 256;
    
    	int w = waterfallWidth;
    	int h = waterfallHeight;
    
    	int waterfall[waterfallWidth][waterfallHeight];
    
    	int padSize = (4 - (w * 3) % 4) % 4;
    	int sizeData = w*h * 3 + h*padSize;
    	int sizeAll = sizeData + sizeof(file) + sizeof(info);
    
    	file[2] = (unsigned char)(sizeAll);
    	file[3] = (unsigned char)(sizeAll >> 8);
    	file[4] = (unsigned char)(sizeAll >> 16);
    	file[5] = (unsigned char)(sizeAll >> 24);
    
    	info[4] = (unsigned char)(w);
    	info[5] = (unsigned char)(w >> 8);
    	info[6] = (unsigned char)(w >> 16);
    	info[7] = (unsigned char)(w >> 24);
    
    	info[8] = (unsigned char)(h);
    	info[9] = (unsigned char)(h >> 8);
    	info[10] = (unsigned char)(h >> 16);
    	info[11] = (unsigned char)(h >> 24);
    
    	info[20] = (unsigned char)(sizeData);
    	info[21] = (unsigned char)(sizeData >> 8);
    	info[22] = (unsigned char)(sizeData >> 16);
    	info[23] = (unsigned char)(sizeData >> 24);
    
    
    
    	stream.write((char*)file, sizeof(file));
    	stream.write((char*)info, sizeof(info));
    
    	unsigned char pad[3] = { 0,0,0 };
    
    	for (int y = 0; y<h; y++)
    	{
    		for (int x = 0; x<w; x++)
    		{
    			long red = lround(255.0 * waterfall[x][y]);
    			if (red < 0) red = 0;
    			if (red > 255) red = 255;
    			long green = red;
    			long blue = red;
    
    			unsigned char pixel[3];
    			pixel[0] = blue;
    			pixel[1] = green;
    			pixel[2] = red;
    			
    			stream.write((char*)pixel, 3);
    		}
    		stream.write((char*)pad, padSize);
    	}
    
    
    
    
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: write a c++ bitmap image file

    lround() rounds a floating point value to the nearest long integer. See https://msdn.microsoft.com/en-us/library/dn329049.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2015
    Posts
    48

    Re: write a c++ bitmap image file

    Quote Originally Posted by 2kaud View Post
    lround() rounds a floating point value to the nearest long integer. See https://msdn.microsoft.com/en-us/library/dn329049.aspx
    ok why this values multiplying 255.0 * waterfall[x][y]

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: write a c++ bitmap image file

    The array waterfall[] is not initialised so contains random values from whatever happens to be in memory. So
    Code:
    long red = lround(255.0 * waterfall[x][y]);
    if (red < 0) red = 0;
    if (red > 255) red = 255;
    sets red to be a long int with a value between 0 and 255 inclusive.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: write a c++ bitmap image file

    Each pixel is made up of red, green and blue (R G B) and the values for these range from 0 to 255 - with 0 meaning that colour is not present and 255 the maximum colour intensity. Other colours are obtained by varying the values for these red, green and blue. eg white is (255, 255, 255), black is (0, 0, 0), yellow is (255, 255, 0) and cyan is (0, 255, 255) etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Dec 2015
    Posts
    48

    Re: write a c++ bitmap image file

    1, what is pad size ( int padSize = (4 - (w * 3) % 4) % 4 )

    2, what is the meaning of 8 , 16, 24 in below code
    Code:
            file[2] = (unsigned char)(sizeAll);
    	file[3] = (unsigned char)(sizeAll >> 8);
    	file[4] = (unsigned char)(sizeAll >> 16);
    	file[5] = (unsigned char)(sizeAll >> 24);
    
    	info[4] = (unsigned char)(w);
    	info[5] = (unsigned char)(w >> 8);
    	info[6] = (unsigned char)(w >> 16);
    	info[7] = (unsigned char)(w >> 24);
    3, how can i save this stream into a file

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: write a c++ bitmap image file

    Code:
    info[4] = (unsigned char)(w);
    info[5] = (unsigned char)(w >> 8);
    info[6] = (unsigned char)(w >> 16);
    info[7] = (unsigned char)(w >> 24);
    if you look at where info[] is defined, bytes 4, 5, 6 & 7 are the width. This codes takes an int w (and assumes it is 32 bits - 4 bytes!) and sets info[4] to info[7] to the individual bytes of the 4 byte int. w >> 8 means right shift w by 8 bits ie put in byte 0 what is byte 1. The unsigned char cast returns the value of byte 0. So w >> 16 shifts right by 16 bits so byte 2 is put into byte 0 etc etc. One byte is 8 bits.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Dec 2015
    Posts
    48

    Re: write a c++ bitmap image file

    when i change image width & height 256 to 1024 getting an error i am using visual studio 2015
    Code:
    #include <iostream>
    #include <ostream>
    #include <fstream>
    
    using namespace std;
    
    unsigned char file[14] = {
    	'B','M', // magic
    	0,0,0,0, // size in bytes
    	0,0, // app data
    	0,0, // app data
    	40 + 14,0,0,0 // start of data offset
    };
    unsigned char info[40] = {
    	40,0,0,0, // info hd size
    	0,0,0,0, // width
    	0,0,0,0, // heigth
    	1,0, // number color planes
    	24,0, // bits per pixel
    	0,0,0,0, // compression is none
    	0,0,0,0, // image bits size
    	0x13,0x0B,0,0, // horz resoluition in pixel / m
    	0x13,0x0B,0,0, // vert resolutions (0x03C3 = 96 dpi, 0x0B13 = 72 dpi)
    	0,0,0,0, // #colors in pallete
    	0,0,0,0, // #important colors
    };
    
    
    
    int main() {
    
    	ofstream stream ("Myimage.bmp",std::ios::binary);
    
    	// mimeType = "image/bmp";
    	unsigned int const waterfallWidth = 1024;
    	unsigned int const waterfallHeight = 1024;
    
    	int w = waterfallWidth;
    	int h = waterfallHeight;
    
    	int waterfall[waterfallWidth][waterfallHeight];
    
    	int padSize = (4 - (w * 3) % 4) % 4;
    	int sizeData = w*h * 3 + h*padSize;
    	int sizeAll = sizeData + sizeof(file) + sizeof(info);
    
    	file[2] = (unsigned char)(sizeAll);
    	file[3] = (unsigned char)(sizeAll >> 8);
    	file[4] = (unsigned char)(sizeAll >> 16);
    	file[5] = (unsigned char)(sizeAll >> 24);
    
    	info[4] = (unsigned char)(w);
    	info[5] = (unsigned char)(w >> 8);
    	info[6] = (unsigned char)(w >> 16);
    	info[7] = (unsigned char)(w >> 24);
    
    	info[8] = (unsigned char)(h);
    	info[9] = (unsigned char)(h >> 8);
    	info[10] = (unsigned char)(h >> 16);
    	info[11] = (unsigned char)(h >> 24);
    
    	info[20] = (unsigned char)(sizeData);
    	info[21] = (unsigned char)(sizeData >> 8);
    	info[22] = (unsigned char)(sizeData >> 16);
    	info[23] = (unsigned char)(sizeData >> 24);
    
    
    
    	stream.write((char*)file, sizeof(file));
    	stream.write((char*)info, sizeof(info));
    
    	unsigned char pad[3] = { 0,0,0 };
    
    	for (int y = 0; y<h; y++)
    	{
    		for (int x = 0; x<w; x++)
    		{
    			/*long red = lround(255.0 * waterfall[x][y]);
    			if (red < 0) red = 0;
    			if (red > 255) red = 255;
    			long green = red;
    			long blue = red;*/
    
    			unsigned char pixel[3];
    			pixel[0] = 35;
    			pixel[1] = 65;
    			pixel[2] = 1;
    			
    			stream.write((char*)pixel, 3);
    		}
    		stream.write((char*)pad, padSize);
    	}
    
    	stream.close();
    
    
    	return 0;
    }
    i want image to be 1024 * 1024 and why i am getting error

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: write a c++ bitmap image file

    I've cleaned up the code somewhat. This now compiles and runs under VS2015 and produces a .bmp file with the specified background colour of the required size.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const string bmpnam = "Myimage.bmp";
    
    unsigned int const bmpWidth = 1024;
    unsigned int const bmpHeight = 1024;
    
    unsigned char file[14] = {
    	'B','M',	        // magic
    	0, 0, 0, 0,		// size in bytes
    	0, 0,			// app data
    	0, 0,			// app data
    	40 + 14, 0, 0, 0	// start of data offset
    };
    
    unsigned char info[40] = {
    	40, 0, 0, 0,	        // info size
    	0, 0, 0, 0,		// width
    	0, 0 ,0, 0,		// height
    	1, 0,			// number color planes
    	24, 0,		        // bits per pixel
    	0, 0, 0, 0,		// compression is none
    	0, 0, 0, 0,		// image bits size
    	0x13, 0x0B, 0, 0,       // horz resolution in pixel / m
    	0x13, 0x0B, 0, 0,       // vert resolutions (0x03C3 = 96 dpi, 0x0B13 = 72 dpi)
    	0, 0, 0, 0,		// #colors in pallet
    	0, 0, 0, 0,		// #important colors
    };
    
    void setInt(unsigned char* mem, int data)
    {
    	for (int i = 0; i < sizeof(data); ++i)
    		*mem++ = (unsigned char)(data >> (8 * i));
    }
    
    int main()
    {
    	ofstream stream(bmpnam, std::ios::binary);
    
    	if (!stream.is_open()) {
    		cout << "Cannot open stream" << endl;
    		return 1;
    	}
    
    	const int padSize = (4 - (bmpWidth * 3) % 4) % 4;
    	const int sizeData = bmpWidth * bmpHeight * 3 + bmpHeight * padSize;
    	const int sizeAll = sizeData + sizeof(file) + sizeof(info);
    
    	setInt(&file[2], sizeAll);
    	setInt(&info[4], bmpWidth);
    	setInt(&info[8], bmpHeight);
    	setInt(&info[20], sizeData);
    
    	stream.write((char*)file, sizeof(file));
    	stream.write((char*)info, sizeof(info));
    
    	const char pad[3] = {0};
    	const char backrgb[3] = { 35, 65, 1 };
    
    	for (int y = 0; y < bmpHeight; ++y) {
    		for (int x = 0; x < bmpWidth; ++x)
    			stream.write(backrgb, 3);
    
    		stream.write(pad, padSize);
    	}
    
    	stream.close();
    
    	return 0;
    }
    Have fun!
    Last edited by 2kaud; December 31st, 2016 at 05:54 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Dec 2015
    Posts
    48

    Re: write a c++ bitmap image file

    thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured