bmp.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef _BMP_H_
  2. # define _BMP_H_
  3. #include <vector>
  4. #include <string>
  5. using std::string;
  6. using std::vector;
  7. struct Color
  8. {
  9. Color(
  10. unsigned char red,
  11. unsigned char green,
  12. unsigned char blue
  13. ) :
  14. mRed(red), mGreen(green), mBlue(blue)
  15. {
  16. }
  17. Color() : mRed(255), mGreen(255), mBlue(255)
  18. {
  19. }
  20. static Color red() { return(Color(255, 0, 0)); }
  21. static Color green() { return(Color(0, 255, 0)); }
  22. static Color blue() { return(Color(0, 0, 255)); }
  23. static Color cyan() { return(Color(0, 255, 255)); }
  24. static Color magenta() { return(Color(255, 0, 255)); }
  25. static Color yellow() { return(Color(255, 255, 0)); }
  26. static Color black() { return(Color(0, 0, 0)); }
  27. static Color white() { return(Color()); }
  28. unsigned char mRed;
  29. unsigned char mGreen;
  30. unsigned char mBlue;
  31. };
  32. typedef vector<Color> ColorRow;
  33. class Bmp
  34. {
  35. public:
  36. Bmp(int width, int height);
  37. void setPixel(
  38. int x,
  39. int y,
  40. unsigned char red,
  41. unsigned char green,
  42. unsigned char blue
  43. );
  44. void setPixel(
  45. int x,
  46. int y,
  47. const Color &theColor
  48. );
  49. void drawRectangle(
  50. int x,
  51. int y,
  52. int width,
  53. int height,
  54. unsigned char red,
  55. unsigned char green,
  56. unsigned char blue
  57. );
  58. void drawRectangle(
  59. int x,
  60. int y,
  61. int width,
  62. int height,
  63. const Color &theColor
  64. );
  65. void fillRectangle(
  66. int x,
  67. int y,
  68. int width,
  69. int height,
  70. unsigned char red,
  71. unsigned char green,
  72. unsigned char blue
  73. );
  74. void fillRectangle(
  75. int x,
  76. int y,
  77. int width,
  78. int height,
  79. const Color &theColor
  80. );
  81. void fillPolygon(
  82. const vector<int> &points,
  83. unsigned char red,
  84. unsigned char green,
  85. unsigned char blue
  86. );
  87. void fillPolygon(
  88. const vector<int> &points,
  89. const Color &theColor
  90. );
  91. void drawLine(
  92. int x0, int y0,
  93. int x1, int y1,
  94. unsigned char red,
  95. unsigned char green,
  96. unsigned char blue
  97. );
  98. void drawLine(
  99. int x0, int y0,
  100. int x1, int y1,
  101. const Color &theColor
  102. );
  103. void drawPolyline(
  104. const vector<int> &points,
  105. unsigned char red,
  106. unsigned char green,
  107. unsigned char blue
  108. );
  109. void drawPolyline(
  110. const vector<int> &points,
  111. const Color &theColor
  112. );
  113. int getWidth() const;
  114. int getHeight() const;
  115. bool write(string &fileName, string &errMsg) const;
  116. private:
  117. int mWidth;
  118. int mHeight;
  119. vector<ColorRow> mImage;
  120. };
  121. #endif