Programlama | Programlama Dilleri | C Programlama Dili |C++

Anasayfa Programlama C++ Builder Resim Ölçekleme


Resim Ölçekleme

AÇIKLAMA

Bmp resimleri ölçekler. Bazı renkli resimlerde renk kaybı olabilir. Amaç ölçekleme işlemini küçültme ve büyültme şeklinde yapan basit örnek bir fonksiyonu sunmaktır.

 

  1.  
  2.  
  3. //---------------------------------------------------------------------------
  4.  
  5. #include <vcl.h>
  6. #pragma hdrstop
  7. using namespace std;
  8. #include <math.h>
  9.  
  10.  
  11. #include "Unit1.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma resource "*.dfm"
  15. #include <FileCtrl.hpp>
  16. TForm1 *Form1;
  17. double **data;
  18. int en,boy;
  19.  
  20. //---------------------------------------------------------------------------
  21. void De_Malloc(double **&dizim,int eni,int boyu)
  22. {
  23.    for (int i=0;i<eni;i++)
  24.        delete []dizim[i];
  25.      delete[] dizim;
  26.    return;
  27. }
  28. //---------------------------------------------------------------------------
  29. void De_Malloc(byte **&dizim,int eni,int boyu)
  30. {
  31.    for (int i=0;i<eni;i++)
  32.        delete []dizim[i];
  33.      delete[] dizim;
  34.    return;
  35. }
  36. //---------------------------------------------------------------------------
  37. void byteMalloc(byte **&imge,int w,int h)
  38. {
  39.   imge=new byte *[w];
  40.   for(int i=0;i<w;i++)imge[i]=new byte[h];
  41.   for(int i=0;i<w;i++)
  42.   for(int j=0;j<h;j++)
  43.   imge[i][j]=0;
  44.   return;
  45. }
  46. //---------------------------------------------------------------------------
  47. void doubleMalloc(double **&imge,int w,int h)
  48. {
  49.   imge=new double *[w];
  50.   for(int i=0;i<w;i++)imge[i]=new double[h];
  51.   for(int i=0;i<w;i++)
  52.   for(int j=0;j<h;j++)
  53.   imge[i][j]=0;
  54.   return;
  55. }
  56. //---------------------------------------------------------------------------
  57.  
  58. void olcekle(double **&imge,int imw,int imh,int w,int h)
  59. {
  60.   double **ara;
  61.     doubleMalloc(ara,w,imh);
  62.    double oran,artim;
  63.  
  64.   if(imw<w)
  65.   {
  66.     oran=(w-1)/(double)(imw-1);
  67.     for(int y=0;y<imh-1;y++)
  68.     { for(int x=0;x<(imw-1);x++)
  69.       {
  70.         artim=(imge[x+1][y]-imge[x][y])/oran;
  71.         int kk=oran*x;
  72.         for(int i=0;i<oran;i++)
  73.          ara[i+kk][y]=(imge[x][y]+i*artim);
  74.       }
  75.   }
  76.    }
  77.   else if(imw>w)
  78.   {
  79.     oran=(imw)/(double)(w);
  80.     for(int y=0;y<imh-1;y++)
  81.     { for(int x=0;x<w;x++)
  82.       {
  83.         int kk=oran*x;double top=0;
  84.         for(int i=0;i<oran;i++)
  85.          top+=imge[i+kk][y];
  86.          ara[x][y]=(top/floor(oran));
  87.       }
  88.   }
  89.  
  90.   }
  91.   else
  92.     for(int y=0;y<imh;y++)
  93.        for(int x=0;x<w;x++)
  94.         ara[x][y]=imge[x][y];
  95.   for(int y=0;y<imh;y++) ara[w-1][y]=imge[imw-1][y];
  96.  
  97.   De_Malloc(imge,imw,imh);
  98.   doubleMalloc(imge,w,h);
  99.  
  100.   if(imh<h)
  101.   {
  102.     oran=(h)/(double)(imh);
  103.     for(int x=0;x<w;x++)
  104.     { imge[x][h-1]=ara[x][imh-1];
  105.       for(int y=0;y<(imh-1);y++)
  106.       {
  107.         artim=(ara[x][y+1]-ara[x][y])/oran;
  108.         int kk=oran*y;
  109.         for(int i=0;i<oran;i++)
  110.          imge[x][i+kk]=(ara[x][y]+i*artim);
  111.       }
  112.    }
  113.  
  114.   }
  115.   else if(imh>h)
  116.   {
  117.     oran=(imh-1)/(double)(h-1);
  118.     for(int x=0;x<w;x++)
  119.     { imge[x][h-1]=ara[x][imh-1];
  120.       for(int y=0;y<h;y++)
  121.       {
  122.         int kk=oran*y;double top=0;
  123.         for(int i=0;i<oran;i++)
  124.           top+=ara[x][i+kk];
  125.         imge[x][y]=(top/floor(oran));
  126.       }
  127.   }
  128.   }
  129.    else
  130.     for(int y=0;y<h;y++)
  131.        for(int x=0;x<imw;x++)
  132.         imge[x][y]=(byte)ara[x][y];
  133.   for(int x=0;x<w;x++) imge[x][h-1]=ara[x][imh-1];
  134.   De_Malloc(ara,w,imh);
  135.   return;
  136. }
  137. //---------------------------------------------------------------------------
  138. __fastcall TForm1::TForm1(TComponent* Owner)
  139.         : TForm(Owner)
  140. {
  141. }
  142. //---------------------------------------------------------------------------
  143.  
  144. void __fastcall TForm1::Button1Click(TObject *Sender)
  145. {
  146.   OpenPictureDialog1->Execute();
  147.   double **data;
  148.  
  149.   Graphics::TBitmap *bmp = new Graphics::TBitmap;
  150.   bmp->LoadFromFile(OpenPictureDialog1->FileName);
  151.   en = bmp->Width;
  152.   boy = bmp->Height;
  153.   byte *ptr;
  154.   bmp->PixelFormat=pf24bit;
  155.  
  156.   doubleMalloc(data,en,boy);
  157.  
  158.   for (int y = 0; y < (bmp->Height); y++)
  159.       {
  160.          ptr = (Byte *)bmp->ScanLine[y];
  161.          for (int x = 0; x < (bmp->Width); x++)
  162.           data[x][y]=(11*ptr[3*x]+59*ptr[3*x+1]+30*ptr[3*x+2])/100;
  163.       }
  164.     Image1->Picture->Bitmap=bmp;
  165.  
  166.     int w=Edit1->Text.ToInt(); int h=Edit2->Text.ToInt();
  167.  
  168.  
  169.   olcekle(data,en,boy,w,h);
  170.   bmp->Width=w;bmp->Height=h;
  171.   bmp->PixelFormat=pf24bit;
  172.   for (int y = 0; y < (bmp->Height); y++)
  173.       {
  174.          ptr = (Byte *)bmp->ScanLine[y];
  175.          for (int x = 0; x < (bmp->Width); x++)
  176.           ptr[3*x]=ptr[3*x+1]=ptr[3*x+2]=(byte)data[x][y];
  177.      }
  178.   Image2->Left=Image1->Left+Image1->Width+20;  
  179.   Image2->Picture->Bitmap=bmp;
  180.  
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TForm1::Kaydet1Click(TObject *Sender)
  184. {
  185.   SavePictureDialog1->Execute();
  186.   Image2->Picture->SaveToFile(SavePictureDialog1->FileName.c_str());
  187. }
  188. //---------------------------------------------------------------------------
  189.  
  190.  

 

 

 

Dictionary
x
+
?
Null.
Yorumlar (0)
Sadece kayıtlı kullanıcılar yorum yazabilir!
Son Güncelleme ( Pazartesi, 29 Ağustos 2011 17:54 )