main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include "bmpdecoderhelper.h"
  5. using namespace std;
  6. using namespace image_codec;
  7. // -------------------------------------------------------------------------------------
  8. class BmpToLogo : public BmpDecoderCallback
  9. {
  10. public:
  11. BmpToLogo() : BmpDecoderCallback(), m_width(0), m_height(0) {}
  12. bool DoBmpToLogo(const char *bmpFilename, const char *logoFilename, bool append)
  13. {
  14. // Open input BMP file
  15. ifstream bmpFile;
  16. bmpFile.open(bmpFilename, ios::in | ios::binary);
  17. if (!bmpFile.is_open()) {
  18. cout << "open " << bmpFilename << " failed!" << endl;
  19. return false;
  20. }
  21. // Open output LOGO file
  22. ofstream logoFile;
  23. ios_base::openmode outMode = (ofstream::out | ofstream::binary);
  24. if (append) outMode |= ofstream::app;
  25. logoFile.open(logoFilename, outMode);
  26. if (!logoFile.is_open()) {
  27. cout << "open " << logoFile << " failed!" << endl;
  28. return false;
  29. }
  30. // Read in BMP file content
  31. int bmpFileSize = GetFileSize(bmpFile);
  32. m_bitstream.resize(bmpFileSize);
  33. bmpFile.read(&m_bitstream[0], m_bitstream.size());
  34. // Decode BMP file to destination buffer
  35. BmpDecoderHelper bmpDec;
  36. bmpDec.DecodeImage(&m_bitstream[0], m_bitstream.size(), INT_MAX, this);
  37. // ConvertToRGB565();
  38. // ConvertToRGB565Dither();
  39. // Convert RGB888 buffer to RGB565
  40. ConvertToARGB8888();
  41. // Write to LOGO file
  42. // logoFile.write(&m_rgb565Buffer[0], m_rgb565Buffer.size());
  43. logoFile.write(&m_argb8888Buffer[0], m_argb8888Buffer.size());
  44. // Close Files
  45. bmpFile.close();
  46. logoFile.close();
  47. return true;
  48. }
  49. virtual uint8* SetSize(int width, int height)
  50. {
  51. m_width = (uint32)width;
  52. m_height = (uint32)height;
  53. m_rgb888Buffer.resize(width * height * 3);
  54. m_rgb565Buffer.resize(width * height * 2);
  55. m_argb8888Buffer.resize(width * height * 4);
  56. return (uint8 *) &m_argb8888Buffer[0];
  57. }
  58. private:
  59. int GetFileSize(ifstream& file)
  60. {
  61. ios::pos_type backup = file.tellg();
  62. file.seekg(0, std::ios::end);
  63. ios::pos_type size = file.tellg();
  64. file.seekg(backup, std::ios::beg);
  65. return (int)size;
  66. }
  67. void ConvertToRGB565()
  68. {
  69. uint8 *rgb888 = (uint8*) &m_rgb888Buffer[0];
  70. uint16 *rgb565 = (uint16*) &m_rgb565Buffer[0];
  71. uint32 R, G, B;
  72. for(uint32 i = 0; i < m_width * m_height; ++ i) {
  73. R = rgb888[0]; G = rgb888[1]; B = rgb888[2];
  74. *rgb565 = ((R & 0xF8) << 8) | ((G & 0xFC) << 3) | ((B & 0xF8) >> 3);
  75. ++ rgb565;
  76. rgb888 += 3;
  77. }
  78. }
  79. void ConvertToARGB8888()
  80. {
  81. uint8 *rgb888 = (uint8*) &m_rgb888Buffer[0];
  82. uint32 *argb8888 = (uint32*) &m_argb8888Buffer[0];
  83. uint32 R, G, B;
  84. for(uint32 i = 0; i < m_width * m_height; ++ i) {
  85. R = rgb888[0]; G = rgb888[1]; B = rgb888[2];
  86. *argb8888 = (0xFF << 24) | (R << 16) | (G << 8) | (B);
  87. ++argb8888;
  88. rgb888 += 3;
  89. }
  90. }
  91. #define CLIP_255(x) ((x)>255?255:(x))
  92. void ConvertToRGB565Dither(void)
  93. {
  94. unsigned short DitherMatrix_3Bit_16[4] = {0x5140, 0x3726, 0x4051, 0x2637};
  95. unsigned int count = 0;
  96. unsigned int sr,sg,sb;
  97. unsigned int x, y;
  98. unsigned short dither_scan = 0;
  99. uint8 *src= (uint8*) &m_rgb888Buffer[0];
  100. uint16 *dst= (uint16*) &m_rgb565Buffer[0];
  101. for(count = 0;count < m_width*m_height;count++)
  102. {
  103. x = count % m_width;
  104. y = count / m_width;
  105. dither_scan = DitherMatrix_3Bit_16[(y) & 3];
  106. unsigned short dither = ((dither_scan >> (((x) & 3) << 2)) & 0xF);
  107. sr = ((CLIP_255(((*(src+0) + dither - (*(src+0) >> 5)))) >> (8 - 5)));
  108. sg = ((CLIP_255(((*(src+1) + (dither >> 1) - (*(src+1) >> 6)))) >> (8 - 6)));
  109. sb = ((CLIP_255(((*(src+2) + dither - (*(src+2) >> 5)))) >> (8 - 5)));
  110. printf("%3d,%3d,%3d|%3d,%3d,%3d|%3d,%3d,%3d|%d,%d|%d,%d\n",
  111. (sr<<3)-*(src+0), (sg<<2)-*(src+1),(sb<<3)-*(src+2),
  112. sr<<3,sg<<2,sb<<3,
  113. *(src+0), *(src+1),*(src+2),
  114. x,y,
  115. dither_scan, dither);
  116. src += 3;
  117. *dst++ = ((uint16_t)((sr << (5 + 6)) | (sg << (5)) | (sb << 0)));
  118. }
  119. }
  120. private:
  121. vector<char> m_bitstream;
  122. vector<char> m_rgb888Buffer;
  123. vector<char> m_argb8888Buffer;
  124. vector<char> m_rgb565Buffer;
  125. uint32 m_width, m_height;
  126. };
  127. // -------------------------------------------------------------------------------------
  128. int main(int argc, const char* argv[])
  129. {
  130. if (argc < 3) {
  131. cout << endl << "[Usage] bmp_to_logo logofile bmpfile1 [bmpfile2] ..." << endl << endl;
  132. cout << "Example: bmp_to_logo fhd.raw fhd_uboot.bmp fhd_kernel.bmp ..." << endl << endl;
  133. return -1;
  134. }
  135. const char *logo_filename = argv[1];
  136. const char *bmp_filename = argv[2];
  137. BmpToLogo bmpToLogo;
  138. if (!bmpToLogo.DoBmpToLogo(bmp_filename, logo_filename, false)) {
  139. return -2;
  140. }
  141. for (int i = 3; i < argc; ++ i) {
  142. bmp_filename = argv[i];
  143. if (!bmpToLogo.DoBmpToLogo(bmp_filename, logo_filename, true)) {
  144. return -2;
  145. }
  146. }
  147. }
  148. // -------------------------------------------------------------------------------------