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

📄 ijg_timing.c

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

/*
//
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//    Copyright (c) 2001-2005 Intel Corporation. All Rights Reserved.
//
//
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "jpeglib.h"
#include "ippdefs.h"
#include "ippcore.h"
#include "ipps.h"
#include "ippi.h"
#include "ippj.h"




#define BITMAPFILEHEADER_SIZE 14
#define BITMAPINFOHEADER_SIZE 40
#define _IPP 1 // set to 0 to disable IPP calls in IJG

typedef enum _JPEG_SAMPLING
{
  SS_OTHER = 0,
  SS_444   = 444,
  SS_422   = 422,
  SS_411   = 411

} JSS;


#if defined ( linux )

#define  BI_BITFIELDS     3
#define  BI_RLE4          2
#define  BI_RLE8          1
#define  BI_RGB           0

#define _MAX_PATH       256
#define _MAX_DRIVE       16
#define _MAX_DIR        256
#define _MAX_FNAME      256
#define _MAX_EXT         16


static void _splitpath(
  char* path,
  char* drive,
  char* dir,
  char* fname,
  char* ext)
{
  char* p;
  char* last_slash  = NULL;
  char* dot         = NULL;
  unsigned int len;

  /* extract drive letter, if any */
  if((strlen(path) >= (_MAX_DRIVE - 2)) && (*(path + _MAX_DRIVE - 2) == ':') )
  {
    if(drive)
    {
      strncpy(drive,path,_MAX_DRIVE - 1);
      *(drive + _MAX_DRIVE - 1) = '\0';
    }
    path += _MAX_DRIVE - 1;
  }
  else if(drive)
  {
    *drive = '\0';
  }

  /* extract path string, if any */
  for(last_slash = NULL, p = path; *p; p++)
  {
    if(*p == '/' || *p == '\\')
      last_slash = p + 1;
    else if(*p == '.')
      dot = p;
  }

  if(last_slash)
  {
    if(dir)
    {
      len = IPP_MIN((unsigned int)(((char*)last_slash - (char*)path) / sizeof(char)),(_MAX_DIR - 1));
      strncpy(dir,path,len);
      *(dir + len) = '\0';
    }
    path = last_slash;
  }
  else if(dir)
  {
    *dir = '\0';
  }

  /* extract file name, if any */
  if(dot && (dot >= path))
  {
    if(fname)
    {
      len = IPP_MIN((unsigned int)(((char*)dot - (char*)path) / sizeof(char)),(_MAX_FNAME - 1));
      strncpy(fname,path,len);
      *(fname + len) = '\0';
    }

    if(ext)
    {
      len = IPP_MIN((unsigned int)(((char*)p - (char*)dot) / sizeof(char)),(_MAX_EXT - 1));
      strncpy(ext,dot,len);
      *(ext + len) = '\0';
    }
  }
  else
  {
    if(fname)
    {
      len = IPP_MIN((unsigned int)(((char*)p - (char*)path) / sizeof(char)),(_MAX_FNAME - 1));
      strncpy(fname,path,len);
      *(fname + len) = '\0';
    }
    if(ext)
    {
      *ext = '\0';
    }
  }

  return;
} /* _splitpath() */


static void _makepath(
  char* path,
  char* drive,
  char* dir,
  char* fname,
  char* ext)
{
  char* p;

  /* make drive */
  if(drive && *drive)
  {
    *path++ = *drive;
    *path++ = ':';
  }

  /* make dir */
  if((p = dir) && *p)
  {
    do
    {
      *path++ = *p++;
    } while(*p);

    if(*(p-1) != '/' && *(p-1) != '\\')
    {
      *path++ = '\\';
    }
  }

  /* make fname */
  if(p = fname)
  {
    while(*p)
      *path++ = *p++;
  }

  /* make ext */
  if(p = ext)
  {
    if(*p && *p != '.')
    {
      *path++ = '.';
    }
    while(*path++ = *p)
      ;
  }
  else
  {
    *path = '\0';
  }

  return;
} /* _makepath() */

#endif /* linux */


