找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 5011|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

ARM NEON加速引擎 使用C語言實(shí)現(xiàn)RGB圖像轉(zhuǎn)灰度圖

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:170466 發(fā)表于 2017-3-13 22:45 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
//使用C語言實(shí)現(xiàn)RGB圖像轉(zhuǎn)灰度圖
//Gray = (R * 77 + G * 151 + B * 28 ) / 256
void reference_convert (uint8_t * __restrict dest, uint8_t * __restrict src, int n)
{
  int i;
  for (i=0; i<n; i++)
  {
    int r = *src++; // load red
    int g = *src++; // load green
    int b = *src++; // load blue

    // build weighted average:
    int y = (r*77)+(g*151)+(b*28);

    // undo the scale by 256 and write to memory:
    *dest++ = (y>>8);
  }
}

//使用NEON Intrinsics優(yōu)化
void neon_convert (uint8_t * __restrict dest, uint8_t * __restrict src, int n)
{
  int i;
  //讀取8字節(jié)的預(yù)設(shè)值到64位寄存器
  uint8x8_t rfac = vdup_n_u8 (77);// 轉(zhuǎn)換權(quán)值 R
  uint8x8_t gfac = vdup_n_u8 (151);// 轉(zhuǎn)換權(quán)值 G
  uint8x8_t bfac = vdup_n_u8 (28);// 轉(zhuǎn)換權(quán)值 B
  n/=8;

  for (i=0; i<n; i++)
  {
    uint16x8_t  temp;
    uint8x8x3_t rgb  = vld3_u8 (src);//一次讀取3個(gè)unit8x8到3個(gè)64位寄存器
    uint8x8_t result;

    temp = vmull_u8 (rgb.val[0],      rfac); // temp=rgb.val[0]*rfac
    temp = vmlal_u8 (temp,rgb.val[1], gfac);// temp=temp+rgb.val[1]*gfac
    temp = vmlal_u8 (temp,rgb.val[2], bfac);//temp=temp+rgb.val[2]*bfac

    result = vshrn_n_u16 (temp, 8); // 128位寄存器每16位右移第二個(gè)參數(shù)位
    vst1_u8 (dest, result); // 轉(zhuǎn)存運(yùn)算結(jié)果到dest
    src  += 8*3;
    dest += 8;
  }
}

//NEON匯編代碼優(yōu)化:
static void neon_asm_convert(uint8_t * __restrict dest, uint8_t * __restrict src, int numPixels)
{
  asm volatile("lsr          %2, %2, #3      \n"
               "# build the three constants: \n"
               "mov         r4, #28          \n" // Blue channel multiplier
               "mov         r5, #151         \n" // Green channel multiplier
               "mov         r6, #77          \n" // Red channel multiplier
               "vdup.8      d4, r4           \n"
               "vdup.8      d5, r5           \n"
               "vdup.8      d6, r6           \n"
               ".loop:                       \n"
               "# load 8 pixels:             \n"
               "vld4.8      {d0-d3}, [%1]!   \n"
               "# do the weight average:     \n"
               "vmull.u8    q7, d0, d4       \n"
               "vmlal.u8    q7, d1, d5       \n"
               "vmlal.u8    q7, d2, d6       \n"
               "# shift and store:           \n"
               "vshrn.u16   d7, q7, #8       \n" // Divide q3 by 256 and store in the d7
               "vst1.8      {d7}, [%0]!      \n"
               "subs        %2, %2, #1       \n" // Decrement iteration count
               "bne         .loop            \n" // Repeat unil iteration count is not zero
               :
               : "r"(dest), "r"(src), "r"(numPixels)
               : "r4", "r5", "r6"
               );
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:216667 發(fā)表于 2020-3-8 22:54 | 只看該作者
牛逼!牛逼!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表