void ImgConverter::Greyscale(char *CurrentImg) { Input.ReadFromFile(CurrentImg); // convert each pixel to grayscale using RGB->YUV for (int j = 0; j < Input.TellHeight(); j++) { for (int i = 0; i < Input.TellWidth(); i++) { int Temp = (int)floor(0.299*Input(i, j)->Red + 0.587*Input(i, j)->Green + 0.114*Input(i, j)->Blue); ebmpBYTE TempBYTE = (ebmpBYTE)Temp; Input(i, j)->Red = TempBYTE; Input(i, j)->Green = TempBYTE; Input(i, j)->Blue = TempBYTE; } } // Create a grayscale color table if necessary if (Input.TellBitDepth() < 16) { CreateGrayscaleColorTable(Input); } // write the output file Input.WriteToFile(temp); }
// creates a BMP from a CImage // assumes all float values have already been scaled to [0.0, 255.0] BMP * CImage::toBmp() { BMP * bmp = new BMP(); bmp->SetSize(m_width, m_height); // for smaller output file size bmp->SetBitDepth(8); // 8-bit bitmaps need a color table CreateGrayscaleColorTable(*bmp); for (int col = 0; col < m_width; col++) { for (int row = 0; row < m_height; row++) { // Output is grayscale, so R, G, and B components are equal. // ScaleAndDisplayDisparityValues() et. al. in stereo.cpp have already // scaled all float pixel values to [0, 255]. ebmpBYTE byteVal = (ebmpBYTE) getValue(row, col, 0); (* bmp)(col, row)->Red = byteVal; (* bmp)(col, row)->Green = byteVal; (* bmp)(col, row)->Blue = byteVal; } } return bmp; }
// same as above, but highlights a range of depths in red, // and, colors all depths outside that range black in refImage // highlight_low and high are values in [1,100] BMP * CImage::toHighlightedBmp(int highlight_low, int highlight_high, BMP * refBmp) { // scale range to be highlighted to [0,255] highlight_low = ((float) highlight_low) * 255.0/100.0 - 2.55; highlight_high = ((float) highlight_high) * 255.0/100.0; BMP * bmp = new BMP(); bmp->SetSize(m_width, m_height); // for smaller output file size bmp->SetBitDepth(8); // 8-bit bitmaps need a color table CreateGrayscaleColorTable(*bmp); // add red to the color table RGBApixel highlightColor; highlightColor.Red = 255; highlightColor.Green = 0; highlightColor.Blue = 0; highlightColor.Alpha = 0; bmp->SetColor(highlight_low, highlightColor); // copy pixels to bmp for (int col = 0; col < m_width; col++) { for (int row = 0; row < m_height; row++) { float pixel = getValue(row, col, 0); if (highlight_low <= pixel && pixel <= highlight_high) { (* bmp)(col, row)->Red = 255; (* bmp)(col, row)->Green = 0; (* bmp)(col, row)->Blue = 0; } else { // Output is grayscale, so R, G, and B components are equal. // ScaleAndDisplayDisparityValues() et. al. in stereo.cpp have already // scaled all float pixel values to [0, 255]. ebmpBYTE byteVal = (ebmpBYTE) pixel; (* bmp)(col, row)->Red = byteVal; (* bmp)(col, row)->Green = byteVal; (* bmp)(col, row)->Blue = byteVal; // color non-highlighted areas black in refBmp (* refBmp)(col, row)->Red = 0; (* refBmp)(col, row)->Green = 0; (* refBmp)(col, row)->Blue = 0; } } } return bmp; }