void ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome, int luminance) { int x, y; int yuv[3]; Uint8 *p, *op; SDL_LockSurface(s); SDL_LockYUVOverlay(o); for (y = 0; y < s->h && y < o->h; y++) { p = ((Uint8 *) s->pixels) + s->pitch * y; op = o->pixels[0] + o->pitches[0] * y; for (x = 0; x < s->w && x < o->w; x++) { RGBtoYUV(p, yuv, monochrome, luminance); if (x % 2 == 0) { *(op++) = yuv[0]; *(op++) = yuv[1]; op[1] = yuv[2]; } else { *op = yuv[0]; op += 2; } p += s->format->BytesPerPixel; } } SDL_UnlockYUVOverlay(o); SDL_UnlockSurface(s); }
inline void ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o) { int x,y; int yuv[3]; Uint8 *p,*op; SDL_LockSurface(s); SDL_LockYUVOverlay(o); for(y=0; y<s->h && y<o->h; y++) { p=s->pixels+s->pitch*y; op=o->pixels[0]+o->pitches[0]*y; for(x=0; x<s->w && x<o->w; x++) { RGBtoYUV(p,yuv); if(x%2==0) { *(op++)=yuv[0]; *(op++)=yuv[2]; op[1]=yuv[1]; } else { *op=yuv[0]; op+=2; } p+=s->format->BytesPerPixel; } } SDL_UnlockYUVOverlay(o); SDL_UnlockSurface(s); }
void ColorSpace::RGBtoYUV (CoImage* pIn, CoImage* pOut) { assert (pIn->GetType() == MAT_Tbyte); assert (pOut->GetType() == MAT_Tfloat); BYTE* pbR = pIn->m_matX.data.ptr[0]; BYTE* pbG = pIn->m_matY.data.ptr[0]; BYTE* pbB = pIn->m_matZ.data.ptr[0]; float* prL = pOut->m_matX.data.fl[0]; float* pra = pOut->m_matY.data.fl[0]; float* prb = pOut->m_matZ.data.fl[0]; for (int i = 0; i < pIn->GetHeight() * pIn->GetWidth(); i ++) { RGBtoYUV(pbR[i], pbG[i], pbB[i], &prL[i], &pra[i], &prb[i]); } }
void ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance) { int x,y; int yuv[3]; Uint8 *p,*op[3]; SDL_LockSurface(s); SDL_LockYUVOverlay(o); /* Black initialization */ /* memset(o->pixels[0],0,o->pitches[0]*o->h); memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2)); memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2)); */ /* Convert */ for(y=0; y<s->h && y<o->h; y++) { p=((Uint8 *) s->pixels)+s->pitch*y; op[0]=o->pixels[0]+o->pitches[0]*y; op[1]=o->pixels[1]+o->pitches[1]*(y/2); op[2]=o->pixels[2]+o->pitches[2]*(y/2); for(x=0; x<s->w && x<o->w; x++) { RGBtoYUV(p, yuv, monochrome, luminance); *(op[0]++)=yuv[0]; if(x%2==0 && y%2==0) { *(op[1]++)=yuv[2]; *(op[2]++)=yuv[1]; } p+=s->format->BytesPerPixel; } } SDL_UnlockYUVOverlay(o); SDL_UnlockSurface(s); }
void ConvertRGBtoNV12(Uint8 *rgb, Uint8 *out, int w, int h, int monochrome, int luminance) { int x, y; int yuv[3]; Uint8 *op[2]; op[0] = out; op[1] = op[0] + w*h; for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { RGBtoYUV(rgb, yuv, monochrome, luminance); *(op[0]++) = yuv[0]; if (x % 2 == 0 && y % 2 == 0) { *(op[1]++) = yuv[1]; *(op[1]++) = yuv[2]; } rgb += 3; } } }
/** * HistogramStretch * \param method: 0 = luminance (default), 1 = linked channels , 2 = independent channels. * \param threshold: minimum percentage level in the histogram to recognize it as meaningful. Range: 0.0 to 1.0; default = 0; typical = 0.005 (0.5%); * \return true if everything is ok * \author [dave] and [nipper]; changes [DP] */ bool CxImage::HistogramStretch(long method, double threshold) { if (!pDib) return false; double dbScaler = 50.0/head.biHeight; long x,y; if ((head.biBitCount==8) && IsGrayScale()){ double p[256]; memset(p, 0, 256*sizeof(double)); for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); if (info.nEscape) break; for (x=0; x<head.biWidth; x++) { p[BlindGetPixelIndex(x, y)]++; } } double maxh = 0; for (y=0; y<255; y++) if (maxh < p[y]) maxh = p[y]; threshold *= maxh; int minc = 0; while (minc<255 && p[minc]<=threshold) minc++; int maxc = 255; while (maxc>0 && p[maxc]<=threshold) maxc--; if (minc == 0 && maxc == 255) return true; if (minc >= maxc) return true; // calculate LUT BYTE lut[256]; for (x = 0; x <256; x++){ lut[x] = (BYTE)max(0,min(255,(255 * (x - minc) / (maxc - minc)))); } for (y=0; y<head.biHeight; y++) { if (info.nEscape) break; info.nProgress = (long)(50.0+y*dbScaler); for (x=0; x<head.biWidth; x++) { BlindSetPixelIndex(x, y, lut[BlindGetPixelIndex(x, y)]); } } } else { switch(method){ case 1: { // <nipper> double p[256]; memset(p, 0, 256*sizeof(double)); for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); if (info.nEscape) break; for (x=0; x<head.biWidth; x++) { RGBQUAD color = BlindGetPixelColor(x, y); p[color.rgbRed]++; p[color.rgbBlue]++; p[color.rgbGreen]++; } } double maxh = 0; for (y=0; y<255; y++) if (maxh < p[y]) maxh = p[y]; threshold *= maxh; int minc = 0; while (minc<255 && p[minc]<=threshold) minc++; int maxc = 255; while (maxc>0 && p[maxc]<=threshold) maxc--; if (minc == 0 && maxc == 255) return true; if (minc >= maxc) return true; // calculate LUT BYTE lut[256]; for (x = 0; x <256; x++){ lut[x] = (BYTE)max(0,min(255,(255 * (x - minc) / (maxc - minc)))); } // normalize image for (y=0; y<head.biHeight; y++) { if (info.nEscape) break; info.nProgress = (long)(50.0+y*dbScaler); for (x=0; x<head.biWidth; x++) { RGBQUAD color = BlindGetPixelColor(x, y); color.rgbRed = lut[color.rgbRed]; color.rgbBlue = lut[color.rgbBlue]; color.rgbGreen = lut[color.rgbGreen]; BlindSetPixelColor(x, y, color); } } } break; case 2: { // <nipper> double pR[256]; memset(pR, 0, 256*sizeof(double)); double pG[256]; memset(pG, 0, 256*sizeof(double)); double pB[256]; memset(pB, 0, 256*sizeof(double)); for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); if (info.nEscape) break; for (long x=0; x<head.biWidth; x++) { RGBQUAD color = BlindGetPixelColor(x, y); pR[color.rgbRed]++; pB[color.rgbBlue]++; pG[color.rgbGreen]++; } } double maxh = 0; for (y=0; y<255; y++) if (maxh < pR[y]) maxh = pR[y]; double threshold2 = threshold*maxh; int minR = 0; while (minR<255 && pR[minR]<=threshold2) minR++; int maxR = 255; while (maxR>0 && pR[maxR]<=threshold2) maxR--; maxh = 0; for (y=0; y<255; y++) if (maxh < pG[y]) maxh = pG[y]; threshold2 = threshold*maxh; int minG = 0; while (minG<255 && pG[minG]<=threshold2) minG++; int maxG = 255; while (maxG>0 && pG[maxG]<=threshold2) maxG--; maxh = 0; for (y=0; y<255; y++) if (maxh < pB[y]) maxh = pB[y]; threshold2 = threshold*maxh; int minB = 0; while (minB<255 && pB[minB]<=threshold2) minB++; int maxB = 255; while (maxB>0 && pB[maxB]<=threshold2) maxB--; if (minR == 0 && maxR == 255 && minG == 0 && maxG == 255 && minB == 0 && maxB == 255) return true; // calculate LUT BYTE lutR[256]; BYTE range = maxR - minR; if (range != 0) { for (x = 0; x <256; x++){ lutR[x] = (BYTE)max(0,min(255,(255 * (x - minR) / range))); } } else lutR[minR] = minR; BYTE lutG[256]; range = maxG - minG; if (range != 0) { for (x = 0; x <256; x++){ lutG[x] = (BYTE)max(0,min(255,(255 * (x - minG) / range))); } } else lutG[minG] = minG; BYTE lutB[256]; range = maxB - minB; if (range != 0) { for (x = 0; x <256; x++){ lutB[x] = (BYTE)max(0,min(255,(255 * (x - minB) / range))); } } else lutB[minB] = minB; // normalize image for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(50.0+y*dbScaler); if (info.nEscape) break; for (x=0; x<head.biWidth; x++) { RGBQUAD color = BlindGetPixelColor(x, y); color.rgbRed = lutR[color.rgbRed]; color.rgbBlue = lutB[color.rgbBlue]; color.rgbGreen = lutG[color.rgbGreen]; BlindSetPixelColor(x, y, color); } } } break; default: { // <dave> double p[256]; memset(p, 0, 256*sizeof(double)); for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); if (info.nEscape) break; for (x=0; x<head.biWidth; x++) { RGBQUAD color = BlindGetPixelColor(x, y); p[RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue)]++; } } double maxh = 0; for (y=0; y<255; y++) if (maxh < p[y]) maxh = p[y]; threshold *= maxh; int minc = 0; while (minc<255 && p[minc]<=threshold) minc++; int maxc = 255; while (maxc>0 && p[maxc]<=threshold) maxc--; if (minc == 0 && maxc == 255) return true; if (minc >= maxc) return true; // calculate LUT BYTE lut[256]; for (x = 0; x <256; x++){ lut[x] = (BYTE)max(0,min(255,(255 * (x - minc) / (maxc - minc)))); } for(y=0; y<head.biHeight; y++){ info.nProgress = (long)(50.0+y*dbScaler); if (info.nEscape) break; for(x=0; x<head.biWidth; x++){ RGBQUAD color = BlindGetPixelColor( x, y ); RGBQUAD yuvClr = RGBtoYUV(color); yuvClr.rgbRed = lut[yuvClr.rgbRed]; color = YUVtoRGB(yuvClr); BlindSetPixelColor( x, y, color ); } } } } } return true; }
//////////////////////////////////////////////////////////////////////////////// // HistogramRoot function by <dave> : dave(at)posortho(dot)com bool CxImage::HistogramRoot() { if (!pDib) return false; //q(i,j) = sqrt(|p(i,j)|); int x, y, i; RGBQUAD color; RGBQUAD yuvClr; double dtmp; unsigned int YVal, high = 1; // Find Highest Luminance Value in the Image if( head.biClrUsed == 0 ){ // No Palette for(y=0; y < head.biHeight; y++){ info.nProgress = (long)(50*y/head.biHeight); if (info.nEscape) break; for(x=0; x < head.biWidth; x++){ color = BlindGetPixelColor( x, y ); YVal = (unsigned int)RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); if (YVal > high ) high = YVal; } } } else { // Palette for(i = 0; i < (int)head.biClrUsed; i++){ color = GetPaletteColor((BYTE)i); YVal = (unsigned int)RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); if (YVal > high ) high = YVal; } } // Root Operator double k = 128.0 / ::log( 1.0 + (double)high ); if( head.biClrUsed == 0 ){ for( y = 0; y < head.biHeight; y++ ){ info.nProgress = (long)(50+50*y/head.biHeight); if (info.nEscape) break; for( x = 0; x < head.biWidth; x++ ){ color = BlindGetPixelColor( x, y ); yuvClr = RGBtoYUV( color ); dtmp = k * ::sqrt( (double)yuvClr.rgbRed ); if ( dtmp > 255.0 ) dtmp = 255.0; if ( dtmp < 0 ) dtmp = 0; yuvClr.rgbRed = (BYTE)dtmp; color = YUVtoRGB( yuvClr ); BlindSetPixelColor( x, y, color ); } } } else { for(i = 0; i < (int)head.biClrUsed; i++){ color = GetPaletteColor( (BYTE)i ); yuvClr = RGBtoYUV( color ); dtmp = k * ::sqrt( (double)yuvClr.rgbRed ); if ( dtmp > 255.0 ) dtmp = 255.0; if ( dtmp < 0 ) dtmp = 0; yuvClr.rgbRed = (BYTE)dtmp; color = YUVtoRGB( yuvClr ); SetPaletteColor( (BYTE)i, color ); } } return true; }
//////////////////////////////////////////////////////////////////////////////// // HistogramNormalize function by <dave> : dave(at)posortho(dot)com bool CxImage::HistogramNormalize() { if (!pDib) return false; int histogram[256]; int threshold_intensity, intense; int x, y, i; unsigned int normalize_map[256]; unsigned int high, low, YVal; RGBQUAD color; RGBQUAD yuvClr; memset( &histogram, 0, sizeof( int ) * 256 ); memset( &normalize_map, 0, sizeof( unsigned int ) * 256 ); // form histogram for(y=0; y < head.biHeight; y++){ info.nProgress = (long)(50*y/head.biHeight); if (info.nEscape) break; for(x=0; x < head.biWidth; x++){ color = BlindGetPixelColor( x, y ); YVal = (unsigned int)RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); histogram[YVal]++; } } // find histogram boundaries by locating the 1 percent levels threshold_intensity = ( head.biWidth * head.biHeight) / 100; intense = 0; for( low = 0; low < 255; low++ ){ intense += histogram[low]; if( intense > threshold_intensity ) break; } intense = 0; for( high = 255; high != 0; high--){ intense += histogram[ high ]; if( intense > threshold_intensity ) break; } if ( low == high ){ // Unreasonable contrast; use zero threshold to determine boundaries. threshold_intensity = 0; intense = 0; for( low = 0; low < 255; low++){ intense += histogram[low]; if( intense > threshold_intensity ) break; } intense = 0; for( high = 255; high != 0; high-- ){ intense += histogram [high ]; if( intense > threshold_intensity ) break; } } if( low == high ) return false; // zero span bound // Stretch the histogram to create the normalized image mapping. for(i = 0; i <= 255; i++){ if ( i < (int) low ){ normalize_map[i] = 0; } else { if(i > (int) high) normalize_map[i] = 255; else normalize_map[i] = ( 255 - 1) * ( i - low) / ( high - low ); } } // Normalize if( head.biClrUsed == 0 ){ for( y = 0; y < head.biHeight; y++ ){ info.nProgress = (long)(50+50*y/head.biHeight); if (info.nEscape) break; for( x = 0; x < head.biWidth; x++ ){ color = BlindGetPixelColor( x, y ); yuvClr = RGBtoYUV( color ); yuvClr.rgbRed = (BYTE)normalize_map[yuvClr.rgbRed]; color = YUVtoRGB( yuvClr ); BlindSetPixelColor( x, y, color ); } } } else { for(i = 0; i < (int)head.biClrUsed; i++){ color = GetPaletteColor( (BYTE)i ); yuvClr = RGBtoYUV( color ); yuvClr.rgbRed = (BYTE)normalize_map[yuvClr.rgbRed]; color = YUVtoRGB( yuvClr ); SetPaletteColor( (BYTE)i, color ); } } return true; }
//////////////////////////////////////////////////////////////////////////////// // HistogramEqualize function by <dave> : dave(at)posortho(dot)com bool CxImage::HistogramEqualize() { if (!pDib) return false; int histogram[256]; int map[256]; int equalize_map[256]; int x, y, i, j; RGBQUAD color; RGBQUAD yuvClr; unsigned int YVal, high, low; memset( &histogram, 0, sizeof(int) * 256 ); memset( &map, 0, sizeof(int) * 256 ); memset( &equalize_map, 0, sizeof(int) * 256 ); // form histogram for(y=0; y < head.biHeight; y++){ info.nProgress = (long)(50*y/head.biHeight); if (info.nEscape) break; for(x=0; x < head.biWidth; x++){ color = BlindGetPixelColor( x, y ); YVal = (unsigned int)RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); histogram[YVal]++; } } // integrate the histogram to get the equalization map. j = 0; for(i=0; i <= 255; i++){ j += histogram[i]; map[i] = j; } // equalize low = map[0]; high = map[255]; if (low == high) return false; for( i = 0; i <= 255; i++ ){ equalize_map[i] = (unsigned int)((((double)( map[i] - low ) ) * 255) / ( high - low ) ); } // stretch the histogram if(head.biClrUsed == 0){ // No Palette for( y = 0; y < head.biHeight; y++ ){ info.nProgress = (long)(50+50*y/head.biHeight); if (info.nEscape) break; for( x = 0; x < head.biWidth; x++ ){ color = BlindGetPixelColor( x, y ); yuvClr = RGBtoYUV(color); yuvClr.rgbRed = (BYTE)equalize_map[yuvClr.rgbRed]; color = YUVtoRGB(yuvClr); BlindSetPixelColor( x, y, color ); } } } else { // Palette for( i = 0; i < (int)head.biClrUsed; i++ ){ color = GetPaletteColor((BYTE)i); yuvClr = RGBtoYUV(color); yuvClr.rgbRed = (BYTE)equalize_map[yuvClr.rgbRed]; color = YUVtoRGB(yuvClr); SetPaletteColor( (BYTE)i, color ); } } return true; }
/** * HistogramStretch * \param method: 0 = luminance (default), 1 = linked channels , 2 = independent channels. * \return true if everything is ok * \author [dave] and [nipper] */ bool CxImage::HistogramStretch(long method) { if (!pDib) return false; if ((head.biBitCount==8) && IsGrayScale()){ // get min/max info BYTE minc = 255, maxc = 0; BYTE gray; long y; double dbScaler = 50.0/head.biHeight; for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); for (long x=0; x<head.biWidth; x++) { gray = GetPixelIndex(x, y); if (gray < minc) minc = gray; if (gray > maxc) maxc = gray; } } if (minc == 0 && maxc == 255) return true; // calculate LUT BYTE lut[256]; BYTE range = maxc - minc; if (range != 0){ for (long x = minc; x <= maxc; x++){ lut[x] = (BYTE)(255 * (x - minc) / range); } } else lut[minc] = minc; for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(50.0+y*dbScaler); for (long x=0; x<head.biWidth; x++) { SetPixelIndex(x, y, lut[GetPixelIndex(x, y)]); } } } else { switch(method){ case 1: { // <nipper> // get min/max info BYTE minc = 255, maxc = 0; RGBQUAD color; long y; for (y=0; y<head.biHeight; y++) { for (long x=0; x<head.biWidth; x++) { color = GetPixelColor(x, y); if (color.rgbRed < minc) minc = color.rgbRed; if (color.rgbBlue < minc) minc = color.rgbBlue; if (color.rgbGreen < minc) minc = color.rgbGreen; if (color.rgbRed > maxc) maxc = color.rgbRed; if (color.rgbBlue > maxc) maxc = color.rgbBlue; if (color.rgbGreen > maxc) maxc = color.rgbGreen; } } if (minc == 0 && maxc == 255) return true; // calculate LUT BYTE lut[256]; BYTE range = maxc - minc; if (range != 0){ for (long x = minc; x <= maxc; x++){ lut[x] = (BYTE)(255 * (x - minc) / range); } } else lut[minc] = minc; // normalize image double dbScaler = 100.0/head.biHeight; for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); for (long x=0; x<head.biWidth; x++) { color = GetPixelColor(x, y); color.rgbRed = lut[color.rgbRed]; color.rgbBlue = lut[color.rgbBlue]; color.rgbGreen = lut[color.rgbGreen]; SetPixelColor(x, y, color); } } } break; case 2: { // <nipper> // get min/max info BYTE minR = 255, maxR = 0; BYTE minG = 255, maxG = 0; BYTE minB = 255, maxB = 0; RGBQUAD color; long y; for (y=0; y<head.biHeight; y++) { for (long x=0; x<head.biWidth; x++) { color = GetPixelColor(x, y); if (color.rgbRed < minR) minR = color.rgbRed; if (color.rgbBlue < minB) minB = color.rgbBlue; if (color.rgbGreen < minG) minG = color.rgbGreen; if (color.rgbRed > maxR) maxR = color.rgbRed; if (color.rgbBlue > maxB) maxB = color.rgbBlue; if (color.rgbGreen > maxG) maxG = color.rgbGreen; } } if (minR == 0 && maxR == 255 && minG == 0 && maxG == 255 && minB == 0 && maxB == 255) return true; // calculate LUT BYTE lutR[256]; BYTE range = maxR - minR; if (range != 0) { for (long x = minR; x <= maxR; x++){ lutR[x] = (BYTE)(255 * (x - minR) / range); } } else lutR[minR] = minR; BYTE lutG[256]; range = maxG - minG; if (range != 0) { for (long x = minG; x <= maxG; x++){ lutG[x] = (BYTE)(255 * (x - minG) / range); } } else lutG[minG] = minG; BYTE lutB[256]; range = maxB - minB; if (range != 0) { for (long x = minB; x <= maxB; x++){ lutB[x] = (BYTE)(255 * (x - minB) / range); } } else lutB[minB] = minB; // normalize image double dbScaler = 100.0/head.biHeight; for (y=0; y<head.biHeight; y++) { info.nProgress = (long)(y*dbScaler); for (long x=0; x<head.biWidth; x++) { color = GetPixelColor(x, y); color.rgbRed = lutR[color.rgbRed]; color.rgbBlue = lutB[color.rgbBlue]; color.rgbGreen = lutG[color.rgbGreen]; SetPixelColor(x, y, color); } } } break; default: { // <dave> // S = ( R - C ) ( B - A / D - C ) double alimit = 0.0; double blimit = 255.0; double lowerc = 255.0; double upperd = 0.0; double tmpGray; RGBQUAD color; RGBQUAD yuvClr; double stretcheds; if ( head.biClrUsed == 0 ){ long x, y, xmin, xmax, ymin, ymax; xmin = ymin = 0; xmax = head.biWidth; ymax = head.biHeight; for( y = ymin; y < ymax; y++ ){ info.nProgress = (long)(50*y/ymax); for( x = xmin; x < xmax; x++ ){ color = GetPixelColor( x, y ); tmpGray = RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); if ( tmpGray < lowerc ) lowerc = tmpGray; if ( tmpGray > upperd ) upperd = tmpGray; } } if (upperd==lowerc) return false; for( y = ymin; y < ymax; y++ ){ info.nProgress = (long)(50+50*y/ymax); for( x = xmin; x < xmax; x++ ){ color = GetPixelColor( x, y ); yuvClr = RGBtoYUV(color); // Stretch Luminance tmpGray = (double)yuvClr.rgbRed; stretcheds = (double)(tmpGray - lowerc) * ( (blimit - alimit) / (upperd - lowerc) ); // + alimit; if ( stretcheds < 0.0 ) stretcheds = 0.0; else if ( stretcheds > 255.0 ) stretcheds = 255.0; yuvClr.rgbRed = (BYTE)stretcheds; color = YUVtoRGB(yuvClr); SetPixelColor( x, y, color ); } } } else { DWORD j; for( j = 0; j < head.biClrUsed; j++ ){ color = GetPaletteColor( (BYTE)j ); tmpGray = RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); if ( tmpGray < lowerc ) lowerc = tmpGray; if ( tmpGray > upperd ) upperd = tmpGray; } if (upperd==lowerc) return false; for( j = 0; j < head.biClrUsed; j++ ){ color = GetPaletteColor( (BYTE)j ); yuvClr = RGBtoYUV( color ); // Stretch Luminance tmpGray = (double)yuvClr.rgbRed; stretcheds = (double)(tmpGray - lowerc) * ( (blimit - alimit) / (upperd - lowerc) ); // + alimit; if ( stretcheds < 0.0 ) stretcheds = 0.0; else if ( stretcheds > 255.0 ) stretcheds = 255.0; yuvClr.rgbRed = (BYTE)stretcheds; color = YUVtoRGB(yuvClr); SetPaletteColor( (BYTE)j, color ); } } } } } return true; }
//////////////////////////////////////////////////////////////////////////////// // HistogramLog function by <dave> : dave(at)posortho(dot)com bool CxImage::HistogramLog() { if (!pDib) return false; //q(i,j) = 255/log(1 + |high|) * log(1 + |p(i,j)|); int32_t x, y, i; RGBQUAD color; RGBQUAD yuvClr; uint32_t YVal, high = 1; // Find Highest Luminance Value in the Image if( head.biClrUsed == 0 ){ // No Palette for(y=0; y < head.biHeight; y++){ info.nProgress = (int32_t)(50*y/head.biHeight); if (info.nEscape) break; for(x=0; x < head.biWidth; x++){ color = BlindGetPixelColor( x, y ); YVal = (uint32_t)RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); if (YVal > high ) high = YVal; } } } else { // Palette for(i = 0; i < (int32_t)head.biClrUsed; i++){ color = GetPaletteColor((uint8_t)i); YVal = (uint32_t)RGB2GRAY(color.rgbRed, color.rgbGreen, color.rgbBlue); if (YVal > high ) high = YVal; } } // Logarithm Operator double k = 255.0 / ::log( 1.0 + (double)high ); if( head.biClrUsed == 0 ){ for( y = 0; y < head.biHeight; y++ ){ info.nProgress = (int32_t)(50+50*y/head.biHeight); if (info.nEscape) break; for( x = 0; x < head.biWidth; x++ ){ color = BlindGetPixelColor( x, y ); yuvClr = RGBtoYUV( color ); yuvClr.rgbRed = (uint8_t)(k * ::log( 1.0 + (double)yuvClr.rgbRed ) ); color = YUVtoRGB( yuvClr ); BlindSetPixelColor( x, y, color ); } } } else { for(i = 0; i < (int32_t)head.biClrUsed; i++){ color = GetPaletteColor( (uint8_t)i ); yuvClr = RGBtoYUV( color ); yuvClr.rgbRed = (uint8_t)(k * ::log( 1.0 + (double)yuvClr.rgbRed ) ); color = YUVtoRGB( yuvClr ); SetPaletteColor( (uint8_t)i, color ); } } return true; }
int main(int argc, char *argv[]) { #if XPAR_MICROBLAZE_0_USE_ICACHE microblaze_init_icache_range(0, XPAR_MICROBLAZE_0_CACHE_BYTE_SIZE); microblaze_enable_icache(); #endif #if XPAR_MICROBLAZE_0_USE_DCACHE microblaze_init_dcache_range(0, XPAR_MICROBLAZE_0_DCACHE_BYTE_SIZE); microblaze_enable_dcache(); #endif rows imago; int crom_flag,i,j,lvl,temprows,tempblocks,offset[]={0,4,32,36},simb,size; int luminance_table[]={16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99}; int crominance_table[]={17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,}; int lum_tab_corr[64], crom_tab_corr[64]; info infoimago; int_rows intimago; huffman_tab tbl_dclum, tbl_aclum, tbl_dccrom, tbl_accrom; FILE *file_in, *file_out; unsigned char buffer,field_free_space=8; buffer = 0x0; //UCCIDI UCCIDI UCCIDI /* xil_printf("\n\rINIZIO COMPRESSIONE!!!"); */ if (argc > 1) { file_in=fopen(argv[1],"r"); } else file_in=fopen("./software/apps/jpeg/img.ppm","r"); //binario? if (!file_in) return(0); //Avvio del timer /* XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0); XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK ); */ imago=rdppm(&infoimago, file_in); // Stop timer e stampa dei cicli di computazione /* XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0); xil_printf("\n\rlettura file: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0)); */ if (imago==NULL) return(0); fclose(file_in); intimago=(p_intblock *) malloc (infoimago.numrows*sizeof(p_intblock)); for (i=0;i<infoimago.numrows;i++) *(intimago+i)=(intblock *) malloc (infoimago.numblocks*sizeof(intblock)); if (infoimago.color==1) { //Avvio del timer /* XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0); XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK ); */ for (i=0;i<infoimago.numrows;i++) for (j=0;j<infoimago.numblocks;j++) RGBtoYUV(&(*(imago+i)+j)->comp1[0],&(*(imago+i)+j)->comp2[0],&(*(imago+i)+j)->comp3[0]); // Stop timer e stampa dei cicli di computazione /* XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0); xil_printf("\n\rrgb to yuv: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0)); */ } if (infoimago.color==1) { rows tempimago; //Avvio del timer /* XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0); XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK ); */ tempimago=expand_image(imago,infoimago.numrows,infoimago.numblocks,&temprows,&tempblocks); for (i=0;i<temprows;i++) for (j=0;j<tempblocks;j++) downsample((*(tempimago+i)+j),(*(imago+i/2)+j/2),offset[j%2+(i%2)*2]); for (i=0;i<temprows;i++) free(*(tempimago+i)); free(tempimago); // Stop timer e stampa dei cicli di computazione /* XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0); xil_printf("\n\rdownsample e espandsione: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0)); */ } lvl= 80; //Avvio del timer /* XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0); XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK ); */ set_quantization_tbl(lvl, &luminance_table[0]); set_quantization_tbl(lvl, &crominance_table[0]); correct_quantization_tbl(luminance_table, lum_tab_corr); correct_quantization_tbl(crominance_table, crom_tab_corr); // Stop timer e stampa dei cicli di computazione /* XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0); xil_printf("\n\rrset quantization table: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0)); */ //Avvio del timer /* XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0); XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK ); */ for (i=0;i<infoimago.numrows;i++) for (j=0;j<infoimago.numblocks;j++) { if ((infoimago.color==1)&&(i<temprows/2)&&(j<tempblocks/2)) crom_flag=1; else crom_flag=0; DCT_and_quantization(*(imago+i)+j,lum_tab_corr,crom_tab_corr,*(intimago+i)+j,crom_flag); } for (i=0;i<infoimago.numrows;i++) free(*(imago+i)); free(imago); for (i=0;i<infoimago.numrows;i++) for (j=0;j<infoimago.numblocks;j++) arrange(*(intimago+i)+j); // Stop timer e stampa dei cicli di computazione /* XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0); xil_printf("\n\rdct and quantization: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0)); */ //Avvio del timer /*XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0); XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK ); */ initialize_huff_tbl(&tbl_dclum,0); initialize_huff_tbl(&tbl_aclum,16); initialize_huff_tbl(&tbl_dccrom,1); initialize_huff_tbl(&tbl_accrom,17); arrange_table(&luminance_table[0]); arrange_table(&crominance_table[0]); if (argc > 2) file_out=fopen(argv[2],"w"); else file_out=fopen("img_out.jpg","w"); //binario?? writeheaders (file_out, infoimago, &luminance_table[0], &crominance_table[0], tbl_aclum,tbl_accrom,tbl_dclum,tbl_dccrom); if (infoimago.color==1) { int oldlum=0, oldcrom1=0, oldcrom2=0; for (i=0;i<infoimago.numrows;i+=2) for (j=0;j<infoimago.numblocks;j+=2) { writescan(oldlum,(*(intimago+i)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago); if ((j+1)<infoimago.numblocks) { writescan((*(intimago+i)+j)->intcomp1[0],(*(intimago+i)+j+1)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago); if ((i+1)<infoimago.numrows) { writescan((*(intimago+i)+j+1)->intcomp1[0],(*(intimago+i+1)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago); writescan((*(intimago+i+1)+j)->intcomp1[0],(*(intimago+i+1)+j+1)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago); oldlum=(*(intimago+i+1)+j+1)->intcomp1[0]; } else { simb=findcode(0,tbl_dclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_aclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_dclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_aclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); oldlum=(*(intimago+i)+j+1)->intcomp1[0]; } } else { simb=findcode(0,tbl_dclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_aclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); if ((i+1)<infoimago.numrows) { writescan((*(intimago+i)+j)->intcomp1[0],(*(intimago+i+1)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago); simb=findcode(0,tbl_dclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_aclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); oldlum=(*(intimago+i+1)+j)->intcomp1[0]; } else { simb=findcode(0,tbl_dclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_aclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_dclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); simb=findcode(0,tbl_aclum,&size); wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0); } } writescan(oldcrom1,(*(intimago+i/2)+j/2)->intcomp2,&field_free_space,&buffer,file_out,tbl_accrom,tbl_dccrom,infoimago); writescan(oldcrom2,(*(intimago+i/2)+j/2)->intcomp3,&field_free_space,&buffer,file_out,tbl_accrom,tbl_dccrom,infoimago); oldcrom1=(*(intimago+i/2)+j/2)->intcomp2[0]; oldcrom2=(*(intimago+i/2)+j/2)->intcomp3[0]; } } else { int oldlum=0; for (i=0;i<infoimago.numrows;i++) for (j=0;j<infoimago.numblocks;j++) { writescan(oldlum,(*(intimago+i)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago); oldlum=(*(intimago+i)+j)->intcomp1[0]; } } write_end_of_image(file_out); fclose(file_out); // Stop timer e stampa dei cicli di computazione /* XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0); xil_printf("\n\rcodifiche entropiche: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0)); xil_printf("\n\rFINE COMPRESSIONE!!!"); */ return 0; }