std::shared_ptr< Surface > flat::PdmFileReader< Surface >::operator() ( ) { # if defined DBG_FLAT_PDM_FILE_READER std::clog << "flat::PdmFileReader< Surface >::PdmFileReader\t|" << " path \"" << path << '\"' << std::endl; # endif size_t index = 0; std::ifstream stream; stream.exceptions( std::ifstream::failbit | std::ifstream::badbit ); stream.open( path.c_str() ); read_file_header( stream ); const size_t num_vertices = get<0>( vertex_field_size ) * get<1>( vertex_field_size ); std::shared_ptr< Surface > surface = Surface::create_with_size( num_vertices ); do { read_sample( stream, surface, index ); } while( ++index < num_vertices ); stream.close(); flat::SimpleRectlinearTriangulator triangulator( vertex_field_size ); triangulator( surface ); return surface; }
struct bitmap_file read_bitmap(char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) { perror(filename); exit(1); } struct bitmap_file image; image.fh = read_file_header(fp); image.ih = read_info_header(fp); image.cont_bytes = image.ih->header_size - sizeof(struct info_header); image.ih_cont = read_info_cont(fp, image, image.cont_bytes); int gap_size = image.fh->offset - sizeof(struct file_header) - sizeof(struct info_header) - image.cont_bytes; if (gap_size) { fseek(fp, gap_size, 1); } image.pixels = read_pixels(fp, image); fclose(fp); return image; }
/* * Reads the file header and the values. The return value is an array * of values that must be freed by the caller. */ static unsigned long *get_values(FILE * infile, unsigned long *n, unsigned long *nbits) { unsigned int err; unsigned long _n, _nbits; unsigned long *ary; err = read_file_header(infile, &_n, &_nbits); if (err) return NULL; ary = malloc(sizeof(*ary) * _n); if (!ary) { perror("Failed to allocate array"); return NULL; } err = read_into_array(infile, _n, ary); if (err) { free(ary); return NULL; } if (n) *n = _n; if (nbits) *nbits = _nbits; return ary; }
void BmpReader::init(FILE* f, size_t& w, size_t& h) { my_file = f; my_pixel_offset = read_file_header(f); size_t bpp, table_num; read_image_header(f, w, h, bpp, table_num); if(bpp == 8) read_color_table(f, my_color_table, table_num); else if(bpp != 24) throw InvalidFormat("unexpected bits per pixel in BMP image header"); }
//----------------------------------------------------------------// ZIPFSZipStream* ZIPFSZipStream_Open ( ZIPFSZipFile* archive, const char* entryname ) { int result; FILE* file = 0; ZIPFSZipStream* self = 0; ZIPFSZipFileEntry* entry; FileHeader fileHeader; entry = ZIPFSZipFile_FindEntry ( archive, entryname ); if ( !entry ) goto error; file = fopen ( archive->mFilename, "rb" ); if ( !file ) goto error; self = ( ZIPFSZipStream* )calloc ( 1, sizeof ( ZIPFSZipStream )); self->mFile = file; self->mEntry = entry; // finfo->entry = (( entry->symlink != NULL ) ? entry->symlink : entry ); // seek to the base of the zip file header result = fseek ( file, entry->mFileHeaderAddr, SEEK_SET ); if ( result ) goto error; // read local header result = read_file_header ( file, &fileHeader ); if ( result ) goto error; // sanity check if ( fileHeader.mCrc32 != entry->mCrc32 ) goto error; // skip the extra field, etc. result = fseek ( file, fileHeader.mNameLength + fileHeader.mExtraFieldLength, SEEK_CUR ); if ( result ) goto error; // this is the base address of the compressed file data self->mBaseAddr = ftell ( file ); // looks like all systems are go, so time to set up the buffers (or not) result = ( entry->mUncompressedSize <= ZIP_STREAM_BUFFER_MAX ) ? ZIPFSZipStream_FullyCache ( self ) : ZIPFSZipStream_InitBuffers ( self ); if ( result ) goto error; return self; error: if ( self ) { ZIPFSZipStream_Close ( self ); } return 0; }
void read_frame( xyuv::frame * frame, std::istream & istream ) { std::vector<file_format_loader> file_format_loaders { fileformat_version_0::read_header, fileformat_version_1::read_header }; io_file_header file_header; read_file_header(istream, file_header); uint16_t version = be_to_host(file_header.version); XYUV_ASSERT(version < file_format_loaders.size() && "ERROR: File format too new for this library."); file_format_loaders[version](istream, frame->format, file_header); validate_format(frame->format); frame->data.reset( new uint8_t[frame->format.size]); read_large_buffer(istream, reinterpret_cast<char*>(frame->data.get()), frame->format.size); }
IFusionSoundBuffer * load_sample (IFusionSound *sound, const char *filename) { DirectResult ret; int fd; FSBufferDescription desc; IFusionSoundBuffer *buffer; void *data; fmtChunk fmt; int len; fd = open (filename, O_RDONLY); if (fd < 0) { perror (filename); return NULL; } if (read_file_header (fd)) { close (fd); return NULL; } while (1) { char magic[4]; len = read_chunk_header (fd, magic); if (len <= 0) { fprintf (stderr, "Could not find format chunk!\n"); close (fd); return NULL; } if (magic[0] == 'f' || magic[1] == 'm' || magic[2] == 't') { if (len < sizeof(fmtChunk)) { fprintf (stderr, "Format chunk has invalid size (%d/%zu)!\n", len, sizeof(fmtChunk)); close (fd); return NULL; } if (read (fd, &fmt, sizeof(fmtChunk)) < sizeof(fmtChunk)) { fprintf (stderr, "Could not read format chunk!\n"); close (fd); return NULL; } if (lseek (fd, len - sizeof(fmtChunk), SEEK_CUR) == (off_t) -1) { fprintf (stderr, "Could not seek past chunk!\n"); close (fd); return NULL; } break; } else { if (lseek (fd, len, SEEK_CUR) == (off_t) -1) { fprintf (stderr, "Could not seek past chunk!\n"); close (fd); return NULL; } } } #ifdef WORDS_BIGENDIAN fixup_fmtchunk( &fmt ); #endif if (fmt.encoding != 1) { fprintf (stderr, "Only PCM supported, yet!\n"); close (fd); return NULL; } if (fmt.bitspersample != 16 && fmt.bitspersample != 8) { fprintf (stderr, "Only 16 or 8 bit supported, yet!\n"); close (fd); return NULL; } desc.flags = FSBDF_LENGTH | FSBDF_CHANNELS | FSBDF_SAMPLEFORMAT | FSBDF_SAMPLERATE; desc.channels = fmt.channels; desc.sampleformat = (fmt.bitspersample == 8) ? FSSF_U8 : FSSF_S16; desc.samplerate = fmt.frequency; while (1) { char magic[4]; len = read_chunk_header (fd, magic); if (len <= 0) { fprintf (stderr, "Could not find data chunk!\n"); close (fd); return NULL; } if (magic[0] == 'd' && magic[1] == 'a' && magic[2] == 't' && magic[3] == 'a') { desc.length = len / fmt.blockalign; break; } else { if (lseek (fd, len, SEEK_CUR) == (off_t) -1) { fprintf (stderr, "Could not seek past chunk!\n"); close (fd); return NULL; } } } ret = sound->CreateBuffer (sound, &desc, &buffer); if (ret) { FusionSoundError ("IFusionSound::CreateBuffer", ret); close (fd); return NULL; } buffer->Lock (buffer, &data, 0, 0); if (read (fd, data, len) < len) fprintf (stderr, "Warning: Could not read all data bytes!\n"); #ifdef WORDS_BIGENDIAN if (fmt.bitspersample == 16) fixup_sampledata (data, len); #endif close (fd); buffer->Unlock (buffer); return buffer; }
int urf_convert(int ifd, int ofd, struct urf_conv_ops *ops, void *arg) { struct urf_context ctx; struct urf_file_header file_hdr; struct urf_page_header page1_hdr; struct urf_page_header page_hdr; struct urf_error error, saved_error; ops->id[15] = '\0'; error.code = saved_error.code = 0; error.msg = saved_error.msg = NULL; ctx.ifd = ifd; ctx.ofd = ofd; ctx.error = &error; ctx.line_data = NULL; ctx.page_fill = 0xff; ctx.file_hdr = &file_hdr; ctx.page1_hdr = &page1_hdr; ctx.page_hdr = &page_hdr; if(!read_file_header(&ctx)) { goto bailout; } if (!read_page_header(&ctx)) { goto bailout; } memcpy(&page1_hdr, &page_hdr, sizeof(struct urf_page_header)); ctx.page_pixel_bytes = page1_hdr.bpp / 8; ctx.page_line_bytes = ctx.page_pixel_bytes * page1_hdr.width; ctx.page_n = 1; ctx.line_data = malloc(ctx.page_line_bytes); if (!ctx.line_data) { URF_SET_ERRNO(&ctx, "malloc"); goto bailout; } if (ops->context_setup) { if (!ops->context_setup(&ctx, arg)) { goto bailout; } } #define OP_CALL(func) op_call(ops->func, ops->id, #func, &ctx, &saved_error) #define OP_CALL_NO_ERR(func) op_call(ops->func, ops->id, #func, &ctx, NULL) if (!OP_CALL(doc_begin)) { goto bailout_context_cleanup; } do { if (!OP_CALL(page_begin)) { goto bailout_doc_end; } if (!OP_CALL(rast_begin)) { goto bailout_page_end; } ctx.line_n = 1; while (ctx.line_n <= ctx.page_hdr->height) { if (!xread(&ctx, &ctx.line_repeat, 1)) { break; } if (!read_page_line(&ctx, ops->rast_lines_raw)) { goto bailout_rast_end; } if (!OP_CALL(rast_lines_raw)) { goto bailout_rast_end; } if (!OP_CALL(rast_lines)) { goto bailout_rast_end; } } if (!OP_CALL(rast_end)) { goto bailout_page_end; } if (!OP_CALL(page_end)) { goto bailout_doc_end; } if (++ctx.page_n > file_hdr.pages) { break; } } while (read_page_header(&ctx)); if (!OP_CALL(doc_end)) { goto bailout_context_cleanup; } cleanup(&ctx, ops); return 0; bailout_rast_end: OP_CALL_NO_ERR(rast_end); bailout_page_end: OP_CALL_NO_ERR(page_end); bailout_doc_end: OP_CALL_NO_ERR(doc_end); bailout_context_cleanup: cleanup(&ctx, ops); bailout: free(ctx.line_data); #undef OP_CALL #undef OP_CALL_NO_ERR struct urf_error *last_error = saved_error.code ? &saved_error : &error; if (last_error->code > 0) { log(LOG_ERR, "%s: %s: %s\n", ops->id, last_error->msg, strerror(last_error->code)); } else { log(LOG_ERR, "%s: %s: error %d\n", ops->id, last_error->msg, last_error->code); } return last_error->code; }