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

📄 ijg_timing.c

📁 jpeg 压缩/解压缩库,是Intel 在ijg基础上改进的libjpeg,encode/decode是ijg的速度的2倍以上。
💻 C
📖 第 1 页 / 共 3 页
字号:
  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 unsigned int read_word(FILE* fi){  unsigned int c;  unsigned 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 unsigned int read_dword(FILE* fi){  unsigned int word;  unsigned 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 = (Ipp16u)read_word(fi);  if((Ipp16u)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((Ipp32u)EOF == bfSize)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  bfReserved1 = (Ipp16u)read_word(fi);  if((Ipp16u)EOF == bfReserved1)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  bfReserved2 = (Ipp16u)read_word(fi);  if((Ipp16u)EOF == bfReserved2)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  bfOffBits = read_dword(fi);  if((Ipp32u)EOF == bfOffBits)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biSize = read_dword(fi);  if((Ipp32u)EOF == biSize)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biWidth = read_dword(fi);  if((Ipp32u)EOF == biWidth)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biHeight = read_dword(fi);  if((Ipp32u)EOF == biHeight)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biPlanes = (Ipp16u)read_word(fi);  if((Ipp16u)EOF == biPlanes)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biBitCount = (Ipp16u)read_word(fi);  if((Ipp16u)EOF == biBitCount)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biCompression = read_dword(fi);  if((Ipp32u)EOF == biCompression)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biSizeImage = read_dword(fi);  if((Ipp32u)EOF == biSizeImage)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biXPelsPerMeter = read_dword(fi);  if((Ipp32u)EOF == biXPelsPerMeter)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biYPelsPerMeter = read_dword(fi);  if((Ipp32u)EOF == biYPelsPerMeter)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biClrUsed = read_dword(fi);  if((Ipp32u)EOF == biClrUsed)  {    printf("can't read from file\n");    res = -1;    goto Exit;  }  biClrImportant = read_dword(fi);  if((Ipp32u)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 unsigned int write_word(Ipp16u word,FILE* fo){  Ipp32u c;  c = (Ipp32u)(word & 0xff);  if(EOF == fputc(c,fo))    return EOF;  c = (Ipp32u)(word >> 8);  if(EOF == fputc(c,fo))    return EOF;  return 0;} /* write_word() */static unsigned int write_dword(Ipp32u dword,FILE* fo){  Ipp16u word;  word = (Ipp16u)(dword & 0xffff);  if(EOF == write_word(word,fo))    return EOF;  word = (Ipp16u)(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;    goto Exit;  }

⌨️ 快捷键说明

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