IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Filtres Anti-aliasing et TBitmap


Sujet :

Langage Delphi

  1. #21
    Membre confirmé
    Avatar de sur_uix
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    379
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2002
    Messages : 379
    Points : 550
    Points
    550
    Par défaut
    Citation Envoyé par Sub0
    J'ai donc remodifié l'unité.
    Confirme-moi qu'elle fonctionne stp, je n'ai pas D2005 installé...

    [EDIT]
    J(utilise aussi le multiplicateur * 4 pour les images 32 bits si j'utilise pByteArray.
    D'ailleurs, peut-être que ce type est compatible avec D2005...
    Je vais modifié l'unité avec ce dernier pour voir (merci de la tester).
    Alors cela dépend de quoi tu parles D2005 win32 ou DotNet ?
    Moi je parle de D2005 version DotNet et dans ce cas, pByteArray n'existe plus !!!! Il faut passer par intPtr. En fait j'ai modifé transform.pas pour être compatible avec D2005 VCL.NET

  2. #22
    Expert confirmé
    Avatar de Sub0
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2002
    Messages
    3 573
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Décembre 2002
    Messages : 3 573
    Points : 4 219
    Points
    4 219
    Par défaut
    ok, dans ce cas, je suppose que l'unité avec pByteArray fonctionne en mode Win32...
    Pour la version .Net, ce serait aussi intéressant de l'inclure dans l'archive.
    Je verrais ça lorsque j'installerai D2005.
    En tous les cas, merci de nous en avoir fait profiter, à+
    De retour parmis vous après 10 ans!!

  3. #23
    Membre confirmé
    Avatar de sur_uix
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    379
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2002
    Messages : 379
    Points : 550
    Points
    550
    Par défaut
    Citation Envoyé par Sub0
    En tous les cas, merci de nous en avoir fait profiter, à+
    De rien

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    {----------------------------------------------------------------}
    { Redimensionnement & filtre anti-aliasing - SubØ  -  08/03/05   }
    { Migration pour Delphi2005 Dot.Net       - sur_uix - 12/04/05   }
    {----------------------------------------------------------------}
    { This function uses part of Graphics32 library                  }
    { Copyright © 2000-2003 Alex Denisov                             }
    {----------------------------------------------------------------}
     
    Unit Transforms;
    Interface
    Uses Windows, Math, Graphics, SysUtils, Classes,
                   System.Runtime.InteropServices;
     
    Procedure Stretch32(Src, Dst: TBitmap);
     
    Type
      TFilter = (sfNearest, sfLinear, sfSpline, sfLanczos, sfMitchell);
     
    Var
      FullEdge: Boolean = True;
      Filter: TFilter = sfLinear;
     
     
    {----------------------------------------------------------------}
    {                       }Implementation{                         }
    {----------------------------------------------------------------}
     
    Procedure Stretch32(Src, Dst: TBitmap);
     
    Type
      TBufferEntry = Record A, R, G, B: Integer; End;
      TPointRec = Record Pos, Weight: Integer; End;
      TCluster = Array Of TPointRec;
      TMappingTable = Array Of TCluster;
     
    Const
      FilterWidths: Array[TFilter] Of Single = (1, 1, 2, 3, 2);
     
     
      {--------------------------------------------------------------}
      Function NearestFilter(Value: Single): Single;
      Begin
        Result := 0;
        If (Value > -0.5) And (Value <= 0.5) Then
          Result := 1;
      End;
     
      {--------------------------------------------------------------}
      Function LinearFilter(Value: Single): Single;
      Begin
        Result := 0;
        If (Value < -1) Then
          Result := 0
        Else If (Value < 0) Then
          Result := 1 + Value
        Else If (Value < 1) Then
          Result := 1 - Value;
      End;
     
      {--------------------------------------------------------------}
      Function SplineFilter(Value: Single): Single;
      Var
        tt: Single;
      Begin
        Result := 0;
        Value := Abs(Value);
        If (Value < 1) Then
        Begin
          tt := Sqr(Value);
          Result := 0.5 * tt * Value - tt + 2 / 3;
        End
        Else If (Value < 2) Then
        Begin
          Value := 2 - Value;
          Result := 1 / 6 * Sqr(Value) * Value;
        End;
      End;
     
      {--------------------------------------------------------------}
      Function LanczosFilter(Value: Single): Single;
     
        Function Sinc(Value: Single): Single;
        Begin
          Result := 1;
          Value := Value * Pi;
          If (Value <> 0) Then
            Result := Sin(Value) / Value;
        End;
     
      Begin
        Result := 0;
        Value := Abs(Value);
        If (Value < 3) Then
          Result := Sinc(Value) * Sinc(Value / 3);
      End;
     
      {--------------------------------------------------------------}
      Function MitchellFilter(Value: Single): Single;
      Var
        tt, ttt: Single;
      Begin
        Result := 0;
        Value := Abs(Value);
        tt := Sqr(Value);
        ttt := tt * Value;
        If (Value < 1) Then
          Result := (7 * ttt + -12 * tt + 16 / 3) / 6
        Else If (Value < 2) Then
          Result := (-7 / 3 * ttt + 12 * tt - 20 * Value + 32 / 3) / 6;
      End;
     
      {--------------------------------------------------------------}
      Function BuildMappingTable(DstLo, DstHi, SrcLo, SrcHi: Integer;
        Filter: TFilter = sfNearest): TMappingTable;
      Var
        SrcW, DstW: Integer;
        I, J, K, Left, Right, Count, Weight: Integer;
        Scale, OldScale, FilterWidth, Center: Single;
     
      Begin
        Result := Nil;
        SrcW := SrcHi - SrcLo;
        DstW := DstHi - DstLo;
        If (SrcW <= 1) Or (DstW <= 0) Then Exit;
        SetLength(Result, DstW);
     
        If (SrcW = 1) Then
        Begin
          For I := 0 To DstW - 1 Do
          Begin
            SetLength(Result[I], 1);
            Result[I][0].Pos := 0;
            Result[I][0].Weight := 256;
          End;
          Exit;
        End;
     
        Scale := (DstW - 1) / (SrcW - 1);
        If (FullEdge) Then Scale := DstW / SrcW;
        FilterWidth := FilterWidths[Filter];
        K := 0;
     
        If (Scale = 0) Then
        Begin
          Assert(Length(Result) = 1);
          SetLength(Result[0], 1);
          Result[0][0].Pos := (SrcLo + SrcHi) Div 2;
          Result[0][0].Weight := 256;
          Exit;
        End;
     
        If (Scale < 1) Then
        Begin
          OldScale := Scale;
          Scale := 1 / Scale;
          FilterWidth := FilterWidth * Scale;
          For I := 0 To DstW - 1 Do
          Begin
            Center := SrcLo + I * Scale;
            If (FullEdge) Then Center := SrcLo - 0.5 + (I + 0.5) * Scale;
            Left := Floor(Center - FilterWidth);
            Right := Ceil(Center + FilterWidth);
            Count := -256;
            For J := Left To Right Do
            Begin
              Case Filter Of
                sfNearest: Weight := Round(256 * NearestFilter((Center - J) * OldScale) * OldScale);
                sfLinear: Weight := Round(256 * LinearFilter((Center - J) * OldScale) * OldScale);
                sfSpline: Weight := Round(256 * SplineFilter((Center - J) * OldScale) * OldScale);
                sfLanczos: Weight := Round(256 * LanczosFilter((Center - J) * OldScale) * OldScale);
                sfMitchell: Weight := Round(256 * MitchellFilter((Center - J) * OldScale) * OldScale);
              End;
              If (Weight <> 0) Then
              Begin
                Inc(Count, Weight);
                K := Length(Result[I]);
                SetLength(Result[I], K + 1);
                Result[I][K].Pos := Min(Max(J, SrcLo), SrcHi - 1);
                Result[I][K].Weight := Weight;
              End;
            End;
            If (Length(Result[I]) = 0) Then
            Begin
              SetLength(Result[I], 1);
              Result[I][0].Pos := Floor(Center);
              Result[I][0].Weight := 256;
            End
            Else If (Count <> 0) Then
              Dec(Result[I][K Div 2].Weight, Count);
          End;
          Exit;
        End;
     
        Scale := 1 / Scale;
        For I := 0 To DstW - 1 Do
        Begin
          Center := SrcLo + I * Scale;
          If (FullEdge) Then Center := SrcLo - 0.5 + (I + 0.5) * Scale;
          Left := Floor(Center - FilterWidth);
          Right := Ceil(Center + FilterWidth);
          Count := -256;
          For J := Left To Right Do
          Begin
            Case Filter Of
              sfNearest: Weight := Round(256 * NearestFilter(Center - J));
              sfLinear: Weight := Round(256 * LinearFilter(Center - J));
              sfSpline: Weight := Round(256 * SplineFilter(Center - J));
              sfLanczos: Weight := Round(256 * LanczosFilter(Center - J));
              sfMitchell: Weight := Round(256 * MitchellFilter(Center - J));
            End;
            If (Weight <> 0) Then
            Begin
              Inc(Count, Weight);
              K := Length(Result[I]);
              SetLength(Result[I], k + 1);
              Result[I][K].Pos := Min(Max(j, SrcLo), SrcHi - 1);
              Result[I][K].Weight := Weight;
            End;
          End;
          If (Count <> 0) Then
            Dec(Result[I][K Div 2].Weight, Count);
        End;
     
      End;
     
    {----------------------------------------------------------------}
    Var
      DstRect, SrcRect: TRect;
      DstW, DstH, SrcW, SrcH: Single;
      MapX, MapY: TMappingTable;
      ClusterX, ClusterY: TCluster;
      HorzBuffer: Array Of TBufferEntry;
      MapXLoPos, MapXHiPos: Integer;
      I, J, X, Y, Ca, Cr, Cg, Cb,itmp: Integer;
      SrcLine, DstLine: IntPtr;//PIntegerArray;
      Spf: TPixelFormat;
     
    Begin
      Spf := Src.PixelFormat;
      Src.PixelFormat := pf32bit;
      SrcRect := Src.Canvas.ClipRect;
      SrcW := SrcRect.Right - SrcRect.Left;
      SrcH := SrcRect.Bottom - SrcRect.Top;
     
      Dst.PixelFormat := pf32bit;
      DstRect := Dst.Canvas.ClipRect;
      DstW := DstRect.Right - DstRect.Left;
      DstH := DstRect.Bottom - DstRect.Top;
     
      ClusterX := Nil;
      ClusterY := Nil;
      MapX := BuildMappingTable(DstRect.Left, DstRect.Right, SrcRect.Left, SrcRect.Right, Filter);
      MapY := BuildMappingTable(DstRect.Top, DstRect.Bottom, SrcRect.Top, SrcRect.Bottom, Filter);
      If (MapX = Nil) Or (MapY = Nil) Then Exit;
     
      MapXLoPos := MapX[0][0].Pos;
      MapXHiPos := MapX[Round(DstW - 1)][High(MapX[Round(DstW - 1)])].Pos;
      SetLength(HorzBuffer, MapXHiPos - MapXLoPos + 1);
     
      For J := DstRect.Top To DstRect.Bottom - 1 Do
      Begin
     
        ClusterY := MapY[J - DstRect.Top];
        For X := MapXLoPos To MapXHiPos Do
        Begin
          HorzBuffer[X - MapXLoPos].A := 0;
          HorzBuffer[X - MapXLoPos].R := 0;
          HorzBuffer[X - MapXLoPos].G := 0;
          HorzBuffer[X - MapXLoPos].B := 0;
          For Y := 0 To Length(ClusterY) - 1 Do
          Begin
            SrcLine := Src.ScanLine[ClusterY[Y].Pos];
            itmp := Marshal.ReadInt32(SrcLine,X*4);
            ca := (itmp And $FF000000) Shr 24;
            cr := (itmp And $00FF0000) Shr 16;
            cg := (itmp And $0000FF00) Shr 8;
            cb := (itmp And $000000FF);
            Inc(HorzBuffer[X - MapXLoPos].A, ClusterY[Y].Weight * ca);
            Inc(HorzBuffer[X - MapXLoPos].R, ClusterY[Y].Weight * cr);
            Inc(HorzBuffer[X - MapXLoPos].G, ClusterY[Y].Weight * cg);
            Inc(HorzBuffer[X - MapXLoPos].B, ClusterY[Y].Weight * cb);
          End;
        End;
     
        DstLine := Dst.ScanLine[J];
        For I := DstRect.Left To DstRect.Right - 1 Do
        Begin
          ClusterX := MapX[I - DstRect.Left];
          Ca := 0;
          Cr := 0;
          Cg := 0;
          Cb := 0;
          For X := 0 To Length(ClusterX) - 1 Do
          Begin
            Inc(Ca, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].A);
            Inc(Cr, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].R);
            Inc(Cg, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].G);
            Inc(Cb, ClusterX[X].Weight * HorzBuffer[ClusterX[X].Pos - MapXLoPos].B);
          End;
          Ca := Max(Min(Ca, $FF0000), 0) And $FF0000;
          Cr := Max(Min(Cr, $FF0000), 0) And $FF0000;
          Cg := Max(Min(Cg, $FF0000), 0) And $FF0000;
          Cb := Max(Min(Cb, $FF0000), 0) And $FF0000;
          Marshal.WriteInt32(DstLine,I*4,((Ca Shl 8) Or (Cr) Or (Cg Shr 8) Or (Cb Shr 16)));
        End;
     
      End;
      MapX := Nil;
      MapY := Nil;
      Src.PixelFormat := Spf;
    End;
     
     
    {----------------------------------------------------------------}
    End.

  4. #24
    Expert confirmé
    Avatar de Sub0
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2002
    Messages
    3 573
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Décembre 2002
    Messages : 3 573
    Points : 4 219
    Points
    4 219
    Par défaut
    Parfait !

    J'en profite pour vous dire que j'ai fait évoluer un peu cette fonction, en particulier pour supporter le masque de transparence alpha en tenant compte d'une image d'arrière-plan (plutôt une couleur d'arrière-plan)... La fonction retourne l'image redimensionnée et un second bitmap (le masque alpha). J'utilise ensuite une procédure d'affichage qui mixe une image d'arrière plan avec l'image source et le masque obtenu. Voici l'algo (code à optimiser selon le contexte) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    Procedure MakeBlend32(clip, back, Alpha: TBitmap);
    Var
      p0, p1, p2: pByteArray;
      x, y, A: Integer;
    Begin
      For y := 0 To Min(Clip.Height, Back.Height) - 1 Do
      Begin
        p0 := Clip.ScanLine[y];
        p1 := Back.scanline[y];
        p2 := Alpha.ScanLine[y];
        For x := 0 To Min(Clip.Width, Back.Width) - 1 Do
        Begin
          A := p2[x * 4];
          If (A = 0) Then
          Begin
            p0[x * 4 + 2] := p1[x * 4 + 2];
            p0[x * 4 + 1] := p1[x * 4 + 1];
            p0[x * 4 + 0] := p1[x * 4 + 0];
          End Else
          If (A < $FF) Then
          Begin
            p0[x * 4 + 2] := Round( Max( Min(
              p1[x * 4 + 2] + A * (p0[x * 4 + 2] - p1[x * 4 + 2]) / $FF, $FF), 0));
            p0[x * 4 + 1] := Round( Max( Min(
              p1[x * 4 + 1] + A * (p0[x * 4 + 1] - p1[x * 4 + 1]) / $FF, $FF), 0));
            p0[x * 4 + 0] := Round( Max( Min(
              p1[x * 4 + 0] + A * (p0[x * 4 + 0] - p1[x * 4 + 0]) / $FF, $FF), 0));
          End;
        End;
      End;
    End;
    J'ai utilisé ce code dans ce projet de jeu d'échecs, à+
    De retour parmis vous après 10 ans!!

  5. #25
    Expert confirmé
    Avatar de Sub0
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2002
    Messages
    3 573
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Décembre 2002
    Messages : 3 573
    Points : 4 219
    Points
    4 219
    Par défaut
    Pour ceux que le sujet intéresse, je suis tombé par hasard sur un pdf sur le traitement d'image 32 bits en DotNet, en particulier, la superposition d'images et l'ajoût du masque de transparence alpha. L'auteur parle surtout de l'optimisation pour accélérer le traitement. A la fin de l'article, vous trouverez aussi quelques liens intéressants :

    http://www.dotnet-tech.com/tutoriels/transparence/TransparenceModeUnsafe.pdf

    à+
    De retour parmis vous après 10 ans!!

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Redimensionnement d'image avec filtres anti-aliasing
    Par Sub0 dans le forum Codes sources à télécharger
    Réponses: 0
    Dernier message: 11/02/2013, 16h35
  2. [DirectX9] Anti-aliasing + gestion caméra
    Par SteelBox dans le forum DirectX
    Réponses: 6
    Dernier message: 21/07/2005, 00h56
  3. Anti aliasing qui ne fonctionne pas
    Par Deus Ex Makina dans le forum OpenGL
    Réponses: 3
    Dernier message: 27/04/2005, 09h57
  4. filtre anti-aliasing
    Par MO GV dans le forum MFC
    Réponses: 14
    Dernier message: 04/03/2005, 19h21
  5. Anti-aliasing
    Par Cazman dans le forum OpenGL
    Réponses: 16
    Dernier message: 27/04/2004, 08h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo