一组rgb转换函数,支持rgb565rgb888xrgb8888之间的数据转换
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
--------47. #endif // #ifndef _mybmp_h__
rgb_convert.c Cpp 代码 1. /********************************************************
************ 2. created: 2012/05/19 3. filename: rgb_convert.c 4. author: 5. 6. purpose: 7. *********************************************************
一组 rgb 转换函数,支持 rgb565rgb888xrgb8888 之间的 数据转换
一组 rgb 转换函数,支持 rgb565/rgb888/xrgb8888 之间的数据转 换
mybmp.h Cpp 代码 1. /********************************************************
t h) 92. { 93. line_reversal(p8888, w, h, 3---------------------------------------------
--------97. //转换 98. static int rgb565_to_rgb888(const void * psrc, int w, i
nt w, int h); 39. void * rgbx8888_to_rgb888_buffer(const void * psrc, i
nt w, int h); 40. RGB_CONVERT_FUN get_convert_func(int frombpp, in
t tobpp); 41. 42. #ifdef __cplusplus 43. }; 44. #endif 45. 46. //----------------------------------------------------------
62. unsigned char * linebuffer = NULL; 63. 64. if (pdata && w > 0 && h > 1) {//至少两行才需要翻转 65. linebuffer = (unsigned char *)malloc(linesize); 66. if (linebuffer) { 67. pline = (unsigned char *)pdata; 68. pline1 = (unsigned char *)pdata + linesize * (h - 1); 69. for (i = 0; i < copylines; i++) { 70. memcpy(linebuffer, pline, linesize); 71. memcpy(pline, pline1, linesize); 72. memcpy(pline1, linebuffer, linesize); 73. pline += linesize; 74. pline1 -= linesize; 75. } 76. free(linebuffer); 77. } 78. } 79. } 80. 81. void rgb565_line_reversal(void * p565, int w, int h) 82. { 83. line_reversal(p565, w, h, 16); 84. } 85. 86. void rgb888_line_reversal(void * p888, int w, int h) 87. { 88. line_reversal(p888, w, h, 24); 89. } 90. 91. void rgbx8888_line_reversal(void * p8888, int w, in
36. void rbg565_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh)
37. { 38. rgb_copy(psrc, pdst, sw, sh, dw, dh, 16); 39. } 40. 41. void rbg888_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh) 42. { 43. rgb_copy(psrc, pdst, sw, sh, dw, dh, 24); 44. } 45. 46. void rbgx8888_copy(const void * psrc, void * pdst, in t sw, int sh, int dw, int dh) 47. { 48. rgb_copy(psrc, pdst, sw, sh, dw, dh, 32); 49. } 50. 51. //-----------------------------------------------------------------52. //行数据翻转 53. void line_reversal(void * pdata, int w, int h, int bpp) 54. { 55. int bytes = UpAlign8(bpp) >> 3; // bpp / 8 56. int linesize = UpAlign4(w * bytes);//4 的整数倍 57. int copylines = h >> 1; 58. 59. int i; 60. unsigned char * pline = NULL; 61. unsigned char * pline1 = NULL;
nt h, void * pdst) 99. { 100. int srclinesize = UpAlign4(w * 2); 101. int dstlinesize = UpAlign4(w * 3); 102. 103. const unsigned char * psrcline; 104. const unsigned short * psrcdot; 105. unsigned char * pdstline; 106. unsigned char * pdstdot; 107. 108. int i,j; 109. 110. if (!psrc || !pdst || w <= 0 || h <= 0) { 111. printf("rgb565_to_rgb888 : parameter error\n"); 112. return -1; 113. } 114. 115. psrcline = (const unsigned char *)psrc; 116. pdstline = (unsigned char *)pdst; 117. for (i=0; i<h; i++) { 118. psrcdot = (const unsigned short *)psrcline;
--------12. 13. #ifdef __cplusplus 14. extern "C" { 15. #endif 16. 17. #define UpAlign4(n) (((n) + 3) & ~3) 18. #define UpAlign8(n) (((n) + 7) & ~7)
19. 20. //拷贝数据 21. void rgb_copy(const void * psrc, void * pdst, int sw, i nt sh, int dw, int dh, int bpp); 22. void rbg565_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh); 23. void rbg888_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh); 24. void rbgx8888_copy(const void * psrc, void * pdst, in t sw, int sh, int dw, int dh); 25. 26. //行数据翻转 27. void line_reversal(void * pdata, int w, int h, int bpp); 28. void rgb565_line_reversal(void * p565, int w, int h); 29. void rgb888_line_reversal(void * p888, int w, int h); 30. void rgbx8888_line_reversal(void * p8888, int w, in t h); 31. 32. //转换 33. typedef void * (* RGB_CONVERT_FUN)(const void * p src, int w, int h); 34. void * rgb565_to_rgb888_buffer(const void * psrc, in t w, int h); 35. void * rgb888_to_rgb565_buffer(const void * psrc, in t w, int h); 36. void * rgb565_to_rgbx8888_buffer(const void * psrc, i nt w, int h); 37. void * rgbx8888_to_rgb565_buffer(const void * psrc, i nt w, int h); 38. void * rgb888_to_rgbx8888_buffer(const void * psrc, i
************/ 8. 9. //----------------------------------------------------------
--------10. #include <stdlib.h>
11. #include <stdio.h> 12. #include <memory.h> 13. #include "mybmp.h" 14. 15. //-----------------------------------------------------------------16. //拷贝 17. void rgb_copy(const void * psrc, void * pdst, int sw, i nt sh, int dw, int dh, int bpp) 18. { 19. int bytes = UpAlign8(bpp) >> 3; // bpp / 8 20. int srclinesize = UpAlign4(sw * bytes); 21. int dstlinesize = UpAlign4(dw * bytes); 22. int copylinesize = srclinesize < dstlinesize ? srclinesiz e : dstlinesize; 23. int copylines = sh < dh ? sh : dh; 24. 25. const unsigned char * psrcline = (const unsigned cha r *)psrc; 26. const unsigned char * pend = psrcline + copylines * s rclinesize; 27. unsigned char * pdstline = (unsigned char *)pdst; 28. 29. while (psrcline < pend) { 30. memcpy(pdstline, psrcline, copylinesize); 31. psrcline += srclinesize; 32. pdstline += dstlinesize; 33. } 34. } 35.
************ 2. created: 2012/04/07 3. filename: mybmp.h 4. author: 5. 6. purpose: 7. *********************************************************
************/ 8. 9. #ifndef _mybmp_h__ 10. #define _mybmp_h__ 11. //----------------------------------------------------------
rgb_convert.c Cpp 代码 1. /********************************************************
************ 2. created: 2012/05/19 3. filename: rgb_convert.c 4. author: 5. 6. purpose: 7. *********************************************************
一组 rgb 转换函数,支持 rgb565rgb888xrgb8888 之间的 数据转换
一组 rgb 转换函数,支持 rgb565/rgb888/xrgb8888 之间的数据转 换
mybmp.h Cpp 代码 1. /********************************************************
t h) 92. { 93. line_reversal(p8888, w, h, 3---------------------------------------------
--------97. //转换 98. static int rgb565_to_rgb888(const void * psrc, int w, i
nt w, int h); 39. void * rgbx8888_to_rgb888_buffer(const void * psrc, i
nt w, int h); 40. RGB_CONVERT_FUN get_convert_func(int frombpp, in
t tobpp); 41. 42. #ifdef __cplusplus 43. }; 44. #endif 45. 46. //----------------------------------------------------------
62. unsigned char * linebuffer = NULL; 63. 64. if (pdata && w > 0 && h > 1) {//至少两行才需要翻转 65. linebuffer = (unsigned char *)malloc(linesize); 66. if (linebuffer) { 67. pline = (unsigned char *)pdata; 68. pline1 = (unsigned char *)pdata + linesize * (h - 1); 69. for (i = 0; i < copylines; i++) { 70. memcpy(linebuffer, pline, linesize); 71. memcpy(pline, pline1, linesize); 72. memcpy(pline1, linebuffer, linesize); 73. pline += linesize; 74. pline1 -= linesize; 75. } 76. free(linebuffer); 77. } 78. } 79. } 80. 81. void rgb565_line_reversal(void * p565, int w, int h) 82. { 83. line_reversal(p565, w, h, 16); 84. } 85. 86. void rgb888_line_reversal(void * p888, int w, int h) 87. { 88. line_reversal(p888, w, h, 24); 89. } 90. 91. void rgbx8888_line_reversal(void * p8888, int w, in
36. void rbg565_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh)
37. { 38. rgb_copy(psrc, pdst, sw, sh, dw, dh, 16); 39. } 40. 41. void rbg888_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh) 42. { 43. rgb_copy(psrc, pdst, sw, sh, dw, dh, 24); 44. } 45. 46. void rbgx8888_copy(const void * psrc, void * pdst, in t sw, int sh, int dw, int dh) 47. { 48. rgb_copy(psrc, pdst, sw, sh, dw, dh, 32); 49. } 50. 51. //-----------------------------------------------------------------52. //行数据翻转 53. void line_reversal(void * pdata, int w, int h, int bpp) 54. { 55. int bytes = UpAlign8(bpp) >> 3; // bpp / 8 56. int linesize = UpAlign4(w * bytes);//4 的整数倍 57. int copylines = h >> 1; 58. 59. int i; 60. unsigned char * pline = NULL; 61. unsigned char * pline1 = NULL;
nt h, void * pdst) 99. { 100. int srclinesize = UpAlign4(w * 2); 101. int dstlinesize = UpAlign4(w * 3); 102. 103. const unsigned char * psrcline; 104. const unsigned short * psrcdot; 105. unsigned char * pdstline; 106. unsigned char * pdstdot; 107. 108. int i,j; 109. 110. if (!psrc || !pdst || w <= 0 || h <= 0) { 111. printf("rgb565_to_rgb888 : parameter error\n"); 112. return -1; 113. } 114. 115. psrcline = (const unsigned char *)psrc; 116. pdstline = (unsigned char *)pdst; 117. for (i=0; i<h; i++) { 118. psrcdot = (const unsigned short *)psrcline;
--------12. 13. #ifdef __cplusplus 14. extern "C" { 15. #endif 16. 17. #define UpAlign4(n) (((n) + 3) & ~3) 18. #define UpAlign8(n) (((n) + 7) & ~7)
19. 20. //拷贝数据 21. void rgb_copy(const void * psrc, void * pdst, int sw, i nt sh, int dw, int dh, int bpp); 22. void rbg565_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh); 23. void rbg888_copy(const void * psrc, void * pdst, int s w, int sh, int dw, int dh); 24. void rbgx8888_copy(const void * psrc, void * pdst, in t sw, int sh, int dw, int dh); 25. 26. //行数据翻转 27. void line_reversal(void * pdata, int w, int h, int bpp); 28. void rgb565_line_reversal(void * p565, int w, int h); 29. void rgb888_line_reversal(void * p888, int w, int h); 30. void rgbx8888_line_reversal(void * p8888, int w, in t h); 31. 32. //转换 33. typedef void * (* RGB_CONVERT_FUN)(const void * p src, int w, int h); 34. void * rgb565_to_rgb888_buffer(const void * psrc, in t w, int h); 35. void * rgb888_to_rgb565_buffer(const void * psrc, in t w, int h); 36. void * rgb565_to_rgbx8888_buffer(const void * psrc, i nt w, int h); 37. void * rgbx8888_to_rgb565_buffer(const void * psrc, i nt w, int h); 38. void * rgb888_to_rgbx8888_buffer(const void * psrc, i
************/ 8. 9. //----------------------------------------------------------
--------10. #include <stdlib.h>
11. #include <stdio.h> 12. #include <memory.h> 13. #include "mybmp.h" 14. 15. //-----------------------------------------------------------------16. //拷贝 17. void rgb_copy(const void * psrc, void * pdst, int sw, i nt sh, int dw, int dh, int bpp) 18. { 19. int bytes = UpAlign8(bpp) >> 3; // bpp / 8 20. int srclinesize = UpAlign4(sw * bytes); 21. int dstlinesize = UpAlign4(dw * bytes); 22. int copylinesize = srclinesize < dstlinesize ? srclinesiz e : dstlinesize; 23. int copylines = sh < dh ? sh : dh; 24. 25. const unsigned char * psrcline = (const unsigned cha r *)psrc; 26. const unsigned char * pend = psrcline + copylines * s rclinesize; 27. unsigned char * pdstline = (unsigned char *)pdst; 28. 29. while (psrcline < pend) { 30. memcpy(pdstline, psrcline, copylinesize); 31. psrcline += srclinesize; 32. pdstline += dstlinesize; 33. } 34. } 35.
************ 2. created: 2012/04/07 3. filename: mybmp.h 4. author: 5. 6. purpose: 7. *********************************************************
************/ 8. 9. #ifndef _mybmp_h__ 10. #define _mybmp_h__ 11. //----------------------------------------------------------