QT 图像大小处理

来源:互联网 发布:小孩拳霸摇杆淘宝 编辑:程序博客网 时间:2024/05/22 13:11
#include "saveFile.h"void GetImageParament(QImage *pImg,struct IMAGEPARAMENT *ppImgParam){if (pImg->isNull()) return;ppImgParam->nWidth   = pImg->width();ppImgParam->nHeight   = pImg->height();ppImgParam->nBitCount  = pImg->bitPlaneCount();//ppImgParam->nBytesPerLine = pImg->bytesPerLine();ppImgParam->nBytesPerLine =(pImg->width()*ppImgParam->nBitCount+31)/32*4;ppImgParam->nBytesPerPixel   = ppImgParam->nBitCount/8;if (ppImgParam->nBitCount<=8) ppImgParam->nNumColors= 1 << ppImgParam->nBitCount;else ppImgParam->nNumColors= 0;ppImgParam->nSize  = ppImgParam->nBytesPerLine*ppImgParam->nHeight;}int InImage(QImage *pImg,int x,int y){structIMAGEPARAMENT  P;GetImageParament(pImg,&P);if ((x<0)||(y<0)||(x>=P.nWidth)||(y>=P.nHeight))  return 0;else  return 1;}////void SetRectValue(QImage *pImg,int x,int y,int Dx,int Dy,unsigned  char *buf){structIMAGEPARAMENT  P;char*lp;inti,dw,dh,x1,y1,alpha,delta,Dxb,dwb;GetImageParament(pImg,&P);if (P.nBitCount<8) return;x1=x;y1=y;alpha=delta=0;if (x<0) { alpha=-x;    x1=0;}if (y<0) { delta=-y;    y1=0;}if (!InImage(pImg,x1,y1)) return;if(Dx >(int) P.nWidth-x1){dw =(int) P.nWidth-x1;}else{dw = Dx;}//dw=min(Dx,(int) P.nWidth-x1);   //dh=min(Dy,(int) P.nHeight-y1);if(Dy >(int) P.nHeight-x1){dh =(int) P.nHeight-x1;}else{dh = Dy;}dw -= alpha;dh -= delta;Dxb = Dx*P.nBytesPerPixel;dwb = dw*P.nBytesPerPixel;lp = (char*) ( pImg->constScanLine(x1)+y1*P.nBytesPerPixel);buf += (delta*Dx+alpha)*P.nBytesPerPixel;for (i=0;i<dh;i++) {memcpy(lp,buf,dwb);  buf += Dxb;lp -= P.nBytesPerLine;}}void SetAllPalette(QImage *pSrcIma,QImage *pImg, QVector<QRgb> verRgb){structIMAGEPARAMENT P;pImg->convertToFormat(pSrcIma->format(), pSrcIma->colorTable() );}void  ReSizeImage(QImage *imaDes, QImage *imaSource, double alpha) { struct IMAGEPARAMENT P; RGBQUAD ColorTab[256]; int i, j, nSize; unsigned char  **list,*sc; int Dx, Dy, x1, y1, x2, y2, flag, bpd; double p, q/*, a, b, c, d, t1, t2, t3*/; //if (ImageType(Imgm) == 2) //flag作为标志,当为1时,表示用双线性内插 //flag = 1; //else flag = 0; //为0时,表示用最近邻点法 GetImageParament(imaSource, &P); Dx = (int) (alpha * imaSource->width());//计算结果位图宽度 Dy = (int) (alpha * imaSource->height());//计算结果位图高度 //imaDes -> destroy(); //imaDes ->create(Dx, Dy, P.nBitCount ); //建立结果位图 bpd = P.nBytesPerPixel; //QVector<QRgb> verRgb;//if (bpd == 1) //256色图像 //{ //for (i = 0; i < P.nNumColors; i++) //{ ////GetAllPalette(imaSource, verRgb); ////verRgb = imaSource->//SetAllPalette(imaSource,imaDes, verRgb); //复制调色板 //} //} nSize = (Dx * P.nBitCount + 31) / 32 * 4;      //(int) ((P.nBytesPerLine + P.nHeight * bpd) * alpha);//计算工作单元大小 //sc = (unsigned char*) malloc(nSize); //申请像素行工作单元 list = (unsigned char**) malloc(P.nHeight * sizeof(unsigned char*));   //malloc(Dy * sizeof(BYTE*)); // 申请指针数组 ////for (i = 0; i < P.nHeight; i++) {; // ->GetPixelAddress(0, 0) - i * P.nBytesPerLine;//生成二维数组 list[i] = (unsigned char*) (imaSource->constScanLine(i) );}//for (j = 0; j < Dy; j++) { q = j / alpha; y1 = (int) q; y2 = y1 + 1; q = q - y1; for (i = 0;i < Dx; i++) { p = i / alpha; x1 = (int) p; //x1,y1为整数部分 x2 = x1 + 1; p = p - x1; //p,q为小数部分 if ((x2 > P.nWidth)||(y2 > P.nHeight )) // 范围检查 continue; if (x2 == P.nWidth )  x2--; if (y2 == P.nHeight )  y2--; if (flag == 0) //flag等于0,采用最近邻点法 { if (q > 0.5)  y1 = y2; if (p > 0.5)  x1 = x2; //char ch = (imaSource->pixelIndex(y1,x1*bpd));memcpy(&sc[i * bpd], &list[y1][x1 * bpd],bpd); //从源位图复制像素数据 } } SetRectValue(imaDes, 0, j, Dx, 1, sc); //处理结果总结果位图 } free(sc); //释放工作单元 free(list); }/****************************************************************************函数名称:SavePictureToFile函数功能:保存头像(只有源头像的一半)输入参数:LPCTSTR pszSourceFileName :源图像,LPCTSTR pszDesFileName:目标图像输出参数:int0:表示成功,-1:表示源图像不存在,-2:表示保存失败作者: 管砥群备注:****************************************************************************/int SavePictureToFile( QString pszSourceFileName , QString pszDesFileName ){QImage imageSource;//载入源图像if( true == imageSource.load(pszSourceFileName)){QImage imageDes;imageDes = imageSource.scaled(imageSource.width()/2, imageSource.height()/2,Qt::KeepAspectRatio);ReSizeImage(&imageDes,&imageSource,0.5);if(0 == imageDes.save(pszDesFileName)){return 0;}//表示保存失败return -2;}//表示源文件不存在return -1;;}

