quickpreview.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Fast but bad debayer method that scales and rotates by skipping source
  3. * pixels and doesn't interpolate any values at all
  4. */
  5. #include "quickpreview.h"
  6. #include <assert.h>
  7. #include <stdio.h>
  8. /* Linear -> sRGB lookup table */
  9. static const int srgb[] = {
  10. 0, 12, 21, 28, 33, 38, 42, 46, 49, 52, 55, 58, 61, 63, 66, 68, 70,
  11. 73, 75, 77, 79, 81, 82, 84, 86, 88, 89, 91, 93, 94, 96, 97, 99, 100,
  12. 102, 103, 104, 106, 107, 109, 110, 111, 112, 114, 115, 116, 117, 118,
  13. 120, 121, 122, 123, 124, 125, 126, 127, 129, 130, 131, 132, 133, 134,
  14. 135, 136, 137, 138, 139, 140, 141, 142, 142, 143, 144, 145, 146, 147,
  15. 148, 149, 150, 151, 151, 152, 153, 154, 155, 156, 157, 157, 158, 159,
  16. 160, 161, 161, 162, 163, 164, 165, 165, 166, 167, 168, 168, 169, 170,
  17. 171, 171, 172, 173, 174, 174, 175, 176, 176, 177, 178, 179, 179, 180,
  18. 181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189,
  19. 190, 191, 191, 192, 193, 193, 194, 194, 195, 196, 196, 197, 197, 198,
  20. 199, 199, 200, 201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206,
  21. 207, 208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214, 214,
  22. 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 222,
  23. 222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, 228, 229, 229,
  24. 230, 230, 231, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236,
  25. 237, 237, 237, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, 243,
  26. 243, 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249,
  27. 250, 250, 251, 251, 251, 252, 252, 253, 253, 254, 254, 255
  28. };
  29. static inline uint32_t
  30. pack_rgb(uint8_t r, uint8_t g, uint8_t b)
  31. {
  32. return (r << 16) | (g << 8) | b;
  33. }
  34. static inline uint32_t
  35. convert_yuv_to_srgb(uint8_t y, uint8_t u, uint8_t v)
  36. {
  37. uint32_t r = 1.164f * y + 1.596f * (v - 128);
  38. uint32_t g = 1.164f * y - 0.813f * (v - 128) - 0.391f * (u - 128);
  39. uint32_t b = 1.164f * y + 2.018f * (u - 128);
  40. return pack_rgb(r, g, b);
  41. }
  42. static inline uint32_t
  43. apply_colormatrix(uint32_t color, const float *colormatrix)
  44. {
  45. if (!colormatrix) {
  46. return color;
  47. }
  48. uint32_t r = (color >> 16) * colormatrix[0] +
  49. ((color >> 8) & 0xFF) * colormatrix[1] +
  50. (color & 0xFF) * colormatrix[2];
  51. uint32_t g = (color >> 16) * colormatrix[3] +
  52. ((color >> 8) & 0xFF) * colormatrix[4] +
  53. (color & 0xFF) * colormatrix[5];
  54. uint32_t b = (color >> 16) * colormatrix[6] +
  55. ((color >> 8) & 0xFF) * colormatrix[7] +
  56. (color & 0xFF) * colormatrix[8];
  57. // Clip colors
  58. if (r > 0xFF)
  59. r = 0xFF;
  60. if (g > 0xFF)
  61. g = 0xFF;
  62. if (b > 0xFF)
  63. b = 0xFF;
  64. return pack_rgb(r, g, b);
  65. }
  66. static inline uint32_t
  67. coord_map(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int rotation,
  68. bool mirrored)
  69. {
  70. uint32_t x_r, y_r;
  71. if (rotation == 0) {
  72. x_r = x;
  73. y_r = y;
  74. } else if (rotation == 90) {
  75. x_r = y;
  76. y_r = height - x - 1;
  77. } else if (rotation == 270) {
  78. x_r = width - y - 1;
  79. y_r = x;
  80. } else {
  81. x_r = width - x - 1;
  82. y_r = height - y - 1;
  83. }
  84. if (mirrored) {
  85. x_r = width - x_r - 1;
  86. }
  87. uint32_t index = y_r * width + x_r;
  88. #ifdef DEBUG
  89. assert(index < width * height);
  90. #endif
  91. return index;
  92. }
  93. static void
  94. quick_preview_rggb8(uint32_t *dst, const uint32_t dst_width,
  95. const uint32_t dst_height, const uint8_t *src,
  96. const uint32_t src_width, const uint32_t src_height,
  97. const MPPixelFormat format, const uint32_t rotation,
  98. const bool mirrored, const float *colormatrix,
  99. const uint8_t blacklevel, const uint32_t skip)
  100. {
  101. uint32_t src_y = 0, dst_y = 0;
  102. while (src_y < src_height) {
  103. uint32_t src_x = 0, dst_x = 0;
  104. while (src_x < src_width) {
  105. uint32_t src_i = src_y * src_width + src_x;
  106. uint8_t b0 = srgb[src[src_i] - blacklevel];
  107. uint8_t b1 = srgb[src[src_i + 1] - blacklevel];
  108. uint8_t b2 = srgb[src[src_i + src_width + 1] - blacklevel];
  109. uint32_t color;
  110. switch (format) {
  111. case MP_PIXEL_FMT_BGGR8:
  112. color = pack_rgb(b2, b1, b0);
  113. break;
  114. case MP_PIXEL_FMT_GBRG8:
  115. color = pack_rgb(b2, b0, b1);
  116. break;
  117. case MP_PIXEL_FMT_GRBG8:
  118. color = pack_rgb(b1, b0, b2);
  119. break;
  120. case MP_PIXEL_FMT_RGGB8:
  121. color = pack_rgb(b0, b1, b2);
  122. break;
  123. default:
  124. assert(false);
  125. }
  126. color = apply_colormatrix(color, colormatrix);
  127. dst[coord_map(dst_x, dst_y, dst_width, dst_height, rotation,
  128. mirrored)] = color;
  129. src_x += 2 + 2 * skip;
  130. ++dst_x;
  131. }
  132. src_y += 2 + 2 * skip;
  133. ++dst_y;
  134. }
  135. }
  136. static void
  137. quick_preview_rggb10(uint32_t *dst, const uint32_t dst_width,
  138. const uint32_t dst_height, const uint8_t *src,
  139. const uint32_t src_width, const uint32_t src_height,
  140. const MPPixelFormat format, const uint32_t rotation,
  141. const bool mirrored, const float *colormatrix,
  142. const uint8_t blacklevel, const uint32_t skip)
  143. {
  144. assert(src_width % 2 == 0);
  145. uint32_t width_bytes = mp_pixel_format_width_to_bytes(format, src_width);
  146. uint32_t src_y = 0, dst_y = 0;
  147. while (src_y < src_height) {
  148. uint32_t src_x = 0, dst_x = 0;
  149. while (src_x < width_bytes) {
  150. uint32_t src_i = src_y * width_bytes + src_x;
  151. uint8_t b0 = srgb[src[src_i] - blacklevel];
  152. uint8_t b1 = srgb[src[src_i + 1] - blacklevel];
  153. uint8_t b2 = srgb[src[src_i + width_bytes + 1] - blacklevel];
  154. uint32_t color;
  155. switch (format) {
  156. case MP_PIXEL_FMT_BGGR10P:
  157. color = pack_rgb(b2, b1, b0);
  158. break;
  159. case MP_PIXEL_FMT_GBRG10P:
  160. color = pack_rgb(b2, b0, b1);
  161. break;
  162. case MP_PIXEL_FMT_GRBG10P:
  163. color = pack_rgb(b1, b0, b2);
  164. break;
  165. case MP_PIXEL_FMT_RGGB10P:
  166. color = pack_rgb(b0, b1, b2);
  167. break;
  168. default:
  169. assert(false);
  170. }
  171. color = apply_colormatrix(color, colormatrix);
  172. dst[coord_map(dst_x, dst_y, dst_width, dst_height, rotation,
  173. mirrored)] = color;
  174. uint32_t advance = 1 + skip;
  175. if (src_x % 5 == 0) {
  176. src_x += 2 * (advance % 2) + 5 * (advance / 2);
  177. } else {
  178. src_x += 3 * (advance % 2) + 5 * (advance / 2);
  179. }
  180. ++dst_x;
  181. }
  182. src_y += 2 + 2 * skip;
  183. ++dst_y;
  184. }
  185. }
  186. static void
  187. quick_preview_yuv(uint32_t *dst, const uint32_t dst_width, const uint32_t dst_height,
  188. const uint8_t *src, const uint32_t src_width,
  189. const uint32_t src_height, const MPPixelFormat format,
  190. const uint32_t rotation, const bool mirrored,
  191. const float *colormatrix, const uint32_t skip)
  192. {
  193. assert(src_width % 2 == 0);
  194. uint32_t width_bytes = src_width * 2;
  195. uint32_t unrot_dst_width = dst_width;
  196. if (rotation != 0 && rotation != 180) {
  197. unrot_dst_width = dst_height;
  198. }
  199. uint32_t src_y = 0, dst_y = 0;
  200. while (src_y < src_height) {
  201. uint32_t src_x = 0, dst_x = 0;
  202. while (src_x < width_bytes) {
  203. uint32_t src_i = src_y * width_bytes + src_x;
  204. uint8_t b0 = src[src_i];
  205. uint8_t b1 = src[src_i + 1];
  206. uint8_t b2 = src[src_i + 2];
  207. uint8_t b3 = src[src_i + 3];
  208. uint32_t color1, color2;
  209. switch (format) {
  210. case MP_PIXEL_FMT_UYVY:
  211. color1 = convert_yuv_to_srgb(b1, b0, b2);
  212. color2 = convert_yuv_to_srgb(b3, b0, b2);
  213. break;
  214. case MP_PIXEL_FMT_YUYV:
  215. color1 = convert_yuv_to_srgb(b0, b1, b3);
  216. color2 = convert_yuv_to_srgb(b2, b1, b3);
  217. break;
  218. default:
  219. assert(false);
  220. }
  221. color1 = apply_colormatrix(color1, colormatrix);
  222. color2 = apply_colormatrix(color2, colormatrix);
  223. uint32_t dst_i1 = coord_map(dst_x, dst_y, dst_width,
  224. dst_height, rotation, mirrored);
  225. dst[dst_i1] = color1;
  226. ++dst_x;
  227. // The last pixel needs to be skipped if we have an odd un-rotated width
  228. if (dst_x < unrot_dst_width) {
  229. uint32_t dst_i2 =
  230. coord_map(dst_x, dst_y, dst_width,
  231. dst_height, rotation, mirrored);
  232. dst[dst_i2] = color2;
  233. ++dst_x;
  234. }
  235. src_x += 4 + 4 * skip;
  236. }
  237. src_y += 1 + skip;
  238. ++dst_y;
  239. }
  240. }
  241. void
  242. quick_preview(uint32_t *dst, const uint32_t dst_width, const uint32_t dst_height,
  243. const uint8_t *src, const uint32_t src_width,
  244. const uint32_t src_height, const MPPixelFormat format,
  245. const uint32_t rotation, const bool mirrored, const float *colormatrix,
  246. const uint8_t blacklevel, const uint32_t skip)
  247. {
  248. switch (format) {
  249. case MP_PIXEL_FMT_BGGR8:
  250. case MP_PIXEL_FMT_GBRG8:
  251. case MP_PIXEL_FMT_GRBG8:
  252. case MP_PIXEL_FMT_RGGB8:
  253. quick_preview_rggb8(dst, dst_width, dst_height, src, src_width,
  254. src_height, format, rotation, mirrored,
  255. colormatrix, blacklevel, skip);
  256. break;
  257. case MP_PIXEL_FMT_BGGR10P:
  258. case MP_PIXEL_FMT_GBRG10P:
  259. case MP_PIXEL_FMT_GRBG10P:
  260. case MP_PIXEL_FMT_RGGB10P:
  261. quick_preview_rggb10(dst, dst_width, dst_height, src, src_width,
  262. src_height, format, rotation, mirrored,
  263. colormatrix, blacklevel, skip);
  264. break;
  265. case MP_PIXEL_FMT_UYVY:
  266. case MP_PIXEL_FMT_YUYV:
  267. quick_preview_yuv(dst, dst_width, dst_height, src, src_width,
  268. src_height, format, rotation, mirrored,
  269. colormatrix, skip);
  270. break;
  271. default:
  272. assert(false);
  273. }
  274. }
  275. static uint32_t
  276. div_ceil(uint32_t x, uint32_t y)
  277. {
  278. return x / y + !!(x % y);
  279. }
  280. void
  281. quick_preview_size(uint32_t *dst_width, uint32_t *dst_height, uint32_t *skip,
  282. const uint32_t preview_width, const uint32_t preview_height,
  283. const uint32_t src_width, const uint32_t src_height,
  284. const MPPixelFormat format, const int rotation)
  285. {
  286. uint32_t colors_x = mp_pixel_format_width_to_colors(format, src_width);
  287. uint32_t colors_y = mp_pixel_format_height_to_colors(format, src_height);
  288. if (rotation != 0 && rotation != 180) {
  289. uint32_t tmp = colors_x;
  290. colors_x = colors_y;
  291. colors_y = tmp;
  292. }
  293. uint32_t scale_x = colors_x / preview_width;
  294. uint32_t scale_y = colors_y / preview_height;
  295. if (scale_x > 0)
  296. --scale_x;
  297. if (scale_y > 0)
  298. --scale_y;
  299. *skip = scale_x > scale_y ? scale_x : scale_y;
  300. *dst_width = div_ceil(colors_x, (1 + *skip));
  301. if (*dst_width <= 0)
  302. *dst_width = 1;
  303. *dst_height = div_ceil(colors_y, (1 + *skip));
  304. if (*dst_height <= 0)
  305. *dst_height = 1;
  306. }