You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.9 KiB

  1. #ifndef _BMP_H_
  2. #define _BMP_H_
  3. /**************************************************************
  4. QDBMP - Quick n' Dirty BMP
  5. v1.0.0 - 2007-04-07
  6. http://qdbmp.sourceforge.net
  7. The library supports the following BMP variants:
  8. 1. Uncompressed 32 BPP (alpha values are ignored)
  9. 2. Uncompressed 24 BPP
  10. 3. Uncompressed 8 BPP (indexed color)
  11. QDBMP is free and open source software, distributed
  12. under the MIT licence.
  13. Copyright (c) 2007 Chai Braudo (braudo@users.sourceforge.net)
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20. The above copyright notice and this permission notice shall be included in
  21. all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29. **************************************************************/
  30. #include <stdio.h>
  31. /* Type definitions */
  32. #ifndef UINT
  33. #define UINT unsigned long int
  34. #endif
  35. #ifndef USHORT
  36. #define USHORT unsigned short
  37. #endif
  38. #ifndef UCHAR
  39. #define UCHAR unsigned char
  40. #endif
  41. /* Version */
  42. #define QDBMP_VERSION_MAJOR 1
  43. #define QDBMP_VERSION_MINOR 0
  44. #define QDBMP_VERSION_PATCH 1
  45. /* Error codes */
  46. typedef enum
  47. {
  48. BMP_OK = 0, /* No error */
  49. BMP_ERROR, /* General error */
  50. BMP_OUT_OF_MEMORY, /* Could not allocate enough memory to complete the operation */
  51. BMP_IO_ERROR, /* General input/output error */
  52. BMP_FILE_NOT_FOUND, /* File not found */
  53. BMP_FILE_NOT_SUPPORTED, /* File is not a supported BMP variant */
  54. BMP_FILE_INVALID, /* File is not a BMP image or is an invalid BMP */
  55. BMP_INVALID_ARGUMENT, /* An argument is invalid or out of range */
  56. BMP_TYPE_MISMATCH, /* The requested action is not compatible with the BMP's type */
  57. BMP_ERROR_NUM
  58. } BMP_STATUS;
  59. /* Bitmap image */
  60. typedef struct _BMP BMP;
  61. /*********************************** Public methods **********************************/
  62. /* Construction/destruction */
  63. BMP* BMP_Create ( UINT width, UINT height, USHORT depth );
  64. void BMP_Free ( BMP* bmp );
  65. /* I/O */
  66. BMP* BMP_ReadFile ( const char* filename );
  67. void BMP_WriteFile ( BMP* bmp, const char* filename );
  68. /* Meta info */
  69. UINT BMP_GetWidth ( BMP* bmp );
  70. UINT BMP_GetHeight ( BMP* bmp );
  71. USHORT BMP_GetDepth ( BMP* bmp );
  72. /* Pixel access */
  73. void BMP_GetPixelRGB ( BMP* bmp, UINT x, UINT y, UCHAR* r, UCHAR* g, UCHAR* b );
  74. void BMP_SetPixelRGB ( BMP* bmp, UINT x, UINT y, UCHAR r, UCHAR g, UCHAR b );
  75. void BMP_GetPixelIndex ( BMP* bmp, UINT x, UINT y, UCHAR* val );
  76. void BMP_SetPixelIndex ( BMP* bmp, UINT x, UINT y, UCHAR val );
  77. /* Palette handling */
  78. void BMP_GetPaletteColor ( BMP* bmp, UCHAR index, UCHAR* r, UCHAR* g, UCHAR* b );
  79. void BMP_SetPaletteColor ( BMP* bmp, UCHAR index, UCHAR r, UCHAR g, UCHAR b );
  80. /* Error handling */
  81. BMP_STATUS BMP_GetError ();
  82. const char* BMP_GetErrorDescription ();
  83. /* Useful macro that may be used after each BMP operation to check for an error */
  84. #define BMP_CHECK_ERROR( output_file, return_value ) \
  85. if ( BMP_GetError() != BMP_OK ) \
  86. { \
  87. fprintf( ( output_file ), "BMP error: %s\n", BMP_GetErrorDescription() ); \
  88. return( return_value ); \
  89. } \
  90. #endif