#define get_pentium_counter ippGetCpuClocks

static void copyright(void)
{
  printf("Test program for IJG library boosted with\n");
  printf("Intel(R) Integrated Performance Primitives\n");
  printf("Copyright(c) 2001-2005 Intel Corporation. All Rights Reserved.\n");
  return;
} /* copyright() */


static void usage(void)
{
  printf("use ./ijg_timing [options]\n");
  printf("where options are:\n");
  printf("  -?|-h show usage information\n");
  printf("  -v show ippJP library version\n");
  printf("  -a auto testing with hardcoded parameters.\n");
  printf("     They are encoding/decoding 640x480 1, 3 and 4 channels images\n");
  printf("     with random pixel values and 444, 422 and 411 sampling and 75%% quality\n");
  printf("  -t<bmp name> encode bmp to jpeg, after that decode jpeg to bmp\n");
  printf("     and compare with original bmp\n");
  printf("  -d<file name> decode JPEG <file name> file\n");
  printf("  -e<file name> encode BMP <file name> file\n");
  printf("  -qQUALITY jpeg quality 1..100\n");
  printf("  -sSAMPLING jpeg sampling 444, 422, 411\n");
  return;
} /* usage() */


static void ipp_version(void)
{
  const IppLibraryVersion* version;

  printf("Intel(R) Integrated Performance Primitives\n");

  /* ippCore version info */
  version = ippGetLibVersion();

  printf("  %s, %s {%d.%d.%d.%d}, build date %s\n",
    version->Name,
    version->Version,
    version->major,version->minor,version->majorBuild,version->build,
    version->BuildDate);

  /* ippSP version info */
  version = ippsGetLibVersion();

  printf("  %s, %s {%d.%d.%d.%d}, build date %s\n",
    version->Name,
    version->Version,
    version->major,version->minor,version->majorBuild,version->build,
    version->BuildDate);

  /* ippIP version info */
  version = ippiGetLibVersion();

  printf("  %s, %s {%d.%d.%d.%d}, build date %s\n",
    version->Name,
    version->Version,
    version->major,version->minor,version->majorBuild,version->build,
    version->BuildDate);

  /* ippJP version info */
  version = ippjGetLibVersion();

  printf("  %s, %s {%d.%d.%d.%d}, build date %s\n",
    version->Name,
    version->Version,
    version->major,version->minor,version->majorBuild,version->build,
    version->BuildDate);

  return;
} /* ipp_version() */


static void set_sampling(
  struct jpeg_compress_struct* cinfo,
  JSS sampling,
  int nchannels)
{
  switch(sampling)
  {
    case SS_444:
      cinfo->comp_info[0].h_samp_factor = 1;
      cinfo->comp_info[0].v_samp_factor = 1;
      if(nchannels == 3)
      {
        cinfo->comp_info[1].h_samp_factor = 1;
        cinfo->comp_info[1].v_samp_factor = 1;
        cinfo->comp_info[2].h_samp_factor = 1;
        cinfo->comp_info[2].v_samp_factor = 1;
      }
      if(nchannels == 4)
      {
        cinfo->comp_info[3].h_samp_factor = 1;
        cinfo->comp_info[3].v_samp_factor = 1;
      }
      break;

    case SS_422:
      cinfo->comp_info[0].h_samp_factor = 2;
      cinfo->comp_info[0].v_samp_factor = 1;
      cinfo->comp_info[1].h_samp_factor = 1;
      cinfo->comp_info[1].v_samp_factor = 1;
      cinfo->comp_info[2].h_samp_factor = 1;
      cinfo->comp_info[2].v_samp_factor = 1;
      if(nchannels == 4)
      {
        cinfo->comp_info[3].h_samp_factor = 2;
        cinfo->comp_info[3].v_samp_factor = 1;
      }
      break;

    case SS_411:
      cinfo->comp_info[0].h_samp_factor = 2;
      cinfo->comp_info[0].v_samp_factor = 2;
      cinfo->comp_info[1].h_samp_factor = 1;
      cinfo->comp_info[1].v_samp_factor = 1;
      cinfo->comp_info[2].h_samp_factor = 1;
      cinfo->comp_info[2].v_samp_factor = 1;
      if(nchannels == 4)
      {
        cinfo->comp_info[3].h_samp_factor = 2;
        cinfo->comp_info[3].v_samp_factor = 2;
      }
      break;

    default:
      printf("unknown sampling - %d\n",sampling);
  }

  return;
} /* set_sampling() */


