static GstBuffer * gst_directsound_sink_payload (GstBaseAudioSink * sink, GstBuffer * buf) { if (gst_directsound_sink_is_spdif_format ((GstDirectSoundSink *) sink)) { gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec); GstBuffer *out; if (framesize <= 0) return NULL; out = gst_buffer_new_and_alloc (framesize); if (!gst_audio_iec61937_payload (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf), GST_BUFFER_DATA (out), GST_BUFFER_SIZE (out), &sink->ringbuffer->spec)) { gst_buffer_unref (out); return NULL; } gst_buffer_copy_metadata (out, buf, GST_BUFFER_COPY_ALL); /* Fix endianness */ _swab ((gchar *) GST_BUFFER_DATA (buf), (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf)); return out; } else { return gst_buffer_ref (buf); } }
int CAacDecoder::GetSpdifBurstPayload(BYTE *pBuffer, DWORD BufferSize) const { if (pBuffer == NULL || m_pAdtsFrame == NULL) return 0; if (m_pAdtsFrame->GetRawDataBlockNum() != 0) { TRACE(TEXT("Invalid no_raw_data_blocks_in_frame (%d)\r\n"), m_pAdtsFrame->GetRawDataBlockNum()); return 0; } const int FrameSize = m_pAdtsFrame->GetFrameLength(); const int DataBurstSize = (FrameSize + 1) & ~1; if (BufferSize < (DWORD)DataBurstSize) return 0; _swab(pointer_cast<char*>(const_cast<BYTE*>(m_pAdtsFrame->GetData())), pointer_cast<char*>(pBuffer), FrameSize & ~1); if (FrameSize & 1) { pBuffer[FrameSize - 1] = 0; pBuffer[FrameSize] = m_pAdtsFrame->GetAt(FrameSize - 1); } return DataBurstSize; }
wchar_t *ReadString(FILE *file, wchar_t *lpwszDest, int nDestLength, int nCodePage) { char *lpDest = (char*)xf_malloc((nDestLength+1)*3); //UTF-8, up to 3 bytes per char support memset(lpDest, 0, (nDestLength+1)*3); memset(lpwszDest, 0, nDestLength*sizeof(wchar_t)); if ((nCodePage == CP_UNICODE) || (nCodePage == CP_REVERSEBOM)) { if (!fgetws(lpwszDest, nDestLength, file)) { xf_free(lpDest); return nullptr; } if (nCodePage == CP_REVERSEBOM) { _swab((char*)lpwszDest, (char*)lpwszDest, nDestLength*sizeof(wchar_t)); wchar_t *Ch = lpwszDest; int nLength = Min(static_cast<int>(wcslen(lpwszDest)), nDestLength); while (*Ch) { if (*Ch == L'\n') { *(Ch+1) = 0; break; } Ch++; } int nNewLength = Min(static_cast<int>(wcslen(lpwszDest)), nDestLength); fseek(file, (nNewLength-nLength)*sizeof(wchar_t), SEEK_CUR); } } else if (nCodePage == CP_UTF8) { if (fgets(lpDest, nDestLength*3, file)) MultiByteToWideChar(CP_UTF8, 0, lpDest, -1, lpwszDest, nDestLength); else { xf_free(lpDest); return nullptr; } } else if (nCodePage != -1) { if (fgets(lpDest, nDestLength, file)) MultiByteToWideChar(nCodePage, 0, lpDest, -1, lpwszDest, nDestLength); else { xf_free(lpDest); return nullptr; } } xf_free(lpDest); return lpwszDest; }
static void test_swab( void ) { char original[] = "BADCFEHGJILKNMPORQTSVUXWZY@#"; char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#"; char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$"; char expected3[] = "$"; char from[30]; char to[30]; int testsize; /* Test 1 - normal even case */ memset(to,'$', sizeof(to)); memset(from,'@', sizeof(from)); testsize = 26; memcpy(from, original, testsize); _swab( from, to, testsize ); ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to); /* Test 2 - uneven case */ memset(to,'$', sizeof(to)); memset(from,'@', sizeof(from)); testsize = 25; memcpy(from, original, testsize); _swab( from, to, testsize ); ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to); /* Test 3 - from = to */ memset(to,'$', sizeof(to)); memset(from,'@', sizeof(from)); testsize = 26; memcpy(to, original, testsize); _swab( to, to, testsize ); ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to); /* Test 4 - 1 bytes */ memset(to,'$', sizeof(to)); memset(from,'@', sizeof(from)); testsize = 1; memcpy(from, original, testsize); _swab( from, to, testsize ); ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to); }
static GstBuffer * gst_directsound_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf) { if (gst_directsound_sink_is_spdif_format (&sink->ringbuffer->spec)) { gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec); GstBuffer *out; GstMapInfo infobuf, infoout; gboolean success; if (framesize <= 0) return NULL; out = gst_buffer_new_and_alloc (framesize); if (!gst_buffer_map (buf, &infobuf, GST_MAP_READWRITE)) { gst_buffer_unref (out); return NULL; } if (!gst_buffer_map (out, &infoout, GST_MAP_READWRITE)) { gst_buffer_unmap (buf, &infobuf); gst_buffer_unref (out); return NULL; } success = gst_audio_iec61937_payload (infobuf.data, infobuf.size, infoout.data, infoout.size, &sink->ringbuffer->spec, G_BYTE_ORDER); if (!success) { gst_buffer_unmap (out, &infoout); gst_buffer_unmap (buf, &infobuf); gst_buffer_unref (out); return NULL; } gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_ALL, 0, -1); /* Fix endianness */ _swab ((gchar *) infoout.data, (gchar *) infoout.data, infobuf.size); gst_buffer_unmap (out, &infoout); gst_buffer_unmap (buf, &infobuf); return out; } else return gst_buffer_ref (buf); }
int __cdecl main(int argc, char **argv) { char before[] = "abcdefghijklmn"; char after[] = "--------------"; const char check[] = "badcfehgjilknm"; /* * Initialize the PAL and return FAIL if this fails */ if (0 != (PAL_Initialize(argc, argv))) { return FAIL; } _swab(before, after, sizeof(before)); if (memcmp(after, check, sizeof(after)) != 0) { Fail ("_swab did not correctly swap adjacent bytes in a buffer.\n"); } PAL_Terminate(); return PASS; }
int GetFileString::GetUnicodeString(LPWSTR* DestStr, int& Length, bool bBigEndian) { int CurLength = 0; int ExitCode = 1; EolType Eol = FEOL_NONE; int x = 0; LPWSTR ReadBufPtr = ReadPos < ReadSize ? wReadBuf + ReadPos / sizeof(wchar_t) : nullptr; // Обработка ситуации, когда у нас пришёл двойной \r\r, а потом не было \n. // В этом случаем считаем \r\r двумя MAC окончаниями строк. if (bCrCr) { *wStr = L'\r'; CurLength = 1; bCrCr = false; } else { for(;;) { if (ReadPos >= ReadSize) { if (!(SrcFile.Read(wReadBuf, ReadBufCount*sizeof(wchar_t), ReadSize) && ReadSize)) { if (!CurLength) { ExitCode = 0; } break; } if (bBigEndian) { _swab(reinterpret_cast<char*>(wReadBuf), reinterpret_cast<char*>(wReadBuf), ReadSize); } ReadPos = 0; ReadBufPtr = wReadBuf; } if (Eol == FEOL_NONE) { // UNIX if (*ReadBufPtr == L'\n') { Eol = FEOL_UNIX; } // MAC / Windows? / Notepad? else if (*ReadBufPtr == L'\r') { Eol = FEOL_MAC; } } else if (Eol == FEOL_MAC) { // Windows if (*ReadBufPtr == L'\n') { Eol = FEOL_WINDOWS; } // Notepad? else if (*ReadBufPtr == L'\r') { Eol = FEOL_MAC2; } else { break; } } else if (Eol == FEOL_WINDOWS || Eol == FEOL_UNIX) { break; } else if (Eol == FEOL_MAC2) { // Notepad if (*ReadBufPtr == L'\n') { Eol = FEOL_NOTEPAD; } else { // Пришёл \r\r, а \n не пришёл, поэтому считаем \r\r двумя MAC окончаниями строк CurLength--; bCrCr = true; break; } } else { break; } ReadPos += sizeof(wchar_t); if (CurLength >= m_nwStrLength - 1) { LPWSTR NewStr = static_cast<LPWSTR>(xf_realloc(wStr, (m_nwStrLength + (DELTA << x)) * sizeof(wchar_t))); if (!NewStr) { return -1; } wStr = NewStr; m_nwStrLength += DELTA << x; x++; } wStr[CurLength++] = *ReadBufPtr; ReadBufPtr++; } } wStr[CurLength] = 0; *DestStr = wStr; Length = CurLength; return ExitCode; }
int OldGetFileString::GetUnicodeString(wchar_t **DestStr, int &Length, bool bBigEndian) { int CurLength = 0; int ExitCode = 1; EolType Eol = FEOL_NONE; int x = 0; wchar_t *ReadBufPtr = ReadPos < ReadSize ? wReadBuf + ReadPos / sizeof(wchar_t) : nullptr; // Обработка ситуации, когда у нас пришёл двойной \r\r, а потом не было \n. // В этом случаем считаем \r\r двумя MAC окончаниями строк. if (bCrCr) { *wStr = L'\r'; CurLength = 1; bCrCr = false; } else { while (1) { if (ReadPos >= ReadSize) { if (!(ReadSize = (int)fread(wReadBuf, sizeof(wchar_t), ReadBufCount, SrcFile))) { if (!CurLength) ExitCode=0; break; } ReadSize *= sizeof(wchar_t); if (bBigEndian) _swab(reinterpret_cast<char*>(wReadBuf), reinterpret_cast<char*>(wReadBuf), ReadSize); ReadPos = 0; ReadBufPtr = wReadBuf; } if (Eol == FEOL_NONE) { // UNIX if (*ReadBufPtr == L'\n') Eol = FEOL_UNIX; // MAC / Windows? / Notepad? else if (*ReadBufPtr == L'\r') Eol = FEOL_MAC; } else if (Eol == FEOL_MAC) { // Windows if (*ReadBufPtr == L'\n') Eol = FEOL_WINDOWS; // Notepad? else if (*ReadBufPtr == L'\r') Eol = FEOL_MAC2; else break; } else if (Eol == FEOL_WINDOWS || Eol == FEOL_UNIX) break; else if (Eol == FEOL_MAC2) { // Notepad if (*ReadBufPtr == L'\n') Eol = FEOL_NOTEPAD; else { // Пришёл \r\r, а \n не пришёл, поэтому считаем \r\r двумя MAC окончаниями строк --CurLength; bCrCr = true; break; } } else break; ReadPos += sizeof(wchar_t); if (CurLength >= m_nwStrLength - 1) { wchar_t *NewStr = (wchar_t *)xf_realloc(wStr, (m_nwStrLength + (DELTA << x)) * sizeof(wchar_t)); if (!NewStr) return (-1); wStr = NewStr; m_nwStrLength += DELTA << x; x++; } wStr[CurLength++] = *ReadBufPtr; ReadBufPtr++; } } wStr[CurLength] = 0; *DestStr = wStr; Length = CurLength; return (ExitCode); }
bool CxImageRAW::Decode(CxFile *hFile) { return false; #if 0 if (hFile==NULL) return false; if (mutexInitialized == false) { #ifdef _LINUX pthread_mutex_init(&rawMutex, 0); #else rawMutex = ::CreateMutex(NULL, false, NULL); #endif mutexInitialized = true; } #ifdef _LINUX pthread_mutex_lock(&rawMutex); #else ::WaitForSingleObject(rawMutex, INFINITE); #endif DCRAW dcr; cx_try { // initialization dcr_init_dcraw(&dcr); dcr.opt.user_qual = GetCodecOption(CXIMAGE_FORMAT_RAW) & 0x03; // setup variables for debugging char szClass[] = "CxImageRAW"; dcr.ifname = szClass; dcr.sz_error = info.szLastError; // setup library options, see dcr_print_manual for the available switches // call dcr_parse_command_line_options(&dcr,0,0,0) to set default options // if (dcr_parse_command_line_options(&dcr,argc,argv,&arg)) if (dcr_parse_command_line_options(&dcr,0,0,0)){ cx_throw("CxImageRAW: unknown option"); } // set return point for error handling if (setjmp (dcr.failure)) { cx_throw(""); } // install file manager CxFileRaw src(hFile,&dcr); // check file header dcr_identify(&dcr); if(!dcr.is_raw){ cx_throw("CxImageRAW: not a raw image"); } if (dcr.load_raw == NULL) { cx_throw("CxImageRAW: missing raw decoder"); } // verify special case if (dcr.load_raw == dcr_kodak_ycbcr_load_raw) { dcr.height += dcr.height & 1; dcr.width += dcr.width & 1; } if (info.nEscape == -1){ head.biWidth = dcr.width; head.biHeight= dcr.height; info.dwType = CXIMAGE_FORMAT_RAW; cx_throw("output dimensions returned"); } // shrinked decoding available and requested? dcr.shrink = dcr.filters && (dcr.opt.half_size || dcr.opt.threshold || dcr.opt.aber[0] != 1 || dcr.opt.aber[2] != 1); dcr.iheight = (dcr.height + dcr.shrink) >> dcr.shrink; dcr.iwidth = (dcr.width + dcr.shrink) >> dcr.shrink; // install custom camera matrix if (dcr.opt.use_camera_matrix && dcr.cmatrix[0][0] > 0.25) { memcpy (dcr.rgb_cam, dcr.cmatrix, sizeof dcr.cmatrix); dcr.raw_color = 0; } else { dcr.opt.use_camera_wb = 1; } // allocate memory for the image dcr.image = (ushort (*)[4]) calloc (dcr.iheight*dcr.iwidth, sizeof *dcr.image); dcr_merror (&dcr, dcr.image, szClass); if (dcr.meta_length) { dcr.meta_data = (char *) malloc (dcr.meta_length); dcr_merror (&dcr, dcr.meta_data, szClass); } // start image decoder hFile->Seek(dcr.data_offset, SEEK_SET); (*dcr.load_raw)(&dcr); // post processing if (dcr.zero_is_bad) dcr_remove_zeroes(&dcr); dcr_bad_pixels(&dcr,NULL); if (dcr.opt.dark_frame) dcr_subtract (&dcr,dcr.opt.dark_frame); dcr.quality = 2 + !dcr.fuji_width; if (dcr.opt.user_qual >= 0) dcr.quality = dcr.opt.user_qual; if (dcr.opt.user_black >= 0) dcr.black = dcr.opt.user_black; #ifdef COLORCHECK dcr_colorcheck(&dcr); #endif #if RESTRICTED if (dcr.is_foveon && !dcr.opt.document_mode) dcr_foveon_interpolate(&dcr); #endif if (!dcr.is_foveon && dcr.opt.document_mode < 2) dcr_scale_colors(&dcr); // pixel interpolation and filters dcr_pre_interpolate(&dcr); if (dcr.filters && !dcr.opt.document_mode) { if (dcr.quality == 0) dcr_lin_interpolate(&dcr); else if (dcr.quality == 1 || dcr.colors > 3) dcr_vng_interpolate(&dcr); else if (dcr.quality == 2) dcr_ppg_interpolate(&dcr); else dcr_ahd_interpolate(&dcr); } if (dcr.mix_green) { long i; for (dcr.colors=3, i=0; i < dcr.height*dcr.width; i++) { dcr.image[i][1] = (dcr.image[i][1] + dcr.image[i][3]) >> 1; } } if (!dcr.is_foveon && dcr.colors == 3) dcr_median_filter(&dcr); if (!dcr.is_foveon && dcr.opt.highlight == 2) dcr_blend_highlights(&dcr); if (!dcr.is_foveon && dcr.opt.highlight > 2) dcr_recover_highlights(&dcr); if (dcr.opt.use_fuji_rotate) dcr_fuji_rotate(&dcr); #ifndef NO_LCMS if (dcr.opt.cam_profile) dcr_apply_profile (dcr.opt.cam_profile, dcr.opt.out_profile); #endif // final conversion dcr_convert_to_rgb(&dcr); if (dcr.opt.use_fuji_rotate) dcr_stretch(&dcr); dcr.iheight = dcr.height; dcr.iwidth = dcr.width; if (dcr.flip & 4) SWAP(dcr.height,dcr.width); // ready to transfer data from dcr.image if (!Create(dcr.width,dcr.height,24,CXIMAGE_FORMAT_RAW)){ cx_throw(""); } uchar *ppm = (uchar *) calloc (dcr.width, dcr.colors*dcr.opt.output_bps/8); ushort *ppm2 = (ushort *) ppm; dcr_merror (&dcr, ppm, szClass); uchar lut[0x10000]; if (dcr.opt.output_bps == 8) dcr_gamma_lut (&dcr, lut); long c, row, col, soff, rstep, cstep; soff = dcr_flip_index (&dcr, 0, 0); cstep = dcr_flip_index (&dcr, 0, 1) - soff; rstep = dcr_flip_index (&dcr, 1, 0) - dcr_flip_index (&dcr, 0, dcr.width); for (row=0; row < dcr.height; row++, soff += rstep) { for (col=0; col < dcr.width; col++, soff += cstep) { if (dcr.opt.output_bps == 8) for (c=0; c < dcr.colors; c++) ppm [col*dcr.colors+c] = lut[dcr.image[soff][c]]; else for (c=0; c < dcr.colors; c++) ppm2[col*dcr.colors+c] = dcr.image[soff][c]; } if (dcr.opt.output_bps == 16 && !dcr.opt.output_tiff && htons(0x55aa) != 0x55aa) #if defined(_LINUX) || defined(__APPLE__) swab ((char*)ppm2, (char*)ppm2, dcr.width*dcr.colors*2); #else _swab ((char*)ppm2, (char*)ppm2, dcr.width*dcr.colors*2); #endif DWORD size = dcr.width * (dcr.colors*dcr.opt.output_bps/8); RGBtoBGR(ppm,size); memcpy(GetBits(dcr.height - 1 - row), ppm, min(size,GetEffWidth())); } free (ppm); dcr_cleanup_dcraw(&dcr); #ifdef _LINUX pthread_mutex_unlock(&rawMutex); #else ::ReleaseMutex(rawMutex); #endif } cx_catch {
bool GetFileString::GetTString(std::vector<T>& From, std::vector<T>& To, bool bBigEndian) { typedef eol<T> eol; bool ExitCode = true; T* ReadBufPtr = ReadPos < ReadSize ? From.data() + ReadPos / sizeof(T) : nullptr; To.clear(); // Обработка ситуации, когда у нас пришёл двойной \r\r, а потом не было \n. // В этом случаем считаем \r\r двумя MAC окончаниями строк. if (bCrCr) { To.emplace_back(T(eol::cr)); bCrCr = false; } else { EolType Eol = FEOL_NONE; for (;;) { if (ReadPos >= ReadSize) { if (!(SrcFile.Read(From.data(), ReadBufCount*sizeof(T), ReadSize) && ReadSize)) { if (To.empty()) { ExitCode = false; } break; } if (bBigEndian && sizeof(T) != 1) { _swab(reinterpret_cast<char*>(From.data()), reinterpret_cast<char*>(From.data()), static_cast<int>(ReadSize)); } ReadPos = 0; ReadBufPtr = From.data(); } if (Eol == FEOL_NONE) { // UNIX if (*ReadBufPtr == eol::lf) { Eol = FEOL_UNIX; } // MAC / Windows? / Notepad? else if (*ReadBufPtr == eol::cr) { Eol = FEOL_MAC; } } else if (Eol == FEOL_MAC) { // Windows if (*ReadBufPtr == eol::lf) { Eol = FEOL_WINDOWS; } // Notepad? else if (*ReadBufPtr == eol::cr) { Eol = FEOL_MAC2; } else { break; } } else if (Eol == FEOL_WINDOWS || Eol == FEOL_UNIX) { break; } else if (Eol == FEOL_MAC2) { // Notepad if (*ReadBufPtr == eol::lf) { Eol = FEOL_NOTEPAD; } else { // Пришёл \r\r, а \n не пришёл, поэтому считаем \r\r двумя MAC окончаниями строк To.pop_back(); bCrCr = true; break; } } else { break; } ReadPos += sizeof(T); if (To.size() == To.capacity()) To.reserve(To.size() * 2); To.emplace_back(*ReadBufPtr); ReadBufPtr++; } } To.push_back(0); return ExitCode; }
static guint gst_directsound_sink_write (GstAudioSink * asink, gpointer data, guint length) { GstDirectSoundSink *dsoundsink; DWORD dwStatus; HRESULT hRes; LPVOID pLockedBuffer1 = NULL, pLockedBuffer2 = NULL; DWORD dwSizeBuffer1, dwSizeBuffer2; DWORD dwCurrentPlayCursor; dsoundsink = GST_DIRECTSOUND_SINK (asink); /* Fix endianness */ if (dsoundsink->buffer_format == GST_IEC958) _swab (data, data, length); GST_DSOUND_LOCK (dsoundsink); /* get current buffer status */ hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus); /* get current play cursor position */ hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary, &dwCurrentPlayCursor, NULL); if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) { DWORD dwFreeBufferSize; calculate_freesize: /* calculate the free size of the circular buffer */ if (dwCurrentPlayCursor < dsoundsink->current_circular_offset) dwFreeBufferSize = dsoundsink->buffer_size - (dsoundsink->current_circular_offset - dwCurrentPlayCursor); else dwFreeBufferSize = dwCurrentPlayCursor - dsoundsink->current_circular_offset; if (length >= dwFreeBufferSize) { Sleep (100); hRes = IDirectSoundBuffer_GetCurrentPosition (dsoundsink->pDSBSecondary, &dwCurrentPlayCursor, NULL); hRes = IDirectSoundBuffer_GetStatus (dsoundsink->pDSBSecondary, &dwStatus); if (SUCCEEDED (hRes) && (dwStatus & DSBSTATUS_PLAYING)) goto calculate_freesize; else { dsoundsink->first_buffer_after_reset = FALSE; GST_DSOUND_UNLOCK (dsoundsink); return 0; } } } if (dwStatus & DSBSTATUS_BUFFERLOST) { hRes = IDirectSoundBuffer_Restore (dsoundsink->pDSBSecondary); /*need a loop waiting the buffer is restored?? */ dsoundsink->current_circular_offset = 0; } hRes = IDirectSoundBuffer_Lock (dsoundsink->pDSBSecondary, dsoundsink->current_circular_offset, length, &pLockedBuffer1, &dwSizeBuffer1, &pLockedBuffer2, &dwSizeBuffer2, 0L); if (SUCCEEDED (hRes)) { // Write to pointers without reordering. memcpy (pLockedBuffer1, data, dwSizeBuffer1); if (pLockedBuffer2 != NULL) memcpy (pLockedBuffer2, (LPBYTE) data + dwSizeBuffer1, dwSizeBuffer2); // Update where the buffer will lock (for next time) dsoundsink->current_circular_offset += dwSizeBuffer1 + dwSizeBuffer2; dsoundsink->current_circular_offset %= dsoundsink->buffer_size; /* Circular buffer */ hRes = IDirectSoundBuffer_Unlock (dsoundsink->pDSBSecondary, pLockedBuffer1, dwSizeBuffer1, pLockedBuffer2, dwSizeBuffer2); } /* if the buffer was not in playing state yet, call play on the buffer except if this buffer is the fist after a reset (base class call reset and write a buffer when setting the sink to pause) */ if (!(dwStatus & DSBSTATUS_PLAYING) && dsoundsink->first_buffer_after_reset == FALSE) { hRes = IDirectSoundBuffer_Play (dsoundsink->pDSBSecondary, 0, 0, DSBPLAY_LOOPING); } dsoundsink->first_buffer_after_reset = FALSE; GST_DSOUND_UNLOCK (dsoundsink); return length; }
CMarkdown::FileImage::FileImage(LPCTSTR path, DWORD trunc, int flags) : pImage(NULL), cbImage(0), nByteOrder(0) { HANDLE hFile = 0; if (flags & Mapping) { pImage = LPVOID(path); cbImage = trunc; } else { hFile = flags & Handle ? HANDLE(path) : CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); if (hFile != INVALID_HANDLE_VALUE) { cbImage = GetFileSize(hFile, 0); if (cbImage != INVALID_FILE_SIZE) { if (trunc && cbImage > trunc) { cbImage = trunc; } pImage = MapFile(hFile, cbImage); if (!(flags & Handle)) { CloseHandle(hFile); } } } } if (pImage == NULL) { cbImage = 0; } else if (cbImage >= 4 && (flags & Octets & (nByteOrder = GuessByteOrder(*(LPDWORD)pImage)))) { LPVOID pCopy; switch (nByteOrder) { case 2 + 1: case 2 + 1 + 8: // big endian: swab first cbImage &= ~1UL; pCopy = MapFile(INVALID_HANDLE_VALUE, cbImage); if (pCopy) { _swab((char *)pImage, (char *)pCopy, cbImage); } UnmapViewOfFile(pImage); pImage = pCopy; if (pImage) { case 2 + 0: case 2 + 0 + 8: // little endian int cchImage = cbImage / 2; LPWCH pchImage = (LPWCH)pImage; if (nByteOrder & 8) { ++pchImage; --cchImage; } cbImage = WideCharToMultiByte(CP_UTF8, 0, pchImage, cchImage, 0, 0, 0, 0); pCopy = MapFile(INVALID_HANDLE_VALUE, cbImage); if (pCopy) { WideCharToMultiByte(CP_UTF8, 0, pchImage, cchImage, (LPCH)pCopy, cbImage, 0, 0); } UnmapViewOfFile(pImage); pImage = pCopy; } break; case 4 + 1: case 4 + 1 + 8: case 4 + 2: case 4 + 2 + 8: // odd word endianness: swab first cbImage &= ~3UL; pCopy = MapFile(INVALID_HANDLE_VALUE, cbImage); if (pCopy) { _swab((char *)pImage, (char *)pCopy, cbImage); } UnmapViewOfFile(pImage); pImage = pCopy; if (pImage) { case 4 + 0: case 4 + 0 + 8: case 4 + 3: case 4 + 3 + 8: int cchImage = cbImage; LPCH pchImage = (LPCH)pImage; if (nByteOrder & 8) { pchImage += 4; cchImage -= 4; } Converter converter("utf-8", nByteOrder & 2 ? "ucs-4be" : "ucs-4le"); cbImage = converter.Convert(pchImage, cchImage, 0, 0); pCopy = MapFile(INVALID_HANDLE_VALUE, cbImage); if (pCopy) { converter.Convert(pchImage, cchImage, (LPCH)pCopy, cbImage); } UnmapViewOfFile(pImage); pImage = pCopy; } break; } } }
NV_INT32 main (NV_INT32 argc, NV_CHAR *argv[]) { NV_INT32 i, j, k, m, shift_lat, shift_lon, n, hit_land, hit_water, pos, swap = 1, prev_value, first, ndx, total_bits, block; uLong total_bytes; uLongf out_bytes; FILE *fp, *block_fp, *ofp; NV_INT16 row[1200][1201], delta[1200 * 1200], bias, start_val = 0, last_val = 0, diff, min_diff, max_diff, num_bits, null_val; NV_CHAR string[512], ofile[512], dir[512], lathem, lonhem, header_block[HEADER_SIZE], block_file[512]; NV_CHAR dir_name[6][40] = {"Africa", "Australia", "Eurasia", "Islands", "North_America", "South_America"}; NV_INT64 lpos; NV_U_BYTE water[4], head[8], *in_buf, *out_buf, map[MAP_BYTES], byte, block_map[64800]; time_t t; struct tm *cur_tm; printf ("\n\n%s\n\n", VERSION); if (argc < 2) { fprintf (stderr, "Usage: %s SRTM3_HGT_DIRECTORY_NAME\n\n", argv[0]); exit (-1); } /* Open and read the block map file. */ if (getenv ("SRTM_DATA") == NULL) { fprintf (stderr, "\n\nYou must set the SRTM_DATA environment variable to point to the location of srtm3 directory\n\n"); exit (-1); } strcpy (dir, getenv ("SRTM_DATA")); /* Check the hgt directory name. */ block = 0; for (i = 0 ; i < 6 ; i++) { if (!strcmp (dir_name[i], argv[1])) { block = i + 1; break; } } if (!block) { fprintf (stderr, "SRTM3_HGT_DIRECTORY_NAME %s is incorrect. Only the following are valid:\n\n", argv[1]); for (i = 0 ; i < 6 ; i++) fprintf (stderr, "\t%s\n", dir_name[i]); fprintf (stderr, "\n\n"); exit (-1); } /* Open and read the block map file. */ sprintf (block_file, "%s%1csrtm3%1csrtm3_block_map.dat", dir, SEPARATOR, SEPARATOR); if ((block_fp = fopen (block_file, "rb+")) == NULL) { if ((block_fp = fopen (block_file, "wb+")) == NULL) { perror (block_file); exit (-1); } /* This must be the first time so initialize it. */ byte = 0; for (i = 0 ; i < 180 ; i++) { for (j = 0 ; j < 360 ; j++) { fwrite (&byte, 1, 1, block_fp); } } } fseek (block_fp, 0, SEEK_SET); fread (block_map, sizeof (block_map), 1, block_fp); /* Are we on a big endian machine? */ if (big_endian ()) swap = 0;; /* Set the water flag for the map area. */ srtm_bit_pack (water, 0, 32, 0); /* Open the output file. */ sprintf (ofile, "%s%1csrtm3%1c%s.cte", dir, SEPARATOR, SEPARATOR, argv[1]); if ((ofp = fopen64 (ofile, "wb")) == NULL) { perror (ofile); exit (-1); } /* Write the (minimalist) ASCII header. */ memset (header_block, 0, HEADER_SIZE); t = time (&t); cur_tm = gmtime (&t); sprintf (header_block, "[HEADER SIZE] = %d\n", HEADER_SIZE); sprintf (&header_block[strlen (header_block)], "[VERSION] = %s\n", VERSION); sprintf (&header_block[strlen (header_block)], "[ZLIB VERSION] = %s\n", zlibVersion ()); sprintf (&header_block[strlen (header_block)], "[CREATION DATE] = %s", asctime (cur_tm)); sprintf (&header_block[strlen (header_block)], "[END OF HEADER]\n"); fwrite (header_block, HEADER_SIZE, 1, ofp); /* Set the default for all map addresses to 2 (undefined). Also, write the blank map area out to the file to save the space. */ for (i = 0 ; i < 180 ; i++) { for (j = 0 ; j < 360 ; j++) { srtm_double_bit_pack (map, (i * 360 + j) * 36, 36, 2); } } fseeko64 (ofp, (NV_INT64) HEADER_SIZE, SEEK_SET); fwrite (map, MAP_BYTES, 1, ofp); /* Loop through -90 to 90 lat. */ for (i = -90 ; i < 90 ; i++) { lathem = 'N'; if (i < 0) lathem = 'S'; /* Shift into the 0 to 180 world. */ shift_lat = i + 90; /* Loop through the entire range of longitudes. */ for (j = -180 ; j < 180 ; j++) { lonhem = 'E'; if (j < 0) lonhem = 'W'; /* Shift into the 0 to 360 world. */ shift_lon = j + 180; /* Look for the file. */ sprintf (string, "%s%1c%1c%02d%1c%03d.hgt", argv[1], SEPARATOR, lathem, abs (i), lonhem, abs (j)); /* If we can open the file, read and process it. */ if ((fp = fopen (string, "rb")) != NULL) { prev_value = -1; pos = 0; /* Note that we're only going to 1200 not 1201 because we don't need the redundant data. */ for (k = 0 ; k < 1200 ; k++) { /* Read one row (all 1201). */ fread (row[k], 1201 * sizeof (NV_INT16), 1, fp); for (m = 0 ; m < 1200 ; m++) { /* If we're on a little endian system we need to swap the bytes. */ if (swap) { #ifdef __GNUC__ swab (&row[k][m], &row[k][m], 2); #else _swab ((NV_CHAR *) &row[k][m], (NV_CHAR *) &row[k][m], 2); #endif } } } first = 1; ndx = 0; min_diff = 32767; max_diff = -32768; hit_land = 0; hit_water = 0; /* Loop through the cell. */ for (k = 0 ; k < 1200 ; k++) { /* Snake dance test. */ if (!(k % 2)) { /* West to east. */ for (m = 0 ; m < 1200 ; m++) { if (row[k][m] != -32768) { /* Set the "hit" flags. */ if (row[k][m]) { hit_land = 1; } else { hit_water = 1; } /* First time through set the last_val to start_val. */ if (first) { start_val = row[k][m]; last_val = start_val; first = 0; } diff = row[k][m] - last_val; if (diff > max_diff) max_diff = diff; if (diff < min_diff) min_diff = diff; last_val = row[k][m]; delta[ndx++] = diff; } else { /* From what I've seen, -32768 means land but no defined elevation. */ hit_land = 1; delta[ndx++] = -32768; } } } else { /* East to west. */ for (m = 1199 ; m >= 0 ; m--) { if (row[k][m] != -32768) { /* Set the "hit" flags. */ if (row[k][m]) { hit_land = 1; } else { hit_water = 1; } /* First time through set the last_val to start_val. */ if (first) { start_val = row[k][m]; last_val = start_val; first = 0; } diff = row[k][m] - last_val; if (diff > max_diff) max_diff = diff; if (diff < min_diff) min_diff = diff; last_val = row[k][m]; delta[ndx++] = diff; } else { /* From what I've seen, -32768 means land but no defined elevation. */ hit_land = 1; delta[ndx++] = -32768; } } } } /* All water cell. */ if (!hit_land && hit_water) { srtm_double_bit_pack (map, (shift_lat * 360 + shift_lon) * 36, 36, 0); } /* Land and water or all land cell. */ else { /* The bias is the negative of the minimum difference. We bias so we don't have to play with sign extension in the bit unpacking. */ bias = -min_diff; /* Add two to the max difference to allow room for the null value. */ max_diff += (bias + 2); /* Compute the number of bits needed to store a delta. */ num_bits = NINT (log10 ((NV_FLOAT64) max_diff) / LOG2 + 0.5L); null_val = NINT (pow (2.0L, (NV_FLOAT64) num_bits)) - 1; /* Compute the total bytes needed to store the block. */ total_bits = 16 + 16 + 4 + 1200 * 1200 * num_bits; total_bytes = total_bits / 8; if (total_bits % 8) total_bytes++; /* Allocate the uncompressed memory block. */ in_buf = (NV_U_BYTE *) calloc (total_bytes, 1); if (in_buf == NULL) { perror ("Allocating in_buf"); exit (-1); } /* Allocate the compressed memory block. */ out_bytes = total_bytes + NINT ((NV_FLOAT32) total_bytes * 0.10) + 12; out_buf = (NV_U_BYTE *) calloc (out_bytes, 1); if (out_buf == NULL) { perror ("Allocating out_buf"); exit (-1); } /* Pack the internal header. */ pos = 0; srtm_bit_pack (in_buf, pos, 16, start_val); pos += 16; srtm_bit_pack (in_buf, pos, 16, bias); pos += 16; srtm_bit_pack (in_buf, pos, 4, num_bits); pos += 4; /* Pack the deltas. */ for (k = 0 ; k < 1200 * 1200 ; k++) { if (delta[k] == -32768) { delta[k] = null_val; } else { delta[k] += bias; } srtm_bit_pack (in_buf, pos, num_bits, delta[k]); pos += num_bits; } /* Compress at maximum level. */ n = compress2 (out_buf, &out_bytes, in_buf, total_bytes, 9); if (n) { fprintf (stderr, "Error %d compressing record\n", n); exit (-1); } free (in_buf); /* Pack the header. */ pos = 0; srtm_bit_pack (head, pos, 3, 1); pos += 3; srtm_bit_pack (head, pos, 30, out_bytes); pos += 30; srtm_bit_pack (head, pos, 31, total_bytes); /* Get the address where we're going to write the compressed block. */ fseek (ofp, 0LL, SEEK_END); lpos = ftell (ofp); /* Write the header to the file. */ fwrite (head, 8, 1, ofp); /* Write the buffer to the file. */ fwrite (out_buf, out_bytes, 1, ofp); free (out_buf); /* Save the address of the block to the map. */ srtm_double_bit_pack (map, (shift_lat * 360 + shift_lon) * 36, 36, lpos); } fclose (fp); /* Set the block map value to the block number (1-6 for Africa, Australia, Eurasia, Islands, North_America, or South_America respectively). */ block_map[shift_lat * 360 + shift_lon] = block; fprintf (stderr, "block: %s lon: %03d lat: %03d \r", argv[1], j, i); fflush (stderr); } } } /* Write the map. */ fseek (ofp, (NV_INT64) HEADER_SIZE, SEEK_SET); fwrite (map, MAP_BYTES, 1, ofp); fclose (ofp); /* Write the block map. */ fseek (block_fp, 0, SEEK_SET); fwrite (block_map, sizeof (block_map), 1, block_fp); fclose (block_fp); fprintf (stderr, "%s processing complete \n\n", argv[1]); fflush (stderr); return (0); }
bool SaveFile(LPCSTR pszFile, int iCodePage, bool bSaveCopy) { HANDLE hFile; bool bWriteSuccess; LPSTR lpData; DWORD cbData; DWORD dwBytesWritten; hFile = CreateFile(pszFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // failure could be due to missing attributes (2k/XP) if (hFile == INVALID_HANDLE_VALUE) { DWORD dwAttributes = GetFileAttributes(pszFile); if (dwAttributes != 0) // INVALID_FILE_ATTRIBUTES FILE_ATTRIBUTE_READONLY { dwAttributes = dwAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM); hFile = CreateFile(pszFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | dwAttributes, NULL); } } if (hFile == INVALID_HANDLE_VALUE) return FALSE; // get text cbData = SendMessage(hwnd,SCI_GETLENGTH,0,0); lpData = GlobalAlloc(GPTR,cbData + 1); SendMessage(hwnd,SCI_GETTEXT,GlobalSize(lpData),(LPARAM)lpData); if (cbData == 0) bWriteSuccess = SetEndOfFile(hFile); else { if (iCodePage & NCP_UNICODE) { LPWSTR lpDataWide; int cbDataWide; CPINFO cpi; UINT uCP_UTF8; // UTF-8 text is interpreted as ANSI when saving as Unicode on Windows 95 uCP_UTF8 = (GetCPInfo(CP_UTF8, &cpi) || IsValidCodePage(CP_UTF8)) ? CP_UTF8 : CP_ACP; lpDataWide = GlobalAlloc(GPTR,cbData * 2 + 16); cbDataWide = MultiByteToWideChar(uCP_UTF8,0,lpData,cbData,lpDataWide,GlobalSize(lpDataWide)); if (iCodePage & NCP_UNICODE_BOM) { if (iCodePage & NCP_UNICODE_REVERSE) WriteFile(hFile,(LPCVOID)"\xFE\xFF",2,&dwBytesWritten,NULL); else WriteFile(hFile,(LPCVOID)"\xFF\xFE",2,&dwBytesWritten,NULL); } if (iCodePage & NCP_UNICODE_REVERSE) { _swab((char*)lpDataWide,(char*)lpDataWide,cbDataWide * sizeof(WCHAR)); } bWriteSuccess = WriteFile(hFile,lpDataWide,cbDataWide * sizeof(WCHAR),&dwBytesWritten,NULL); GlobalFree(lpData); } else if (iCodePage & NCP_UTF8) { if (iCodePage & NCP_UTF8_SIGN) WriteFile(hFile,(LPCVOID)"\xEF\xBB\xBF",3,&dwBytesWritten,NULL); bWriteSuccess = WriteFile(hFile,lpData,cbData,&dwBytesWritten,NULL); } else // convert text to 8bit { //LPWSTR lpDataWide = GlobalAlloc(GPTR,cbData * 2 + 16); //int cbDataWide = MultiByteToWideChar(CP_UTF8,0,lpData,cbData,lpDataWide,GlobalSize(lpDataWide)); //ZeroMemory(lpData,GlobalSize(lpData)); //cbData = WideCharToMultiByte(CP_ACP,0,lpDataWide,cbDataWide,lpData,GlobalSize(lpData),NULL,NULL); //GlobalFree(lpDataWide); bWriteSuccess = WriteFile(hFile,lpData,cbData,&dwBytesWritten,NULL); GlobalFree(lpData); } } CloseHandle(hFile); if (bWriteSuccess) { if (!bSaveCopy) SendMessage(hwnd,SCI_SETSAVEPOINT,0,0); return TRUE; } else return FALSE; }