01 | #include<QApplication> |
05 | const QString rgbFile( "special.jpg" ); |
06 | const QString grayFile( "gray.jpg" ); |
08 | static bool convertToGray(); |
10 | int main( int argc, char *argv[]) |
12 | QApplication app(argc,argv); |
13 | QLabel rgbLabel,grayLabel; |
19 | rgbLabel.setPixmap(QPixmap(rgbFile)); |
20 | grayLabel.setPixmap(QPixmap(grayFile)); |
27 | static bool convertToGray() |
29 | QImage rgbImage(rgbFile); |
30 | QSize size=rgbImage.size(); |
31 | QImage grayImage(size,QImage::Format_Indexed8); |
32 | int width=size.width(); |
33 | int height=size.height(); |
34 | uchar * rgbImageData=rgbImage.bits(); |
35 | uchar * grayImageData=grayImage.bits(); |
37 | if (rgbImage.isGrayscale()){ |
38 | qDebug()<< "Image is already gray!Conversion stopped!" ; |
43 | int realWidth1=rgbImage.bytesPerLine(); |
44 | int realWidth2=grayImage.bytesPerLine(); |
45 | uchar * backup1=rgbImageData; |
46 | uchar * backup2=grayImageData; |
50 | rgbImageData=backup1+realWidth1*i, |
51 | grayImageData=backup2+realWidth2*i){ |
52 | for ( int j=0;j<width;j++){ |
53 | *grayImageData=*(rgbImageData+1); |
59 | QVector<QRgb> grayColorTable; |
61 | for ( int i=0;i<256;i++){ |
62 | grayColorTable.append(rgb); |
66 | grayImage.setColorTable(grayColorTable); |
67 | grayImage.save(grayFile); |
Qt生成灰度图
http://blog.csdn.net/NRC_DouNingBo/archive/2010/10/09/5929106.aspx
http://www.oschina.net/code/snippet_96486_4027