⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ijg_timing.c

📁 这是在PCA下的基于IPP库示例代码例子,在网上下了IPP的库之后,设置相关参数就可以编译该代码.
💻 C
📖 第 1 页 / 共 3 页
字号:

  jpeg_calc_output_dimensions(&cinfo);

  bmp_width = cinfo.output_components * cinfo.output_width;

  while(bmp_width & 3)
  {
    bmp_width++;
  }

  img_size = bmp_width * cinfo.output_height;

  img_buff = (Ipp8u*)malloc(img_size);
  if(NULL == img_buff)
  {
    printf("can't allocate %d bytes\n",img_size);
    res = -1;
    goto Exit;
  }

  tmp_buff = (Ipp8u*)malloc(bmp_width);
  if(NULL == tmp_buff)
  {
    printf("can't allocate %d bytes\n",bmp_width);
    res = -1;
    goto Exit;
  }
#ifdef _WIN32
  SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
  Sleep(0);
#endif
  clk0 = get_pentium_counter();

  jpeg_start_decompress(&cinfo);

  cur_output_row = 0;

  while(cinfo.output_scanline < cinfo.output_height)
  {
    if(jpeg_read_scanlines(&cinfo,&tmp_buff,1))
    {
      cur_output_row++;
      outptr = img_buff + bmp_width * (cinfo.output_height - cur_output_row);
      inptr  = tmp_buff;
      for(col = cinfo.output_width; col--;)
      {
        switch(cinfo.output_components)
        {
        case 3:
          outptr[2] = inptr[0];
          outptr[1] = inptr[1];
          outptr[0] = inptr[2];
          inptr  += 3;
          outptr += 3;
          break;

        case 4:
          outptr[0] = inptr[0];
          outptr[1] = inptr[1];
          outptr[2] = inptr[2];
          outptr[3] = inptr[3];
          inptr  += 4;
          outptr += 4;
          break;

        case 1:
          *outptr++ = *inptr++;
          break;

        default:
          break;
        }
      }

      while((int)outptr & 3)
      {
        *outptr++ = 0;
      }
    }
  }

  get_sampling(&cinfo,&ss);

  jpeg_finish_decompress(&cinfo);

  clk1 = get_pentium_counter();
#ifdef _WIN32
  SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
#endif
  *buffer    = img_buff;
  *width     = cinfo.output_width;
  *height    = cinfo.output_height;
  *nchannels = cinfo.output_components;
  *sampling  = ss;

  *cpu_clocks = (unsigned int)(clk1 - clk0);

  res = 0;

Exit:

  jpeg_destroy_decompress(&cinfo);

  /* free memory and close files */
  if(NULL != fi)
  {
    fclose(fi);
  }

  if(NULL != tmp_buff)
  {
    free(tmp_buff);
  }

  if(0 != res)
  {
    if(NULL != img_buff)
    {
      free(img_buff);
    }
  }

  return res;
} /* ijg_decode() */


static int DIB_PAD_BYTES(
  int width,
  int nchannels)
{
  int pad_bytes;
  int dib_align;
  int uwidth;
  int awidth;

  dib_align = sizeof(Ipp32u) - 1;

  uwidth = width * nchannels;
  awidth = (uwidth + dib_align) & (~dib_align);

  pad_bytes = awidth - uwidth;

  return pad_bytes;
} /* DIB_PAD_BYTES() */


static int read_word(FILE* fi)
{
  int c;
  int word;

  c = fgetc(fi);
  if(EOF == c)
    return c;

  word = c;

  c = fgetc(fi);
  if(EOF == c)
    return c;

  word |= c << 8;

  return word;
} /* read_word() */


static int read_dword(FILE* fi)
{
  int word;
  int dword;

  word = read_word(fi);
  if(EOF == word)
    return word;

  dword = word;

  word = read_word(fi);
  if(EOF == word)
    return word;

  dword |= word << 16;

  return dword;
} /* read_dword() */


