c++ - Converting gray scale BMP to full color -
i'm learning basic image processing , using gray scale bmp file work algorithms i'd convert code put out color bmp files instead of gray scale. i'm using easybmp library , have following read in , write bmp file:
bool image::readfrombmpfile(const std::string & inputfilename){ bool success = true; // use bmp object read image bmp inputimage; success = inputimage.readfromfile(inputfilename.c_str() ); if( success ){ // allocate memory image (deleting old, if exists) m_numrows = inputimage.tellheight(); m_numcols = inputimage.tellwidth(); if( m_pixels != null ){ // deallocate old memory delete [] m_pixels; } m_pixels = new double[m_numrows * m_numcols]; // copy pixels for( int r = 0; r < m_numrows; ++r ){ for( int c = 0; c < m_numcols; ++c ){ rgbapixel pixelval = inputimage.getpixel(c, r); double val = (double) pixelval.blue + (double) pixelval.green + (double) pixelval.red; val = (val / 3.0 + 0.5); m_pixels[r * m_numcols + c] = val; } } } return success; } bool image::writetobmpfile(const std::string & outputfilename){ bool success = true; if( m_pixels != null ){ // create bitmap image bmp outputimage; outputimage.setsize(m_numcols, m_numrows); outputimage.setbitdepth( 24 ); double maxval = m_pixels[0]; double minval = m_pixels[0]; // maximum , minimum values for( int = 1; < m_numrows * m_numcols; ++i ){ if( m_pixels[i] > maxval ){ maxval = m_pixels[i]; } if( m_pixels[i] <= minval ){ minval = m_pixels[i]; } } for( int r = 0; r < m_numrows; ++r ){ for( int c = 0; c < m_numcols; ++c ){ // pixel value , clamp between 0 , 255 double val = 255.0 * (m_pixels[r * m_numcols + c] - minval) / (maxval - minval); if( val < 0 ){ val = 0; } if( val > 255 ){ val = 255; } // set output color based on mapping rgbapixel pixelval; pixelval.blue = (int)val; pixelval.green = (int)val; pixelval.red = (int)val; outputimage.setpixel(c, r, pixelval); } } // write file success = outputimage.writetofile( outputfilename.c_str() ); } else { success = false; } return success; }
what kind of steps try make program compatible rgb images?
Comments
Post a Comment