#ifndef _PROCESS_IMAGE_H#define _PROCESS_IMAGE_H#include <qimage.h>#define  LPCTSTR (const char*);/****************************************************************************结构体名称:IMAGEPARAMENT intnWidth;         //图像的宽度intnHeight;        //图像的高度intnBitCount;      //指定每个像素位数(BPP)的数量intnBytesPerLine;  //每行的字节数intnBytesPerPixel; //每个像数的字节数intnNumColors;intnSize;          //结构体的大小****************************************************************************/struct IMAGEPARAMENT {       intnWidth;intnHeight;intnBitCount;intnBytesPerLine;intnBytesPerPixel;intnNumColors;intnSize;};typedef struct tagRGBQUAD {char    rgbBlue;char    rgbGreen;char    rgbRed;char    rgbReserved;} RGBQUAD;typedef RGBQUAD * LPRGBQUAD;void GetImageParament(QImage *pImg,struct IMAGEPARAMENT *ppImgParam);//void GetAllPalette(QImage *pSrcIma,QImage *pDesImg,RGBQUAD *ColorTab);void SetRectValue(QImage *pImg,int x,int y,int Dx,int Dy,unsigned char *buf);void SetAllPalette(QImage *pImg, RGBQUAD *ColorTab);int InImage(QImage *pImg,int x,int y);void  ReSizeImage(QImage *imaDes, QImage *imaSource, float alpha) ;//int SavePictureToFile( QString pszSourceFileName , QString pszDesFileName );#endif