void ImageCompressorBlockPalette::QuantizeToPalette(const Bitmap &bmp, OutputDataStream &stream) { const UINT width = RoundDown(bmp.Width()); const UINT height = RoundDown(bmp.Height()); stream << width << height; for(UINT x = 0; x < width; x += blockDimension) { for(UINT y = 0; y < height; y += blockDimension) { Block32 curBlock; for(UINT yOffset = 0; yOffset < blockDimension; yOffset++) { for(UINT xOffset = 0; xOffset < blockDimension; xOffset++) { curBlock.c[yOffset][xOffset] = bmp[y + yOffset][x + xOffset]; } } UINT bestPaletteError = 0xFFFFFFFF; UINT bestPaletteIndex = 0; for(UINT paletteIndex = 0; paletteIndex < paletteSize; paletteIndex++) { UINT curPaletteError = CompareBlocks(curBlock, _palette.colors[paletteIndex]); if(curPaletteError < bestPaletteError) { bestPaletteIndex = paletteIndex; bestPaletteError = curPaletteError; } } stream << BYTE(bestPaletteIndex); //stream << UINT16(bestPaletteIndex); } } }
static void resize_display(struct dispinfo *d, int new_width, int new_height) { int total_squares; uint h; char *cp; /* round down to even multiple of square size */ new_width = RoundDown(new_width, d->pixel_xmult); new_height = RoundDown(new_height, d->pixel_ymult); if (new_width <= 0 || new_height <= 0) return; total_squares = (new_width * new_height) / PixelSize(d); d->bytes_vp = d->alloc_unit; /* compute how many bytes of memory one square on the display should * represent, so you have the smallest granularity without the window size * exceeding the initial height. */ while (total_squares < DivideRoundedUp(d->arena_size, d->bytes_vp)) d->bytes_vp *= 2; h = DivideRoundedUp(d->arena_size, d->bytes_vp) * PixelSize(d); new_height = DivideRoundedUp(h, new_width); new_height = RoundUp(new_height, d->pixel_ymult); cp = strchr(d->title, '='); if (cp) sprintf(cp, "= %d bytes)", d->bytes_vp); if (!(d->memory_pixel = DXReAllocate((Pointer)d->memory_pixel, new_width * new_height))) { xerror = 1; return; } d->win_width = new_width; d->win_height = new_height; memset (d->memory_pixel, color_free, d->win_width * d->win_height); /* don't let X free our pixel buffer while destroying old image */ d->memory_image->data = NULL; XDestroyImage(d->memory_image); d->memory_image = XCreateImage(d->disp, XDefaultVisual(d->disp, XDefaultScreen(d->disp)), 8, ZPixmap, 0, (char *) d->memory_pixel, d->win_width, d->win_height, 8, 0); XResizeWindow(d->disp, d->wind, d->win_width, d->win_height); XStoreName(d->disp, d->wind, d->title); }
bool iupPlotTickIterLog::AdjustRange (double &ioMin, double &ioMax) const { double theBase = mAxis->mLogBase; if (mAxis->mMaxDecades > 0) ioMin = ioMax/iupPlotExp (mAxis->mMaxDecades, theBase); if (ioMin == 0 && ioMax == 0) { ioMin = kLogMinClipValue; ioMax = 1.0; } if (ioMin <= 0 || ioMax<=0) return false; ioMin = RoundDown(ioMin*kLittleIncrease); ioMax = RoundUp(ioMax*kLittleDecrease); if (ioMin<kLogMinClipValue) ioMin = kLogMinClipValue; if (mAxis->mMaxDecades > 0) ioMin = ioMax/iupPlotExp (mAxis->mMaxDecades, theBase); return true; }
int Round(double x, Rounding rounding) { switch (rounding) { case ROUND_UP : return RoundUp (x); case ROUND_DOWN: return RoundDown (x); default : return RoundNearest(x); } }
static void _UpdateRange(_Inout_ OVS_PI_RANGE* pPiRange, SIZE_T offset, SIZE_T size) { SIZE_T startPos = 0; SIZE_T endPos = 0; OVS_CHECK(pPiRange); startPos = RoundDown(offset, sizeof(UINT64)); endPos = RoundUp(offset + size, sizeof(UINT64)); if (!pPiRange) { return; } //i.e. in the beginning if (pPiRange->startRange == pPiRange->endRange) { pPiRange->startRange = startPos; pPiRange->endRange = endPos; } else { //i.e. if it was set before if (pPiRange->startRange > startPos) { pPiRange->startRange = startPos; } if (pPiRange->endRange < endPos) { pPiRange->endRange = endPos; } } }
/* compute (xUL,yUL) and nrRows, nrCols from some coordinates * RcomputeExtend computes parameters to create a raster maps * from minimum and maximum x and y coordinates, projection information, * cellsize and units. The resulting parameters are computed that the * smallest raster map can be created that will include the two * coordinates given, assuming a default angle of 0. * Which coordinates are the maximum or minimum are * determined by the function itself. */ void RcomputeExtend( REAL8 *xUL, /* write-only, resulting xUL */ REAL8 *yUL, /* write-only, resulting yUL */ size_t *nrRows, /* write-only, resulting nrRows */ size_t *nrCols, /* write-only, resulting nrCols */ double x_1, /* first x-coordinate */ double y_1, /* first y-coordinate */ double x_2, /* second x-coordinate */ double y_2, /* second y-coordinate */ CSF_PT projection, /* required projection */ REAL8 cellSize, /* required cellsize, > 0 */ double rounding) /* assure that (xUL/rounding), (yUL/rouding) * (xLL/rounding) and (yLL/rounding) will * will all be an integers values > 0 */ { /* * xUL ______ | | | | | | ------ */ double yLL,xUR = x_1 > x_2 ? x_1 : x_2; *xUL = x_1 < x_2 ? x_1 : x_2; *xUL = RoundDown(*xUL, rounding); /* Round down */ xUR = RoundUp( xUR, rounding); /* Round up */ POSTCOND(*xUL <= xUR); *nrCols = (size_t)ceil((xUR - *xUL)/cellSize); if (projection == PT_YINCT2B) { yLL = y_1 > y_2 ? y_1 : y_2; /* highest value at bottom */ *yUL = y_1 < y_2 ? y_1 : y_2; /* lowest value at top */ *yUL = RoundDown(*yUL, rounding); yLL = RoundUp( yLL, rounding); } else { yLL = y_1 < y_2 ? y_1 : y_2; /* lowest value at bottom */ *yUL = y_1 > y_2 ? y_1 : y_2; /* highest value at top */ *yUL = RoundUp( *yUL, rounding); yLL = RoundDown( yLL, rounding); } *nrRows = (size_t)ceil(fabs(yLL - *yUL)/cellSize); }
int reducedToExpanded(int size, double rate, int phases) { if (phases == 0) return size; size = RoundUp(size, minOnRequest); size = size + (2 * NumProc) * minOnRequest; size = (int) size / (1.0 - computeReserve(rate, phases)); size = RoundDown(size, minOnRequest); return size; }
//========================================================================= // Internal function: // // Will attempt allocate a block of memory within the specified range, as // near as possible to the specified function. //========================================================================= static MHOOKS_TRAMPOLINE* BlockAlloc(PBYTE pSystemFunction, PBYTE pbLower, PBYTE pbUpper) { SYSTEM_INFO sSysInfo = {0}; ::GetSystemInfo(&sSysInfo); // Always allocate in bulk, in case the system actually has a smaller allocation granularity than MINALLOCSIZE. const ptrdiff_t cAllocSize = max(sSysInfo.dwAllocationGranularity, MHOOK_MINALLOCSIZE); MHOOKS_TRAMPOLINE* pRetVal = NULL; PBYTE pModuleGuess = (PBYTE) RoundDown((size_t)pSystemFunction, cAllocSize); int loopCount = 0; for (PBYTE pbAlloc = pModuleGuess; pbLower < pbAlloc && pbAlloc < pbUpper; ++loopCount) { // determine current state MEMORY_BASIC_INFORMATION mbi; ODPRINTF((L"mhooks: BlockAlloc: Looking at address %p", pbAlloc)); if (!VirtualQuery(pbAlloc, &mbi, sizeof(mbi))) break; // free & large enough? if (mbi.State == MEM_FREE && mbi.RegionSize >= (unsigned)cAllocSize) { // and then try to allocate it pRetVal = (MHOOKS_TRAMPOLINE*) VirtualAlloc(pbAlloc, cAllocSize, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (pRetVal) { size_t trampolineCount = cAllocSize / sizeof(MHOOKS_TRAMPOLINE); ODPRINTF((L"mhooks: BlockAlloc: Allocated block at %p as %d trampolines", pRetVal, trampolineCount)); pRetVal[0].pPrevTrampoline = NULL; pRetVal[0].pNextTrampoline = &pRetVal[1]; // prepare them by having them point down the line at the next entry. for (size_t s = 1; s < trampolineCount; ++s) { pRetVal[s].pPrevTrampoline = &pRetVal[s - 1]; pRetVal[s].pNextTrampoline = &pRetVal[s + 1]; } // last entry points to the current head of the free list pRetVal[trampolineCount - 1].pNextTrampoline = g_pFreeList; if (g_pFreeList) { g_pFreeList->pPrevTrampoline = &pRetVal[trampolineCount - 1]; } break; } } // This is a spiral, should be -1, 1, -2, 2, -3, 3, etc. (* cAllocSize) ptrdiff_t bytesToOffset = (cAllocSize * (loopCount + 1) * ((loopCount % 2 == 0) ? -1 : 1)); pbAlloc = pbAlloc + bytesToOffset; } return pRetVal; }
static void _streamOpen(DWORD file, DWORD offset, bool loop) { if (s_pState->m_Stream.m_Open) { // if a stream is current playing, interrupt it s_pState->m_Stream.m_pVoice->Flush(); s_pState->m_Stream.m_pFile->Release(); s_pState->m_Stream.m_Playing = false; s_pState->m_Stream.m_Open = false; } const char* name = Sys_GetFileCodeName(file); WaitForSingleObject(Sys_FileStreamMutex, INFINITE); // open the file for streaming LPCWAVEFORMATEX fmt; if (DS_OK == XWaveFileCreateMediaObject( name, &fmt, &s_pState->m_Stream.m_pFile)) { // set the voice based on the file format s_pState->m_Stream.m_pVoice->SetFormat(fmt); #ifdef _FIVE_CHANNEL DSMIXBINVOLUMEPAIR dsmbvp[6] = { DSMIXBINVOLUMEPAIRS_DEFAULT_5CHANNEL_3D, }; DSMIXBINS dsmb; dsmb.dwMixBinCount = 6; dsmb.lpMixBinVolumePairs = dsmbvp; s_pState->m_Stream.m_pVoice->SetMixBins(&dsmb); #endif // seek the requested start position s_pState->m_Stream.m_pFile->Seek(RoundDown(offset, 72), FILE_BEGIN, NULL); s_pState->m_Stream.m_StartTime = 0; s_pState->m_Stream.m_Looping = loop; s_pState->m_Stream.m_Playing = true; s_pState->m_Stream.m_Open = true; } ReleaseMutex(Sys_FileStreamMutex); }
BOOLEAN PacketInfo_Equal(const OVS_OFPACKET_INFO* pLhs, const OVS_OFPACKET_INFO* pRhs, SIZE_T endRange) { SIZE_T startRange = 0; //if we have tunnelInfo.ipv4Destination, it means that we have a valid tunnel key. //This means that this packet (for which we have extracted packet info) is encapsulated (e.g. in GRE) //So the flow that is constructed must match flows against the tunnelInfo part as well. //And the tunnelInfo part starts at offset 0 in a packet info. if (pRhs->tunnelInfo.ipv4Destination) { startRange = 0; } else { startRange = RoundDown(OFFSET_OF(OVS_OFPACKET_INFO, physical), sizeof(INT64)); } return PacketInfo_EqualAtRange(pLhs, pRhs, startRange, endRange); }
inline TV RoundUp(TV Value, TM Multiple) { return(RoundDown(Value, Multiple) + (((Value % Multiple) > 0) ? Multiple : 0)); }
/*! * Aligns the starting and ending addresses of the range. Afterwards, the * original range is contained by the new one, and the start and end are aligned. * * @param[in] alignment Desired alignement (bytes). */ void AlignEndpoints(size_t alignment) { ADDRTYPE end = RoundUp(GetEnd(), alignment); _base = RoundDown(_base, alignment); _size = end - _base; }