algorytm.org

Implementacja w C/C++



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Zmiana wielkości obrazu - Interpolacja dwukwadratowa - Implementacja w C/C++
Ocena użytkownikóww: *****  / 1
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 05 kwietnia 2006 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

Zmiana wielkosci obrazu - Biquadratic Interpolation - C++/Bqi.cpp:
// Zmiana wielkosci obrazka -
// Algorytm Biquadric Interpolation - algorytm podwojnej interpolacji kwadratowej
// www.algorytm.org
// Tomasz Lubinski (c) 2006
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Bqi.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TPixel org[122][100];

//---------------------------------------------------------------------------

//interpolacja kwadratowa
float TForm1::Inter(float f1, float f2, float f3, float d)
{
   float result;
   result = (f2 + (f3 - f1)*d + (f1 - 2*f2 + f3)*d*d);
   return result;
}

//interpolacja kwadratowa z normalizacja do wartosci 0-255
float TForm1::InterNorm(float f1, float f2, float f3, float d)
{
   float result;
   result = (f2 + (f3 - f1)*d + (f1 - 2*f2 + f3)*d*d);

   if (result > 255) return 255;
   if (result < 0) return 0;
   return result;
}

//---------------------------------------------------------------------------
//interpolacja dwukwadratowa
TPixel TForm1::Biquadratic_Inter(float x, float y)
{
  int x0,y0,x1,y1,x2,y2;
  float dx,dy;
  TPixel result;

  x1 = x;  //floor
  y1 = y;  //floor
  dx = (x-x1)*0.5;
  dy = (y-y1)*0.5;
  if (x1 - 1 >= 0)
     x0 = x1 - 1;
  else
     x0 = x1;
  if (y1 - 1 >= 0)
     y0 = y1 - 1;
  else
     y0 = y1;
  if (x1 + 1 >= src->Width)
     x2 = x1;
  else
     x2 = x1 + 1;
  if (y1 + 1 >= src->Width)
     y2 = y1;
  else
     y2 = y1 + 1;

  result.r = (InterNorm(Inter(org[x0][y0].r, org[x1][y0].r, org[x2][y0].r, dx),
                        Inter(org[x0][y1].r, org[x1][y1].r, org[x2][y1].r, dx),
                        Inter(org[x0][y2].r, org[x1][y2].r, org[x2][y2].r, dx), dy));
  result.g = (InterNorm(Inter(org[x0][y0].g, org[x1][y0].g, org[x2][y0].g, dx),
                        Inter(org[x0][y1].g, org[x1][y1].g, org[x2][y1].g, dx),
                        Inter(org[x0][y2].g, org[x1][y2].g, org[x2][y2].g, dx), dy));
  result.b = (InterNorm(Inter(org[x0][y0].b, org[x1][y0].b, org[x2][y0].b, dx),
                        Inter(org[x0][y1].b, org[x1][y1].b, org[x2][y1].b, dx),
                        Inter(org[x0][y2].b, org[x1][y2].b, org[x2][y2].b, dx), dy));

  return result;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   int i,j;;
   float ratiox, ratioy;
   TPixel *pixel;

   //pobierz wartosci obrazu oryginalnego
   src->Picture->Bitmap->PixelFormat = pf32bit;
   for (i=0; i<src->Height; i++)
   {
      pixel = (TPixel *)src->Picture->Bitmap->ScanLine[i];
      for (j=0; j<src->Width; j++)
      {
         org[j][i] = *pixel;
         pixel++;
      }
   }

   //pobierz wielkosc nowego obrazu
   dst->Width = StrToInt(Edit2->Text);
   dst->Height = StrToInt(Edit1->Text);
   dst->Picture->Bitmap->Width = dst->Width;
   dst->Picture->Bitmap->Height = dst->Height;
   ratiox = (src->Width*1.0)/(dst->Width*1.0);
   ratioy = (src->Height*1.0)/(dst->Height*1.0);

   dst->Canvas->Brush->Color = clWhite;
   dst->Canvas->Rectangle(0, 0, dst->Width, dst->Height);
   dst->Picture->Bitmap->PixelFormat = pf32bit;

   //wygeneruj nowy obraz
   for (i=0; i<dst->Height; i++)
   {
      pixel = (TPixel *)dst->Picture->Bitmap->ScanLine[i];
      for (j=0; j<dst->Width; j++)
      {
         *pixel = Biquadratic_Inter(j*ratiox, i*ratioy);
         pixel++;
      }
   }
}
//---------------------------------------------------------------------------
 
Dodaj komentarz