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

📄 spy.c

📁 基于格子Boltzmann方法开源可视化软件的源代码 具有很高的实用价值。对学习LBM方法及其软件开发非常游泳
💻 C
📖 第 1 页 / 共 2 页
字号:
//##############################################################################//// Copyright (C), 2005, Danny Thorne//// spy.c////  - "spy" a bitmap file and print info about it.////  - If it is small, print matrix of pixel values.//#include <stdio.h>#include <math.h>// Reference://   http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html//   (See text at bottom of this file.)struct bitmap_file_header{  // 1 2 bfType 19778 must always be set to 'BM' to declare that this is  // a .bmp-file.  char bfType[2];  // 3 4 bfSize ?? specifies the size of the file in bytes.  char bfSize[4];  // 7 2 bfReserved1 0 must always be set to zero.  char bfReserved1[2];  // 9 2 bfReserved2 0 must always be set to zero.  char bfReserved2[2];  // 11 4 bfOffBits 1078 specifies the offset from the beginning of the  // file to the bitmap data.  char bfOffBits[4];};struct bitmap_info_header{  // 15 4 biSize 40 specifies the size of the BITMAPINFOHEADER structure,  // in bytes.  char biSize[4];  // 19 4 biWidth 100 specifies the width of the image, in pixels.  char biWidth[4];  // 23 4 biHeight 100 specifies the height of the image, in pixels.  char biHeight[4];  // 27 2 biPlanes 1 specifies the number of planes of the target device,  // must be set to zero. [DT: Should be set to one, right? Not zero.]  char biPlanes[2];  // 29 2 biBitCount 8 specifies the number of bits per pixel.  char biBitCount[2];  // 31 4 biCompression 0 Specifies the type of compression, usually set  // to zero (no compression).  char biCompression[4];  // 35 4 biSizeImage 0 specifies the size of the image data, in bytes.  // If there is no compression, it is valid to set this member to zero.  char biSizeImage[4];  // 39 4 biXPelsPerMeter 0 specifies the the horizontal pixels per meter  // on the designated targer device, usually set to zero.  char biXPelsPerMeter[4];  // 43 4 biYPelsPerMeter 0 specifies the the vertical pixels per meter  // on the designated targer device, usually set to zero.  char biYPelsPerMeter[4];  // 47 4 biClrUsed 0 specifies the number of colors used in the bitmap,  // if set to zero the number of colors is calculated using the biBitCount  // member.  char biClrUsed[4];  // 51 4 biClrImportant 0 specifies the number of color that are  // 'important' for the bitmap, if set to zero, all colors are important.  char biClrImportant[4];};struct rgb_quad{  // 1 1 rgbBlue - specifies the blue part of the color.  char Blue;  // 2 1 rgbGreen - specifies the green part of the color.  char Green;  // 3 1 rgbRed - specifies the red part of the color.  char Red;  // 4 1 rgbReserved - must always be set to zero.  char Reserved;};main( int argc, int **argv){  int **spy,       height, width,      i, j;  char filename[1024];  FILE *o;  //printf("\n");  //printf("main() -- Hi!\n");  if( argc == 1)  {    printf("\n");    printf("usage>> ./spy [ input_file_name [output_file_name]]\n");    printf("\n");    exit(1);  }  else  {    sprintf( filename, "%s", *(argv+1));    printf("\n");    printf("Input filename is \"%s\".\n", filename);  }  read_bmp( filename);  spy_bmp( filename, &spy, &height, &width);  printf(">> spy( 1:%d, 1:%d)\n", height, width);  printf("\n");  if( height <= 10 && width <= 40)  {    for( j=0; j<height; j++)    {      for( i=0; i<width; i++)      {        printf(" %d", spy[j][i]);      }      printf("\n");    }    printf("\n");  }  if( argc > 2)  {    if( argc != 3)    {      printf("usage>> spy [ input_file_name [output_file_name]]\n");    }    sprintf( filename, "%s", *(argv+2));    if( !( o = fopen( filename, "w+")))    {      printf("Error opening \"%s\".\n", filename);    }    else    {      printf("Outputting spy results to \"%s\".\n", filename);      for( j=0; j<height; j++)      {        for( i=0; i<width; i++)        {          fprintf( o, " %d", spy[j][i]);        }        fprintf( o, "\n");      }      fclose(o);    }  }  // Deallocate memory used by the spy matrix.  for( j=0; j<height; j++)  {    free( spy[j]);  }  free( spy);  printf("\n");  printf("Done.\n");  printf("\n");  return 0;} /* main( int argc, int **argv) */// read_bmp( char *filename)//##############################################################################//// R E A D   B M P //read_bmp( char *filename){  FILE *in;  int i, n, m;  int pad, bytes_per_row;  char k;  char b;  struct bitmap_file_header bmfh;  struct bitmap_info_header bmih;  struct rgb_quad rgb;  int *int_ptr;  short int *short_int_ptr;  int *width_ptr;  int *height_ptr;  short int *bitcount_ptr;  printf("\n");  printf("read_bmp() -- Hi!\n");  printf("\n");  //printf("sizeof(char) = %d\n", sizeof(char));  //printf("sizeof(int)  = %d\n", sizeof(int));  //printf("\n");  //printf("sizeof( struct bitmap_file_header) = %d\n",  //    sizeof(struct bitmap_file_header));  //printf("sizeof( struct bitmap_info_header) = %d\n",  //    sizeof(struct bitmap_info_header));  //printf("\n");  //in = fopen( "junk.bmp", "r");  in = fopen( filename, "r");  // n = fread( void *BUF, size_t SIZE, size_t COUNT, FILE *FP);  n = fread( &bmfh, sizeof(struct bitmap_file_header), 1, in );  if( strncmp(bmfh.bfType,"BM",2))  {    printf("ERROR: Can't process this file type.  Exiting!\n");    printf("\n");    exit(1);  }  printf("struct bitmap_file_header:\n");  //char bfType[2];  printf("  bfType:      \"%c%c\"\n", bmfh.bfType[0], bmfh.bfType[1]);  //char bfSize[4];  int_ptr = (int*)bmfh.bfSize;  printf("  bfSize:       %d bytes\n", *int_ptr);  //char bfReserved1[2];  short_int_ptr = (short int*)bmfh.bfReserved1;  printf("  bfReserved1:  %d\n", *short_int_ptr);  //char bfReserved2[2];  short_int_ptr = (short int*)bmfh.bfReserved2;  printf("  bfReserved2:  %d\n", *short_int_ptr);  //char bfOffBits[4];  int_ptr = (int*)bmfh.bfOffBits;  printf("  bfOffBits:    %d\n", *int_ptr);  printf("\n");  n = fread( &bmih, sizeof(struct bitmap_info_header), 1, in );  int_ptr = (int*)bmih.biCompression;  if( *int_ptr != 0)  {    printf("ERROR: Can't handle compression.  Exiting!\n");    printf("\n");    exit(1);  }  printf("struct bitmap_info_header:\n");  // char biSize[4]  int_ptr = (int*)bmih.biSize;  printf("  biSize:          %d bytes\n", *int_ptr);  // char biWidth[4]  int_ptr = (int*)bmih.biWidth;  printf("  biWidth:         %d pixels\n", *int_ptr);  // char biHeight[4]  int_ptr = (int*)bmih.biHeight;  printf("  biHeight:        %d pixels\n", *int_ptr);  // char biPlanes[2]  short_int_ptr = (short int*)bmih.biPlanes;  printf("  biPlanes:        %d\n", *short_int_ptr);  // char biBitCount[2]  short_int_ptr = (short int*)bmih.biBitCount;  printf("  biBitCount:      %d bits per pixel\n", (int)bmih.biBitCount[0]);  // char biCompression[4]  int_ptr = (int*)bmih.biCompression;  printf("  biCompression:   %d\n", *int_ptr);  // char biSizeImage[4]  int_ptr = (int*)bmih.biSizeImage;  printf("  biSizeImage:     %d bytes\n", *int_ptr);  // char biXPelsPerMeter[4]  int_ptr = (int*)bmih.biXPelsPerMeter;  printf("  biXPelsPerMeter: %d\n", *int_ptr);  // char biYPelsPerMeter[4]  int_ptr = (int*)bmih.biYPelsPerMeter;  printf("  biYPelsPerMeter: %d\n", *int_ptr);  // char biClrUsed[4]  int_ptr = (int*)bmih.biClrUsed;  printf("  biClrUsed:       %d\n", *int_ptr);  // char biClrImportant[4]  int_ptr = (int*)bmih.biClrImportant;  printf("  biClrImportant:  %d\n", *int_ptr);  printf("\n");  if( (int)*(bmih.biBitCount) < 24)  {    //printf("(double)*(bmih.biBitCount) = %f\n", (double)*(bmih.biBitCount));    n = (double)pow(2.,(double)*(bmih.biBitCount));    printf("n = %d entries in palette.\n", n);    for( i=0; i<n; i++)    {      k = fread( &rgb, sizeof(struct rgb_quad), 1, in );      if( k!=1)      {        printf("Error reading palette entry %d.  Exiting!\n", i);        exit(1);      }#if 0      printf("%4d: [ \"%c\" \"%c\" \"%c\"] (\"%c\")\n",         i, rgb.Red, rgb.Green, rgb.Blue, rgb.Reserved );#else      printf("%4d: #", i);      printf("%1x%1x", (rgb.Red & 0xf0)>>4, (rgb.Red & 0x0f));      printf("%1x%1x", (rgb.Green & 0xf0)>>4, (rgb.Green & 0x0f));      printf("%1x%1x", (rgb.Blue & 0xf0)>>4, (rgb.Blue & 0x0f));      printf(" ");      printf("( %3d, %3d, %3d)",         (rgb.Red+256)%256,         (rgb.Green+256)%256,         (rgb.Blue+256)%256   );      printf("\n");#endif    }  }  else  {    printf("BitCount = %d, so no color table.\n",(int)*(bmih.biBitCount));  }  printf("\n");  width_ptr = (int*)bmih.biWidth;  height_ptr = (int*)bmih.biHeight;  bitcount_ptr = (short int*)bmih.biBitCount;  // Bytes per row of the bitmap.  bytes_per_row =     ((int)ceil(( (((double)(*width_ptr))*((double)(*bitcount_ptr)))/8.)));  // Bitmaps pad rows to preserve 4-byte boundaries.  // The length of a row in the file will be bytes_per_row + pad .  pad = ((4) - bytes_per_row%4)%4;  printf("----------------------------------------\n");  printf("Pixel Data:  ");  if( !( *width_ptr <= 5 && *height_ptr <= 10))  {    printf("<Too large to display.>\n");  }  else  {    printf("\n");  }  n = 0;  m = 0;  n+=( k = fread( &b, 1, 1, in ));  while( !feof(in))  {#if 0    printf("%d", (b & 0x80) != 0);    printf("%d", (b & 0x40) != 0);    printf("%d", (b & 0x20) != 0);    printf("%d", (b & 0x10) != 0);    printf("%d", (b & 0x08) != 0);    printf("%d", (b & 0x04) != 0);    printf("%d", (b & 0x02) != 0);    printf("%d", (b & 0x01) != 0);    printf(" (%3d)", (b+256)%256);    printf(" (%2d,%2d) |", (b&0xf0)>>4, b&0x0f);#else  if( *width_ptr <= 5 && *height_ptr <= 10)  {    printf(" ");    switch(*bitcount_ptr)    {      case 1: // Monochrome.        printf("%d", (b & 0x80) != 0);        printf("%d", (b & 0x40) != 0);        printf("%d", (b & 0x20) != 0);        printf("%d", (b & 0x10) != 0);        printf("%d", (b & 0x08) != 0);        printf("%d", (b & 0x04) != 0);        printf("%d", (b & 0x02) != 0);        printf("%d", (b & 0x01) != 0);        break;      case 4: // 16 colors.        printf("%2d", (b&0xf0)>>4);        printf("  ");        printf("%2d", b&0x0f);        break;      case 8: // 256 colors.        printf("%3d", (b+256)%256);        break;      case 24: // 24-bit colors.        printf("%d", (b & 0x80) != 0);        printf("%d", (b & 0x40) != 0);        printf("%d", (b & 0x20) != 0);        printf("%d", (b & 0x10) != 0);        printf("%d", (b & 0x08) != 0);        printf("%d", (b & 0x04) != 0);        printf("%d", (b & 0x02) != 0);        printf("%d", (b & 0x01) != 0);        break;      default: // 32-bit colors?        printf("ERROR: Unhandled color depth, "            "BitCount = %d. Exiting!\n", *(bmih.biBitCount));        exit(1);        break;    }

⌨️ 快捷键说明

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