📄 medib.cpp
字号:
if(nOfComponents==0) return; if(nOfComponents==1) { Copy(*src, dst, size); ConvertDynRange(dst, size, srcDepth[0].BitDepth(), srcDepth[0].IsSigned(), dstDepth.BitDepth(), dstDepth.IsSigned()); return; } double factor = 1.0 / (double)nOfComponents; Ipp32s *dstData = dst.Data(); unsigned int dstLineStep = dst.LineStep(); FixedBuffer<const Ipp32s*> srcDataPtr (nOfComponents); FixedBuffer<unsigned int> srcLineStep(nOfComponents); FixedBuffer<int> shift (nOfComponents); FixedBuffer<int> add (nOfComponents); unsigned int component; for(component = 0; component < nOfComponents; component++) { srcDataPtr [component] = src[component].Data(); srcLineStep[component] = src[component].LineStep(); shift[component] = srcDepth[component].BitDepth() - dstDepth.BitDepth(); if(dstDepth.IsSigned()) { add[component] = srcDepth[component].IsSigned() ? 0 : -(Ipp32s)1 << (Ipp32s)srcDepth[component].BitDepth(); } else { add[component] = srcDepth[component].IsSigned() ? (Ipp32s)1 << (Ipp32s)srcDepth[component].BitDepth() : 0; } } unsigned int width = size.Width(); unsigned int height = size.Height(); for(unsigned int y = 0; y < height; y++) { for(unsigned int x = 0; x < width; x++) { double sum = 0.0; for(component = 0; component < nOfComponents; component++) { Ipp32s normalized = shift[component] > 0 ? srcDataPtr[component][x] << shift[component] : srcDataPtr[component][x] >> shift[component]; normalized += add[component]; sum += (double)normalized; } sum *= factor; dstData[x] = (Ipp32s)(sum + 0.5); } addrInc(dstData, dstLineStep); for(component = 0; component < nOfComponents; component++) addrInc(srcDataPtr[component], srcLineStep[component]); }}/*inline Rect ScaleToSize(const Rect &src, const RectSize &sizeToScale){ Rect dst; dst.SetSize(sizeToScale); if(sizeToScale == src.Size()) { dst.SetOrigin(src.Origin()); } else { double xScale = (double)dst.Width () / src.Width (); double yScale = (double)dst.Height() / src.Height(); double xOrigin = src.X() * xScale; double yOrigin = src.Y() * yScale; dst.SetOrigin(Point((int)xOrigin, (int)yOrigin)); } return dst;}*///// Palette is not used here.//// 'isAveragingPrefferable' is actual only when it's hard to keep all channels in DIB// if it's false only first channels will be placed in DIB// if it's true the average value will be placed in DIB for all DIB channels//inline void ConverDirectImageComponentsToDIB( const ImageCoreC<Ipp32s, 1> *srcComponents, const MRstrImageDepth *srcDepth, unsigned int nOfSrcComponents, const RectSize &srcSize, const ImageCoreC<Ipp32s, 1> *dstComponents, unsigned int nOfDstComponents, const RectSize &dstSize, bool isAveragingPrefferable, BDiagnOutputPtr &diagnOutputPtr){ unsigned int component; if(nOfSrcComponents == nOfDstComponents || (!isAveragingPrefferable)) { unsigned int nOfActualComponents = Min(nOfSrcComponents, nOfDstComponents); for(component = 0; component < nOfActualComponents; component++) {#ifdef XSCALE if(srcSize != dstSize) throw DiagnDescrCT<MEDIBException,compsAreResampledForDIB>(); Copy(srcComponents[component], dstComponents[component], srcSize);#else Resize( srcComponents[component], srcSize, dstComponents[component], dstSize);#endif ConvertDynRangeForDIB( dstComponents[component], srcDepth [component], srcSize); Saturate(dstComponents[component], srcSize, 0, 255); } for( ; component < nOfDstComponents; component++) Zero(dstComponents[component], dstSize); } else { // multiple input channels, single output ... // sometimes it's hard to do something better in this case diagnOutputPtr->Warning(DiagnDescrCT<MEDIBWarning, MEDIBAveragingOfComponents>()); MRstrImageDepth dstDepth(7, false); ImageC<Ipp32s,1> dst(srcSize); ConvertDynRangeAndAverage(srcComponents, srcDepth, nOfSrcComponents, dst, dstDepth, srcSize);#ifdef XSCALE if(srcSize != dstSize) throw DiagnDescrCT<MEDIBException,compsAreResampledForDIB>(); Copy(dst, dstComponents[0], srcSize);#else Resize(dst, srcSize, dstComponents[0], dstSize);#endif Saturate(dst, srcSize, 0, 255); for(component = 1; component < nOfDstComponents; component++) Copy(dstComponents[0], dstComponents[component], dstSize); }}// return false if impossibleinline void DepalettizeForDIB( const MRstrImagePalette &palette, const ImageCoreC<Ipp32s, 1> *srcComponent, unsigned int nOfComponents, const ImageCoreC<Ipp32s, 1> &dst, const RectSize &size, Ipp16u channel){ unsigned int compIndex = palette.ComponentIndex(channel); if(compIndex >= nOfComponents) Zero(dst, size); const ImageCoreC<Ipp32s, 1> &src = srcComponent[compIndex]; unsigned int nOfEntries = palette.NOfEntries(); FixedBuffer<Ipp64s> paletteVector(nOfEntries); for(unsigned int i = 0; i < nOfEntries; i++) paletteVector[i] = palette.ChannelValue(channel, i); FixedBuffer<Ipp8u> paletteVector8u(nOfEntries); ConvertDynRangeToDIB( paletteVector, paletteVector8u, nOfEntries, palette.ChannelDepth(channel).BitDepth(), palette.ChannelDepth(channel).IsSigned()); const Ipp32s *srcData = src.Data(); unsigned int srcLineStep = src.LineStep(); Ipp32s *dstData = dst.Data(); unsigned int dstLineStep = dst.LineStep(); unsigned int width = size.Width(); unsigned int height = size.Height(); for(unsigned int y = 0; y < height; y++) { for(unsigned int x = 0; x < width; x++) { Ipp32s index = srcData[x]; if (index > (Ipp32s)nOfEntries) index = nOfEntries - 1; else if(index < 0) index = 0; dstData[x] = paletteVector8u[(unsigned int)index]; } addrInc(srcData, srcLineStep); addrInc(dstData, dstLineStep); }}inline void DepalettizeForDIB( const MRstrImagePalette &palette, const ImageCoreC<Ipp32s, 1> *srcComponent, unsigned int nOfComponents, ImagePn<Ipp32s> &dst, const RectSize &size){ dst.ReAlloc(size, palette.NOfChannels()); for(Ipp16u channel = 0; channel < palette.NOfChannels(); channel++) { DepalettizeForDIB( palette, srcComponent, nOfComponents, dst.Channel(channel), size, channel); }}// isAveragingPrefferable is actual only when it's hard to keep all channels in DIB// if it's false only first channels will be placed in DIB// if it's true the average value will be placed in DIB for all DIB channelsvoid ConvertImageChannelsToDIB( const MRstrImage &rstrImage, const DIBInfo &dibInfo, const ImageCoreC<Ipp32s, 1> *dibImageChannels, bool isAveragingPrefferable, BDiagnOutputPtr &diagnOutputPtr){ unsigned int nOfRstrComponents = rstrImage.NOfComponents(); unsigned int nOfDIBChannels = NOfChannels(dibInfo.Depth()); unsigned int rstrComponent; Rect rstrRect = rstrImage.RefGridRect(); // resample all of components for single sampling factor ImagePn<Ipp32s> rstrResizedComponents(rstrRect.Size(), rstrImage.NOfComponents()); for(rstrComponent = 0; rstrComponent < nOfRstrComponents; rstrComponent++) {#ifdef XSCALE if( rstrImage.ComponentImage() [rstrComponent].Size() != rstrRect.Size() || rstrImage.ComponentSampleSize() [rstrComponent] != RectSize(1,1) ) throw DiagnDescrCT<MEDIBException,compsAreResampledForDIB>(); Copy( rstrImage.ComponentImage() [rstrComponent], rstrResizedComponents.Channels()[rstrComponent], rstrImage.ComponentImage() [rstrComponent].Size());#else ResizeUp( rstrImage.ComponentImage() [rstrComponent], rstrImage.ComponentImage() [rstrComponent].Size(), rstrImage.ComponentSampleSize() [rstrComponent], rstrResizedComponents.Channels()[rstrComponent], rstrRect);#endif } if(rstrImage.Palette().IsActual() && nOfDIBChannels != 1) { ImagePn<Ipp32s> depalletizedComponents; DepalettizeForDIB( rstrImage.Palette(), rstrResizedComponents.Channels(), nOfRstrComponents, depalletizedComponents, rstrResizedComponents.Size()); FixedBuffer<MRstrImageDepth> dibDepth(depalletizedComponents.NOfChannels()); for(unsigned int i = 0; i < depalletizedComponents.NOfChannels(); i++) dibDepth[i] = MRstrImageDepth(7, false); ConverDirectImageComponentsToDIB( depalletizedComponents.Channels(), dibDepth, depalletizedComponents.NOfChannels(), depalletizedComponents.Size(), dibImageChannels, nOfDIBChannels, dibInfo.Size(), isAveragingPrefferable, diagnOutputPtr); } else { ConverDirectImageComponentsToDIB( rstrResizedComponents.Channels(), rstrImage.ComponentDepth(), rstrResizedComponents.NOfChannels(), rstrResizedComponents.Size(), dibImageChannels, nOfDIBChannels, dibInfo.Size(), isAveragingPrefferable, diagnOutputPtr); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -