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

📄 spy.c

📁 基于格子Boltzmann方法开源可视化软件的源代码 具有很高的实用价值。对学习LBM方法及其软件开发非常游泳
💻 C
📖 第 1 页 / 共 2 页
字号:
    printf(" |");  }#endif    if( !(n%(bytes_per_row+pad)))     {       if( *width_ptr <= 5 && *width_ptr <= 10) { printf("\n");}      m++;    }    n+=( k = fread( &b, 1, 1, in ));  } /* while( !feof(in)) */  if( *width_ptr <= 5 && *width_ptr <= 10) { printf("\n");}  printf("----------------------------------------\n");  printf("\n");  printf("%d bytes.\n", n);  printf("%d rows.\n", m);  printf("%d bytes containing data per row.\n", bytes_per_row);  printf("%d bytes of padding per row.\n", pad);  printf("( %d + %d) * %d = %d\n",     bytes_per_row, pad, m, (bytes_per_row+pad)*m);  printf("\n");  if( (bytes_per_row+pad)*m!=n)  {    printf("WARNING: Num bytes read = %d versus num bytes predicted = %d .\n",        n, (bytes_per_row+pad)*m);  }  if( m != *height_ptr)  {    printf("WARNING: m (%d) != bmih.biHeight (%d).\n", m, *height_ptr);  }  fclose(in);  printf("read_bmp() -- Bye!\n");  printf("\n");} /* read_bmp( char *filename) */// spy_bmp( char *filename, int ***spy)//##############################################################################//// S P Y   B M P //spy_bmp( char *filename, int ***spy, int *height, int *width){  FILE *in;  int i, j, 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("spy_bmp() -- Hi!\n");  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);  }  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);  }  width_ptr = (int*)bmih.biWidth;  height_ptr = (int*)bmih.biHeight;  bitcount_ptr = (short int*)bmih.biBitCount;  if( *bitcount_ptr < 24)  {    n = (double)pow(2.,(double)*bitcount_ptr); // Num palette entries.    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);      }    }  }  // Allocate memory for the spy matrix.  *height = *height_ptr;  *width = *width_ptr;  *spy = (int**)malloc( (*height)*sizeof(int*));  for( m=0; m<*height; m++)  {    (*spy)[m] = (int*)malloc( (*width)*sizeof(int));  }#if 0  memset( &((*spy)[0][0]), 0, (*width)*(*height)*sizeof(int));#else  for( j=0; j<*height; j++)  {    for( i=0; i<*width; i++)    {      (*spy)[j][i] = 0;    }  }#endif  // 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;  n = 0;  m = 0;  n+=( k = fread( &b, 1, 1, in ));  i = 0;  j = *height-1;  while( !feof(in))  {    switch(*bitcount_ptr)    {      case 1: // Monochrome.        if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x80) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x40) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x20) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x10) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x08) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x04) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x02) == 0); }        i++;             if( i < *width_ptr) { (*spy)[j][i] = ( (b & 0x01) == 0); }        i++;        break;      case 4: // 16 colors.        if( i < *width_ptr) { (*spy)[j][i] = ( (b&0xf0)>>4 != 15); }        i++;        if( i < *width_ptr) { (*spy)[j][i] = ( (b&0x0f) != 15); }        i++;        break;      case 8: // 256 colors.        if( i < *width_ptr) { (*spy)[j][i] = ( (b&0xff) != 255); }        i++;        break;      case 24: // 24-bit colors.        if( i < 3*(*width_ptr))         { //printf("(*spy)[%d][%d] = %d -> ", j, (int)floor((double)i/3.),//    (*spy)[j][(int)floor((double)i/3.)]);          (*spy)[j][(int)floor((double)i/3.)]  |= ( (b&0xff) != 255);//printf("%d\n", (*spy)[j][(int)floor((double)i/3.)]);//printf("( (%d&0xff) != 255) = %d\n", b, ( (b&0xff) != 255));        }        i++;        break;      default: // 32-bit colors?        printf("ERROR: Unhandled color depth, "            "BitCount = %d. Exiting!\n", *bitcount_ptr);        exit(1);        break;    } /* switch(*(bmih.biBitCount)) */    if( !(n%(bytes_per_row+pad))) { m++; i=0; j--;}    n+=( k = fread( &b, 1, 1, in ));  } /* while( !feof(in)) */  if( (bytes_per_row+pad)*m!=n)  {    printf("WARNING: Num bytes read = %d versus num bytes predicted = %d .\n",        n, (bytes_per_row+pad)*m);  }  if( m != *height_ptr)  {    printf("WARNING: m (%d) != bmih.biHeight (%d).\n", m, *height_ptr);  }  fclose(in);  printf("spy_bmp() -- Bye!\n");  printf("\n");} /* spy_bmp( char *filename, int ***spy, int *height, int *width) *//*<http://www.fortunecity.com> 	web hosting<http://www.fortunecity.com/> 	domain names<http://www.fortunecity.com/> 	email addresses <http://www.v3.com/> 	search <http://www.fortunecity.com/marketplace/> 	<http://ads.fortunecity.com/RealMedia/ads/click_nx.cgi/en/m_computers_and_internet/410390@Top>  The .bmp file format      Introduction:The .bmp file format (sometimes also saved as .dib) is the standard fora Windows 3.0 or later DIB(device independent bitmap) <bmsinwin.html>file. It may use compression (though I never came across a compressed.bmp-file) and is (by itself) not capable of storing animation. However,you can animate a bitmap using different methods but you have to writethe code which performs the animation. There are different ways tocompress a .bmp-file, but I won't explain them here because they are sorarely used. The image data itself can either contain pointers toentries in a color table or literal RGB values (this is explained later).      Basic structure:A .bmp file contains of the following data structures:BITMAPFILEHEADER    bmfh;BITMAPINFOHEADER    bmih;RGBQUAD             aColors[];BYTE                aBitmapBits[];/bmfh/ contains some information about the bitmap file (about the file,not about the bitmap itself). /bmih/ contains information about thebitmap such as size, colors,... The /aColors array/ contains a colortable. The rest is the image data, which format is specified by the/bmih/ structure.      Exact structure:The following tables give exact information about the data structuresand also contain the settings for a bitmap with the followingdimensions: size 100x100, 256 colors, no compression. The /start/-valueis the position of the byte in the file at which the explained dataelement of the structure starts, the /size/-value contains the nuber ofbytes used by this data element, the /name/-value is the name assignedto this data element by the Microsoft API documentation. /Stdvalue/stands for standard value. There actually is no such a thing as astandard value but this is the value Paint assigns to the data elementif using the bitmap dimensions specified above (100x100x256). The/meaning/-column gives a short explanation of the purpose of this dataelement.      The BITMAPFILEHEADER:*start* 	*size* 	*name* 	*stdvalue* 	*purpose*1 	2 	bfType 	19778 	must always be set to 'BM' to declare that this isa .bmp-file.3 	4 	bfSize 	?? 	specifies the size of the file in bytes.7 	2 	bfReserved1 	0 	must always be set to zero.9 	2 	bfReserved2 	0 	must always be set to zero.11 	4 	bfOffBits 	1078 	specifies the offset from the beginning of thefile to the bitmap data.      The BITMAPINFOHEADER:start 	size 	name 	stdvalue 	purpose15 	4 	biSize 	40 	specifies the size of the BITMAPINFOHEADER structure,in bytes.19 	4 	biWidth 	100 	specifies the width of the image, in pixels.23 	4 	biHeight 	100 	specifies the height of the image, in pixels.27 	2 	biPlanes 	1 	specifies the number of planes of the target device,must be set to zero.29 	2 	biBitCount 	8 	specifies the number of bits per pixel.31 	4 	biCompression 	0 	Specifies the type of compression, usually setto zero (no compression).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.39 	4 	biXPelsPerMeter 	0 	specifies the the horizontal pixels per meteron the designated targer device, usually set to zero.43 	4 	biYPelsPerMeter 	0 	specifies the the vertical pixels per meteron the designated targer device, usually set to zero.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 biBitCountmember.51 	4 	biClrImportant 	0 	specifies the number of color that are'important' for the bitmap, if set to zero, all colors are important.Note that /biBitCount/ actually specifies the color resolution of thebitmap. The possible values are: 1 (black/white); 4 (16 colors); 8 (256colors); 24 (16.7 million colors). The biBitCount data element alsodecides if there is a color table in the file and how it looks like. In1-bit mode the color table has to contain 2 entries (usually white andblack). If a bit in the image data is clear, it points to the firstpalette entry. If the bit is set, it points to the second. In 4-bit modethe color table must contain 16 colors. Every byte in the image datarepresents two pixels. The byte is split into the higher 4 bits and thelower 4 bits and each value of them points to a palette entry. There arealso standard colors for 16 colors mode (16 out of Windows 20 reservedcolors <rescolrs.html> (without the entries 8, 9, 246, 247)). Note thatyou do not need to use this standard colors if the bitmap is to bedisplayed on a screen which support 256 colors or more, however (nearly)every 4-bit image uses this standard colors. In 8-bit mode every byterepresents a pixel. The value points to an entry in the color tablewhich contains 256 entries (for details see Palettes in Windows<palinwin.html>. In 24-bit mode three bytes represent one pixel. Thefirst byte represents the red part, the second the green and the thirdthe blue part. There is no need for a palette because every pixelcontains a literal RGB-value, so the palette is omitted.      The RGBQUAD array:The following table shows a single RGBQUAD structure:start 	size 	name 	stdvalue 	purpose1 	1 	rgbBlue 	- 	specifies the blue part of the color.2 	1 	rgbGreen 	- 	specifies the green part of the color.3 	1 	rgbRed 	- 	specifies the red part of the color.4 	1 	rgbReserved 	- 	must always be set to zero.Note that the term /palette/ does not refer to a RGBQUAD array, which iscalled /color table/ instead. Also note that, in a color table(RGBQUAD), the specification for a color starts with the blue byte. In apalette a color always starts with the red byte. There is no simple wayto map the whole color table into a LOGPALETTE structure, which you willneed to display the bitmap. You will have to write a function thatcopies byte after byte.      The pixel data:It depens on the BITMAPINFOHEADER structure how the pixel data is to beinterpreted (see above <bmpffrmt.html#bmih>).It is important to know that the rows of a DIB are stored upside down.That means that the uppest row which appears on the screen actually isthe lowest row stored in the bitmap, a short example:	pixels displayed on the screen 	pixels stored in .bmp-fileYou do not need to turn around the rows manually. The API functionswhich also display the bitmap will do that for you automatically.Another important thing is that the number of bytes in one row mustalways be adjusted to fit into the border of a multiple of four. Yousimply append zero bytes until the number of bytes in a row reaches amultiple of four, an example:6 bytes that represent a row in the bitmap: 	A0 37 F2 8B 31 C4must be saved as: 	A0 37 F2 8B 31 C4 *00 00*to reach the multiple of four which is the next higher after six(eight). If you keep these few rules in mind while working with.bmp files it should be easy for you, to master it.Back to the main page <index.html>------------------------------------------------------------------------Copyright 1998 Stefan Hetzl. If you have questions or comments or havediscovered an error, send mail <mailto:hetzl@teleweb.at> tohetzl@teleweb.at. You may forward this document or publish it on yourwebpage as long as you don't change it and leave this notice at the end.<http://www.fortunecity.com/?sid=fcfootergif>web hosting <http://www.fortunecity.com/?sid=fcfooterhosting> ? domainnames <http://www.fortunecity.com/?sid=fcfooterdomains> ? web design<http://www.fortunecity.com/web-design.shtml?sid=fcfooterdesign>online games <http://www.hotgames.com/> ? digital cameras<http://www.onlinereviewguide.com/> ? photo album<http://www.myphotoalbum.com/>advertising online <http://www.ampiramedia.com/> ? online casino<http://www.newyorkscasinos.com/>*/

⌨️ 快捷键说明

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