UInt8* ConvertPixels<PixelFormatType_LA8, PixelFormatType_RGBA4>(const UInt8* start, const UInt8* end, UInt8* dst) { UInt16* ptr = reinterpret_cast<UInt16*>(dst); while (start < end) { UInt16 l = static_cast<UInt16>(c8to4(start[0])); *ptr = (l << 12) | (l << 8) | (l << 4) | c8to4(start[1]); #ifdef NAZARA_BIG_ENDIAN SwapBytes(ptr, sizeof(UInt16)); #endif ptr++; start += 2; } return dst; }
UInt8* ConvertPixels<PixelFormatType_RGB5A1, PixelFormatType_RGB8>(const UInt8* start, const UInt8* end, UInt8* dst) { while (start < end) { UInt16 pixel = *reinterpret_cast<const UInt16*>(start); #ifdef NAZARA_BIG_ENDIAN SwapBytes(&pixel, sizeof(UInt16)); #endif *dst++ = c5to8((pixel & 0xF800) >> 11); *dst++ = c5to8((pixel & 0x07C0) >> 6); *dst++ = c5to8((pixel & 0x003E) >> 1); start += 2; } return dst; }
UInt8* ConvertPixels<PixelFormatType_RGBA4, PixelFormatType_BGR8>(const UInt8* start, const UInt8* end, UInt8* dst) { while (start < end) { UInt16 pixel = *reinterpret_cast<const UInt16*>(start); #ifdef NAZARA_BIG_ENDIAN SwapBytes(&pixel, sizeof(UInt16)); #endif *dst++ = c4to8((pixel & 0x00F0) >> 4); *dst++ = c4to8((pixel & 0x0F00) >> 8); *dst++ = c4to8((pixel & 0xF000) >> 12); start += 2; } return dst; }
word input_word(int nHandle, word *p) { CARRIERDATA_STRUCT* pCarrier; /* local carrier */ unsigned long data[2]; pCarrier = GetCarrier(nHandle); if(pCarrier == NULL) return 0; if( p ) { /* place address to read word from in data [0]; */ data[0] = (unsigned long) p; /* pram3 = function: 1=read8bits,2=read16bits */ read( pCarrier->nCarrierDeviceHandle, &data[0], 2 ); return( SwapBytes( (word)data[1] ) ); } return((word)0); }
void output_word(int nHandle, word *p, word v) { CARRIERDATA_STRUCT* pCarrier; /* local carrier */ unsigned long data[2]; pCarrier = GetCarrier(nHandle); if(pCarrier == NULL) return; if( p ) { /* place address to write word in data [0]; */ data[0] = (unsigned long) p; /* place value to write @ address data [1]; */ data[1] = (unsigned long) SwapBytes( v ); /* pram3 = function: 1=write8bits,2=write16bits */ write( pCarrier->nCarrierDeviceHandle, &data[0], 2 ); } }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Rc4Output // // Outputs the requested number of bytes from the RC4 stream /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Rc4Output ( Rc4Context* Context, void* Buffer, uint32_t Size ) { uint32_t n; for( n=0; n<Size; n++ ) { Context->i = ( Context->i + 1 ) % 256; Context->j = ( Context->j + Context->S[Context->i] ) % 256; SwapBytes( Context->S[Context->i], Context->S[Context->j] ); ((uint8_t*)Buffer)[n] = Context->S[ (Context->S[Context->i] + Context->S[Context->j]) % 256 ]; } }
UInt8* ConvertPixels<PixelFormatType_LA8, PixelFormatType_RGB5A1>(const UInt8* start, const UInt8* end, UInt8* dst) { UInt16* ptr = reinterpret_cast<UInt16*>(dst); while (start < end) { UInt16 l = static_cast<UInt16>(c8to5(start[0])); *ptr = (l << 11) | (l << 6) | (l << 1) | ((start[1] > 0xF) ? 1 : 0); #ifdef NAZARA_BIG_ENDIAN SwapBytes(ptr, sizeof(UInt16)); #endif ptr++; start += 2; } return reinterpret_cast<UInt8*>(ptr); }
UInt8* ConvertPixels<PixelFormatType_A8, PixelFormatType_RGB5A1>(const UInt8* start, const UInt8* end, UInt8* dst) { UInt16* ptr = reinterpret_cast<UInt16*>(dst); while (start < end) { *ptr = (static_cast<UInt16>(0x1F) << 11) | (static_cast<UInt16>(0x1F) << 6) | (static_cast<UInt16>(0x1F) << 1) | ((*start > 0xF) ? 1 : 0); // > 128 #ifdef NAZARA_BIG_ENDIAN SwapBytes(ptr, sizeof(UInt16)); #endif ptr++; start += 1; } return reinterpret_cast<UInt8*>(ptr); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Rc4Xor // // XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting // and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Rc4Xor ( Rc4Context* Context, void* InBuffer, void* OutBuffer, uint32_t Size ) { uint32_t n; for( n=0; n<Size; n++ ) { Context->i = ( Context->i + 1 ) % 256; Context->j = ( Context->j + Context->S[Context->i] ) % 256; SwapBytes( Context->S[Context->i], Context->S[Context->j] ); ((uint8_t*)OutBuffer)[n] = ((uint8_t*)InBuffer)[n] ^ ( Context->S[ (Context->S[Context->i] + Context->S[Context->j]) % 256 ] ); } }
UInt8* ConvertPixels<PixelFormatType_RGBA4, PixelFormatType_L8>(const UInt8* start, const UInt8* end, UInt8* dst) { while (start < end) { UInt16 pixel = *reinterpret_cast<const UInt16*>(start); #ifdef NAZARA_BIG_ENDIAN SwapBytes(&pixel, sizeof(UInt16)); #endif UInt16 r = c4to8((pixel & 0xF000) >> 12); UInt16 g = c4to8((pixel & 0x0F00) >> 8); UInt16 b = c4to8((pixel & 0x00F0) >> 4); *dst++ = static_cast<UInt8>(r * 0.3f + g * 0.59f + b * 0.11f); start += 2; } return dst; }
int sendData(const void *buffer, size_t length, int swaptype) { register char *bptr=(char *)buffer; int *tmp = NULL; register int written; register int nbytes=length; if (coSimLibData.verbose>3) fprintf(stderr,"coSimClient sending %d Bytes to Socket %d\n", (int)length, coSimLibData.soc); if (coSimLibData.swap && swaptype) { // bad, but a fast hack ... tmp = (int *)calloc(length, sizeof(int)); memcpy(tmp, buffer, length*sizeof(int)); SwapBytes(tmp, length); bptr = (char *)tmp; } while (nbytes>0) { written = write(coSimLibData.soc,(void *) bptr,nbytes); if (written < 0) { fprintf(stderr,"coSimClient error: write returned %d\n",written); perror("coSimClient sendData error: "); if (tmp) free(tmp); return -1; } nbytes-=written; bptr+=written; if (written==0) { if (tmp) free(tmp); return -2; } } if (coSimLibData.verbose>3) fprintf(stderr,"coSimClient sent %d Bytes\n",(int)length); if (tmp) free(tmp); return length; }
UInt8* ConvertPixels<PixelFormatType_RGB5A1, PixelFormatType_LA8>(const UInt8* start, const UInt8* end, UInt8* dst) { while (start < end) { UInt16 pixel = *reinterpret_cast<const UInt16*>(start); #ifdef NAZARA_BIG_ENDIAN SwapBytes(&pixel, sizeof(UInt16)); #endif UInt8 r = c5to8((pixel & 0xF800) >> 11); UInt8 g = c5to8((pixel & 0x07C0) >> 6); UInt8 b = c5to8((pixel & 0x003E) >> 1); *dst++ = static_cast<UInt8>(r * 0.3f + g * 0.59f + b * 0.11f); *dst++ = static_cast<UInt8>((pixel & 0x1)*0xFF); start += 2; } return dst; }
int recvData(void *buffer, size_t length, int swaptype) { register char *bptr=(char*) buffer; register int nread; register int nbytes=length; if (coSimLibData.verbose>3) fprintf(stderr," coSimClient waiting for %d Bytes from Socket %d\n", (int)length,coSimLibData.soc); while (nbytes>0) { nread = read(coSimLibData.soc, (void*)bptr, nbytes); if (nread < 0) { fprintf(stderr,"coSimClient error: received %d Bytes\n",nread); perror("coSimClient recvData error: "); assert(-1); return -1; } nbytes-=nread; bptr+=nread; if (nread==0) break; } if (nbytes) { fprintf(stderr," error: received 0 Bytes while %d left\n",nbytes); perror("coSimClient recvData error: "); return -2; } else { if (coSimLibData.verbose>3) fprintf(stderr,"coSimClient received %d Bytes\n",(int)length); if (coSimLibData.swap && swaptype) SwapBytes(buffer, length/4); return length; } }
bool OW_VALUE_STREAM_CLASS::StreamPatternTo(DATA_TF_CLASS& data_transfer) // DESCRIPTION : Stream a OW pattern to the data_transfer. // PRECONDITIONS : // POSTCONDITIONS : // EXCEPTIONS : // NOTES : //<<=========================================================================== { UINT row_start = start_valueM; UINT row_inc = rows_incrementM; UINT col_inc = columns_incrementM; UINT rows_same = rows_sameM; UINT columns_same = columns_sameM; bool byte_swap = IsByteSwapRequired(); if (rows_same == 0) { rows_same++; } if (columns_same == 0) { columns_same++; } // get hold of the current bits allocated if (bits_allocatedM == 16) { UINT16 value; // generate a simple test pattern const UINT nr_of_OT_in_OW = 2; if ((rowsM * columnsM * nr_of_OT_in_OW) != lengthM) { if (loggerM_ptr) { loggerM_ptr->text(LOG_ERROR, 1, "OW data generation failure - rows (%d) * columns (%d) not equal length (%d)", rowsM, columnsM, lengthM); } return false; } UINT32 length = columnsM; // columns = row length (NP) UINT16 *data_ptr = new UINT16 [length]; for (UINT rows = 0; rows < rowsM; rows++) { value = (UINT16)row_start; for (UINT columns = 0; columns < columnsM; columns++) { data_ptr[columns] = value; if ((columns + 1) % columns_same == 0) { if ((value == 0xFFFF) && (col_inc != 0)) { value = 0; } else { value = (UINT16)(value + col_inc); } } } // swap the data bytes if required if (byte_swap) { SwapBytes((BYTE*) data_ptr, length * 2); } // write data into buffer data_transfer.writeBinary((BYTE*) data_ptr, length * 2); if ((rows + 1) % rows_same == 0) { if ((row_start == 0xFFFF) && (row_inc !=0)) { row_start = 0; } else { row_start += row_inc; } } } // cleanup delete data_ptr; } else if (bits_allocatedM == 8) { BYTE value; // generate a simple test pattern - values have previously been range checked if ((rowsM * columnsM) != lengthM) { if (loggerM_ptr) { loggerM_ptr->text(LOG_ERROR, 1, "OW data generation failure - rows (%d) * columns (%d) not equal length (%d)", rowsM, columnsM, lengthM); } return false; } UINT32 length = columnsM; // columns = row length (NP) BYTE *data_ptr = new BYTE [length]; for (UINT rows = 0; rows < rowsM; rows++) { value = (BYTE)row_start; for (UINT columns = 0; columns < columnsM; columns++) { data_ptr[columns] = value; if ((columns + 1) % columns_same == 0) { if ((value == 0xFF) && (col_inc != 0)) { value = 0; } else { value = (BYTE)(value + col_inc); } } } // swap the data bytes if required if (byte_swap) { SwapBytes(data_ptr, length); } // write data into buffer data_transfer.writeBinary(data_ptr, length); if ((rows + 1) % rows_same == 0) { if ((row_start == 0xFFFF) && (row_inc !=0)) { row_start = 0; } else { row_start += row_inc; } } } // check for odd length - last fragment! if (lengthM & 0x1) { data_ptr[0] = 0x00; // write data into buffer data_transfer.writeBinary(data_ptr, 1); } // cleanup delete data_ptr; } // return result return true; }
bool dpx::Header::WriteOffsetData(OutStream* io) { // calculate the number of elements this->CalculateNumberOfElements(); // write the image offset const long FIELD2 = 4; // offset to image in header if(io->Seek(FIELD2, OutStream::kStart) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->imageOffset); if(io->Write(&this->imageOffset, sizeof(U32)) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->imageOffset); // write the file size const long FIELD4 = 16; // offset to total image file size in header if(io->Seek(FIELD4, OutStream::kStart) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->fileSize); if(io->Write(&this->fileSize, sizeof(U32)) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->fileSize); // write the number of elements const long FIELD19 = 770; // offset to number of image elements in header if(io->Seek(FIELD19, OutStream::kStart) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->numberOfElements); if(io->Write(&this->numberOfElements, sizeof(U16)) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->numberOfElements); // write the image offsets const long FIELD21_12 = 808; // offset to image offset in image element data structure const long IMAGE_STRUCTURE = 72; // sizeof the image data structure int i; for(i = 0; i < MAX_ELEMENTS; i++) { // only write if there is a defined image description if(this->chan[i].descriptor == kUndefinedDescriptor) continue; // seek to the image offset entry in each image element if(io->Seek((FIELD21_12 + (IMAGE_STRUCTURE * i)), OutStream::kStart) == false) return false; // write if(this->RequiresByteSwap()) SwapBytes(this->chan[i].dataOffset); if(io->Write(&this->chan[i].dataOffset, sizeof(U32)) == false) return false; if(this->RequiresByteSwap()) SwapBytes(this->chan[i].dataOffset); } return true; }
_xai_packet* generatePacketFromeDataOne(_xai_packet_param_data* ctrl_param_data){ if (ctrl_param_data == NULL) return NULL; _xai_packet* ctrl_data = generatePacket(); char* payload = malloc(1000); memset(payload,0,1000); if(!payload) return NULL; //big uint16_t big_len = CFSwapInt16(ctrl_param_data->data_len); //存入固定格式 param_to_packet_helper(payload, &ctrl_param_data->data_type, _XPP_CD_TYPE_START, _XPP_CD_TYPE_END); param_to_packet_helper(payload, &big_len, _XPP_CD_LEN_START, _XPP_CD_LEN_END); ctrl_data->pre_load = malloc(_XPPS_CD_FIXED_ALL); memcpy(ctrl_data->pre_load, payload, _XPPS_CD_FIXED_ALL); int pos = _XPPS_CD_FIXED_ALL; if (NULL != ctrl_param_data->data) { /*type 大端 小端转化*/ void* in_data = malloc(ctrl_param_data->data_len); memset(in_data, 0, ctrl_param_data->data_len); memcpy(in_data, ctrl_param_data->data, ctrl_param_data->data_len); if (XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN == ctrl_param_data->data_type) { SwapBytes(in_data, ctrl_param_data->data_len); } if (XAI_DATA_TYPE_BIN_APSN == ctrl_param_data->data_type) { SwapBytes(in_data, ctrl_param_data->data_len); } if (XAI_DATA_TYPE_BIN_LUID == ctrl_param_data->data_type) { SwapBytes(in_data, ctrl_param_data->data_len); } ctrl_data->data_load = malloc(ctrl_param_data->data_len); memset(ctrl_data->data_load, 0, ctrl_param_data->data_len); memcpy(ctrl_data->data_load, in_data, ctrl_param_data->data_len); //param_to_packet_helper(payload, in_data, 0 , 0 + ctrl_param_data->data_len); memcpy(payload+pos, in_data, ctrl_param_data->data_len); pos += ctrl_param_data->data_len; free(in_data); in_data = NULL; }else{ ctrl_data->data_load = NULL; } ctrl_data->all_load = malloc(pos); memcpy(ctrl_data->all_load, payload, pos); ctrl_data->size = pos; free(payload); return ctrl_data; }
UCHAR* TextureTools::LoadDDS(CHAR* file, ImageInfoDDS &info) { const INT ddsHeightOffset = 12; const INT ddsWidthOffset = 16; const INT ddsLinearSizeOffset = 20; const INT ddsMipMapNumOffset = 28; const INT ddsFourCCOffset = 84; const INT ddsImageDataOffset = 128; EndianType e = GetEndian(); BOOL byteSwap = FALSE; if (e == ENDIAN_Big) byteSwap = TRUE; FILE *fp = fopen(file, "rb"); if (fp == nullptr) return nullptr; CHAR imageID[4]; fread(imageID, 1, 4, fp); if(strncmp(imageID, "DDS ", 4) != 0) { fclose(fp); return FALSE; } UINT dwHeight = 0, dwWidth = 0, dwLinearSize, dwMipMaps = 0, dwFourCC = 0; fseek(fp, ddsHeightOffset, SEEK_SET); fread(&dwHeight, sizeof(UINT), 1, fp); if(byteSwap == TRUE) SwapBytes((CHAR*)&dwHeight, sizeof(UINT)); fseek(fp, ddsWidthOffset, SEEK_SET); fread(&dwWidth, sizeof(UINT), 1, fp); if(byteSwap == TRUE) SwapBytes((CHAR*)&dwWidth, sizeof(UINT)); fseek(fp, ddsLinearSizeOffset, SEEK_SET); fread(&dwLinearSize, sizeof(UINT), 1, fp); if(byteSwap == TRUE) SwapBytes((CHAR*)&dwLinearSize, sizeof(UINT)); fseek(fp, ddsMipMapNumOffset, SEEK_SET); fread(&dwMipMaps, sizeof(UINT), 1, fp); if(byteSwap == TRUE) SwapBytes((CHAR*)&dwMipMaps, sizeof(UINT)); fseek(fp, ddsFourCCOffset, SEEK_SET); fread(&dwFourCC, sizeof(UINT), 1, fp); if(byteSwap == TRUE) SwapBytes((CHAR*)&dwFourCC, sizeof(UINT)); if(dwLinearSize == 0) dwLinearSize = dwHeight * dwWidth; if(dwLinearSize <= 0) { fclose(fp); return nullptr; } info.m_numMipMaps = dwMipMaps; info.m_width = dwWidth; info.m_height = dwHeight; INT mipFactor = 0; switch(dwFourCC) { case DS_FOURCC_DXT1: mipFactor = 2; info.m_components = 3; info.m_type = DDS_DXT1; break; case DS_FOURCC_DXT3: mipFactor = 4; info.m_components = 4; info.m_type = DDS_DXT3; break; case DS_FOURCC_DXT5: mipFactor = 4; info.m_components = 4; info.m_type = DDS_DXT5; break; default: fclose(fp); return nullptr; break; } INT totalSize = 0; // Take into account multiple mipmaps. if(dwMipMaps > 1) totalSize = dwLinearSize * mipFactor; else totalSize = dwLinearSize; UCHAR* image = nullptr; image = new UCHAR[totalSize * sizeof(UCHAR)]; if(image != nullptr) { fseek(fp, ddsImageDataOffset, SEEK_SET); fread(image, 1, totalSize, fp); } fclose(fp); return image; };
inline void TestSwap64() { UNIT_ASSERT_EQUAL(0x1234567890abcdefULL, SwapBytes((ui64)ULL(0xefcdab9078563412))); }
void LittleEndian32(void *Value) { SwapBytes(Value, 0, 2); SwapBytes(Value, 1, 3); }
DVT_STATUS OD_VALUE_STREAM_CLASS::Compare(LOG_MESSAGE_CLASS *message_ptr, OD_VALUE_STREAM_CLASS &refOfStream) // DESCRIPTION : Compare two OF file streams. // PRECONDITIONS : // POSTCONDITIONS : // EXCEPTIONS : // NOTES : //<<=========================================================================== { char message[256]; // read the file headers bool resultSrc = ReadFileHeader(); bool resultRef = refOfStream.ReadFileHeader(); if ((!resultSrc) || (!resultRef)) return MSG_ERROR; // display the headers for debugging DisplayHeaderDetail(); refOfStream.DisplayHeaderDetail(); // check the length bool srcOk = false; bool refOk = false; if (GetLength(&srcOk) != refOfStream.GetLength(&refOk)) return MSG_NOT_EQUAL; if ((srcOk == false) || (refOk == false)) return MSG_NOT_EQUAL; // now check the file contents - endianess // - may need byte swap to be able to compare the data bool byteSwap = true; if (fs_codeM & TS_LITTLE_ENDIAN) { if (refOfStream.fs_codeM & TS_LITTLE_ENDIAN) { byteSwap = false; } } else if (fs_codeM & TS_BIG_ENDIAN) { if (refOfStream.fs_codeM & TS_BIG_ENDIAN) { byteSwap = false; } } if (byteSwap) { // need to byte swap either source or reference before comparison sprintf (message, "Source OD File data being byte swapped before comparison with Reference OD data"); if (message_ptr) message_ptr->AddMessage(VAL_RULE_D_OTHER_18, message); } UINT32 lengthToCompare = (UINT32)lengthM; // read the data in blocks and compare UINT32 blockLength = MAX_OV_BLOCK_LENGTH; BYTE *srcData = new BYTE [blockLength]; BYTE *refData = new BYTE [blockLength]; // compare the data DVT_STATUS status = MSG_EQUAL; while ((lengthToCompare > 0) && (status == MSG_EQUAL)) { // get the next block length if (lengthToCompare < blockLength) blockLength = lengthToCompare; // read data into buffer UINT32 srcLengthRead = readBinary(srcData, blockLength); if (srcLengthRead == blockLength) { UINT32 refLengthRead = refOfStream.readBinary(refData, blockLength); if (refLengthRead == blockLength) { // check for byte swap if (byteSwap) { SwapBytes(refData, blockLength); } // now compare the data if (!byteCompare(srcData, refData, blockLength)) { status = MSG_NOT_EQUAL; } // move to next block lengthToCompare -= blockLength; } else { status = MSG_ERROR; } } else { status = MSG_ERROR; } } // cleanup delete srcData; delete refData; // return status return status; }
bool OD_VALUE_STREAM_CLASS::StreamPatternTo(DATA_TF_CLASS& data_transfer) // DESCRIPTION : Stream a OF pattern to the data_transfer. // PRECONDITIONS : // POSTCONDITIONS : // EXCEPTIONS : // NOTES : //<<=========================================================================== { float value; UINT row_start = start_valueM; UINT row_inc = rows_incrementM; UINT col_inc = columns_incrementM; UINT rows_same = rows_sameM; UINT columns_same = columns_sameM; bool byte_swap = IsByteSwapRequired(); const UINT nr_of_OT_in_OF = 4; // generate a simple test pattern if ((rowsM * columnsM * nr_of_OT_in_OF) != lengthM) { if (loggerM_ptr) { loggerM_ptr->text(LOG_ERROR, 1, "OD data generation failure - rows (%d) * columns (%d) not equal length (%d)", rowsM, columnsM, lengthM); } return false; } UINT32 length = columnsM; // columns = row length (NP) float *data_ptr = new float [length]; if (rows_same == 0) { rows_same++; } if (columns_same == 0) { columns_same++; } for (UINT rows = 0; rows < rowsM; rows++) { value = (float)row_start; for (UINT columns = 0; columns < columnsM; columns++) { data_ptr[columns] = value; if ((columns + 1) % columns_same == 0 ) { if ((value == 0xFFFF) && (col_inc != 0)) { value = 0; } else { value += (float)col_inc; } } } // swap the data bytes if required if (byte_swap) { SwapBytes((BYTE*) data_ptr, length * 4); } // write data into buffer data_transfer.writeBinary((BYTE*) data_ptr, length * 4); if ((rows + 1) % rows_same == 0) { if ((row_start == 0xFFFF) && (row_inc !=0)) { row_start = 0; } else { row_start += row_inc; } } } // cleanup delete data_ptr; // return result return true; }
ALUTAPI ALvoid ALUTAPIENTRY alutLoadWAVMemory(ALbyte *memory,ALenum *format,ALvoid **data,ALsizei *size,ALsizei *freq) { WAVChunkHdr_Struct ChunkHdr; WAVFmtExHdr_Struct FmtExHdr; WAVFileHdr_Struct FileHdr; WAVSmplHdr_Struct SmplHdr; WAVFmtHdr_Struct FmtHdr; int i; ALbyte *Stream; *format=AL_FORMAT_MONO16; *data=NULL; *size=0; *freq=22050; if (memory) { Stream=memory; if (Stream) { memcpy(&FileHdr,Stream,sizeof(WAVFileHdr_Struct)); Stream+=sizeof(WAVFileHdr_Struct); SwapWords(&FileHdr.Size); FileHdr.Size=((FileHdr.Size+1)&~1)-4; while ((FileHdr.Size!=0)&&(memcpy(&ChunkHdr,Stream,sizeof(WAVChunkHdr_Struct)))) { Stream+=sizeof(WAVChunkHdr_Struct); SwapWords(&ChunkHdr.Size); if ((ChunkHdr.Id[0] == 'f') && (ChunkHdr.Id[1] == 'm') && (ChunkHdr.Id[2] == 't') && (ChunkHdr.Id[3] == ' ')) { memcpy(&FmtHdr,Stream,sizeof(WAVFmtHdr_Struct)); SwapBytes(&FmtHdr.Format); if (FmtHdr.Format==0x0001) { SwapBytes(&FmtHdr.Channels); SwapBytes(&FmtHdr.BitsPerSample); SwapWords(&FmtHdr.SamplesPerSec); SwapBytes(&FmtHdr.BlockAlign); *format=(FmtHdr.Channels==1? (FmtHdr.BitsPerSample==8?AL_FORMAT_MONO8:AL_FORMAT_MONO16): (FmtHdr.BitsPerSample==8?AL_FORMAT_STEREO8:AL_FORMAT_STEREO16)); *freq=FmtHdr.SamplesPerSec; Stream+=ChunkHdr.Size; } else { memcpy(&FmtExHdr,Stream,sizeof(WAVFmtExHdr_Struct)); Stream+=ChunkHdr.Size; } } else if ((ChunkHdr.Id[0] == 'd') && (ChunkHdr.Id[1] == 'a') && (ChunkHdr.Id[2] == 't') && (ChunkHdr.Id[3] == 'a')) { if (FmtHdr.Format==0x0001) { *size=ChunkHdr.Size; if(*data == NULL){ *data=malloc(ChunkHdr.Size + 31); memset(*data,0,ChunkHdr.Size+31); } else{ realloc(*data,ChunkHdr.Size + 31); memset(*data,0,ChunkHdr.Size+31); } if (*data) { memcpy(*data,Stream,ChunkHdr.Size); memset(((char *)*data)+ChunkHdr.Size,0,31); Stream+=ChunkHdr.Size; if (FmtHdr.BitsPerSample == 16) { for (i = 0; i < (ChunkHdr.Size / 2); i++) { SwapBytes(&(*(unsigned short **)data)[i]); } } } } else if (FmtHdr.Format==0x0011) { //IMA ADPCM } else if (FmtHdr.Format==0x0055) { //MP3 WAVE } } else if ((ChunkHdr.Id[0] == 's') && (ChunkHdr.Id[1] == 'm') && (ChunkHdr.Id[2] == 'p') && (ChunkHdr.Id[3] == 'l')) { memcpy(&SmplHdr,Stream,sizeof(WAVSmplHdr_Struct)); Stream+=ChunkHdr.Size; } else Stream+=ChunkHdr.Size; Stream+=ChunkHdr.Size&1; FileHdr.Size-=(((ChunkHdr.Size+1)&~1)+8); } } } }
bool dpx::Header::Validate() { // check magic cookie if(!this->ValidMagicCookie(this->magicNumber)) return false; // determine if bytes needs to be swapped around if(this->DetermineByteSwap(this->magicNumber)) { // File information SwapBytes(this->imageOffset); SwapBytes(this->fileSize); SwapBytes(this->dittoKey); SwapBytes(this->genericSize); SwapBytes(this->industrySize); SwapBytes(this->userSize); SwapBytes(this->encryptKey); // Image information SwapBytes(this->imageOrientation); SwapBytes(this->numberOfElements); SwapBytes(this->pixelsPerLine); SwapBytes(this->linesPerElement); for(int i = 0; i < MAX_ELEMENTS; i++) { SwapBytes(this->chan[i].dataSign); SwapBytes(this->chan[i].lowData); SwapBytes(this->chan[i].lowQuantity); SwapBytes(this->chan[i].highData); SwapBytes(this->chan[i].highQuantity); SwapBytes(this->chan[i].descriptor); SwapBytes(this->chan[i].transfer); SwapBytes(this->chan[i].colorimetric); SwapBytes(this->chan[i].bitDepth); SwapBytes(this->chan[i].packing); SwapBytes(this->chan[i].encoding); SwapBytes(this->chan[i].dataOffset); SwapBytes(this->chan[i].endOfLinePadding); SwapBytes(this->chan[i].endOfImagePadding); } // Image Origination information SwapBytes(this->xOffset); SwapBytes(this->yOffset); SwapBytes(this->xCenter); SwapBytes(this->yCenter); SwapBytes(this->xOriginalSize); SwapBytes(this->yOriginalSize); SwapBytes(this->border[0]); SwapBytes(this->border[1]); SwapBytes(this->border[2]); SwapBytes(this->border[3]); SwapBytes(this->aspectRatio[0]); SwapBytes(this->aspectRatio[1]); // Motion Picture Industry Specific SwapBytes(this->framePosition); SwapBytes(this->sequenceLength); SwapBytes(this->heldCount); SwapBytes(this->frameRate); SwapBytes(this->shutterAngle); // Television Industry Specific SwapBytes(this->timeCode); SwapBytes(this->userBits); SwapBytes(this->interlace); SwapBytes(this->fieldNumber); SwapBytes(this->videoSignal); SwapBytes(this->zero); SwapBytes(this->horizontalSampleRate); SwapBytes(this->verticalSampleRate); SwapBytes(this->temporalFrameRate); SwapBytes(this->timeOffset); SwapBytes(this->gamma); SwapBytes(this->blackLevel); SwapBytes(this->blackGain); SwapBytes(this->breakPoint); SwapBytes(this->whiteLevel); SwapBytes(this->integrationTimes); } return true; }
VBoolean VPackData (VRepnKind repn, size_t nels, VPointer unpacked, VPackOrder packed_order, size_t *length, VPointer *packed, VBoolean *alloced) { VPackOrder unpacked_order; size_t unpacked_elsize = VRepnSize (repn) * CHAR_BIT; size_t packed_elsize = VRepnPrecision (repn); size_t packed_length = (nels * packed_elsize + 7) / 8; /* If space for the packed data was supplied, ensure there's enough of it: */ if (! alloced && packed_length > *length) { VWarning ("VPackData: Insufficient space for packed data"); return FALSE; } *length = packed_length; /* Determine the present machine's internal byte order: */ unpacked_order = MachineByteOrder (); /* If the desired byte order matches that of the present machine's, and the unpacked and packed data element sizes are identical, just return the unpacked data: */ if (unpacked_order == packed_order && unpacked_elsize == packed_elsize) { if (alloced) { *packed = unpacked; *alloced = FALSE; } else if (unpacked != packed) memcpy (*packed, unpacked, packed_length); return TRUE; } /* Allocate a buffer for the packed data if none was provided: */ if (alloced) { *packed = VMalloc (packed_length); *alloced = TRUE; } /* Pack data elements into the buffer: */ if (unpacked_elsize == packed_elsize) { /* If the packed and unpacked are the same size, do a straight copy: */ if (unpacked != *packed) memcpy (*packed, unpacked, packed_length); /* Swap bytes if necessary: */ if (packed_order != unpacked_order && packed_elsize > 8) SwapBytes (nels, packed_elsize / 8, (char *) *packed); } else if (packed_elsize == 1) { /* If the elements are VBits, this packs them: */ VPackBits (nels, packed_order, (VBit *) unpacked, (char *) *packed); } else /* Packing multi-byte integers or floats is currently not supported: */ VError ("VPackData: Packing %s from %d to %d bits is not supported", VRepnName (repn), unpacked_elsize, packed_elsize); return TRUE; }
int GModel::readSTL(const std::string &name, double tolerance) { FILE *fp = Fopen(name.c_str(), "rb"); if(!fp){ Msg::Error("Unable to open file '%s'", name.c_str()); return 0; } // store triplets of points for each solid found in the file std::vector<std::vector<SPoint3> > points; SBoundingBox3d bbox; // "solid", or binary data header char buffer[256]; if(!fgets(buffer, sizeof(buffer), fp)){ fclose(fp); return 0; } bool binary = strncmp(buffer, "solid", 5) && strncmp(buffer, "SOLID", 5); // ASCII STL if(!binary){ points.resize(1); while(!feof(fp)) { // "facet normal x y z" or "endsolid" if(!fgets(buffer, sizeof(buffer), fp)) break; if(!strncmp(buffer, "endsolid", 8) || !strncmp(buffer, "ENDSOLID", 8)){ // "solid" if(!fgets(buffer, sizeof(buffer), fp)) break; if(!strncmp(buffer, "solid", 5) || !strncmp(buffer, "SOLID", 5)){ points.resize(points.size() + 1); // "facet normal x y z" if(!fgets(buffer, sizeof(buffer), fp)) break; } } // "outer loop" if(!fgets(buffer, sizeof(buffer), fp)) break; // "vertex x y z" for(int i = 0; i < 3; i++){ if(!fgets(buffer, sizeof(buffer), fp)) break; char s1[256]; double x, y, z; if(sscanf(buffer, "%s %lf %lf %lf", s1, &x, &y, &z) != 4) break; SPoint3 p(x, y, z); points.back().push_back(p); bbox += p; } // "endloop" if(!fgets(buffer, sizeof(buffer), fp)) break; // "endfacet" if(!fgets(buffer, sizeof(buffer), fp)) break; } } // check if we could parse something bool empty = true; for(unsigned int i = 0; i < points.size(); i++){ if(points[i].size()){ empty = false; break; } } if(empty) points.clear(); // binary STL (we also try to read in binary mode if the header told // us the format was ASCII but we could not read any vertices) if(binary || empty){ if(binary) Msg::Info("Mesh is in binary format"); else Msg::Info("Wrong ASCII header or empty file: trying binary read"); rewind(fp); while(!feof(fp)) { char header[80]; if(!fread(header, sizeof(char), 80, fp)) break; unsigned int nfacets = 0; size_t ret = fread(&nfacets, sizeof(unsigned int), 1, fp); bool swap = false; if(nfacets > 100000000){ Msg::Info("Swapping bytes from binary file"); swap = true; SwapBytes((char*)&nfacets, sizeof(unsigned int), 1); } if(ret && nfacets){ points.resize(points.size() + 1); char *data = new char[nfacets * 50 * sizeof(char)]; ret = fread(data, sizeof(char), nfacets * 50, fp); if(ret == nfacets * 50){ for(unsigned int i = 0; i < nfacets; i++) { float *xyz = (float *)&data[i * 50 * sizeof(char)]; if(swap) SwapBytes((char*)xyz, sizeof(float), 12); for(int j = 0; j < 3; j++){ SPoint3 p(xyz[3 + 3 * j], xyz[3 + 3 * j + 1], xyz[3 + 3 * j + 2]); points.back().push_back(p); bbox += p; } } } delete [] data; } } } std::vector<GFace*> faces; for(unsigned int i = 0; i < points.size(); i++){ if(points[i].empty()){ Msg::Error("No facets found in STL file for solid %d", i); fclose(fp); return 0; } if(points[i].size() % 3){ Msg::Error("Wrong number of points (%d) in STL file for solid %d", points[i].size(), i); fclose(fp); return 0; } Msg::Info("%d facets in solid %d", points[i].size() / 3, i); // create face GFace *face = new discreteFace(this, getMaxElementaryNumber(2) + 1); faces.push_back(face); add(face); } // create triangles using unique vertices double eps = norm(SVector3(bbox.max(), bbox.min())) * tolerance; std::vector<MVertex*> vertices; for(unsigned int i = 0; i < points.size(); i++) for(unsigned int j = 0; j < points[i].size(); j++) vertices.push_back(new MVertex(points[i][j].x(), points[i][j].y(), points[i][j].z())); MVertexPositionSet pos(vertices); std::set<MFace,Less_Face> unique; int nbDuplic = 0; for(unsigned int i = 0; i < points.size(); i ++){ for(unsigned int j = 0; j < points[i].size(); j += 3){ MVertex *v[3]; for(int k = 0; k < 3; k++){ double x = points[i][j + k].x(); double y = points[i][j + k].y(); double z = points[i][j + k].z(); v[k] = pos.find(x, y, z, eps); } MFace mf (v[0], v[1], v[2]); if (unique.find(mf) == unique.end()){ faces[i]->triangles.push_back(new MTriangle(v[0], v[1], v[2])); unique.insert(mf); } else{ nbDuplic++; } } } if (nbDuplic) Msg::Warning("%d duplicate triangles in STL file", nbDuplic); _associateEntityWithMeshVertices(); _storeVerticesInEntities(vertices); // will delete unused vertices fclose(fp); return 1; }
inline void TestSwap32() { UNIT_ASSERT_EQUAL(0x12345678, SwapBytes(0x78563412)); }
bool cineon::Header::Validate() { // check magic cookie if (!this->ValidMagicCookie(this->magicNumber)) return false; // determine if bytes needs to be swapped around if (this->DetermineByteSwap(this->magicNumber)) { // File information SwapBytes(this->imageOffset); SwapBytes(this->genericSize); SwapBytes(this->industrySize); SwapBytes(this->userSize); SwapBytes(this->fileSize); // Image information for (int i = 0; i < MAX_ELEMENTS; i++) { SwapBytes(this->chan[i].pixelsPerLine); SwapBytes(this->chan[i].linesPerElement); SwapBytes(this->chan[i].lowData); SwapBytes(this->chan[i].lowQuantity); SwapBytes(this->chan[i].highData); SwapBytes(this->chan[i].highQuantity); SwapBytes(this->chan[i].bitDepth); } SwapBytes(this->whitePoint[0]); SwapBytes(this->whitePoint[1]); SwapBytes(this->redPrimary[0]); SwapBytes(this->redPrimary[1]); SwapBytes(this->greenPrimary[0]); SwapBytes(this->greenPrimary[1]); SwapBytes(this->bluePrimary[0]); SwapBytes(this->bluePrimary[1]); SwapBytes(this->endOfLinePadding); SwapBytes(this->endOfImagePadding); // Image Origination information SwapBytes(this->xOffset); SwapBytes(this->yOffset); SwapBytes(this->xDevicePitch); SwapBytes(this->yDevicePitch); SwapBytes(this->gamma); // Motion Picture Industry Specific SwapBytes(this->prefix); SwapBytes(this->count); SwapBytes(this->framePosition); SwapBytes(this->frameRate); } return true; }
inline void TestSwap16() { UNIT_ASSERT_EQUAL((ui16)0x1234, SwapBytes((ui16)0x3412)); }
_xai_packet_param_data* generateParamDataOneFromData(void* data,int size){ if (size < _XPPS_CD_FIXED_ALL) { printf("XAI - CTRL DATA FIXED DATA SIZE ENOUGH"); return NULL; } _xai_packet_param_data* ctrl_param_data = generatePacketParamData(); //fixed packet_to_param_helper(&ctrl_param_data->data_type, data, _XPP_CD_TYPE_START, _XPP_CD_TYPE_END); packet_to_param_helper(&ctrl_param_data->data_len, data, _XPP_CD_LEN_START, _XPP_CD_LEN_END); ctrl_param_data->data_len = CFSwapInt16(ctrl_param_data->data_len); if (size < _XPPS_CD_FIXED_ALL + ctrl_param_data->data_len) { purgePacketParamData(ctrl_param_data); printf("XAI - CTRL DATA UNFIXED DATA SIZE ENOUGH"); return NULL; } /*type 大端 小端转化*/ void* in_data = malloc(ctrl_param_data->data_len); memset(in_data, 0, ctrl_param_data->data_len); memcpy(in_data, data+_XPP_CD_DATA_START, ctrl_param_data->data_len); //packet_to_param_helper(in_data, data, _XPP_CD_DATA_START, _XPP_CD_DATA_START+ctrl_param_data->data_len); if (XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN == ctrl_param_data->data_type) { SwapBytes(in_data, ctrl_param_data->data_len); } if (XAI_DATA_TYPE_BIN_APSN == ctrl_param_data->data_type) { SwapBytes(in_data, ctrl_param_data->data_len); } if (XAI_DATA_TYPE_BIN_LUID == ctrl_param_data->data_type) { SwapBytes(in_data, ctrl_param_data->data_len); } //unfixed ctrl_param_data->data = malloc(ctrl_param_data->data_len); memset(ctrl_param_data->data, 0, ctrl_param_data->data_len); //packet_to_param_helper(ctrl_param_data->data, data, _XPP_CD_DATA_START, _XPP_CD_DATA_START+ctrl_param_data->data_len); packet_to_param_helper(ctrl_param_data->data, in_data, 0, ctrl_param_data->data_len); free(in_data); return ctrl_param_data; }
bool LoadPPM(const char *pathname, unsigned char *&pixels, int &width, int &height, int &maxval) { FILE *fp; if ( ( fp = fopen( pathname, "rb" ) ) == NULL ) { printf( "fail to open file %s!\n", pathname ); return false; } //read header read_comment(fp); char magic[16]; fscanf(fp, "%s", magic); if (strcmp(magic, "P6")!=0) { printf("LoadPPM : invalid magic number in %s!\n", pathname); return false; } fgetc(fp); read_comment(fp); fscanf(fp, "%d", &width); if (width <= 0) { printf("LoadPPM : invalid width in %s!\n", pathname); return false; } fgetc(fp); read_comment(fp); fscanf(fp, "%d", &height); if (height <= 0) { printf("LoadPPM : invalid height in %s!\n", pathname); return false; } fgetc(fp); read_comment(fp); fscanf(fp, "%d", &maxval); if (maxval < 1 || maxval > 65535) { printf("LoadPPM : invalid maxval in %s\n", pathname); return false; } fgetc(fp); //read pixels int pixDepth = ( maxval < 256 ? 1 : 2 ); int numPixels = width * height * 3; if (pixels == NULL) { pixels = new unsigned char[pixDepth*numPixels]; } size_t n = fread(pixels, sizeof(unsigned char)*pixDepth, numPixels, fp); fclose(fp); if (n == numPixels) { //PPM is Most Significant Byte First for 16 bit //convert to Little-Endian for PC if (pixDepth == 2) SwapBytes(pixels, numPixels, pixDepth); return true; } else { printf("LoadPGM : fail to read pixels in %s!\n", pathname); return false; } }