static int load_bmp(
  char*   bmp_name,
  Ipp8u** bmp_buff,
  int*    width,
  int*    height,
  int*    nchannels)
{
  int              res;
  int              img_size;
  int              pad_bytes;
  int              width_step;
  size_t           cnt;
  FILE*            fi = NULL;
  Ipp8u*           buff;

  Ipp16u           bfType;
  Ipp32u           bfSize;
  Ipp16u           bfReserved1;
  Ipp16u           bfReserved2;
  Ipp32u           bfOffBits;

  Ipp32u           biSize;
  Ipp32u           biWidth;
  Ipp32u           biHeight;
  Ipp16u           biPlanes;
  Ipp16u           biBitCount;
  Ipp32u           biCompression;
  Ipp32u           biSizeImage;
  Ipp32u           biXPelsPerMeter;
  Ipp32u           biYPelsPerMeter;
  Ipp32u           biClrUsed;
  Ipp32u           biClrImportant;

  fi = fopen(bmp_name,"rb");
  if(NULL == fi)
  {
    printf("can't open file %s\n",bmp_name);
    res = -1;
    goto Exit;
  }

  bfType = read_word(fi);
  if(EOF == bfType)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  if(bfType != 0x4d42) /* BMP signature 'BM' */
  {
    printf("invalid BMP file\n");
    res = -1;
    goto Exit;
  }

  bfSize = read_dword(fi);
  if(EOF == bfSize)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  bfReserved1 = read_word(fi);
  if(EOF == bfReserved1)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  bfReserved2 = read_word(fi);
  if(EOF == bfReserved2)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  bfOffBits = read_dword(fi);
  if(EOF == bfOffBits)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biSize = read_dword(fi);
  if(EOF == biSize)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biWidth = read_dword(fi);
  if(EOF == biWidth)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biHeight = read_dword(fi);
  if(EOF == biHeight)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biPlanes = read_word(fi);
  if(EOF == biPlanes)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biBitCount = read_word(fi);
  if(EOF == biBitCount)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biCompression = read_dword(fi);
  if(EOF == biCompression)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biSizeImage = read_dword(fi);
  if(EOF == biSizeImage)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biXPelsPerMeter = read_dword(fi);
  if(EOF == biXPelsPerMeter)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biYPelsPerMeter = read_dword(fi);
  if(EOF == biYPelsPerMeter)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biClrUsed = read_dword(fi);
  if(EOF == biClrUsed)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  biClrImportant = read_dword(fi);
  if(EOF == biClrImportant)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  if(biSize != BITMAPINFOHEADER_SIZE || biCompression != BI_RGB)
  {
    printf("Unsupported BMP file\n");
    res = -1;
    goto Exit;
  }

  switch(biBitCount)
  {
  case 24:
    pad_bytes = DIB_PAD_BYTES(biWidth,3);

    width_step = (biWidth * 3) + pad_bytes;

    img_size = width_step * abs(biHeight);
    break;

  case 32:
    width_step = biWidth * 4;

    img_size = width_step * abs(biHeight);
    break;

  default:
    printf("Unsupported BMP file\n");
    res = -1;
    goto Exit;
  }

  buff = (Ipp8u*)malloc(img_size);
  if(NULL == buff)
  {
    printf("can't allocate %d bytes\n",img_size);
    res = -1;
    goto Exit;
  }

  cnt = fread(buff,sizeof(Ipp8u),img_size,fi);
  if(cnt != (size_t)img_size)
  {
    printf("can't read from file\n");
    res = -1;
    goto Exit;
  }

  *bmp_buff  = buff;
  *width     = biWidth;
  *height    = biHeight;
  *nchannels = biBitCount >> 3;

  res = 0;

Exit:

  if(NULL != fi)
  {
    fclose(fi);
  }

  return res;
} /* load_bmp() */


static int write_word(Ipp16u word,FILE* fo)
{
  int c;

  c = word & 0xff;

  if(EOF == fputc(c,fo))
    return EOF;

  c = word >> 8;

  if(EOF == fputc(c,fo))
    return EOF;

  return 0;
} /* write_word() */


static int write_dword(Ipp32u dword,FILE* fo)
{
  int word;

  word = dword & 0xffff;

  if(EOF == write_word(word,fo))
    return EOF;

  word = dword >> 16;

  if(EOF == write_word(word,fo))
    return EOF;

  return 0;
} /* write_dword() */


static int write_bmp(
  Ipp8u* buffer,
  int    width,
  int    height,
  int    nchannels,
  char*  name)
{
  int     i;
  int     res;
  int     row;
  int     bmp_size;
  int     img_size;
  int     bmp_width;
  size_t  cnt;
  FILE*   fo = NULL;
  Ipp8u*  outptr = NULL;

  Ipp16u  bfType;
  Ipp32u  bfSize;
  Ipp16u  bfReserved1;
  Ipp16u  bfReserved2;
  Ipp32u  bfOffBits;

  Ipp32u  biSize;
  Ipp32u  biWidth;
  Ipp32u  biHeight;
  Ipp16u  biPlanes;
  Ipp16u  biBitCount;
  Ipp32u  biCompression;
  Ipp32u  biSizeImage;
  Ipp32u  biXPelsPerMeter;
  Ipp32u  biYPelsPerMeter;
  Ipp32u  biClrUsed;
  Ipp32u  biClrImportant;

  Ipp8u   palette[256*4];

  fo = fopen(name,"wb+");
  if(NULL == fo)
  {
    printf("can't open file - %s\n",name);
    res = -1;
    goto Exit;
  }

  bmp_width = nchannels * width;

  while(bmp_width & 3)
  {
    bmp_width++;
  }

  img_size = bmp_width * height;
  bmp_size = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE + img_size;

  if(nchannels == 1)
  {
    bmp_size += sizeof(palette);
  }

  bfType      = 0x4d42; /* BMP signature 'BM' */
  bfSize      = bmp_size;
  bfReserved1 = 0;
  bfReserved2 = 0;
  bfOffBits   = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;

  if(EOF == write_word(bfType,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_dword(bfSize,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_word(bfReserved1,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_word(bfReserved2,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(nchannels == 1)
  {
    bfOffBits += sizeof(palette);
  }

  if(EOF == write_dword(bfOffBits,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  biSize          = BITMAPINFOHEADER_SIZE;
  biWidth         = width;
  biHeight        = height;
  biPlanes        = 1;
  biBitCount      = (unsigned short)(nchannels << 3);
  biCompression   = BI_RGB;
  biSizeImage     = 0;
  biXPelsPerMeter = 0;
  biYPelsPerMeter = 0;
  biClrUsed       = (nchannels == 1) ? 256 : 0;
  biClrImportant  = (nchannels == 1) ? 256 : 0;

  if(EOF == write_dword(biSize,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_dword(biWidth,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_dword(biHeight,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_word(biPlanes,fo))
  {
    printf("can't write to file\n");
    res = -1;
    goto Exit;
  }

  if(EOF == write_word(biBitCount,fo))
  {
    printf("can't write to file\n");
    res = -1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -