大概思路:图像的位深度,就存储在文件的byte数组里,找到位置,就能获取到。
//获取bmp图片位深
int getBmpBitDepth(constQString&imagePath)
{
QFile imageFile(imagePath);
if(!imageFile.open(QIODevice::ReadWrite)){
return -1;
}
QByteArray ba =imageFile.readAll();
int bitsPerPixel =(ba[28]& 0xff)| (ba[29]& 0xff)<< 8;
printf("Bmpdepth=%d\n",bitsPerPixel);
imageFile.close();
return bitsPerPixel;
}
//获取png图片位深
int getPngBitDepth(constQString&imagePath)
{
QFile imageFile(imagePath);
if(!imageFile.open(QIODevice::ReadWrite)){
return -1;
}
QByteArray ba =imageFile.readAll();
int bitsPerPixel =ba[24]& 0xff;
if((ba[25]& 0xff)== 2){
bitsPerPixel*= 3;
}else if((ba[25]& 0xff)== 6){
bitsPerPixel*= 4;
}
printf("Pngdepth=%d\n",bitsPerPixel);
imageFile.close();
return bitsPerPixel;
}
//获取Jpeg图片位深
int getJpegBitDepth(constQString&imagePath)
{
QFile imageFile(imagePath);
if(!imageFile.open(QIODevice::ReadWrite)){
return -1;
}
QByteArray ba =imageFile.readAll();
int i =2;
int bitsPerPixel=0;
while(true){
int marker =(ba[i] &0xff)<< 8| (ba[i+ 1]& 0xff);
int size =(ba[i +2]& 0xff)<< 8| (ba[i+ 3]& 0xff);
if(marker >=0xffc0&& marker<= 0xffcf&& marker!= 0xffc4
&&marker !=0xffc8){
bitsPerPixel= (ba[i+ 4]& 0xff)* (ba[i+ 9]& 0xff);
break;
}else{
i+= size+ 2;
}
}
printf("Jpegdepth=%d\n",bitsPerPixel);
imageFile.close();
return bitsPerPixel;
}
//获取gif位深
gif:获取到是第一张图片的位深度,对于那种多张图片组成的动态图的位深度,应该是所有图片中位深度最高的那个才是最终的值。
int bitsPerPixel=(bytes[10] & 0x07) + 1;
int main()
{
QString path_0("D:\\workspace\\aaa.bmp");
int bmpDepth =getBmpBitDepth(path_0);
QString path_1("D:\\workspace\\tray.png");
int pngDepth =getPngBitDepth(path_1);
QString path_2("D:\\workspace\\1111.jpg");
int jpegDepth =getJpegBitDepth(path_2);
}