jcgryext-neon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Grayscale colorspace conversion (Arm Neon)
  3. *
  4. * Copyright (C) 2020, Arm Limited. All Rights Reserved.
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any damages
  8. * arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute it
  12. * freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. /* This file is included by jcgray-neon.c */
  23. /* RGB -> Grayscale conversion is defined by the following equation:
  24. * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  25. *
  26. * Avoid floating point arithmetic by using shifted integer constants:
  27. * 0.29899597 = 19595 * 2^-16
  28. * 0.58700561 = 38470 * 2^-16
  29. * 0.11399841 = 7471 * 2^-16
  30. * These constants are defined in jcgray-neon.c
  31. *
  32. * This is the same computation as the RGB -> Y portion of RGB -> YCbCr.
  33. */
  34. void jsimd_rgb_gray_convert_neon(JDIMENSION image_width, JSAMPARRAY input_buf,
  35. JSAMPIMAGE output_buf, JDIMENSION output_row,
  36. int num_rows)
  37. {
  38. JSAMPROW inptr;
  39. JSAMPROW outptr;
  40. /* Allocate temporary buffer for final (image_width % 16) pixels in row. */
  41. ALIGN(16) uint8_t tmp_buf[16 * RGB_PIXELSIZE];
  42. while (--num_rows >= 0) {
  43. inptr = *input_buf++;
  44. outptr = output_buf[0][output_row];
  45. output_row++;
  46. int cols_remaining = image_width;
  47. for (; cols_remaining > 0; cols_remaining -= 16) {
  48. /* To prevent buffer overread by the vector load instructions, the last
  49. * (image_width % 16) columns of data are first memcopied to a temporary
  50. * buffer large enough to accommodate the vector load.
  51. */
  52. if (cols_remaining < 16) {
  53. memcpy(tmp_buf, inptr, cols_remaining * RGB_PIXELSIZE);
  54. inptr = tmp_buf;
  55. }
  56. #if RGB_PIXELSIZE == 4
  57. uint8x16x4_t input_pixels = vld4q_u8(inptr);
  58. #else
  59. uint8x16x3_t input_pixels = vld3q_u8(inptr);
  60. #endif
  61. uint16x8_t r_l = vmovl_u8(vget_low_u8(input_pixels.val[RGB_RED]));
  62. uint16x8_t r_h = vmovl_u8(vget_high_u8(input_pixels.val[RGB_RED]));
  63. uint16x8_t g_l = vmovl_u8(vget_low_u8(input_pixels.val[RGB_GREEN]));
  64. uint16x8_t g_h = vmovl_u8(vget_high_u8(input_pixels.val[RGB_GREEN]));
  65. uint16x8_t b_l = vmovl_u8(vget_low_u8(input_pixels.val[RGB_BLUE]));
  66. uint16x8_t b_h = vmovl_u8(vget_high_u8(input_pixels.val[RGB_BLUE]));
  67. /* Compute Y = 0.29900 * R + 0.58700 * G + 0.11400 * B */
  68. uint32x4_t y_ll = vmull_n_u16(vget_low_u16(r_l), F_0_298);
  69. uint32x4_t y_lh = vmull_n_u16(vget_high_u16(r_l), F_0_298);
  70. uint32x4_t y_hl = vmull_n_u16(vget_low_u16(r_h), F_0_298);
  71. uint32x4_t y_hh = vmull_n_u16(vget_high_u16(r_h), F_0_298);
  72. y_ll = vmlal_n_u16(y_ll, vget_low_u16(g_l), F_0_587);
  73. y_lh = vmlal_n_u16(y_lh, vget_high_u16(g_l), F_0_587);
  74. y_hl = vmlal_n_u16(y_hl, vget_low_u16(g_h), F_0_587);
  75. y_hh = vmlal_n_u16(y_hh, vget_high_u16(g_h), F_0_587);
  76. y_ll = vmlal_n_u16(y_ll, vget_low_u16(b_l), F_0_113);
  77. y_lh = vmlal_n_u16(y_lh, vget_high_u16(b_l), F_0_113);
  78. y_hl = vmlal_n_u16(y_hl, vget_low_u16(b_h), F_0_113);
  79. y_hh = vmlal_n_u16(y_hh, vget_high_u16(b_h), F_0_113);
  80. /* Descale Y values (rounding right shift) and narrow to 16-bit. */
  81. uint16x8_t y_l = vcombine_u16(vrshrn_n_u32(y_ll, 16),
  82. vrshrn_n_u32(y_lh, 16));
  83. uint16x8_t y_h = vcombine_u16(vrshrn_n_u32(y_hl, 16),
  84. vrshrn_n_u32(y_hh, 16));
  85. /* Narrow Y values to 8-bit and store to memory. Buffer overwrite is
  86. * permitted up to the next multiple of ALIGN_SIZE bytes.
  87. */
  88. vst1q_u8(outptr, vcombine_u8(vmovn_u16(y_l), vmovn_u16(y_h)));
  89. /* Increment pointers. */
  90. inptr += (16 * RGB_PIXELSIZE);
  91. outptr += 16;
  92. }
  93. }
  94. }