图像编程学习笔记7——图像缩放

假设放大因子为ratio,(为了避免新图过大或过小,我们在程序中限制0.25≤ratio≤4),缩放(zoom)的变换矩阵很简单:

(2.13)

缩放变换的源代码如下,因为和转置的那段程序很类似,程序中的注释就简单一些。

[cpp] view plaincopy
 
  1. /** 
  2. * 函数名: zoom 
  3. * 参  数: ratio -- 缩放率 
  4. * 功  能: 对图片进行水平和垂直镜像操作 
  5. *         只保存原图大小的图像数据,如果没有就用白色填充 
  6. */  
  7. void zoom(double ratio)  
  8. {  
  9.     int height = bmpInfoHeader.biHeight;     
  10.     int width = bmpInfoHeader.biWidth;    
  11.     int imgSize = bmpInfoHeader.biSizeImage;  
  12.     int lineByte = (width * 8 +31) / 32 * 4;  //每行像素所占字节数  
  13.     pNewBmpData = new unsigned char[imgSize];   
  14.     memset(pNewBmpData,(BYTE)255,sizeof(unsigned char )*imgSize);   //先全部用白色填充,处理的时候没有数据的自然就是白色了  
  15.     int x0,y0;  
  16.     for(int i = 0; i < height; i++ )  
  17.     {  
  18.         for(int j = 0; j < width; j++ )  
  19.         {  
  20.             x0 = j / ratio;  
  21.             y0 = i / ratio;  
  22.             if( (x0>=0) && (x0<width) && (y0>=0) && (y0<height))  
  23.             {  
  24.                 *(pNewBmpData + (height - 1 - i)*lineByte + j) = *(pBmpData + (height - 1 - y0)*lineByte + x0);   
  25.             }  
  26.         }  
  27.     }  
  28. }  
原文地址:https://www.cnblogs.com/lidabo/p/3701998.html