static void get_sampling(
  struct jpeg_decompress_struct* cinfo,
  JSS* sampling)
{
  if(cinfo->num_components == 1)
  {
    *sampling = SS_444;
  }
  else
  if(cinfo->num_components == 3)
  {
    if(cinfo->comp_info[0].h_samp_factor == 1 && cinfo->comp_info[0].v_samp_factor == 1 &&
       cinfo->comp_info[1].h_samp_factor == 1 && cinfo->comp_info[1].v_samp_factor == 1 &&
       cinfo->comp_info[2].h_samp_factor == 1 && cinfo->comp_info[2].v_samp_factor == 1)
    {
      *sampling = SS_444;
    }
    else
    if(cinfo->comp_info[0].h_samp_factor == 2 && cinfo->comp_info[0].v_samp_factor == 1 &&
       cinfo->comp_info[1].h_samp_factor == 1 && cinfo->comp_info[1].v_samp_factor == 1 &&
       cinfo->comp_info[2].h_samp_factor == 1 && cinfo->comp_info[2].v_samp_factor == 1)
    {
      *sampling = SS_422;
    }
    else
    if(cinfo->comp_info[0].h_samp_factor == 2 && cinfo->comp_info[0].v_samp_factor == 2 &&
       cinfo->comp_info[1].h_samp_factor == 1 && cinfo->comp_info[1].v_samp_factor == 1 &&
       cinfo->comp_info[2].h_samp_factor == 1 && cinfo->comp_info[2].v_samp_factor == 1)
    {
      *sampling = SS_411;
    }
    else
    {
      printf("unknown sampling\n");
      *sampling = SS_OTHER;
    }
  }
  else
  if(cinfo->num_components == 4)
  {
    if(cinfo->comp_info[0].h_samp_factor == 1 && cinfo->comp_info[0].v_samp_factor == 1 &&
       cinfo->comp_info[1].h_samp_factor == 1 && cinfo->comp_info[1].v_samp_factor == 1 &&
       cinfo->comp_info[2].h_samp_factor == 1 && cinfo->comp_info[2].v_samp_factor == 1 &&
       cinfo->comp_info[3].h_samp_factor == 1 && cinfo->comp_info[3].v_samp_factor == 1)
    {
      *sampling = SS_444;
    }
    else
    if(cinfo->comp_info[0].h_samp_factor == 2 && cinfo->comp_info[0].v_samp_factor == 1 &&
       cinfo->comp_info[1].h_samp_factor == 1 && cinfo->comp_info[1].v_samp_factor == 1 &&
       cinfo->comp_info[2].h_samp_factor == 1 && cinfo->comp_info[2].v_samp_factor == 1 &&
       cinfo->comp_info[3].h_samp_factor == 2 && cinfo->comp_info[3].v_samp_factor == 1)
    {
      *sampling = SS_422;
    }
    else
    if(cinfo->comp_info[0].h_samp_factor == 2 && cinfo->comp_info[0].v_samp_factor == 2 &&
       cinfo->comp_info[1].h_samp_factor == 1 && cinfo->comp_info[1].v_samp_factor == 1 &&
       cinfo->comp_info[2].h_samp_factor == 1 && cinfo->comp_info[2].v_samp_factor == 1 &&
       cinfo->comp_info[3].h_samp_factor == 2 && cinfo->comp_info[3].v_samp_factor == 2)
    {
      *sampling = SS_411;
    }
    else
    {
      printf("unknown sampling\n");
      *sampling = SS_OTHER;
    }
  }
  else
  {
    printf("bad number of channels - %d\n",cinfo->num_components);
  }

  return;
} /* get_sampling() */


