Skip to content

Commit

Permalink
add grafixImage
Browse files Browse the repository at this point in the history
add grafixImage
optimized drawImage
limit imagebuffer to 100
  • Loading branch information
ManojTGN committed Apr 3, 2023
1 parent 91cca5d commit 978ad12
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 45 deletions.
15 changes: 7 additions & 8 deletions src/grafix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "grafix.h"
#include "time.h"

static int ID = 0;
static int WINDOW_ID = 0;
static char grafixError[100];
static int grafixInitiated = 0;
grafixWindow* WINDOWS[MAX_WINDOW];
Expand All @@ -11,7 +11,7 @@ _grafixFrameBuffer BUFFERS[MAX_WINDOW];
void grafixInit(){
if(grafixInitiated) return;

ID = 0;
WINDOW_ID = 0;
grafixInitiated = 1;
printf("Initiated Grafix!\n");

Expand Down Expand Up @@ -86,12 +86,12 @@ void updateGrafixWindow(grafixWindow window){
}

int createGrafixWindow(grafixWindow* window, int WIDTH, int HEIGHT, char* NAME){
if(ID == MAX_WINDOW){
if(WINDOW_ID == MAX_WINDOW){
strcpy(grafixError, "grafixError:Unable To CreateWindow;reason:Window Limit Reached Maximum(10);");
return 0;
}

window->id = ID++;
window->id = WINDOW_ID++;
window->height = HEIGHT;
window->width = WIDTH;

Expand All @@ -111,7 +111,7 @@ int createGrafixWindow(grafixWindow* window, int WIDTH, int HEIGHT, char* NAME){

window->_hwnd = CreateWindow((*window)._cname, NAME, WS_OVERLAPPEDWINDOW, 0, 0, WIDTH, HEIGHT, NULL, NULL, GetModuleHandle(NULL), NULL);
if(window->_hwnd == NULL){
ID--;
WINDOW_ID--;
window = NULL;
strcpy(grafixError, "grafixError:Unable To CreateWindow;reason:No Memory Available For Window;");
return 0;
Expand All @@ -126,9 +126,8 @@ int createGrafixWindow(grafixWindow* window, int WIDTH, int HEIGHT, char* NAME){
frame.id = window->id;
frame.frameBuffer = (unsigned char*)malloc(WIDTH * HEIGHT * 3);
if(frame.frameBuffer == NULL){
ID--;
free(WINDOWS[window->id]);
window = NULL;
WINDOW_ID--;
free(WINDOWS[window->id]);window = NULL;
strcpy(grafixError, "grafixError:Unable To CreateWindow;reason:No Memory Available For FrameBuffer;");
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/grafix.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "shapes.h"
#include "time.h"
#include "event.h"
#include "image.h"

#ifndef GRAFIX_H
#define GRAFIX_H
Expand Down Expand Up @@ -36,7 +37,7 @@ typedef struct FRAME{
BITMAPINFO bmi;
} _grafixFrameBuffer;

static int ID;
static int WINDOW_ID;
static char grafixError[100];
static int grafixInitiated;
extern grafixWindow* WINDOWS[MAX_WINDOW];
Expand Down
80 changes: 80 additions & 0 deletions src/image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#include "grafix.h"
#include <stdlib.h>

static int IMAGE_ID;
grafixImage _IMAGES[MAX_IMAGE];

int createGrafixImage(grafixImage *image, const char* filePath, int fileType){
image->_isInit = 0;
image->id = IMAGE_ID++;
image->fileType = fileType;

FILE *image_file;
image_file = fopen(filePath, "rb");
if (image_file == NULL){
IMAGE_ID--;
setGrafixError("grafixError:Unable To Load Image;Reason:No File Found");
return 0;
}

image->height = 0;
image->width = 0;
fseek(image_file, 0x0012, SEEK_SET);
fread(&(image->width), sizeof(image->width), 1, image_file);
fread(&(image->height), sizeof(image->height), 1, image_file);
fseek(image_file, 0x001C, SEEK_SET);
fread(&(image->bitCount), sizeof(image->bitCount), 1, image_file);
int pixelSize = (image->bitCount == 24) ? 3 : 4;

image->_frame = (unsigned char*) malloc(image->width * image->height * 3);
if(image->_frame == NULL){
IMAGE_ID--;
fclose(image_file);
setGrafixError("grafixError:Unable To Load Image;Reason:No Memory Available");
return 0;
}

int index = 0;unsigned char pixel[4];
fseek(image_file, 54, SEEK_SET);
for (int y = image->height-1; y >= 0 ; y--) {
for (int x = 0; x < image->width; x++) {

if(fread(pixel, sizeof(unsigned char), pixelSize, image_file) != pixelSize) break;

index = ( x + y*image->width ) *3;
image->_frame[ index+0 ] = pixel[0];
image->_frame[ index+1 ] = pixel[1];
image->_frame[ index+2 ] = pixel[2];
//pixel[3] if(bitCount == 24)

}
}

fclose(image_file);
image->_isInit = 1;

return 1;
}

void drawGrafixImage(grafixWindow window, grafixImage image, int x, int y){

if(image._isInit != 1){
setGrafixError("grafixError:Unable To Draw Image;Reason:Image Is Not Initiated;");
return;
}

int index;int imageIndex;
for (int _x = x; _x < x+image.width; _x++) {
for (int _y = y; _y < y+image.height ; _y++) {

index = (_x + _y * window.width) * 3;
imageIndex = ( (_x-x) + (_y-y)*image.width) * 3;
BUFFERS[window.id].frameBuffer[index] = image._frame[imageIndex];
BUFFERS[window.id].frameBuffer[index+1] = image._frame[imageIndex+1];
BUFFERS[window.id].frameBuffer[index+2] = image._frame[imageIndex+2];

}
}

}
36 changes: 36 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#ifndef IMAGE_H
#define IMAGE_H

#include "grafix.h"

#define MAX_IMAGE 100

#define FT_BMP 0
#define FT_PNG 1
#define FT_JPG 2

typedef struct IMAGE {
int id;

char* filePath;
int fileType;
int bitCount;

int height;
int width;
int x;
int y;

int _isInit;
unsigned char* _frame;

} grafixImage;

static int IMAGE_ID;
extern grafixImage _IMAGES[MAX_IMAGE];

int createGrafixImage(grafixImage *image, const char* filePath, int fileType);
void drawGrafixImage(grafixWindow window, grafixImage image, int x, int y);

#endif /* IMAGE_H */
35 changes: 0 additions & 35 deletions src/shapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,6 @@ void drawGrafixCircle(grafixWindow window, int xc, int yc, int r, grafixColor co

}

void drawGrafixImage(grafixWindow window,const char* imagePath, int x, int y){
if( window.isDead ) return;

int index = 0;
FILE *image_file;
unsigned char pixel[4];
int width = 0, height = 0;
int bitCount = 0;

image_file = fopen(imagePath, "rb");
if (image_file == NULL) return;

fseek(image_file, 0x0012, SEEK_SET);
fread(&width, sizeof(width), 1, image_file);
fread(&height, sizeof(height), 1, image_file);
fseek(image_file, 0x001C, SEEK_SET);
fread(&bitCount, sizeof(bitCount), 1, image_file);
int pixelSize = (bitCount == 24) ? 3 : 4;

fseek(image_file, 54, SEEK_SET);
for (int _y = y+height; _y >= 0 ; _y--) {
for (int _x = x; _x < x+width; _x++) {

if(fread(pixel, sizeof(unsigned char), pixelSize, image_file) != pixelSize) break;

if (bitCount == 24) _setPixel(window,_x,_y,(grafixColor){pixel[2],pixel[1],pixel[0]});
else _setPixel(window,_x,_y,(grafixColor){pixel[2],pixel[1],pixel[0]});//pixel[3]

}
}

fclose(image_file);

}

void drawGrafixText(grafixWindow window, int x, int y, char* text, int fontSize, grafixColor color){
if( window.isDead ) return;

Expand Down
1 change: 0 additions & 1 deletion src/shapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ void drawGrafixRect(grafixWindow window, int x, int y, int width, int height, gr
void drawGrafixCircle(grafixWindow window, int x, int y, int radius, grafixColor color, int fillMode, int borderWidth);

//I haven't created a seperate header file
void drawGrafixImage(grafixWindow window, const char* filePath, int x, int y);
void drawGrafixText(grafixWindow window, int x, int y, char* text, int fontSize, grafixColor color);

#endif /* SHAPES_H */

0 comments on commit 978ad12

Please sign in to comment.