Advertisement
kknndd_

Untitled

Apr 23rd, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #include "transformations.h"
  6.  
  7. struct bmp_header* read_bmp_header(FILE* stream){
  8.  
  9.     if(stream == NULL){
  10.         return NULL;
  11.     }
  12.  
  13.     struct bmp_header* bmpHeader =malloc(sizeof(struct bmp_header));
  14.  
  15.     int res = fread (bmpHeader, sizeof (struct bmp_header), 1, stream);
  16.  
  17.     if( !res ) {
  18.         free(bmpHeader);
  19.         return NULL;
  20.     }
  21.  
  22.     return bmpHeader;
  23.  
  24. }
  25.  
  26. struct pixel* read_data(FILE* stream, const struct bmp_header* header){
  27.  
  28.     if (stream == NULL || header == NULL){
  29.         return NULL;
  30.     }
  31.  
  32.     uint32_t height = header->height;
  33.     uint32_t width = header->width;
  34.     uint32_t padding = width%4;
  35.  
  36.     struct pixel* image_data = malloc(sizeof(struct pixel)*height*width);
  37.  
  38.     fseek (stream, sizeof(struct bmp_header), 0);
  39.  
  40.     for (uint32_t i = 0; i < height; i++){
  41.         //
  42.         for(uint32_t j = 0;j<width;j++){
  43.             //
  44.             fread (&image_data[(i*width)+j], sizeof (struct pixel), 1, stream);
  45.         }
  46.         //
  47.         fseek (stream, padding, SEEK_CUR);
  48. //
  49.     }
  50.  
  51.     return image_data;
  52.  
  53. }
  54.  
  55. struct bmp_image* read_bmp(FILE* stream){
  56.  
  57.     struct bmp_image* image = malloc(sizeof(struct bmp_image));
  58.  
  59.     if(stream == NULL){
  60.         return NULL;
  61.     }
  62.  
  63.     image->header = read_bmp_header(stream);
  64.     image->data = read_data(stream, image->header);
  65.  
  66.     for (int i = 0; i < image->header->width*image->header->height; ++i) {
  67.         printf("%d %d %d\n", image->data[i].red, image->data[i].green, image->data[i].blue);
  68.     }
  69.  
  70.     return image;
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement