From 978ad12eeaf8ccbc37cf14b3cc69f61a12f41b41 Mon Sep 17 00:00:00 2001
From: ManojTGN <manoj.thunderviz@gmail.com>
Date: Mon, 3 Apr 2023 15:37:48 +0530
Subject: [PATCH] add grafixImage

add grafixImage
optimized drawImage
limit imagebuffer to 100
---
 src/grafix.c | 15 +++++-----
 src/grafix.h |  3 +-
 src/image.c  | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/image.h  | 36 +++++++++++++++++++++++
 src/shapes.c | 35 -----------------------
 src/shapes.h |  1 -
 6 files changed, 125 insertions(+), 45 deletions(-)
 create mode 100644 src/image.c
 create mode 100644 src/image.h

diff --git a/src/grafix.c b/src/grafix.c
index b1811dd..cf94730 100644
--- a/src/grafix.c
+++ b/src/grafix.c
@@ -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];
@@ -11,7 +11,7 @@ _grafixFrameBuffer BUFFERS[MAX_WINDOW];
 void grafixInit(){
     if(grafixInitiated) return;
 
-    ID = 0;
+    WINDOW_ID = 0;
     grafixInitiated = 1;
     printf("Initiated Grafix!\n");
     
@@ -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;
     
@@ -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;
@@ -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;
     }
diff --git a/src/grafix.h b/src/grafix.h
index c3abbca..f2e33c4 100644
--- a/src/grafix.h
+++ b/src/grafix.h
@@ -6,6 +6,7 @@
 #include "shapes.h"
 #include "time.h"
 #include "event.h"
+#include "image.h"
 
 #ifndef GRAFIX_H
 #define GRAFIX_H
@@ -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];
diff --git a/src/image.c b/src/image.c
new file mode 100644
index 0000000..0376f99
--- /dev/null
+++ b/src/image.c
@@ -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];
+
+    }
+    }
+
+}
\ No newline at end of file
diff --git a/src/image.h b/src/image.h
new file mode 100644
index 0000000..8bd0756
--- /dev/null
+++ b/src/image.h
@@ -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 */
\ No newline at end of file
diff --git a/src/shapes.c b/src/shapes.c
index ebc9299..9fd29df 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -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;
 
diff --git a/src/shapes.h b/src/shapes.h
index 13b3398..39b117b 100644
--- a/src/shapes.h
+++ b/src/shapes.h
@@ -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 */
\ No newline at end of file