📄 lbio_03082005.c
字号:
//// S P Y B M P //// - Returns matrix 'spy' of ones and zeros.//// - Zeros for white pixels.//// - Ones for non-white pixels.//void spy_bmp( char *filename, lattice_ptr lattice, int **spy){ FILE *in, *o; int i, j, n, m; int pad, bytes_per_row; char k; char b, g, r; 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"); // Clear the spy array. for( j=0; j<lattice->param.LY; j++) { for( i=0; i<lattice->param.LX; i++) { spy[j][i] = 0; } } if( !( in = fopen( filename, "r"))) {#if 1 printf("spy_bmp() -- Error opening file \"%s\".\n", filename); exit(1);#else printf(" %s::spy_bmp() %d >> File \"%s\" cannot be opened for reading.\n", __FILE__, __LINE__, filename); if( !( o = fopen( filename, "w+"))) { // TODO: Write blank bmp file. } printf(" %s::spy_bmp() %d >> Wrote a blank \"%s\" file.\n", __FILE__, __LINE__, filename); printf(" %s::spy_bmp() %d >> Returning all zeros!\n", __FILE__, __LINE__); fclose( o); return;#endif } // 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( *width_ptr != lattice->param.LX) { printf("%s %d >> ERROR: LX %d does not match the " "width %d of the BMP file. Exiting!\n", __FILE__, __LINE__, lattice->param.LX, *width_ptr); exit(1); } if( *height_ptr != lattice->param.LY) { printf("%s %d >> ERROR: LY %d does not match the " "height %d of the BMP file. Exiting!\n", __FILE__, __LINE__, lattice->param.LY, *height_ptr); exit(1); } if( *bitcount_ptr < 24) { n = (int)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); } } } // 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 = 0; while( !feof(in)) { switch(*bitcount_ptr) { case 1: // Monochrome. printf("spy_bmp() -- " "Support for Monochrome BMPs is pending. " "Exiting!\n"); exit(1); 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. printf("spy_bmp() -- " "Support for 16 color BMPs is pending. " "Exiting!\n"); exit(1); 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. printf("spy_bmp() -- " "Support for 256 color BMPs is pending. " "Exiting!\n"); exit(1); if( i < *width_ptr) { (spy)[j][i] = ( (b&0xff) != 255); } i++; break; case 24: // 24-bit colors. if( i < 3*(*width_ptr)) { i++; n+=( k = fread( &g, 1, 1, in )); i++; n+=( k = fread( &r, 1, 1, in )); if( ( (b&0xff) == 0) &&( (g&0xff) == 0) &&( (r&0xff) == 0) ) { (spy)[j][(int)floor((double)i/3.)] = 1; }#if 0 if( ( (b&0xff) == 0) &&( (g&0xff) == 0) &&( (r&0xff) == 255) ) { // Red ==> Inflow, Pressure boundaries. if( (int)floor((double)i/3.) == 0 || (int)floor((double)i/3.) == lattice->param.LX-1 ) { if( !( j==0 || j == lattice->param.LY-1)) { lattice->periodic_x[subs] = 0; } } if( j == 0 || j == lattice->param.LY-1 ) { if( !( (int)floor((double)i/3.) == 0 || (int)floor((double)i/3.) == lattice->param.LX-1)) { lattice->periodic_y[subs] = 0; } } } if( ( (b&0xff) == 0) &&( (g&0xff) == 255) &&( (r&0xff) == 0) ) { // Green ==> Outflow, Pressure boundaries. if( (int)floor((double)i/3.) == 0 || (int)floor((double)i/3.) == lattice->param.LX-1 ) { if( !( j==0 || j == lattice->param.LY-1)) { lattice->periodic_x[subs] = 0; } } if( j == 0 || j == lattice->param.LY-1 ) { if( !( (int)floor((double)i/3.) == 0 || (int)floor((double)i/3.) == lattice->param.LX-1)) { lattice->periodic_y[subs] = 0; } } }#endif } 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) */// void read_bcs( char *filename, int **bcs)//##############################################################################//// R E A D B C S//// - Read boundary condition information from file.//void read_bcs( lattice_ptr lattice, int **bcs){ FILE *in; char filename[1024]; int i, j, n, m; int ei, ej; int pad, bytes_per_row; char k; char b, g, r; 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; int subs; printf("read_bcs() -- Hi!\n"); for( subs=0; subs<NUM_FLUID_COMPONENTS; subs++) { // Clear the bcs array. for( j=0; j<lattice->param.LY; j++) { for( i=0; i<lattice->param.LX; i++) { bcs[j][i] = 0; } } sprintf( filename, "./in/%dx%dbc_subs%02d.bmp", lattice->param.LX, lattice->param.LY, subs); if( !( in = fopen( filename, "r"))) { printf("read_bcs() -- Error opening file \"%s\".\n", filename); exit(1); } // Read the headers. 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( *height_ptr != lattice->param.LY) { printf("ERROR: Lattice height does not match " "soil matrix data \"%s\". (%d!=%d) Exiting!\n", filename, lattice->param.LY, *height_ptr ); printf("\n"); exit(1); } if( *width_ptr != lattice->param.LX) { printf("ERROR: Lattice width does not match " "soil matrix data \"%s\". (%d!=%d) Exiting!\n", filename, lattice->param.LX, *width_ptr ); printf("\n"); exit(1); } // Read the palette, if necessary. if( *bitcount_ptr < 24) { n = (int)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); } } } // 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_ptr-1; j = 0;//*height_ptr-1; while( !feof(in)) { switch(*bitcount_ptr) { case 1: // Monochrome. printf("read_bcs() -- " "Support for Monochrome BMPs is pending. " "Exiting!\n"); exit(1); if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x80) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x40) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x20) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x10) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x08) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x04) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x02) == 0); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b & 0x01) == 0); } i++; break; case 4: // 16 colors. printf("read_bcs() -- " "Support for 16 color BMPs is pending. " "Exiting!\n"); exit(1); if( i < *width_ptr) { (bcs)[j][i] = ( (b&0xf0)>>4 != 15); } i++; if( i < *width_ptr) { (bcs)[j][i] = ( (b&0x0f) != 15); } i++; break; case 8: // 256 colors. printf("read_bcs() -- " "Support for 256 color BMPs is pending. " "Exiting!\n"); exit(1); if( i < *width_ptr) { (bcs)[j][i] = ( (b&0xff) != 255); } i++; break; case 24: // 24-bit colors. if( i < 3*(*width_ptr)) { i++; n+=( k = fread( &g, 1, 1, in )); i++; n+=( k = fread( &r, 1, 1, in )); if( ( (b&0xff) == 0) &&( (g&0xff) == 0) &&( (r&0xff) == 255) ) { // R E D ==> Inflow, Pressure boundaries. bcs[j][(int)floor((double)i/3.)] = 1; } if( ( (b&0xff) == 0) &&( (g&0xff) == 255) &&( (r&0xff) == 0) ) { // G R E E N ==> Outflow, Pressure boundaries. bcs[j][(int)floor((double)i/3.)] = 2; } } i++; break; default: // 32-bit colors? printf("ERROR: Unhandled color depth, " "BitCount = %d. Exiting!\n", *bitcount_ptr); exit(1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -