int finishBin() { try { // make sure file contains an even number of bytes if (fileSize % 2) // if odd size writeBigEndian(0, 1); // write an extra byte of 0 currentFP = ftell(binFile); // save current file position // position file at previous sections byte count fseek(binFile, byteCountFP, SEEK_SET); writeBigEndian(byteCount, LONG_SIZE); // write previous sections byte count rewind(binFile); // rewind to start of file // write Teeside header to file removed in EASy68K v4.4.4 //writeBigEndian(0x0001, WORD_SIZE); // version # ? //writeBigEndian(sectionCount, LONG_SIZE); // # of sections //writeBigEndian(0, LONG_SIZE); // unknown //writeBigEndian(startAddress, LONG_SIZE); // starting address //writeBigEndian(0x2800, WORD_SIZE); // unknown //writeBigEndian(0, LONG_SIZE); // unknown // close the file fclose(binFile); } catch( ... ) { sprintf(buffer, "ERROR: An exception occurred in routine 'finishBin'. \n"); printError(NULL, EXCEPTION, 0); return MILD_ERROR; } return NORMAL; }
static void etc1_encode_block(etc1_byte* pIn, int inMask, etc1_byte* pOut) { etc1_byte colors[6]; etc1_byte flippedColors[6]; etc_average_colors_subblock(pIn, inMask, colors, false, false); etc_average_colors_subblock(pIn, inMask, colors + 3, false, true); etc_average_colors_subblock(pIn, inMask, flippedColors, true, false); etc_average_colors_subblock(pIn, inMask, flippedColors + 3, true, true); etc_compressed a, b; etc_encode_block_helper(pIn, inMask, colors, &a, false); etc_encode_block_helper(pIn, inMask, flippedColors, &b, true); take_best(&a, &b); writeBigEndian(pOut, a.high); writeBigEndian(pOut + 4, a.low); }
int outputBin(int newAddr, int data, int size) { // int zero = 0; try { if (offsetMode) // don't write data if processing Offset directive return NORMAL; // init starting address of new file if (newFile) { binAddr = newAddr; newFile = false; sectionCount++; // count sections writeBigEndian(newAddr, LONG_SIZE); // write new address to file byteCountFP = ftell(binFile); // save file position of byteCount writeBigEndian(0, LONG_SIZE); // save room for byte count (update later) fileSize += 8; // keep track of file size } // If the new address doesn't follow the previous data's address if (newAddr != binAddr) { currentFP = ftell(binFile); // save current file position // position file at previous sections byte count fseek(binFile, byteCountFP, SEEK_SET); writeBigEndian(byteCount, LONG_SIZE); // write previous sections byte count fseek(binFile, currentFP, SEEK_SET); // return file to current position byteCount = 0; // clear byte count for new section sectionCount++; // count sections writeBigEndian(newAddr, LONG_SIZE); // write new address to file byteCountFP = ftell(binFile); // save file position of byteCount writeBigEndian(0, LONG_SIZE); // save room for byte count (update later) binAddr = newAddr; fileSize += 8; // keep track of file size } // if writing WORD_SIZE or LONG_SIZE data to odd address if ((size != BYTE_SIZE) && (binAddr % 2)) { writeBigEndian(0, BYTE_SIZE); // pad with byte of 0 byteCount++; // count the byte binAddr++; // next address fileSize += 1; // keep track of file size } writeBigEndian(data, size); // write the data to the file byteCount += (int) size; // count the bytes binAddr += (int) size; // next address fileSize += size; // keep track of file size } catch( ... ) { sprintf(buffer, "ERROR: An exception occurred in routine 'outputBin'. \n"); printError(NULL, EXCEPTION, 0); return MILD_ERROR; } return NORMAL; }
static void write_cell_scalar(FILE *fp, const char *name, const double *data, int nx, int ny) { fprintf(fp, "SCALARS %s float 1\n", name); fprintf(fp, "LOOKUP_TABLE default\n"); for (int j=1; j<=ny; j++) { for (int i=1; i<=nx; i++) { int idx = i + j*(nx+2); float f = data[idx]; writeBigEndian(fp, &f, 1); } } fprintf(fp, "\n"); }
inline void writeBigEndianCastFloat(FILE* fp,const double* data,size_t count) { float buffer[256]; size_t index=0; for(;;){ size_t packed=0; for(size_t i=0;i<COUNTOF(buffer) && i+index<count;i++,packed++){ buffer[i]=(float)data[index+i]; } writeBigEndian(fp,buffer,packed); if(packed<COUNTOF(buffer))break; index+=packed; } }
void OUTPUT_VTK3D( const int &step, const double &time, const double *dens, const double *xh, const double *yh, const double *zh, // face position const int &nx, const int &ny, const int &nz) { char fname[256]; sprintf(fname, "output/fluid%04d.vtk", step); FILE* fp = fopen(fname, "wb"); if (!fp) { fprintf(stderr, "Failed to open %s\n", fname); exit(1); } // header fprintf(fp, "# vtk DataFile Version 2.0\n"); // title fprintf(fp, "Fluid Data\n"); // type fprintf(fp, "BINARY\n"); fprintf(fp, "DATASET RECTILINEAR_GRID\n"); // XYZ fprintf(fp, "DIMENSIONS %d %d %d\n", nx+1, ny+1, nz+1); fprintf(fp, "X_COORDINATES %d float\n", nx+1); for (int i=0; i<=nx; i++) { float f = xh[i]; writeBigEndian(fp, &f, 1); } fprintf(fp, "\n"); fprintf(fp, "Y_COORDINATES %d float\n", ny+1); for (int j=0; j<=ny; j++) { float f = yh[j]; writeBigEndian(fp, &f, 1); } fprintf(fp, "\n"); fprintf(fp, "Z_COORDINATES %d float\n", nz+1); for (int k=0; k<=nz; k++) { float f = zh[k]; writeBigEndian(fp, &f, 1); } fprintf(fp, "\n"); fprintf(fp, "FIELD FieldData 1\n"); fprintf(fp, "TIME 1 1 float\n"); writeBigEndianCastFloat(fp, &time, 1); // fprintf(fp, "CELL_DATA %d\n", nx*ny*nz); //write_cell_scalar(fp, "color", color, nx, ny); //write_cell_scalar(fp, "dens", dens, nx, ny, nz); //write_cell_scalar(fp, "visc", visc, nx, ny); //write_cell_scalar(fp, "pres", pres, nx, ny); // fprintf(fp, "POINT_DATA %d\n", (nx+1)*(ny+1)*(nz+1)); fprintf(fp, "SCALARS rho1 float 1\n"); fprintf(fp, "LOOKUP_TABLE default\n"); for (int k=0; k<=nz; k++) { for (int j=0; j<=ny; j++) { for (int i=0; i<=nx; i++) { float data = 0; for (int kk=k; kk<=k+1; kk++) { for (int jj=j; jj<=j+1; jj++) { for (int ii=i; ii<=i+1; ii++) { int idx = ii + jj*(nx+2) + kk*(ny+2)*(nx+2); data += dens[idx]; } } } data /= 8; writeBigEndian(fp, &data, 1); } } } fprintf(fp, "\n"); fclose(fp); printf("Saved %s\n", fname); return; }
void OUTPUT_CSV( const int &step, const double &time, const double *color, const double *dens, const double *visc, const double *pres, const double *uu, const double *vv, const double *x, const double *y, // cell position const double *xh, const double *yh, // face position const int &nx, const int &ny) { char fname[256]; sprintf(fname, "output/fluid%04d.vtk", step); FILE* fp = fopen(fname, "wb"); if (!fp) { fprintf(stderr, "Failed to open %s\n", fname); exit(1); } // header fprintf(fp, "# vtk DataFile Version 2.0\n"); // title fprintf(fp, "Fluid Data\n"); // type fprintf(fp, "BINARY\n"); fprintf(fp, "DATASET RECTILINEAR_GRID\n"); // XYZ fprintf(fp, "DIMENSIONS %d %d %d\n", nx+1, ny+1, 1); fprintf(fp, "X_COORDINATES %d float\n", nx+1); for (int i=0; i<=nx; i++) { float f = xh[i]; writeBigEndian(fp, &f, 1); } fprintf(fp, "\n"); fprintf(fp, "Y_COORDINATES %d float\n", ny+1); for (int j=0; j<=ny; j++) { float f = yh[j]; writeBigEndian(fp, &f, 1); } fprintf(fp, "\n"); fprintf(fp, "Z_COORDINATES %d float\n", 1); { float f = 0.0; writeBigEndian(fp, &f, 1); } fprintf(fp, "\n"); fprintf(fp, "FIELD FieldData 1\n"); fprintf(fp, "TIME 1 1 float\n"); writeBigEndianCastFloat(fp, &time, 1); // fprintf(fp, "CELL_DATA %d\n", nx*ny); //write_cell_scalar(fp, "color", color, nx, ny); write_cell_scalar(fp, "dens", dens, nx, ny); //write_cell_scalar(fp, "visc", visc, nx, ny); //write_cell_scalar(fp, "pres", pres, nx, ny); if (0) { // fprintf(fp, "POINT_DATA %d\n", (nx+1)*(ny+1)); fprintf(fp, "VECTORS vel float\n"); for (int j=0; j<=ny; j++) { for (int i=0; i<=nx; i++) { int idx = i + j*(nx+1); float data[] = { uu[idx], vv[idx], 0.0 }; writeBigEndian(fp, data, 3); } } fprintf(fp, "\n"); } fclose(fp); printf("Saved %s\n", fname); return; }