Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Creating BMP Image contex
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
copthesaint




PostPosted: Sun Mar 31, 2013 6:42 pm   Post subject: Creating BMP Image contex

So for fun I am making this image buffer in c++. Im at the point now where I want to take the information I have from my buffer and compile it into a 32 bit (or 24 bit to start) BMP Image then draw it. however I don't know where to start, I already understand bit shifting and my values in the buffer are all 8 bit each. I also don't want to use other people's libraries. Could anyone point me in a good direction? because right now I'm lost in Google, looking at other peoples code. Here is the code for my current project, btw, plz don't cry because I'm drawing every pixel at such a high level, I'm only doing this because I wanna see how I can and I am very aware of graphics languages:

c++:
//ImageBuffer.h
#ifndef _IMAGEBUFFER_H
#define _IMAGEBUFFER_H

#include <new>
#include <iostream>
#include "Point2D.h"
#include "RGBA.h"
using namespace std;

#define PI 3.14159265

class ImageBuffer{
private:
        RGBA * buffer;
        unsigned __int32 size;
        unsigned __int32 width;
        unsigned __int32 height;
        unsigned __int32 depth;
        unsigned __int32 maxColor;
        bool alpha;
public:
        ImageBuffer(unsigned __int32,unsigned __int32,unsigned __int32, bool);
        ~ImageBuffer();

        RGBA& operator[] (unsigned __int32);
        RGBA& operator[] (Point2D);

        void SetSize (unsigned __int32,unsigned __int32);
        void SetColorDepth(unsigned __int32);
        void SetAlpha (bool);

        void Clear(RGBA);
        void Invert ();
        void DrawDot (Point2D,RGBA);
        void DrawOval (Point2D,float,float,RGBA);
        void DrawArc (Point2D,float,float,float,float,RGBA);

        unsigned __int32 Width ();
        unsigned __int32 Height ();

};

#endif


c++:
//ImageBuffer.cpp
#include "ImageBuffer.h"
#ifdef _IMAGEBUFFER_H
        ImageBuffer::ImageBuffer(unsigned __int32 w,unsigned __int32 h,unsigned __int32 d, bool a)/*: buffer(0), size (-1), width (w), height(h), depth(d),maxColor(1 << d), alpha(a)*/{
                buffer = 0;
                size = -1;
                width = w;
                height = h;
                depth = d;
                maxColor = (1 << d);
                alpha = a;
                buffer = new (nothrow) RGBA [width * height]; //0 -- (w*h)-1
                if (buffer == 0){
                        cout << "Could not allocate memory" << endl;
                        cin.ignore();
                        return;
                }
                size = width * height;
                RGBA *tempAdd = buffer;
                for (int i = 0; i < size; i++){
                        tempAdd->Set(0,0,0,0);
                        tempAdd++;
                }
                return;
        }

        ImageBuffer::~ImageBuffer(){
                if (buffer != 0){
                        delete [] buffer;
                        buffer = 0;
                }
                return;
        }

        RGBA& ImageBuffer::operator[] (unsigned __int32 index){
                return buffer [index];
        }
        RGBA& ImageBuffer::operator[] (Point2D p2D){
                return buffer [p2D.ArrGet(width)];
        }

        void ImageBuffer::SetSize (unsigned __int32 w ,unsigned __int32 h){
                if ((w * h != size) || width != w || height != h){
                        RGBA * tempBuffer = new (nothrow) RGBA [w * h];
                        if (tempBuffer == 0){
                                cout << "could not allocate memory" << endl;
                                cin.ignore();
                                return;
                        }
                        RGBA * tempBufferAdd = tempBuffer;           
                        for (int i = 0; i < (w * h); i++){
                                tempBufferAdd->Set(0,0,0,0);
                                tempBufferAdd++;
                        }
                       
                        RGBA * bufferAdd = buffer;
                        for (__int32 x = 1; x <= width; x++){
                                for (__int32 y = 1; y <= height; y++){
                                        if (x <= w && x > 0 && y <= h && y > 0){
                                                Point2D temp = Point2D((float)x,(float)y);
                                                tempBuffer[temp.ArrGet(w)] = *bufferAdd;
                                        }
                                        bufferAdd++;
                                }
                        }
                        if (buffer != 0){
                                delete [] buffer;
                        }
                        buffer = tempBuffer;
                }
                width = w;
                height = h;
                size = width * height; 
                return;
        }
        void ImageBuffer::SetColorDepth(unsigned __int32 d){
                depth = d;
                unsigned __int32 tempMax = maxColor;
                maxColor = (1 << d);
                if (maxColor != tempMax){
                        RGBA * tempAdd = buffer;
                        for (int i = 0; i < size; i++){
                                tempAdd ->clr[0] = (unsigned __int16)((float)tempAdd->clr[0] * ((float)maxColor / (float)tempMax));
                                tempAdd ->clr[1] = (unsigned __int16)((float)tempAdd->clr[1] * ((float)maxColor / (float)tempMax));
                                tempAdd ->clr[2] = (unsigned __int16)((float)tempAdd->clr[2] * ((float)maxColor / (float)tempMax));
                                tempAdd ->alpha = (unsigned __int16) ((float)tempAdd->alpha  * ((float)maxColor / (float)tempMax));
                                tempAdd++;
                        }
                }
                return;
        }
        void ImageBuffer::SetAlpha (bool a){
                alpha = a;
                if (!a){
                        RGBA *tempAdd = buffer;
                        for (int i = 0; i < size; i++){
                                tempAdd ->alpha = 0;
                                tempAdd++;
                        }
                }
                return;
        }

        void ImageBuffer::Clear(RGBA clr){
                RGBA * tempAdd = buffer;
                for (int i = 0; i < size; i ++){
                        *tempAdd = clr;
                        tempAdd++;
                }
                return;
        }
        void ImageBuffer::Invert (){
                RGBA * tempAdd = buffer;
                for (int i = 0; i < size; i++){
                        tempAdd->Set ((maxColor-1) - tempAdd->clr[0],(maxColor-1) - tempAdd->clr[1],(maxColor-1) - tempAdd->clr[2],tempAdd->alpha);
                        tempAdd++;
                }
                return;
        }
        void ImageBuffer::DrawDot (Point2D p2D,RGBA clr){
                if (p2D.xPos < width && p2D.xPos > 0 && p2D.yPos < height && p2D.yPos > 0){
                        buffer[p2D.ArrGet(width)] = clr;
                }
                return;
        }
        void ImageBuffer::DrawOval (Point2D p2D,float radiusX,float radiusY,RGBA clr){
                float perimeter = PI * (float) (radiusX * radiusY);
                float angleInc = 360 / perimeter;
                for (int i = 0; i < perimeter; i++){
                        DrawDot (Point2D(p2D.xPos + (radiusX * cos (((angleInc * (float) i)* PI) / 180) ), p2D.yPos + (radiusY * sin (((angleInc * (float) i)* PI) / 180) )),clr);
                }
                return;
        }
        void ImageBuffer::DrawArc (Point2D p2D,float radiusX,float radiusY,float initAngle,float finalAngle,RGBA clr){
                float perimeter =((finalAngle - initAngle) / 360) * PI * (float) (radiusX * radiusY);
                float angleInc = (finalAngle - initAngle) / perimeter;
                for (int i = 0; i < perimeter; i++){
                        DrawDot (Point2D(p2D.xPos + (radiusX * cos (((initAngle + angleInc * (float) i) * PI) / 180) ), p2D.yPos + (radiusY * sin (((initAngle + angleInc * (float) i)*PI)/180) )),clr);
                }
                return;
        }

        unsigned __int32 ImageBuffer::Width (){
                return width;
        }
        unsigned __int32 ImageBuffer::Height (){
                return height;
        }

#endif



Colour Buffer.zip
 Description:
All files needed for this project. *if you actually want to run it*

Download
 Filename:  Colour Buffer.zip
 Filesize:  3.72 KB
 Downloaded:  320 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sun Mar 31, 2013 7:01 pm   Post subject: RE:Creating BMP Image contex

put of curiosity, why do you name your variables with double underscores?
copthesaint




PostPosted: Sun Mar 31, 2013 7:31 pm   Post subject: Re: Creating BMP Image contex

[url]http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx[/url]
Those are the data types, above is a complete list of c++ data types.
Raknarg




PostPosted: Sun Mar 31, 2013 7:46 pm   Post subject: RE:Creating BMP Image contex

Oh I wasn't reading those lines properly -.-' Thanks
copthesaint




PostPosted: Sun Mar 31, 2013 9:42 pm   Post subject: RE:Creating BMP Image contex

np, I don't know what the real reason is that the data types have two underscores as well. One wasn't good enough I guess.
TerranceN




PostPosted: Tue Apr 02, 2013 5:50 pm   Post subject: Re: Creating BMP Image contex

You need to figure out how to output binary, then you have to follow the BMP specification. In fact, I made a simple (24bit) BMP reader in Turing as an example.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: