Wątek zablokowany 2014-03-06 15:52 przez msm.

Funkcja delphi na c++

0

Witam

Czy mógłby ktoś mi przepisać tę funkcję na c++ ?
Nie znam delphi a ucze się programować w c++Builder personal

procedure BitmapBlur(var theBitmap: TBitmap);
var
  x, y: Integer;
  yLine,
  xLine: PByteArray;
begin
  for y := 1 to theBitmap.Height -2 do begin
    yLine := theBitmap.ScanLine[y -1];
    xLine := theBitmap.ScanLine[y];
    for x := 1 to theBitmap.Width -2 do begin
      xLine^[x * 3] := (
        xLine^[x * 3 -3] + xLine^[x * 3 +3] +
        yLine^[x * 3 -3] + yLine^[x * 3 +3] +
        yLine^[x * 3] + xLine^[x * 3 -3] +
        xLine^[x * 3 +3] + xLine^[x * 3]) div 8;
      xLine^[x * 3 +1] := (
        xLine^[x * 3 -2] + xLine^[x * 3 +4] +
        yLine^[x * 3 -2] + yLine^[x * 3 +4] +
        yLine^[x * 3 +1] + xLine^[x * 3 -2] +
        xLine^[x * 3 +4] + xLine^[x * 3 +1]) div 8;
      xLine^[x * 3 +2] := (
        xLine^[x * 3 -1] + xLine^[x * 3 +5] +
        yLine^[x * 3 -1] + yLine^[x * 3 +5] +
        yLine^[x * 3 +2] + xLine^[x * 3 -1] +
        xLine^[x * 3 +5] + xLine^[x * 3 +2]) div 8;
    end;
  end;
end;

tagi <code>

0

yLine,xLine:PByteArray jest odpowiednikiem unsigned char *yLine,*xLine; pętle chyba rozumiesz, operacje matematyczne też powinieneś, zamiast div zwykłe dzielenie /.

0

Dziękuje
Po wszystkim funkcja wygląda tak:

 
bool TForm1::Blur(Graphics::TBitmap *lpBmp)
{
    if( !lpBmp )
        return false;

    int x, y;
    unsigned char *yLine,*xLine;

    try
    {
        for( y = 1; y<lpBmp->Height-2;y++ )
        {
            yLine = (Byte*)lpBmp->ScanLine[y-1];
            xLine = (Byte*)lpBmp->ScanLine[y];

            for( x = 1; x < lpBmp->Width-2; x++ )
            {
                xLine[x*3]    = ( xLine[x * 3 -3] + xLine[x * 3 +3] + yLine[x * 3 -3] + yLine[x * 3 +3] + yLine[x * 3] + xLine[x * 3 -3] + xLine[x * 3 +3] + xLine[x * 3])  / 8;
                xLine[x*3 +1] = ( xLine[x * 3 -2] + xLine[x * 3 +4] + yLine[x * 3 -2] + yLine[x * 3 +4] + yLine[x * 3 +1] + xLine[x * 3 -2] + xLine[x * 3 +4] + xLine[x * 3 +1]) / 8;
                xLine[x*3 +2] = ( xLine[x * 3 -1] + xLine[x * 3 +5] + yLine[x * 3 -1] + yLine[x * 3 +5] + yLine[x * 3 +2] + xLine[x * 3 -1] + xLine[x * 3 +5] + xLine[x * 3 +2]) / 8;
            }
        }
        return true;
    }
    catch(...)
    {
        return false;
    }
}
1
  1. Używaj przyrostkowej wersji inkrementacji.
for( y = 1; y<lpBmp->Height-1;++y)
for( x = 1; x < lpBmp->Width-1; ++x)
0

Czemu? jaka jest różnica?

0

Nie będę zaczynał kolejnego wątku dla takiej bzdury, ale tu przykład w załącznikach - czym się różni post inkrementacja od pre-inkrementacji.

ASM z VS 2010 (debug, optymalizator OFF).

Podsumowując - NICZYM.

0

Widzę, że walka tutaj trwa między ++x, a x++ :)
Może ten link rozwiąże tą walkę: http://www.tantalon.com/pete/cppopt/asyougo.htm#UsePrefixOperators wersja prefix lepiej wpływa na wydajność.

0

Dla swojego testu w GCC Explorer i braku optymalizacji. Dostałem jedną różnicę. post posiada o jedną więcej linijkę:

movl	$0, %esi

Umiejscowiona jest ona 3 linijki powyżej .L4 nie znam się na asm, więc nie wiem co ona oznacza, ale jest minimalna różnica :)

 
#include <iostream>
#include <vector>

using namespace std;

void post(vector<int> n) { 
	vector<int>::iterator it = n.begin();
	for(it; it != n.end(); it++) {
		cout << *it << " ";
	}
}

void pre(vector<int> n) {
	vector<int>::iterator it = n.begin();
	for(it; it != n.end(); ++it) {
		cout << *it << " ";
	}
}

int main() {
	vector<int> n;
	for(int i = 0; i < 10; ++i) 
		n.push_back(i);
	
	post(n);
	pre(n);
  
	return 0;
}

Kod asm w linku.

0

Główny temat wątku (chyba) zakończony, proponuję przenieść się z kłótnią o wyższości ++i nad i++ i odwrotnie do dedykowanego wątku bo robimy sobie wstyd jak zwykle offtopowaniem/kłóceniem się/tym wątkiem.
Jestem tylko zdziwiony że dzielenie w (... xLine[x * 3 +2]) / 8 nie zostało wytknięte i poprawione na wyraźnie lepsze przesunięcie bitowe. ;)

A, tak, zamykam jeśli można.

1 użytkowników online, w tym zalogowanych: 0, gości: 1