bmp.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef _BMP_H_
  2. #define _BMP_H_
  3. // ====== DO NOT MODIFY THIS FILE ===========
  4. // Set data alignment to 1 byte boundary
  5. #pragma pack(1)
  6. /*
  7. * BMP files are laid out in the following fashion:
  8. * --------------------------
  9. * | Header | 54 bytes
  10. * |-------------------------
  11. * | Palette (optional) | 0 bytes (for 24-bit RGB images)
  12. * |-------------------------
  13. * | Image Data | file size - 54 (for 24-bit images)
  14. * --------------------------
  15. */
  16. /**
  17. * BMP header (54 bytes).
  18. */
  19. typedef unsigned short int uint16_t;
  20. typedef unsigned int uint32_t;
  21. typedef int int32_t;
  22. typedef struct {
  23. uint16_t type; // Magic identifier
  24. uint32_t size; // File size in bytes
  25. uint16_t reserved1; // Not used
  26. uint16_t reserved2; // Not used
  27. uint32_t offset; // Offset to image data in bytes
  28. uint32_t header_size; // Header size in bytes
  29. int32_t width; // Width of the image
  30. int32_t height; // Height of image
  31. uint16_t planes; // Number of color planes
  32. uint16_t bits; // Bits per pixel
  33. uint32_t compression; // Compression type
  34. uint32_t imagesize; // Image size in bytes
  35. int32_t xresolution; // Pixels per meter
  36. int32_t yresolution; // Pixels per meter
  37. uint32_t ncolours; // Number of colors
  38. uint32_t importantcolours; // Important colors
  39. } BMP_Header;
  40. typedef struct {
  41. BMP_Header header;
  42. int data_size;
  43. int width;
  44. int height;
  45. int bytes_per_pixel; // This amount should be equals to number of bits/8
  46. char *data;
  47. } BMP_Image;
  48. // Constructor - creates a new BMP image structure
  49. BMP_Image *BMP_create(BMP_Header *header);
  50. // Load a BMP image given a filename (will create a new BMP image structure)
  51. // Returns NULL if failure.
  52. BMP_Image *BMP_load(const char *filename);
  53. // Save a BMP image given a filename (returns 0 if failure)
  54. int BMP_save(BMP_Image *image, const char *filename);
  55. // Destructor - deallocates a BMP image structure
  56. void BMP_destroy(BMP_Image *image);
  57. // Invert all of the image data in a BMP image (value = 255 - value)
  58. void BMP_invert(BMP_Image *image);
  59. // Print the contents of the BMP header (provided)
  60. void BMP_printHeader(BMP_Header *header);
  61. // Check whether the BMP file is a valid file (provided)
  62. int BMP_checkValid(BMP_Header *header);
  63. #endif /* bmp.h */