yail.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "iPodDevice.h"
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include <api/service/waservicefactory.h>
  5. #include <api/service/svcs/svc_imgload.h>
  6. #include "api.h"
  7. #include <tataki/export.h>
  8. #include "yail.h"
  9. extern PMPDevicePlugin plugin;
  10. static void recTransform (RGB565 *destination, RGB565 *source, int width, int height, int row_stride);
  11. static __forceinline ARGB32 pixto32bit(RGB565 pix0, int format) {
  12. unsigned long pix = pix0;
  13. if(format == RGB_565)
  14. return (ARGB32)(((pix & 0x001F) << 3) | ((pix & 0x07E0) << 5) | ((pix & 0xF800) << 8) | 0xff000000);
  15. else // format == RGB_555. Ignore alpha channel.
  16. return (ARGB32)(((pix & 0x001F) << 3) | ((pix & 0x03E0) << 6) | ((pix & 0x7C00) << 9) | 0xff000000);
  17. }
  18. static __forceinline RGB565 pixto16bit(ARGB32 pix, int format) {
  19. // 10987654321098765432109876543210
  20. // ARGB32 is AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB
  21. // RGB565 is RRRRRGGGGGGBBBBB
  22. // RGB555 is ARRRRRGGGGGBBBBB
  23. if(format == RGB_565)
  24. return (RGB565)( ((pix >> 8) & 0xF800) | ((pix >> 5) & 0x07E0) | ((pix >> 3) & 0x001F) );
  25. else // format == RGB_555. set A to 1 for now.
  26. return (RGB565)( 0x8000 | ((pix >> 9) & 0x7C00) | ((pix >> 6) & 0x03E0) | ((pix >> 3) & 0x001F) );
  27. }
  28. Image::Image(const ARGB32 * d, int w, int h) : width(w), height(h) {
  29. int size = sizeof(ARGB32)*w*h;
  30. data = (ARGB32*)calloc(size,1);
  31. memcpy(data,d,size);
  32. }
  33. #define ALIGN(size, boundary) ((((boundary) - ((size) % (boundary))) % (boundary)) + (size))
  34. Image::Image(const RGB565 * d, int w, int h, int format, int alignRowBytes, int alignImageBytes) : width(w), height(h) {
  35. data = (ARGB32*)calloc(w*h*sizeof(ARGB32),1);
  36. int rowgap = (ALIGN(sizeof(RGB565) * width, alignRowBytes) / sizeof(RGB565)) - width;
  37. int p=0, q=0;
  38. for(int j=0; j<height; j++)
  39. {
  40. for(int i=0; i<width; i++)
  41. {
  42. data[p++] = pixto32bit(d[q++], format);
  43. }
  44. q += rowgap;
  45. }
  46. }
  47. Image::~Image() {
  48. if(data) free(data); data=0;
  49. }
  50. void Image::exportToRGB565(RGB565* d_, int format, int alignRowBytes, int alignImageBytes) const {
  51. int p=0, q=0;
  52. int rowgap = (ALIGN(sizeof(RGB565) * width, alignRowBytes) / sizeof(RGB565)) - width;
  53. RGB565* d;
  54. if(format == RGB_555_REC)
  55. {
  56. int l = get16BitSize(width, height, alignRowBytes, alignImageBytes);
  57. d = (RGB565*)calloc(l, 1);
  58. }
  59. else
  60. d = d_;
  61. for(int j=0; j<height; j++)
  62. {
  63. for(int i=0; i<width; i++)
  64. {
  65. d[p++] = pixto16bit(data[q++], format);
  66. }
  67. p += rowgap;
  68. }
  69. if(format == RGB_555_REC)
  70. { // do wierd transform
  71. recTransform(d_, d, width, height, width + rowgap);
  72. free(d);
  73. }
  74. }
  75. void Image::exportToARGB32(ARGB32* d) const {
  76. memcpy(d,data,sizeof(ARGB32)*width*height);
  77. }
  78. int Image::get16BitSize(int width, int height, int alignRowBytes, int alignImageBytes) {
  79. int rowSize = ALIGN(sizeof(RGB565) * width, alignRowBytes);
  80. return ALIGN(rowSize * height, alignImageBytes);
  81. }
  82. static void recTransform (RGB565 *destination, RGB565 *source, int width, int height, int row_stride)
  83. {
  84. if (width == 1)
  85. {
  86. *destination = *source;
  87. }
  88. else
  89. {
  90. recTransform(destination, source, width/2, height/2, row_stride);
  91. recTransform(destination + (width/2)*(height/2), source + (height/2)*row_stride, width/2, height/2, row_stride);
  92. recTransform(destination + 2*(width/2)*(height/2), source + width/2, width/2, height/2, row_stride);
  93. recTransform(destination + 3*(width/2)*(height/2), source + (height/2)*row_stride + width/2, width/2, height/2, row_stride);
  94. }
  95. }