Browse Source

GIF now returns frame delays, added PM_image_free

Iver 6 days ago
parent
commit
8ccf2ca6a8
6 changed files with 42 additions and 12 deletions
  1. BIN
      builds/libpitmap.so
  2. BIN
      builds/main.bin
  3. 3 6
      changelog.txt
  4. 10 4
      src/headers/pitmap.h
  5. 11 1
      src/launch_program/main.c
  6. 18 1
      src/library/main.c

BIN
builds/libpitmap.so


BIN
builds/main.bin


+ 3 - 6
changelog.txt

@@ -1,6 +1,3 @@
--fixed GIF errors that were holding me up for weeks. In the standard code addition branch, first_code was mistakenly set to previous code instead of code, & clear code was not reseting current_highest_defined_code
--flipped output of GIF parser
--returned image has animation data
--frame is equal to previous frame
--if pixel is the transparent index ignore it
--un-flipped parsers that flipped data before
+-GIF parser now returns frame delay times
+-added PM_free_image to unallocate an image
+-BMP parser fully initializes PM_image GIF value to NULL ones

+ 10 - 4
src/headers/pitmap.h

@@ -4,12 +4,18 @@
 #include <stdint.h>
 #include <stdint.h>
 
 
 typedef struct {
 typedef struct {
-    uint32_t* frame_buffer;
-    int width, height;
-    int frame_count;
-    int frame_height;
+    uint32_t* frame_buffer; // the array that stores the pixel data
+    int width; // width of the image
+    int height; // height of the image INCLUDING all stacked frames
+    int frame_height; // height of each FRAME
+    int frame_count; // number of frames
+    uint16_t* frame_delays; // centisecond delay for each frame
 } PM_image;
 } PM_image;
 
 
+// loads an image file and returns a PM_image
 PM_image* PM_load_image(const char* filename, unsigned char debug_mode);
 PM_image* PM_load_image(const char* filename, unsigned char debug_mode);
 
 
+// unallocated a PM_image*
+void PM_free_image(PM_image* image);
+
 #endif
 #endif

+ 11 - 1
src/launch_program/main.c

@@ -43,7 +43,17 @@ int write_tga(const char* path, const PM_image* img) {
 }
 }
 
 
 int main(){
 int main(){
-    write_tga("./image.tga", PM_load_image("images/test.gif.gif", 0));
+    PM_image* image = PM_load_image("images/test.gif.gif", 0);
+
+    write_tga("./image.tga", image);
+
+    for (int i = 0; i < image->frame_count; i++){
+        printf("%f seconds, ", (double)image->frame_delays[i] / 100.0);
+    }
+
+    printf("\n");
+
+    PM_free_image(image);
 
 
     return 0;
     return 0;
 }
 }

+ 18 - 1
src/library/main.c

@@ -133,6 +133,9 @@ PM_image* PM_load_bitmap(unsigned char debug_mode){
     }
     }
     
     
     image->frame_buffer = malloc(sizeof(uint32_t) * image_width * image_height);
     image->frame_buffer = malloc(sizeof(uint32_t) * image_width * image_height);
+    image->frame_delays = NULL;
+    image->frame_count = 1;
+    image->frame_height = image_height;
     image->width = image_width;
     image->width = image_width;
     image->height = image_height;
     image->height = image_height;
 
 
@@ -284,6 +287,7 @@ PM_image* PM_load_gif(unsigned char debug_mode){
     image->frame_height = image_height;
     image->frame_height = image_height;
 
 
     image->frame_buffer = NULL;
     image->frame_buffer = NULL;
+    image->frame_delays = NULL;
 
 
     image->width = image_width;
     image->width = image_width;
 
 
@@ -363,10 +367,14 @@ PM_image* PM_load_gif(unsigned char debug_mode){
 
 
                 uint8_t gce_size = get_1();
                 uint8_t gce_size = get_1();
                 uint8_t packed_field = get_1(); // bit field
                 uint8_t packed_field = get_1(); // bit field
-                skip(2);
+                uint16_t delay_time = get_2();
                 transparent_color_gct_index = get_1();
                 transparent_color_gct_index = get_1();
                 skip(1);
                 skip(1);
 
 
+                image->frame_delays = realloc(image->frame_delays, sizeof(uint16_t) * frame_count);
+
+                image->frame_delays[frame_count - 1] = delay_time;
+
                 // "disposal method" is the first variable in the packed field
                 // "disposal method" is the first variable in the packed field
                 // we can ignore this because I think it's mostly for UI programs
                 // we can ignore this because I think it's mostly for UI programs
 
 
@@ -746,3 +754,12 @@ PM_image* PM_load_image(const char *filename, unsigned char debug_mode){
 
 
     return NULL;
     return NULL;
 }   
 }   
+
+void PM_free_image(PM_image* image){
+    if (image){
+        if (image->frame_buffer) free(image->frame_buffer);
+        if (image->frame_delays) free(image->frame_delays);
+        free(image);
+    } else  
+        printf("can't free a NULL image!\n");
+}