Skip to main content

Part0-Digital Art Projects on C++, Challenge Series-Ep.0

     In this series we will try to develop kindly applications. In the beginning you and I try to develop it with our information. When I complete the first part I will share my experiences, information and source code. You can get help while this challenge from internet and if you stuck you can look my example up. However, if you try harder and harder to do it yourself, you can gain more and permanent experience. I will also direct you to necessary sources for doing this. 

Challenge: Develop a program that create art with using different shapes, functions and color schemes by using user input to BMP image file. 

    Before starting:

    This project is not suitable for beginner level coders. If you feel you are weak at programming first develop yourself with this sources and some challenges. (I will share some challenges with their solutions soon)

  1. learncpp
  2. geeksforgeeks
  3. edx.org
  4. coursera.org

Some titles that you need to know (if you don't know some, do not worry you can handle them while challenge) : 

  1. All basics
  2. OOP
  3. Vectors
  4. Algorithm logic
  5. File manipulations
Get starting:
    As I said we use BMP files for this project. You can look this file structure below. (We use most general version. that Windows BMP file, often referred to as the "BITMAPINFOHEADER" format.)



For better understanding I will explain it like this:
(Each byte represent 8 bit)

1-) File Header (14 bytes):

  • Signature (2 bytes): The first two bytes are 'B' and 'M' (ASCII values 66 and 77) to identify the file as a BMP.
  • File Size (4 bytes): The size of the entire BMP file in bytes.
  • Reserved (4 bytes): Reserved for future use.
  • Data Offset (4 bytes): The offset, in bytes, from the beginning of the file to the bitmap image data.
2-) Bitmap Information Header (DIB Header)(40 bytes):
    
  • Header Size (4 bytes): The size of the header (always 40 bytes for the BITMAPINFOHEADER).
  • Image Width (4 bytes): The width of the bitmap image in pixels.
  • Image Height (4 bytes): The height of the bitmap image in pixels. Positive for bottom-up, negative for top-down.
  • Number of Color Planes (2 bytes): Usually set to 1.
  • Bits Per Pixel (2 bytes): The number of bits used to represent each pixel (e.g., 24 bits for RGB).
  • Compression Method (4 bytes): The compression method used (usually 0 for no compression).
  • Image Size (4 bytes): The size of the raw bitmap data (can be set to 0 for no compression).
  • Horizontal Resolution (4 bytes): Pixels per meter (optional).
  • Vertical Resolution (4 bytes): Pixels per meter (optional).
  • Number of Colors in Palette (4 bytes): The number of colors in the color palette (0 for full color).
  • Number of Important Colors (4 bytes): Usually set to 0.(This because Every color is important)

  1. 3-)Color Palette (Optional): For images with indexed color (e.g., 1-bit or 8-bit BMPs), there may be a color palette immediately following the DIB header. (We don't use it)


  2. 4-)Bitmap Data: The raw pixel data of the image, typically organized in rows from bottom to top, with each row aligned to a multiple of 4 bytes (padding bytes are added if necessary).

  3. (Our main work on that area)

Now let's see this structure on C++ code.
//Define structures
struct BMPFileHeader
{
    char signature[2] = {'B', 'M'};
    uint32_t filesize; //Calculate
    uint16_t reserved1 = 0;
    uint16_t reserved2 = 0;
    uint32_t dataOffset; // Calculate
};
struct BMPDIBHeader
{
    uint32_t DIBHeaderSize = 40;
    uint32_t ImageWidth;    
    uint32_t ImageHeight;
    uint16_t NumberOfColorPlanes = 1;
    uint16_t BitsPerPixel = 24;
    uint32_t CompressionMethod = 0;
    uint32_t ImageSize; //Calculate
    uint32_t HorizontalResolution = 0;
    uint32_t VerticalResolution = 0;
    uint32_t NumberOfColorsInPallette = 0;
    uint32_t NumberOfImportantColors = 0;
};
⮚Our header data struct is ready, now let's create a blank image.


int main()
{
    BMPFileHeader fileHeader;
    BMPDIBHeader dibHeader;

    //Decleration width and height and implamet
    const int WIDTH = 150;
    const int HEIGHT = 150;

    dibHeader.ImageWidth = static_cast<uint32_t>(WIDTH);
    dibHeader.ImageHeight = static_cast<uint32_t>(HEIGHT);

    //Creating image
    uint8_t image[HEIGHT][WIDTH + (WIDTH%4)][3];

    for (int y = 0; y < HEIGHT; y++)
    {
        for ( int x = 0; x < WIDTH; x++)
        {
            image[y][x][0] = 255;
            image[y][x][1] = 255;
            image[y][x][2] = 255;
        }
        for (int p = 0; p < WIDTH % 4; p++)
        {
            image[y][WIDTH + p][0] = 255;
            image[y][WIDTH + p][1] = 255;
            image[y][WIDTH + p][2] = 255;
        }
    }

    //Calculations
    uint32_t imageSize = sizeof(image);

    dibHeader.ImageSize = imageSize;
    fileHeader.dataOffset = sizeof(fileHeader) + sizeof(dibHeader);
    fileHeader.filesize = fileHeader.dataOffset + imageSize;

    //Create file
    std::ofstream outFile("image.bmp", std::ios::binary);

    //Ensureing is open
    if (!outFile)
    {
        std::cout << "File could not be create" << std::endl;
        return 1;
    }

    //Write Bmp file

    outFile.write(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader));
    outFile.write(reinterpret_cast<char*>(&dibHeader), sizeof(dibHeader));
    outFile.write(reinterpret_cast<char*>(&image), imageSize);

    //Close file
    outFile.close();
   
    std::cout << "Success" << std:: endl;
   
    return 0;
}



⮚On next part we will develop a class for create different shapes and colors. Also we will prepare a UI for make program more user friendly.














Comments

Popular posts from this blog

Story - 0 -How did my journey start?

How did my journey start?      I always have an interest for computer. When I was started to high school, this interest got bigger and bigger day by day. Because I was  really introduce to computer science by computer lessons on school. However, these lessons were not enough for me, but these were enough for wake my enthusiasm up. Then I started coding with own my own I didn't know anything, but i was search always. I was started with Visual-Basic and just using by Visual studio form app. when I was started , in my perspective this is a big fault as I think VB not a good point to start learning coding. On that point I have an another drawback that is my weak English knowledge, so I was also find less source and if I found, I don't understand or less understand most of them. You can ask, there were so many sources how could not you find them, yes there were many sources but as I said my English was poor and I was try to find Turkish documents or tutorials. While this ...

0. Should you be a computer(engineer, scientist)

  Before beginning:                 First of all, how can you decide to be a computer engineer (or study computer science) or develop yourself on coding, engineering etc. If you interested in computer or want to investigate your interest, you should test your interest. Because, this industry is develop really fast and have many different branch and also these are request much time to learn. Secondly, please aware of yourself, if you do not want to sit whole day or update yourself with technology, maybe this area are not suitable for you. However before decide this as I said you can check it out for a mile. How can you test your interest?                 You can test it with some main points on this area with less time and requirements. I have a method for this. This method have 2 steps, each step are really important for test yo...