ColorImage* rgbToHSV(const ColorImage& src, ColorImage* dst) { ColorImage* result = createResultBuffer(src, dst); #ifdef NICE_USELIB_IPP IppStatus ret = ippiRGBToHSV_8u_C3R(src.getPixelPointer(), src.getStepsize(), result->getPixelPointer(), result->getStepsize(), makeROIFullImage(src)); if(ret!=ippStsNoErr) fthrow(ImageException, ippGetStatusString(ret)); #else // NICE_USELIB_IPP double min,max,diff, r,g,b, h; const Ipp8u* pSrcStart = src.getPixelPointer(); Ipp8u* pDstStart = result->getPixelPointer(); const Ipp8u* pSrc; Ipp8u* pDst; for(int y=0; y<src.height(); ++y) { pSrc = pSrcStart; pDst = pDstStart; for(int x=0; x<src.width(); ++x) { r = *pSrc/255.0; ++pSrc; g = *pSrc/255.0; ++pSrc; b = *pSrc/255.0; ++pSrc; min = std::min(g,b); min = std::min(r, min); max = std::max(g,b); max = std::max(r, max); diff = max-min; // H h = 0; if(diff!=0) { if(max==r) { h = ((g-b)/diff )*60; } else if(max==g) { h = ((b-r)/diff + 2)*60; } else if(max==b) { h = ((r-g)/diff + 4)*60; } } h += (h<0)?360:0; *pDst = static_cast<unsigned char>(h*17/24); ++pDst; // *255/360 *pDst = static_cast<unsigned char>((max==0)?0:(diff*255)/max); ++pDst; *pDst = static_cast<unsigned char>(max*255); ++pDst; // v = max; } pSrcStart += src.getStepsize(); pDstStart += result->getStepsize(); } #endif // NICE_USELIB_IPP return result; }
ColorImage* yuvToRGB(const ColorImage& src, ColorImage* dst) { ColorImage* result = createResultBuffer(src.width(), src.height(), dst); #ifdef NICE_USELIB_IPP IppStatus ret = ippiYUVToRGB_8u_C3R(src.getPixelPointer(), src.getStepsize(), result->getPixelPointer(), result->getStepsize(), makeROIFullImage(src)); if(ret!=ippStsNoErr) fthrow(ImageException, ippGetStatusString(ret)); #else // NICE_USELIB_IPP double y,u,v, r,g,b; const Ipp8u* pSrcStart = src.getPixelPointer(); Ipp8u* pDstStart = result->getPixelPointer(); const ColorImage::Pixel* pSrc; ColorImage::Pixel* pDst; for(int ty=0; ty<src.height(); ++ty) { pSrc = pSrcStart; pDst = pDstStart; for(int tx=0; tx<src.width(); ++tx) { y = *pSrc/255.0; ++pSrc; u = *pSrc/255.0-0.5; ++pSrc; v = *pSrc/255.0-0.5; ++pSrc; r = y + 1.140*v; g = y - 0.394*u - 0.581*v; b = y + 2.032*u; r = std::min(1.0, r); r = std::max(0.0, r); g = std::min(1.0, g); g = std::max(0.0, g); b = std::min(1.0, b); b = std::max(0.0, b); *pDst = static_cast<Ipp8u>(r*255); ++pDst; *pDst = static_cast<Ipp8u>(g*255); ++pDst; *pDst = static_cast<Ipp8u>(b*255); ++pDst; } pSrcStart += src.getStepsize(); pDstStart += result->getStepsize(); } #endif // NICE_USELIB_IPP return result; }
ColorImage* rgbToYUV(const ColorImage& src, ColorImage* dst) { ColorImage* result = createResultBuffer(src.width(), src.height(), dst); #ifdef NICE_USELIB_IPP IppStatus ret = ippiRGBToYUV_8u_C3R(src.getPixelPointer(), src.getStepsize(), result->getPixelPointer(), result->getStepsize(), makeROIFullImage(src)); if(ret!=ippStsNoErr) fthrow(ImageException, ippGetStatusString(ret)); #else // NICE_USELIB_IPP double r,g,b, y,u,v; const Ipp8u* pSrcStart = src.getPixelPointer(); Ipp8u* pDstStart = result->getPixelPointer(); const ColorImage::Pixel* pSrc; ColorImage::Pixel* pDst; for(int ty=0; ty<src.height(); ++ty) { pSrc = pSrcStart; pDst = pDstStart; for(int tx=0; tx<src.width(); ++tx) { r = *pSrc/255.0; ++pSrc; g = *pSrc/255.0; ++pSrc; b = *pSrc/255.0; ++pSrc; y = 0.299*r + 0.587*g + 0.114*b; u = 0.492*(b-y); v = 0.877*(r-y); v+= 0.5; v = std::max(0.0, v); v = std::min(1.0, v); *pDst = static_cast<Ipp8u>(y*255); ++pDst; *pDst = static_cast<Ipp8u>((u+0.5)*255); ++pDst; *pDst = static_cast<Ipp8u>(v*255); ++pDst; } pSrcStart += src.getStepsize(); pDstStart += result->getStepsize(); } #endif // NICE_USELIB_IPP return result; }
ColorImage* remap(const ColorImage& src, const FloatImage &px, const FloatImage &py, ColorImage* dst, int interpolation) { #ifdef NICE_USELIB_IPP ColorImage ci(src); ColorImage::Pixel* cursor = ci.getPixelPointerY(0); *cursor++ = 0; *cursor++ = 0; *cursor = 0; ColorImage * result = createResultBuffer(ci.width(), ci.height(), dst); IppStatus ret = ippiRemap_8u_C3R(ci.getPixelPointer(), makeROIFullImage(ci), ci.getStepsize(), makeRectFullImage(ci), px.getPixelPointer(), px.getStepsize(), py.getPixelPointer(), py.getStepsize(), result->getPixelPointer(), result->getStepsize(), makeROIFullImage(ci), interpolation); if(ret!=ippStsNoErr) fthrow(ImageException, ippGetStatusString(ret)); return result; #else // NICE_USELIB_IPP fthrow(ImageException,"Not yet supported without IPP."); #endif // NICE_USELIB_IPP }
ColorImage* hsvToRGB(const ColorImage& src, ColorImage* dst) { ColorImage* result = createResultBuffer(src.width(), src.height(), dst); #ifdef NICE_USELIB_IPP IppStatus ret = ippiHSVToRGB_8u_C3R(src.getPixelPointer(), src.getStepsize(), result->getPixelPointer(), result->getStepsize(), makeROIFullImage(src)); if(ret!=ippStsNoErr) fthrow(ImageException, ippGetStatusString(ret)); #else // NICE_USELIB_IPP double h,s,v, r,g,b, k,m,n, f; int i; const Ipp8u* pSrcStart = src.getPixelPointer(); Ipp8u* pDstStart = result->getPixelPointer(); const ColorImage::Pixel* pSrc; ColorImage::Pixel* pDst; for(int y=0; y<src.height(); ++y) { pSrc = pSrcStart; pDst = pDstStart; for(int x=0; x<src.width(); ++x) { h =(*pSrc/255.0)*360; ++pSrc; s = *pSrc/255.0; ++pSrc; v = *pSrc/255.0; ++pSrc; r = g = b = v; // default case if s==0 if(s!=0) { h = (h==360)?0:h/60; i = static_cast<int>(h); f = h-i; m = v*(1-s); n = v*(1-s*f); k = v*(1-s*(1-f)); switch(i) { case 0: r = v; g = k; b = m; break; case 1: r = n; g = v; b = m; break; case 2: r = m; g = v; b = k; break; case 3: r = m; g = n; b = v; break; case 4: r = k; g = m; b = v; break; case 5: r = v; g = m; b = n; break; } } *pDst = static_cast<Ipp8u>(r*255); ++pDst; *pDst = static_cast<Ipp8u>(g*255); ++pDst; *pDst = static_cast<Ipp8u>(b*255); ++pDst; } pSrcStart += src.getStepsize(); pDstStart += result->getStepsize(); } #endif // NICE_USELIB_IPP return(result); }