void swap_endian_grids(const char *filename_in,const char *filename_out,int mode){ SID_log("Swapping endian of grids...",SID_LOG_OPEN); // Sanity check if(check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_FROM_NATIVE) && check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_FROM_NATIVE)) SID_trap_error("Invalid mode flag (%d) in swap_endian_grids().",ERROR_LOGIC,mode); // Open input and output files FILE *fp_in =NULL; FILE *fp_out=NULL; if((fp_in=fopen(filename_in,"r"))==NULL) SID_log("not present.",SID_LOG_CLOSE,filename_in); else{ if((fp_out=fopen(filename_out,"w"))==NULL) SID_trap_error("Could not open {%s} for writing.",ERROR_IO_OPEN,filename_out); // Read the needed header information and rewind int n[3]; double L[3]; int n_grids; int scheme; fread_verify(&(n[0]), sizeof(int), 1,fp_in); fread_verify(&(n[1]), sizeof(int), 1,fp_in); fread_verify(&(n[2]), sizeof(int), 1,fp_in); fread_verify(&(L[0]), sizeof(double),1,fp_in); fread_verify(&(L[1]), sizeof(double),1,fp_in); fread_verify(&(L[2]), sizeof(double),1,fp_in); fread_verify(&n_grids,sizeof(int), 1,fp_in); fread_verify(&scheme, sizeof(int), 1,fp_in); if(check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_TO_NATIVE)){ swap_endian((char *)(n), 3,sizeof(int)); swap_endian((char *)(&n_grids),1,sizeof(int)); } int grid_size=n[0]*n[1]*n[2]; rewind(fp_in); // Create a read buffer char *buffer=(char *)SID_malloc(sizeof(char)*grid_size*sizeof(fftw_real)); // Process the file rewrite_swap_endian(fp_in,fp_out,3,sizeof(int), buffer); rewrite_swap_endian(fp_in,fp_out,3,sizeof(double),buffer); rewrite_swap_endian(fp_in,fp_out,2,sizeof(int), buffer); for(int i_grid=0;i_grid<n_grids;i_grid++){ rewrite_swap_endian(fp_in,fp_out,GRID_IDENTIFIER_SIZE,sizeof(char), buffer); rewrite_swap_endian(fp_in,fp_out,grid_size, sizeof(fftw_real),buffer); } // Free the read buffer SID_free(SID_FARG buffer); // Close files fclose(fp_in); fclose(fp_out); SID_log("Done.",SID_LOG_CLOSE); } }
void swap_endian_halos_subgroups_local(const char *filename_in_root, const char *filename_out_root, const char *filename_halo_type, int snap_number, int mode) { SID_log("Swapping endian of subgroup file...", SID_LOG_OPEN); // Sanity check if(SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_FROM_NATIVE) && SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_FROM_NATIVE)) SID_exit_error("Invalid mode flag (%d) in swap_endian_halos_subgroups_local().", SID_ERROR_LOGIC, mode); // Set filenames char filename_in[SID_MAX_FILENAME_LENGTH]; char filename_out[SID_MAX_FILENAME_LENGTH]; sprintf(filename_in, "%s/%s_%03d.catalog_subgroups", filename_in_root, filename_halo_type, snap_number); sprintf(filename_out, "%s/%s_%03d.catalog_subgroups", filename_out_root, filename_halo_type, snap_number); // Open input and output files FILE *fp_in = NULL; FILE *fp_out = NULL; if((fp_in = fopen(filename_in, "r")) == NULL) SID_exit_error("Could not open {%s} for reading.", SID_ERROR_IO_OPEN, filename_in); if((fp_out = fopen(filename_out, "w")) == NULL) SID_exit_error("Could not open {%s} for writing.", SID_ERROR_IO_OPEN, filename_out); // Read the needed header information and rewind int n_subgroups; int offset_size_bytes; SID_fread_verify(&n_subgroups, sizeof(int), 1, fp_in); SID_fread_verify(&offset_size_bytes, sizeof(int), 1, fp_in); if(SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_TO_NATIVE)) { swap_endian((char *)(&n_subgroups), 1, sizeof(int)); swap_endian((char *)(&offset_size_bytes), 1, sizeof(int)); } rewind(fp_in); // Create a read buffer char *buffer = (char *)SID_malloc(sizeof(char) * offset_size_bytes); // Process the file rewrite_swap_endian(fp_in, fp_out, 2, sizeof(int), buffer); for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++) rewrite_swap_endian(fp_in, fp_out, 1, sizeof(int), buffer); for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++) rewrite_swap_endian(fp_in, fp_out, 1, offset_size_bytes, buffer); for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++) rewrite_swap_endian(fp_in, fp_out, 1, sizeof(int), buffer); // Free the read buffer SID_free(SID_FARG buffer); // Close files fclose(fp_in); fclose(fp_out); SID_log("Done.", SID_LOG_CLOSE); }
bool SgiInput::read_header() { if (!fread(&m_sgi_header.magic, sizeof(m_sgi_header.magic), 1) || !fread(&m_sgi_header.storage, sizeof(m_sgi_header.storage), 1) || !fread(&m_sgi_header.bpc, sizeof(m_sgi_header.bpc), 1) || !fread(&m_sgi_header.dimension, sizeof(m_sgi_header.dimension), 1) || !fread(&m_sgi_header.xsize, sizeof(m_sgi_header.xsize), 1) || !fread(&m_sgi_header.ysize, sizeof(m_sgi_header.ysize), 1) || !fread(&m_sgi_header.zsize, sizeof(m_sgi_header.zsize), 1) || !fread(&m_sgi_header.pixmin, sizeof(m_sgi_header.pixmin), 1) || !fread(&m_sgi_header.pixmax, sizeof(m_sgi_header.pixmax), 1) || !fread(&m_sgi_header.dummy, sizeof(m_sgi_header.dummy), 1) || !fread(&m_sgi_header.imagename, sizeof(m_sgi_header.imagename), 1)) return false; m_sgi_header.imagename[79] = '\0'; if (! fread(&m_sgi_header.colormap, sizeof(m_sgi_header.colormap), 1)) return false; //don't read dummy bytes fseek (m_fd, 404, SEEK_CUR); if (littleendian()) { swap_endian(&m_sgi_header.magic); swap_endian(&m_sgi_header.dimension); swap_endian(&m_sgi_header.xsize); swap_endian(&m_sgi_header.ysize); swap_endian(&m_sgi_header.zsize); swap_endian(&m_sgi_header.pixmin); swap_endian(&m_sgi_header.pixmax); swap_endian(&m_sgi_header.colormap); } return true; }
bool ZfileInput::read_native_scanline(int subimage, int miplevel, int y, int z, void* data) { lock_guard lock(m_mutex); if (!seek_subimage(subimage, miplevel)) return false; if (m_next_scanline > y) { // User is trying to read an earlier scanline than the one we're // up to. Easy fix: close the file and re-open. ImageSpec dummyspec; int subimage = current_subimage(); if (!close() || !open(m_filename, dummyspec) || !seek_subimage(subimage, miplevel)) return false; // Somehow, the re-open failed ASSERT(m_next_scanline == 0 && current_subimage() == subimage); } while (m_next_scanline <= y) { // Keep reading until we're read the scanline we really need gzread(m_gz, data, m_spec.width * sizeof(float)); ++m_next_scanline; } if (m_swab) swap_endian((float*)data, m_spec.width); return true; }
T readLittle(std::istream &is) { T x; is.read(reinterpret_cast<char *>(&x), sizeof(T)); x = swap_endian(x); return x; }
bool SgiInput::read_offset_tables () { int tables_size = m_sgi_header.ysize * m_sgi_header.zsize; start_tab.resize(tables_size); length_tab.resize(tables_size); if (!fread (&start_tab[0], sizeof(uint32_t), tables_size) || !fread (&length_tab[0], sizeof(uint32_t), tables_size)) return false; if (littleendian ()) { swap_endian (&length_tab[0], length_tab.size ()); swap_endian (&start_tab[0], start_tab.size()); } return true; }
bool PNGOutput::write_scanline(int y, int z, TypeDesc format, const void* data, stride_t xstride) { y -= m_spec.y; m_spec.auto_stride(xstride, format, spec().nchannels); const void* origdata = data; data = to_native_scanline(format, data, xstride, m_scratch, m_dither, y, z); if (data == origdata) { m_scratch.assign((unsigned char*)data, (unsigned char*)data + m_spec.scanline_bytes()); data = &m_scratch[0]; } // PNG specifically dictates unassociated (un-"premultiplied") alpha if (m_convert_alpha) { if (m_spec.format == TypeDesc::UINT16) deassociateAlpha((unsigned short*)data, m_spec.width, m_spec.nchannels, m_spec.alpha_channel, m_gamma); else deassociateAlpha((unsigned char*)data, m_spec.width, m_spec.nchannels, m_spec.alpha_channel, m_gamma); } // PNG is always big endian if (littleendian() && m_spec.format == TypeDesc::UINT16) swap_endian((unsigned short*)data, m_spec.width * m_spec.nchannels); if (!PNG_pvt::write_row(m_png, (png_byte*)data)) { error("PNG library error"); return false; } return true; }
int font_load(FONT *font, const char *filename) { FONT_DATA font_data; IOHANDLE file; file = engine_openfile(filename, IOFLAG_READ); if(file) { int i; io_read(file, &font_data, sizeof(FONT_DATA)); io_close(file); #if defined(CONF_ARCH_ENDIAN_BIG) swap_endian(&font_data, 2, sizeof(FONT_DATA)/2); #endif { float scale_factor_x = 1.0f/font_data.size; float scale_factor_y = 1.0f/font_data.size; float scale_factor_tex_x = 1.0f/font_data.width; float scale_factor_tex_y = 1.0f/font_data.height; for (i = 0; i < 256; i++) { float tex_x0 = font_data.characters[i].x*scale_factor_tex_x; float tex_y0 = font_data.characters[i].y*scale_factor_tex_y; float tex_x1 = (font_data.characters[i].x+font_data.characters[i].width)*scale_factor_tex_x; float tex_y1 = (font_data.characters[i].y+font_data.characters[i].height)*scale_factor_tex_y; float width = font_data.characters[i].width*scale_factor_x; float height = font_data.characters[i].height*scale_factor_y; float x_offset = font_data.characters[i].x_offset*scale_factor_x; float y_offset = font_data.characters[i].y_offset*scale_factor_y; float x_advance = (font_data.characters[i].x_advance)*scale_factor_x; font->characters[i].tex_x0 = tex_x0; font->characters[i].tex_y0 = tex_y0; font->characters[i].tex_x1 = tex_x1; font->characters[i].tex_y1 = tex_y1; font->characters[i].width = width; font->characters[i].height = height; font->characters[i].x_offset = x_offset; font->characters[i].y_offset = y_offset; font->characters[i].x_advance = x_advance; } for (i = 0; i < 256*256; i++) { font->kerning[i] = (char)font_data.kerning[i]; } } return 0; } else return -1; }
bool ZfileInput::open (const std::string &name, ImageSpec &newspec) { m_filename = name; FILE *fd = Filesystem::fopen (name, "rb"); m_gz = (fd) ? gzdopen (fileno (fd), "rb") : NULL; if (! m_gz) { if (fd) fclose (fd); error ("Could not open file \"%s\"", name.c_str()); return false; } ZfileHeader header; ASSERT (sizeof(header) == 136); gzread (m_gz, &header, sizeof(header)); if (header.magic != zfile_magic && header.magic != zfile_magic_endian) { error ("Not a valid Zfile"); return false; } m_swab = (header.magic == zfile_magic_endian); if (m_swab) { swap_endian (&header.width); swap_endian (&header.height); swap_endian ((float *)&header.worldtoscreen, 16); swap_endian ((float *)&header.worldtocamera, 16); } m_spec = ImageSpec (header.width, header.height, 1, TypeDesc::FLOAT); if (m_spec.channelnames.size() == 0) m_spec.channelnames.emplace_back("z"); else m_spec.channelnames[0] = "z"; m_spec.z_channel = 0; m_spec.attribute ("worldtoscreen", TypeDesc::TypeMatrix, (float *)&header.worldtoscreen); m_spec.attribute ("worldtocamera", TypeDesc::TypeMatrix, (float *)&header.worldtocamera); newspec = spec (); return true; }
bool SgiOutput::create_and_write_header() { sgi_pvt::SgiHeader sgi_header; sgi_header.magic = sgi_pvt::SGI_MAGIC; sgi_header.storage = sgi_pvt::VERBATIM; sgi_header.bpc = m_spec.format.size(); if (m_spec.height == 1 && m_spec.nchannels == 1) sgi_header.dimension = sgi_pvt::ONE_SCANLINE_ONE_CHANNEL; else if (m_spec.nchannels == 1) sgi_header.dimension = sgi_pvt::MULTI_SCANLINE_ONE_CHANNEL; else sgi_header.dimension = sgi_pvt::MULTI_SCANLINE_MULTI_CHANNEL; sgi_header.xsize = m_spec.width; sgi_header.ysize = m_spec.height; sgi_header.zsize = m_spec.nchannels; sgi_header.pixmin = 0; sgi_header.pixmax = (sgi_header.bpc == 1) ? 255 : 65535; sgi_header.dummy = 0; ParamValue* ip = m_spec.find_attribute("ImageDescription", TypeDesc::STRING); if (ip && ip->data()) { const char** img_descr = (const char**)ip->data(); strncpy(sgi_header.imagename, *img_descr, 80); sgi_header.imagename[79] = 0; } sgi_header.colormap = sgi_pvt::NORMAL; if (littleendian()) { swap_endian(&sgi_header.magic); swap_endian(&sgi_header.dimension); swap_endian(&sgi_header.xsize); swap_endian(&sgi_header.ysize); swap_endian(&sgi_header.zsize); swap_endian(&sgi_header.pixmin); swap_endian(&sgi_header.pixmax); swap_endian(&sgi_header.colormap); } char dummy[404] = { 0 }; if (!fwrite(&sgi_header.magic) || !fwrite(&sgi_header.storage) || !fwrite(&sgi_header.bpc) || !fwrite(&sgi_header.dimension) || !fwrite(&sgi_header.xsize) || !fwrite(&sgi_header.ysize) || !fwrite(&sgi_header.zsize) || !fwrite(&sgi_header.pixmin) || !fwrite(&sgi_header.pixmax) || !fwrite(&sgi_header.dummy) || !fwrite(sgi_header.imagename, 1, 80) || !fwrite(&sgi_header.colormap) || !fwrite(dummy, 404, 1)) { error("Error writing to \"%s\"", m_filename); return false; } return true; }
void *CDataFileReader::GetDataImpl(int Index, int Swap) { if(!m_pDataFile) { return 0; } // load it if needed if(!m_pDataFile->m_ppDataPtrs[Index]) { // fetch the data size int DataSize = GetDataSize(Index); #if defined(CONF_ARCH_ENDIAN_BIG) int SwapSize = DataSize; #endif if(m_pDataFile->m_Header.m_Version == 4) { // v4 has compressed data void *pTemp = (char *)mem_alloc(DataSize, 1); unsigned long UncompressedSize = m_pDataFile->m_Info.m_pDataSizes[Index]; unsigned long s; dbg_msg("datafile", "loading data index=%d size=%d uncompressed=%d", Index, DataSize, UncompressedSize); m_pDataFile->m_ppDataPtrs[Index] = (char *)mem_alloc(UncompressedSize, 1); // read the compressed data io_seek(m_pDataFile->m_File, m_pDataFile->m_DataStartOffset+m_pDataFile->m_Info.m_pDataOffsets[Index], IOSEEK_START); io_read(m_pDataFile->m_File, pTemp, DataSize); // decompress the data, TODO: check for errors s = UncompressedSize; uncompress((Bytef*)m_pDataFile->m_ppDataPtrs[Index], &s, (Bytef*)pTemp, DataSize); // ignore_convention #if defined(CONF_ARCH_ENDIAN_BIG) SwapSize = s; #endif // clean up the temporary buffers mem_free(pTemp); } else { // load the data dbg_msg("datafile", "loading data index=%d size=%d", Index, DataSize); m_pDataFile->m_ppDataPtrs[Index] = (char *)mem_alloc(DataSize, 1); io_seek(m_pDataFile->m_File, m_pDataFile->m_DataStartOffset+m_pDataFile->m_Info.m_pDataOffsets[Index], IOSEEK_START); io_read(m_pDataFile->m_File, m_pDataFile->m_ppDataPtrs[Index], DataSize); } #if defined(CONF_ARCH_ENDIAN_BIG) if(Swap && SwapSize) swap_endian(m_pDataFile->m_ppDataPtrs[Index], sizeof(int), SwapSize/sizeof(int)); #endif } return m_pDataFile->m_ppDataPtrs[Index]; }
bool read_n(FILE * file, T * data, size_t n) { if (fread(data, sizeof(*data), n, file) != n) return false; if (is_bigendian()) { for (size_t i = 0; i < n; ++i) data[i] = swap_endian(data[i]); } return true; }
bool FitsInput::read_native_scanline (int y, int z, void *data) { // we return true just to support 0x0 images if (!m_naxes) return true; std::vector<unsigned char> data_tmp (m_spec.scanline_bytes ()); long scanline_off = (m_spec.height - y) * m_spec.scanline_bytes (); fseek (m_fd, scanline_off, SEEK_CUR); size_t n = fread (&data_tmp[0], 1, m_spec.scanline_bytes(), m_fd); if (n != m_spec.scanline_bytes()) { if (feof (m_fd)) error ("Hit end of file unexpectedly"); else error ("read error"); return false; // Read failed } // in FITS image data is stored in big-endian so we have to switch to // little-endian on little-endian machines if (littleendian ()) { if (m_spec.format == TypeDesc::USHORT) swap_endian ((unsigned short*)&data_tmp[0], data_tmp.size () / sizeof (unsigned short)); else if (m_spec.format == TypeDesc::UINT) swap_endian ((unsigned int*)&data_tmp[0], data_tmp.size () / sizeof (unsigned int)); else if (m_spec.format == TypeDesc::FLOAT) swap_endian ((float*)&data_tmp[0], data_tmp.size () / sizeof (float)); else if (m_spec.format == TypeDesc::DOUBLE) swap_endian ((double*)&data_tmp[0], data_tmp.size () / sizeof (double)); } memcpy (data, &data_tmp[0], data_tmp.size ()); // after reading scanline we set file pointer to the start of image data fsetpos (m_fd, &m_filepos); return true; };
bool BmpFileHeader::read_header(FILE* fd) { if (!fread(fd, &magic) || !fread(fd, &fsize) || !fread(fd, &res1) || !fread(fd, &res2) || !fread(fd, &offset)) { return false; } if (bigendian()) swap_endian(); return true; }
bool PicFileHeader::read_header (FILE* fd) { int byte_count = 0; byte_count += fread (this, 1, sizeof (PicFileHeader), fd); // Check if we're running on a little endian processor if (littleendian ()) swap_endian(); return (byte_count == sizeof (PicFileHeader)); }
bool SgiInput::read_native_scanline (int y, int z, void *data) { if (y < 0 || y > m_spec.height) return false; y = m_spec.height - y - 1; int bpc = m_sgi_header.bpc; std::vector<std::vector<unsigned char> > channeldata (m_spec.nchannels); if (m_sgi_header.storage == sgi_pvt::RLE) { // reading and uncompressing first channel (red in RGBA images) for (int c = 0; c < m_spec.nchannels; ++c) { int off = y + c*m_spec.height; // offset for this scanline/channel int scanline_offset = start_tab[off]; int scanline_length = length_tab[off]; channeldata[c].resize (m_spec.width * bpc); uncompress_rle_channel (scanline_offset, scanline_length, &(channeldata[c][0])); } } else { // non-RLE case -- just read directly into our channel data for (int c = 0; c < m_spec.nchannels; ++c) { int off = y + c*m_spec.height; // offset for this scanline/channel int scanline_offset = sgi_pvt::SGI_HEADER_LEN + off * m_spec.width * bpc; fseek (m_fd, scanline_offset, SEEK_SET); channeldata[c].resize (m_spec.width * bpc); if (! fread (&(channeldata[c][0]), 1, m_spec.width * bpc)) return false; } } if (m_spec.nchannels == 1) { // If just one channel, no interleaving is necessary, just memcpy memcpy (data, &(channeldata[0][0]), channeldata[0].size()); } else { unsigned char *cdata = (unsigned char *)data; for (int x = 0; x < m_spec.width; ++x) { for (int c = 0; c < m_spec.nchannels; ++c) { *cdata++ = channeldata[c][x*bpc]; if (bpc == 2) *cdata++ = channeldata[c][x*bpc+1]; } } } // Swap endianness if needed if (bpc == 2 && littleendian()) swap_endian ((unsigned short *)data, m_spec.width*m_spec.nchannels); return true; }
bool BmpFileHeader::write_header(FILE* fd) { if (bigendian()) swap_endian(); if (!fwrite(fd, &magic) || !fwrite(fd, &fsize) || !fwrite(fd, &res1) || !fwrite(fd, &res2) || !fwrite(fd, &offset)) { return false; } return true; }
int datafile_add_data_swapped(DATAFILE_OUT *df, int size, void *data) { #if defined(CONF_ARCH_ENDIAN_BIG) void *swapped = mem_alloc(size, 1); /* temporary buffer that we use duing compression */ int index; mem_copy(swapped, data, size); swap_endian(&swapped, sizeof(int), size/sizeof(int)); index = datafile_add_data(df, size, swapped); mem_free(swapped); return index; #else return datafile_add_data(df, size, data); #endif }
unsigned int read_stream_float(T &var) { std::vector<unsigned char> data; // assign type T from stream for(unsigned int i = 0; i < sizeof(T); ++i) { if(available() == END_OF_STREAM) return END_OF_STREAM; data.push_back(buff.at(pos++)); } if(swap) swap_endian(data); var = atof((char *) data.data()); return SUCCESS; }
int CDataFileWriter::AddDataSwapped(int Size, void *pData) { #if defined(CONF_ARCH_ENDIAN_BIG) void *pSwapped = mem_alloc(Size, 1); // temporary buffer that we use duing compression int Index; mem_copy(pSwapped, pData, Size); swap_endian(&pSwapped, sizeof(int), Size/sizeof(int)); Index = AddData(Size, Swapped); mem_free(pSwapped); return Index; #else return AddData(Size, pData); #endif }
bool write_n(FILE * file, const T * data, size_t n) { if (is_bigendian()) { // This is not optimized for speed, however it's only big endian writing.... bool okay = true; for (size_t i = 0; i < n; ++i) { T swapped = swap_endian(data[i]); okay &= fwrite(&swapped, sizeof(swapped), 1, file) == 1; } return okay; } else { return fwrite(data, sizeof(*data), n, file) == n; } }
int CDataFileWriter::AddDataSwapped(int Size, void *pData) { dbg_assert(Size%sizeof(int) == 0, "incorrect boundary"); #if defined(CONF_ARCH_ENDIAN_BIG) void *pSwapped = mem_alloc(Size, 1); // temporary buffer that we use during compression mem_copy(pSwapped, pData, Size); swap_endian(pSwapped, sizeof(int), Size/sizeof(int)); int Index = AddData(Size, pSwapped); mem_free(pSwapped); return Index; #else return AddData(Size, pData); #endif }
static void *datafile_get_data_impl(DATAFILE *df, int index, int swap) { /* load it if needed */ if(!df->data_ptrs[index]) { /* fetch the data size */ int datasize = datafile_get_datasize(df, index); int swapsize = datasize; if(df->header.version == 4) { /* v4 has compressed data */ void *temp = (char *)mem_alloc(datasize, 1); unsigned long uncompressed_size = df->info.data_sizes[index]; unsigned long s; dbg_msg("datafile", "loading data index=%d size=%d uncompressed=%d", index, datasize, uncompressed_size); df->data_ptrs[index] = (char *)mem_alloc(uncompressed_size, 1); /* read the compressed data */ io_seek(df->file, df->data_start_offset+df->info.data_offsets[index], IOSEEK_START); io_read(df->file, temp, datasize); /* decompress the data, TODO: check for errors */ s = uncompressed_size; uncompress((Bytef*)df->data_ptrs[index], &s, (Bytef*)temp, datasize); swapsize = s; /* clean up the temporary buffers */ mem_free(temp); } else { /* load the data */ dbg_msg("datafile", "loading data index=%d size=%d", index, datasize); df->data_ptrs[index] = (char *)mem_alloc(datasize, 1); io_seek(df->file, df->data_start_offset+df->info.data_offsets[index], IOSEEK_START); io_read(df->file, df->data_ptrs[index], datasize); } #if defined(CONF_ARCH_ENDIAN_BIG) if(swap && swapsize) swap_endian(df->data_ptrs[index], sizeof(int), swapsize/sizeof(int)); #endif } return df->data_ptrs[index]; }
bool DibInformationHeader::read_header(FILE* fd) { if (!fread(fd, &size)) return false; if (size == WINDOWS_V3 || size == WINDOWS_V4 || size == WINDOWS_V5) { if (!fread(fd, &width) || !fread(fd, &height) || !fread(fd, &cplanes) || !fread(fd, &bpp) || !fread(fd, &compression) || !fread(fd, &isize) || !fread(fd, &hres) || !fread(fd, &vres) || !fread(fd, &cpalete) || !fread(fd, &important)) { return false; } if (size == WINDOWS_V4 || size == WINDOWS_V5) { if (!fread(fd, &red_mask) || !fread(fd, &blue_mask) || !fread(fd, &green_mask) || !fread(fd, &alpha_mask) || !fread(fd, &cs_type) || !fread(fd, &red_x) || !fread(fd, &red_y) || !fread(fd, &red_z) || !fread(fd, &green_x) || !fread(fd, &green_y) || !fread(fd, &green_z) || !fread(fd, &blue_x) || !fread(fd, &blue_y) || !fread(fd, &blue_z) || !fread(fd, &gamma_x) || !fread(fd, &gamma_y) || !fread(fd, &gamma_z)) { return false; } } if (size == WINDOWS_V5) { if (!fread(fd, &intent) || !fread(fd, &profile_data) || !fread(fd, &profile_size) || !fread(fd, &reserved)) { return false; } } } else if (size == OS2_V1) { // some of theses fields are smaller then in WINDOWS_Vx headers, // so we use hardcoded counts width = 0; height = 0; if (!fread(fd, &width, 2) || !fread(fd, &height, 2) || !fread(fd, &cplanes) || !fread(fd, &bpp)) { return false; } } if (bigendian()) swap_endian(); return true; }
bool TIFFInput::valid_file (const std::string &filename) const { FILE *file = Filesystem::fopen (filename, "r"); if (! file) return false; // needs to be able to open unsigned short magic[2] = { 0, 0 }; fread (magic, sizeof(unsigned short), 2, file); fclose (file); if (magic[0] != TIFF_LITTLEENDIAN && magic[0] != TIFF_BIGENDIAN) return false; // not the right byte order if ((magic[0] == TIFF_LITTLEENDIAN) != littleendian()) swap_endian (&magic[1], 1); return (magic[1] == 42 /* Classic TIFF */ || magic[1] == 43 /* Big TIFF */); }
bool DibInformationHeader::write_header(FILE* fd) { if (bigendian()) swap_endian(); if (!fwrite(fd, &size) || !fwrite(fd, &width) || !fwrite(fd, &height) || !fwrite(fd, &cplanes) || !fwrite(fd, &bpp) || !fwrite(fd, &compression) || !fwrite(fd, &isize) || !fwrite(fd, &hres) || !fwrite(fd, &vres) || !fwrite(fd, &cpalete) || !fwrite(fd, &important)) { return false; } return (true); }
unsigned int read_stream(T &var) { std::vector<unsigned char> data; // assign type T from stream unsigned int width = sizeof(T); for(unsigned int i = 0; i < width; ++i) { if(available() == END_OF_STREAM) return END_OF_STREAM; data.push_back(buff.at(pos++)); } if(swap) swap_endian(data); var = 0; for(unsigned int i = 0; i < width; ++i) var |= (data.at(i) << (8 * ((width - 1) - i))); return SUCCESS; }
bool SgiOutput::write_scanline (int y, int z, TypeDesc format, const void *data, stride_t xstride) { y = m_spec.height - y - 1; data = to_native_scanline (format, data, xstride, m_scratch, m_dither, y, z); // In SGI format all channels are saved to file separately: firsty all // channel 1 scanlines are saved, then all channel2 scanlines are saved // and so on. // // Note that since SGI images are pretty archaic and most probably // people won't be too picky about full flexibility writing them, we // content ourselves with only writing uncompressed data, and don't // attempt to write with RLE encoding. int bpc = m_spec.format.size(); // bytes per channel std::vector<unsigned char> channeldata (m_spec.width * bpc); for (int c = 0; c < m_spec.nchannels; ++c) { unsigned char *cdata = (unsigned char *)data + c*bpc; for (int x = 0; x < m_spec.width; ++x) { channeldata[x*bpc] = cdata[0]; if (bpc == 2) channeldata[x*bpc+1] = cdata[1]; cdata += m_spec.nchannels * bpc; // advance to next pixel } if (bpc == 2 && littleendian()) swap_endian ((unsigned short *)&channeldata[0], m_spec.width); long scanline_offset = sgi_pvt::SGI_HEADER_LEN + (c * m_spec.height + y) * m_spec.width * bpc; fseek (m_fd, scanline_offset, SEEK_SET); if (!fwrite (&channeldata[0], 1, m_spec.width * bpc)) { return false; } } return true; }
static void convert_pack_bits (T *data, int nvals) { const int BITS_FROM = sizeof(T)*8; T *in = data; T *out = in - 1; // because we'll increment first time through int bitstofill = 0; // Invariant: the next value to convert is *in. We're going to write // the result of the conversion starting at *out, which still has // bitstofill bits left before moving on to the next slot. for (int i = 0; i < nvals; ++i) { // Grab the next value and convert it T val = bit_range_convert<BITS_FROM,BITS_TO> (*in++); // If we have no more bits to fill in the slot, move on to the // next slot. if (bitstofill == 0) { ++out; *out = 0; // move to next slot and clear its bits bitstofill = BITS_FROM; // all bits are for the taking } if (bitstofill >= BITS_TO) { // we can fit the whole val in this slot *out |= val << (bitstofill - BITS_TO); bitstofill -= BITS_TO; // printf ("\t\t%d bits left\n", bitstofill); } else { // not enough bits -- will need to split across slots int bitsinnext = BITS_TO - bitstofill; *out |= val >> bitsinnext; val &= (1 << bitsinnext) - 1; // mask out bits we saved ++out; *out = 0; // move to next slot and clear its bits *out |= val << (BITS_FROM - bitsinnext); bitstofill = BITS_FROM - bitsinnext; } } // Because we filled in a big-endian way, swap bytes if we need to if (littleendian()) swap_endian (data, nvals); }
int main( int argc, char* argv[] ) { unsigned long tottime, starttime; double filetime; size_t counter; SKP_int32 k, args, totPackets, totActPackets, ret; SKP_int16 nBytes; double sumBytes, sumActBytes, avg_rate, act_rate, nrg; SKP_uint8 payload[ MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES ]; SKP_int16 in[ FRAME_LENGTH_MS * MAX_API_FS_KHZ * MAX_INPUT_FRAMES ]; char speechInFileName[ 150 ], bitOutFileName[ 150 ]; FILE *bitOutFile, *speechInFile; SKP_int32 encSizeBytes; void *psEnc; #ifdef _SYSTEM_IS_BIG_ENDIAN SKP_int16 nBytes_LE; #endif /* default settings */ SKP_int32 API_fs_Hz = 24000; SKP_int32 max_internal_fs_Hz = 0; SKP_int32 targetRate_bps = 25000; SKP_int32 smplsSinceLastPacket, packetSize_ms = 20; SKP_int32 frameSizeReadFromFile_ms = 20; SKP_int32 packetLoss_perc = 0; #if LOW_COMPLEXITY_ONLY SKP_int32 complexity_mode = 0; #else SKP_int32 complexity_mode = 2; #endif SKP_int32 DTX_enabled = 0, INBandFEC_enabled = 0, quiet = 0; SKP_SILK_SDK_EncControlStruct encControl; // Struct for input to encoder SKP_SILK_SDK_EncControlStruct encStatus; // Struct for status of encoder if( argc < 3 ) { print_usage( argv ); exit( 0 ); } /* get arguments */ args = 1; strcpy( speechInFileName, argv[ args ] ); args++; strcpy( bitOutFileName, argv[ args ] ); args++; while( args < argc ) { if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_API" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &API_fs_Hz ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-Fs_maxInternal" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &max_internal_fs_Hz ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-packetlength" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &packetSize_ms ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-rate" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &targetRate_bps ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-loss" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &packetLoss_perc ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-complexity" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &complexity_mode ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-inbandFEC" ) == 0 ) { sscanf( argv[ args + 1 ], "%d", &INBandFEC_enabled ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-DTX") == 0 ) { sscanf( argv[ args + 1 ], "%d", &DTX_enabled ); args += 2; } else if( SKP_STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-quiet" ) == 0 ) { quiet = 1; args++; } else { printf( "Error: unrecognized setting: %s\n\n", argv[ args ] ); print_usage( argv ); exit( 0 ); } } /* If no max internal is specified, set to minimum of API fs and 24 kHz */ if( max_internal_fs_Hz == 0 ) { max_internal_fs_Hz = 24000; if( API_fs_Hz < max_internal_fs_Hz ) { max_internal_fs_Hz = API_fs_Hz; } } /* Print options */ if( !quiet ) { printf("********** Silk Encoder (Fixed Point) v %s ********************\n", SKP_Silk_SDK_get_version()); printf("********** Compiled for %d bit cpu ******************************* \n", (int)sizeof(void*) * 8 ); printf( "Input: %s\n", speechInFileName ); printf( "Output: %s\n", bitOutFileName ); printf( "API sampling rate: %d Hz\n", API_fs_Hz ); printf( "Maximum internal sampling rate: %d Hz\n", max_internal_fs_Hz ); printf( "Packet interval: %d ms\n", packetSize_ms ); printf( "Inband FEC used: %d\n", INBandFEC_enabled ); printf( "DTX used: %d\n", DTX_enabled ); printf( "Complexity: %d\n", complexity_mode ); printf( "Target bitrate: %d bps\n", targetRate_bps ); } /* Open files */ speechInFile = fopen( speechInFileName, "rb" ); if( speechInFile == NULL ) { printf( "Error: could not open input file %s\n", speechInFileName ); exit( 0 ); } bitOutFile = fopen( bitOutFileName, "wb" ); if( bitOutFile == NULL ) { printf( "Error: could not open output file %s\n", bitOutFileName ); exit( 0 ); } /* Add Silk header to stream */ { static const char Silk_header[] = "#!SILK_V3"; fwrite( Silk_header, sizeof( char ), strlen( Silk_header ), bitOutFile ); } /* Create Encoder */ ret = SKP_Silk_SDK_Get_Encoder_Size( &encSizeBytes ); if( ret ) { printf( "\nError: SKP_Silk_create_encoder returned %d\n", ret ); exit( 0 ); } psEnc = malloc( encSizeBytes ); /* Reset Encoder */ ret = SKP_Silk_SDK_InitEncoder( psEnc, &encStatus ); if( ret ) { printf( "\nError: SKP_Silk_reset_encoder returned %d\n", ret ); exit( 0 ); } /* Set Encoder parameters */ encControl.API_sampleRate = API_fs_Hz; encControl.maxInternalSampleRate = max_internal_fs_Hz; encControl.packetSize = ( packetSize_ms * API_fs_Hz ) / 1000; encControl.packetLossPercentage = packetLoss_perc; encControl.useInBandFEC = INBandFEC_enabled; encControl.useDTX = DTX_enabled; encControl.complexity = complexity_mode; encControl.bitRate = ( targetRate_bps > 0 ? targetRate_bps : 0 ); if( API_fs_Hz > MAX_API_FS_KHZ * 1000 || API_fs_Hz < 0 ) { printf( "\nError: API sampling rate = %d out of range, valid range 8000 - 48000 \n \n", API_fs_Hz ); exit( 0 ); } tottime = 0; totPackets = 0; totActPackets = 0; smplsSinceLastPacket = 0; sumBytes = 0.0; sumActBytes = 0.0; smplsSinceLastPacket = 0; while( 1 ) { /* Read input from file */ counter = fread( in, sizeof( SKP_int16 ), ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000, speechInFile ); #ifdef _SYSTEM_IS_BIG_ENDIAN swap_endian( in, counter ); #endif if( ( SKP_int )counter < ( ( frameSizeReadFromFile_ms * API_fs_Hz ) / 1000 ) ) { break; } /* max payload size */ nBytes = MAX_BYTES_PER_FRAME * MAX_INPUT_FRAMES; starttime = GetHighResolutionTime(); /* Silk Encoder */ ret = SKP_Silk_SDK_Encode( psEnc, &encControl, in, (SKP_int16)counter, payload, &nBytes ); if( ret ) { printf( "\nSKP_Silk_Encode returned %d", ret ); } tottime += GetHighResolutionTime() - starttime; /* Get packet size */ packetSize_ms = ( SKP_int )( ( 1000 * ( SKP_int32 )encControl.packetSize ) / encControl.API_sampleRate ); smplsSinceLastPacket += ( SKP_int )counter; if( ( ( 1000 * smplsSinceLastPacket ) / API_fs_Hz ) == packetSize_ms ) { /* Sends a dummy zero size packet in case of DTX period */ /* to make it work with the decoder test program. */ /* In practice should be handled by RTP sequence numbers */ totPackets++; sumBytes += nBytes; nrg = 0.0; for( k = 0; k < ( SKP_int )counter; k++ ) { nrg += in[ k ] * (double)in[ k ]; } if( ( nrg / ( SKP_int )counter ) > 1e3 ) { sumActBytes += nBytes; totActPackets++; } /* Write payload size */ #ifdef _SYSTEM_IS_BIG_ENDIAN nBytes_LE = nBytes; swap_endian( &nBytes_LE, 1 ); fwrite( &nBytes_LE, sizeof( SKP_int16 ), 1, bitOutFile ); #else fwrite( &nBytes, sizeof( SKP_int16 ), 1, bitOutFile ); #endif /* Write payload */ fwrite( payload, sizeof( SKP_uint8 ), nBytes, bitOutFile ); smplsSinceLastPacket = 0; if( !quiet ) { fprintf( stderr, "\rPackets encoded: %d", totPackets ); } } } /* Write dummy because it can not end with 0 bytes */ nBytes = -1; /* Write payload size */ fwrite( &nBytes, sizeof( SKP_int16 ), 1, bitOutFile ); /* Free Encoder */ free( psEnc ); fclose( speechInFile ); fclose( bitOutFile ); filetime = totPackets * 1e-3 * packetSize_ms; avg_rate = 8.0 / packetSize_ms * sumBytes / totPackets; act_rate = 8.0 / packetSize_ms * sumActBytes / totActPackets; if( !quiet ) { printf( "\nFile length: %.3f s", filetime ); printf( "\nTime for encoding: %.3f s (%.3f%% of realtime)", 1e-6 * tottime, 1e-4 * tottime / filetime ); printf( "\nAverage bitrate: %.3f kbps", avg_rate ); printf( "\nActive bitrate: %.3f kbps", act_rate ); printf( "\n\n" ); } else { /* print time and % of realtime */ printf("%.3f %.3f %d ", 1e-6 * tottime, 1e-4 * tottime / filetime, totPackets ); /* print average and active bitrates */ printf( "%.3f %.3f \n", avg_rate, act_rate ); } return 0; }