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

📄 saveudbmp.c

📁 JPEG压缩和解压程序和一些相关的说明文档 内容比较全
💻 C
字号:
/* *saveudbmp.c is reedited from IJG code by Fujian Shi(fieagle@yahoo.com.cn). * This file and commondecls.h can save a graycolor bmp image to a compact file. * */#include "commondecls.h"		/* Common decls for cjpeg/djpeg applications *//* Macros to deal with unsigned chars as efficiently as compiler allows */typedef unsigned char U_CHAR;#define UCH(x)	((int) (x))#define JFREAD(file,buf,sizeofbuf)  \  ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))#define JFWRITE(file,buf,sizeofbuf)  \  ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))#define	ReadOK(file,buffer,len)	(JFREAD(file,buffer,len) == ((size_t) (len)))#define WriteOK(file,buffer,len) (JFWRITE(file,buffer,len)==((size_t) (len)))#define LOCAL(type) static type/* Private version of data source object */typedef struct _bmp_source_struct * bmp_source_ptr;typedef struct _bmp_source_struct {    FILE *inputfile;              /*source file pointer*/  FILE *outputfile;                /*destination file pointer*/  JSAMPROW buffer;              /*buffer pointer*/  JSAMPARRAY colormap;		/* BMP colormap (converted to my format) */  UINT8 colormap_used;          /*the tag of the colormap,in the real_color image,the colormap isn't used*/  JDIMENSION image_width;  JDIMENSION image_height;      /* BMP image height*/  /* Needed to reverse row order ,?reserved temply.*/  /*JDIMENSION source_row;	 Current source row number */  JDIMENSION row_width;		/* Physical width of scanlines in file */  int bits_per_pixel;		/* remembers 8- or 24-bit format */ UINT16 X_density ;             /* 100 cm per meter */ UINT16 Y_density ; UINT8  density_unit ;  	/* dots/cm */  } bmp_source_struct;/*arrage a row buffer*/LOCAL(void  *)alloc_one_row(size_t size_object){    return (void *) malloc(size_object);}/*arrage several rows buffer,that is a array*/LOCAL(JSAMPARRAY)alloc_sarray (JDIMENSION samplesperrow, JDIMENSION numrows)/* Allocate a 2-D sample array */{  JSAMPARRAY result;  JSAMPROW workspace;  int currow;   /* Get space for row pointers (small object) */  result = (JSAMPARRAY) alloc_one_row((size_t) (numrows * SIZEOF(JSAMPROW)));  /* Get the rows themselves (large objects) */  currow = 0;  while (currow < numrows) {    workspace = (JSAMPROW) alloc_one_row((size_t) (samplesperrow		  * SIZEOF(JSAMPLE)));    result[currow++] = workspace;      }  return result;}LOCAL(int)read_byte (bmp_source_ptr sinfo)/* Read next byte from BMP file */{  register FILE *infile =sinfo->inputfile;   register int c=0;  if ((c = getc(infile)) == EOF){    printf("The wholefile has been input");    exit (0);  }  return c;}LOCAL(void)read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)/* Read the colormap from a BMP file *//*cmalen is the number of color in the bmp image*/{  int i;  switch (mapentrysize) {  case 3:    /* BGR format (occurs in OS/2 files) */    for (i = 0; i < cmaplen; i++) {      sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);      sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);      sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);    }    break;  case 4:    /* BGR0 format (occurs in MS Windows files) */    for (i = 0; i < cmaplen; i++) {      sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);      sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);      sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);      (void) read_byte(sinfo);    }    break;  default:    exit (0);    break;  }}/* * This method loads the image into whole_image during the first call on * get_pixel_rows.  The get_pixel_rows pointer is then adjusted to call * get_8bit_row or get_24bit_row on subsequent calls. */LOCAL(JDIMENSION)process_image (bmp_source_ptr sinfo){  register FILE *infile = sinfo->inputfile;  register FILE *outfile=sinfo->outputfile;  register int c;  register JSAMPROW buffer_ptr;             /*buffer pointer,the buffer is only one row*/  JDIMENSION row, col;  long position_adju;  /* read and write data. */  for (row = 0; row < sinfo->image_height; row++) {        buffer_ptr=sinfo->buffer;           for (col = sinfo->row_width; col > 0; col--) {  /*by the cycle input one row sample into the buffer*/      /* inline copy of read_byte() for speed */      if ((c = getc(infile)) == EOF)	exit(0);      *buffer_ptr++ = (JSAMPLE) c;            if(row==(sinfo->image_height-1)&&(col==2||col==1))      printf("the oribmp.bmp last pixel element is%d\n",c);       }       if(!WriteOK(outfile,sinfo->buffer,sinfo->image_width)){      printf("the data cann't be write into the new image.\n");  	 exit(0);    }          }}/*write_2byte_to_buffer can input a 32bits data byte by byte to the buffer*/LOCAL(void)write_2byte_to_buffer(JSAMPROW new_bmp_header,int offset,UINT16 data){  int j;  unsigned char c;  for (j=0;j<=1;j++){    *(new_bmp_header+offset+j)=( U_CHAR) (data);    data>>=8;  }}/* * Read the file header; return image size and component count. */LOCAL(void)start_input_bmp (bmp_source_ptr sinfo){  U_CHAR bmpfileheader[14];  U_CHAR bmpinfoheader[64];  U_CHAR new_bmp_header[54];#define GET_2B(array,offset)  ((unsigned int) UCH(array[offset]) + \			       (((unsigned int) UCH(array[offset+1])) << 8))#define GET_4B(array,offset)  ((INT32) UCH(array[offset]) + \			       (((INT32) UCH(array[offset+1])) << 8) + \			       (((INT32) UCH(array[offset+2])) << 16) + \			       (((INT32) UCH(array[offset+3])) << 24))  INT32 bfOffBits;  INT32 headerSize;  INT32 biWidth = 0;		/* initialize to avoid compiler warning */  INT32 biHeight = 0;  unsigned int biPlanes;  INT32 biCompression;  INT32 biXPelsPerMeter,biYPelsPerMeter;  INT32 biClrUsed = 0;  int mapentrysize = 0;		/* 0 indicates no colormap */  INT32 bPad;  JDIMENSION row_width;  /* Read and verify the bitmap file header */  if (! ReadOK(sinfo->inputfile, bmpfileheader, 14)){    printf("error when read the head of file");    exit(0);  }    if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */{    printf("the file is not bmp format file");    exit(0);  }  bfOffBits = (INT32) GET_4B(bmpfileheader,10);  /* We ignore the remaining fileheader fields */  /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),   * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.   */  if (! ReadOK(sinfo->inputfile, bmpinfoheader, 4)){    printf("Error when read infohead");    exit(0);  }    headerSize = (INT32) GET_4B(bmpinfoheader,0);  if (headerSize < 12 || headerSize > 64){    printf("the infohead is damaged");    exit(0);  }  printf("the bmpinfoheader is \%d\n",headerSize);    if (! ReadOK(sinfo->inputfile, bmpinfoheader+4, headerSize-4)){    printf("the infohead is damaged");    exit(0);  }  switch ((int) headerSize) {    /*case 12:    * Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) *    biWidth = (INT32) GET_2B(bmpinfoheader,4);    biHeight = (INT32) GET_2B(bmpinfoheader,6);    biPlanes = GET_2B(bmpinfoheader,8);    source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10);    switch (source->bits_per_pixel) {    case 8:			* colormapped image *      mapentrysize = 3;		* OS/2 uses RGBTRIPLE colormap *      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);      break;    case 24:			* RGB image *      TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);      break;    default:      ERREXIT(cinfo, JERR_BMP_BADDEPTH);      break;    }    if (biPlanes != 1)      ERREXIT(cinfo, JERR_BMP_BADPLANES);    break;*/  case 40:  case 64:    /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */    /* or OS/2 2.x header, which has additional fields that we ignore */    biWidth = GET_4B(bmpinfoheader,4);    printf("the width of image is%d\n",biWidth);    biHeight = GET_4B(bmpinfoheader,8);    printf("the height of image is%d\n",biHeight);    biPlanes = GET_2B(bmpinfoheader,12);    sinfo->bits_per_pixel = (int) GET_2B(bmpinfoheader,14);    printf("the bits_per_pixel of image is%d\n",sinfo->bits_per_pixel);    biCompression = GET_4B(bmpinfoheader,16);    biXPelsPerMeter = GET_4B(bmpinfoheader,24);    biYPelsPerMeter = GET_4B(bmpinfoheader,28);    biClrUsed = GET_4B(bmpinfoheader,32);    /* biSizeImage, biClrImportant fields are ignored */    switch (sinfo->bits_per_pixel) {    case 8:			/* colormapped image */      mapentrysize = 4;		/* Windows uses RGBQUAD colormap */      break;    case 24:			/* RGB image */      printf("Sorry,the programe only dispose 8bit bmp image/n");      break;    default:      printf("the bis of every pixel is not 8 or 24,I can't deal with it now");      break;    }    if (biPlanes != 1)      exit(0);    if (biCompression != 0)      exit(0);    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {      /* Set JFIF density parameters from the BMP data */      sinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */      sinfo->Y_density = (UINT16) (biYPelsPerMeter/100);      sinfo->density_unit = 2;	/* dots/cm */    }    break;    default:      printf("the infohead is not 40 or 64 bytes");      exit(0);      break;  }  /* Compute distance to bitmap data --- will adjust for colormap below */  bPad = bfOffBits - (headerSize + 14);  printf("the bpad before colormap is %d\n",bPad);  /* Read the colormap, if any */  if (mapentrysize > 0) {    if (biClrUsed <= 0)      biClrUsed = 256;		/* assume it's 256 */    else if (biClrUsed > 256)      printf("the color used is not real_color and 256");    /* Allocate space to store the colormap */    sinfo->colormap=(JSAMPARRAY) (alloc_sarray      ((JDIMENSION) biClrUsed, (JDIMENSION) 3));    sinfo->colormap_used=1;    printf("the image use colormap\n");    /* and read it from the file */    read_colormap(sinfo, (int) biClrUsed, mapentrysize);    /* account for size of colormap */    bPad -= biClrUsed * mapentrysize;  }  /* Skip any remaining pad bytes */  if (bPad < 0)	{    printf("incorrect bfooffbits");		/* incorrect bfOffBits value? */    exit(0);  }  while (--bPad >= 0) {    (void) read_byte(sinfo);  }  printf("The bpad=%d\n",bPad);  /* Compute row width in file, including padding to 4-byte boundary */  if (sinfo->bits_per_pixel == 24)    row_width = (JDIMENSION) (biWidth * 3);  else    row_width = (JDIMENSION) biWidth;  while ((row_width & 3) != 0) row_width++;    sinfo->row_width = row_width;    printf("the row_width  is%d\n",row_width);    /* Allocate one-row buffer for returned data */  sinfo->buffer =(JSAMPROW) (alloc_one_row    ((size_t) ((size_t) (row_width))*sizeof(JSAMPLE)));    sinfo->image_width = (JDIMENSION) biWidth;  sinfo->image_height = (JDIMENSION) biHeight;    /*write image_height and width ,xy resolution to the file for JFIF image format*/  write_2byte_to_buffer(new_bmp_header,0,biWidth);  write_2byte_to_buffer(new_bmp_header,2,biHeight);  /*write_2byte_to_buffer(new_bmp_header,4,sinfo->Y_density);   *write_2byte_to_buffer(new_bmp_header,6,sinfo->X_density);   *  *(new_bmp_header+8)=sinfo->density_unit = 2;   */    /*write the header into the file*/  if (!WriteOK(sinfo->outputfile,new_bmp_header,4)){     printf("the data cann't be write into the new image.\n");        exit(0);  } }/*free the memory*/LOCAL(void)free_mem(bmp_source_ptr sinfo){  int i;  free((void *)(sinfo->buffer));  for (i=0;i<=2;i++)    free((void *) (sinfo->colormap[i]));  if(sinfo->colormap_used)    free((void *) (sinfo->colormap));   }  main(int argc,char *argv[])    {      bmp_source_struct main_sinfo;      bmp_source_ptr sinfo=&main_sinfo;      sinfo->colormap_used=0;               /*We asume the image is real-color image*/     if((sinfo->inputfile=fopen(argv[1],"rb"))==NULL){	printf("The %s can't be opended\n",argv[1]);	exit(0);      }      if((sinfo->outputfile=fopen(argv[2],"wb+"))==NULL){	printf("The %s can't be created\n",argv[2]);	exit(0);      }      start_input_bmp(sinfo);      process_image(sinfo);      free_mem(sinfo);      /*fseek(sinfo->inputfile,-2,2);	printf("the mumber is%d\n",getc(sinfo->inputfile));*/      fclose(sinfo->inputfile);      fclose(sinfo->outputfile);     }

⌨️ 快捷键说明

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