static int ijg_encode(
  Ipp8u*        inbuf,
  int           width,
  int           height,
  int           nchannels,
  JSS           sampling,
  int           quality,
  char*         outname,
  unsigned int* cpu_clocks)
{
  int    i;
  int    res;
  int    col;
  int    in_line_step;
  int    cur_input_row;
  Ipp64u clk0;
  Ipp64u clk1;
  Ipp8u* inptr;
  Ipp8u* outptr;
  Ipp8u* temp_buf = NULL;
  FILE*  fo = NULL;

  struct jpeg_error_mgr jerr;
  struct jpeg_compress_struct cinfo;

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

  cinfo.err = jpeg_std_error(&jerr);

  jpeg_create_compress(&cinfo);

  cinfo.UseIPP           = _IPP;
  cinfo.data_precision   = 8;
  cinfo.image_width      = width;
  cinfo.image_height     = height;
  cinfo.input_components = nchannels;
  cinfo.in_color_space   = ( (nchannels == 1) ? JCS_GRAYSCALE : ( (nchannels == 3) ? JCS_RGB : JCS_CMYK ) );

  jpeg_set_defaults(&cinfo);

  cinfo.jpeg_color_space = ( (nchannels == 1) ? JCS_GRAYSCALE : ( (nchannels == 3) ? JCS_YCbCr : JCS_YCCK) );

  jpeg_stdio_dest(&cinfo,fo);

  jpeg_set_quality(&cinfo,quality,TRUE);

  set_sampling(&cinfo,sampling,nchannels);

  in_line_step = cinfo.input_components * cinfo.image_width;

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

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

  jpeg_start_compress(&cinfo,TRUE);

  cur_input_row = 0;

  while(cinfo.next_scanline < cinfo.image_height)
  {
    cur_input_row++;
    inptr  = inbuf + in_line_step * (cinfo.image_height - cur_input_row);
    outptr = temp_buf;

    for(col = cinfo.image_width; col--;)
    {
      switch(cinfo.input_components)
      {
      case 1:
        *outptr++ = *inptr++;
        break;

      case 3:
        for(i = cinfo.input_components; i--;)
        {
          outptr[0] = inptr[2];
          outptr[1] = inptr[1];
          outptr[2] = inptr[0];
        }
        inptr  += 3;
        outptr += 3;
        break;

      case 4:
        for(i = cinfo.input_components; i--;)
        {
          outptr[0] = inptr[3];
          outptr[1] = inptr[2];
          outptr[2] = inptr[1];
          outptr[3] = inptr[0];
        }
        inptr  += 4;
        outptr += 4;
        break;
      }
    }

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

    jpeg_write_scanlines(&cinfo,&temp_buf,1);
  }

  jpeg_finish_compress(&cinfo);

  clk1 = get_pentium_counter();
#ifdef _WIN32
  SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
#endif
  *cpu_clocks = (unsigned int)(clk1 - clk0);

  res = 0;

Exit:

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

  jpeg_destroy_compress(&cinfo);

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

  return res;
} /* ijg_encode() */


static int ijg_decode(
  char*         name,
  Ipp8u**       buffer,
  int*          width,
  int*          height,
  int*          nchannels,
  JSS*          sampling,
  unsigned int* cpu_clocks)
{
  int    res;
  int    col;
  int    bmp_width;
  int    cur_output_row;
  int    img_size;
  JSS    ss;
  Ipp64u clk0;
  Ipp64u clk1;
  Ipp8u* inptr = NULL;
  Ipp8u* outptr = NULL;
  Ipp8u* tmp_buff = NULL;
  Ipp8u* img_buff = NULL;
  FILE*  fi = NULL;

  struct jpeg_error_mgr jerr;
  struct jpeg_decompress_struct cinfo;

  cinfo.err = jpeg_std_error(&jerr);

  jpeg_create_decompress(&cinfo);

  cinfo.UseIPP = _IPP;

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

  jpeg_stdio_src(&cinfo,fi);

  jpeg_read_header(&cinfo,TRUE);

⌨️ 快捷键说明

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