Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: Glow, Blur in 2d with no opengl or d3d ? possible ?

  1. #1

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    The titles explains itself :?

    sorry my bad english
    From brazil (:

    Pascal pownz!

  2. #2

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    i have some code to blur a image somewhere if you need that.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  3. #3
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    Not really. :roll: What platform, compiler, language are you using?

    Anything is possible, only the exact implementation can be impossible.

    btw, Welcome to PGD.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #4

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    Quote Originally Posted by WILL
    Not really. :roll: What platform, compiler, language are you using?

    Anything is possible, only the exact implementation can be impossible.

    btw, Welcome to PGD.
    Opz sorry :?

    Windows,
    Delphi 2006 win32,
    Object pascal...
    From brazil (:

    Pascal pownz!

  5. #5

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    Quote Originally Posted by Delfi
    i have some code to blur a image somewhere if you need that.
    Ohh it will be great ^^
    From brazil (:

    Pascal pownz!

  6. #6

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    Code:
    {
    
    Gaussian Blur in Delphi
    
    From: ullrich@math.okstate.edu
    You can do it like so. In informal testing it appears to take roughly twice as much time as Adobe Photoshop takes to do the same thing, which seems pretty OK to me - there are a lot of things you could do to speed it up.
    The gaussian kernel exp(-(x^2 + y^2)) is of the form f(x)*g(y), which means that you can perform a two-dimensional convolution by doing a sequence of one-dimensional convolutions - first you convolve each row and then each column. This is much faster (an N^2 becomes an N*2). Any convolution requires some temporary storage - below the BlurRow routine allocates and frees the memory, meaning that it gets allocated and freed once for each row. Probably changing this would speed it up some, it's not entirely clear how much.
    The kernel "size" is limited to 200 entries. In fact if you use radius anything like that large it will take forever - you want to try this with a radius = 3 or 5 or something. For a kernel with that many entries a straight convolution is the thing to do, while when the kernel gets much larger Fourier transform techniques will be better (I couldn't say what the actual cutoff is.)
    One comment that needs to be made is that a gaussian blur has the magical property that you can blur each row one by one and then blur each column - this is much faster than an actual 2-d convolution.
    Anyway, you can do this:
    }
    
    unit GBlur2;
    
    interface
    
    uses Windows, Graphics;
    
    type
        PRGBTriple = ^TRGBTriple;
        TRGBTriple = packed record
         b: byte; //easier to type than rgbtBlue...
         g: byte;
         r: byte;
        end;
    
        PRow = ^TRow;
        TRow = array[0..1000000] of TRGBTriple;
    
        PPRows = ^TPRows;
        TPRows = array[0..1000000] of PRow;
    
    
    const MaxKernelSize = 100;
    
    type
    
        TKernelSize = 1..MaxKernelSize;
    
        TKernel = record
         Size: TKernelSize;
         Weights: array[-MaxKernelSize..MaxKernelSize] of single;
        end;
    //the idea is that when using a TKernel you ignore the Weights
    //except for Weights in the range -Size..Size.
    
    procedure GBlur(theBitmap: TBitmap; radius: single);
    
    implementation
    
    uses SysUtils;
    
    procedure MakeGaussianKernel(var K: TKernel; radius: single;
                                MaxData, DataGranularity: single);
    //makes K into a gaussian kernel with standard deviation = radius.
    //for the current application you set MaxData = 255,
    //DataGranularity = 1. Now the procedure sets the value of
    //K.Size so that when we use K we will ignore the Weights
    //that are so small they can't possibly matter. (Small Size
    //is good because the execution time is going to be
    //propertional to K.Size.)
    var j: integer; temp, delta: single; KernelSize: TKernelSize;
    begin
      for j:= Low(K.Weights) to High(K.Weights) do
      begin
        temp:= j/radius;
        K.Weights[j]:= exp(- temp*temp/2);
      end;
    
    //now divide by constant so sum(Weights) = 1:
    
      temp:= 0;
      for j:= Low(K.Weights) to High(K.Weights) do
         temp:= temp + K.Weights[j];
      for j:= Low(K.Weights) to High(K.Weights) do
         K.Weights[j]:= K.Weights[j] / temp;
    
    
    //now discard (or rather mark as ignorable by setting Size)
    //the entries that are too small to matter -
    //this is important, otherwise a blur with a small radius
    //will take as long as with a large radius...
      KernelSize:= MaxKernelSize;
      delta:= DataGranularity / (2*MaxData);
      temp:= 0;
      while &#40;temp <delta> 1&#41; do
       begin
         temp&#58;= temp + 2 * K.Weights&#91;KernelSize&#93;;
         dec&#40;KernelSize&#41;;
       end;
    
      K.Size&#58;= KernelSize;
    
    //now just to be correct go back and jiggle again so the
    //sum of the entries we'll be using is exactly 1&#58;
    
      temp&#58;= 0;
      for j&#58;= -K.Size to K.Size do
         temp&#58;= temp + K.Weights&#91;j&#93;;
      for j&#58;= -K.Size to K.Size do
         K.Weights&#91;j&#93;&#58;= K.Weights&#91;j&#93; / temp;
    
    end;
    
    function TrimInt&#40;Lower, Upper, theInteger&#58; integer&#41;&#58; integer;
    begin
     if &#40;theInteger <Upper>= Lower&#41; then
      result&#58;= theInteger
     else
      if theInteger > Upper then
       result&#58;= Upper
        else
         result&#58;= Lower;
    end;
    
    function TrimReal&#40;Lower, Upper&#58; integer; x&#58; single&#41;&#58; integer;
    begin
     if &#40;x <upper>= lower&#41; then
      result&#58;= trunc&#40;x&#41;
     else
      if x > Upper then
       result&#58;= Upper
        else
         result&#58;= Lower;
    end;
    
    procedure BlurRow&#40;var theRow&#58; array of TRGBTriple; K&#58; TKernel; P&#58; PRow&#41;;
    var j, n, LocalRow&#58; integer; tr, tg, tb&#58; single; //tempRed, etc
           w&#58; single;
    begin
    
    for j&#58;= 0 to High&#40;theRow&#41; do
      begin
        tb&#58;= 0;
        tg&#58;= 0;
        tr&#58;= 0;
        for n&#58;= -K.Size to K.Size do
        begin
          w&#58;= K.Weights&#91;n&#93;;
    
    //the TrimInt keeps us from running off the edge of the row...
          with theRow&#91;TrimInt&#40;0, High&#40;theRow&#41;, j - n&#41;&#93; do
          begin
            tb&#58;= tb + w * b;
            tg&#58;= tg + w * g;
            tr&#58;= tr + w * r;
          end;
        end;
        with P&#91;j&#93; do
        begin
          b&#58;= TrimReal&#40;0, 255, tb&#41;;
          g&#58;= TrimReal&#40;0, 255, tg&#41;;
          r&#58;= TrimReal&#40;0, 255, tr&#41;;
        end;
      end;
    
    Move&#40;P&#91;0&#93;, theRow&#91;0&#93;, &#40;High&#40;theRow&#41; + 1&#41; * Sizeof&#40;TRGBTriple&#41;&#41;;
    end;
    
    procedure GBlur&#40;theBitmap&#58; TBitmap; radius&#58; single&#41;;
    var Row, Col&#58; integer; theRows&#58; PPRows; K&#58; TKernel; ACol&#58; PRow; P&#58;PRow;
    begin
    if &#40;theBitmap.HandleType <> bmDIB&#41; or &#40;theBitmap.PixelFormat <> pf24Bit&#41; then
     raise exception.Create&#40;'GBlur only works for 24-bit bitmaps'&#41;;
    
    
    MakeGaussianKernel&#40;K, radius, 255, 1&#41;;
    GetMem&#40;theRows, theBitmap.Height * SizeOf&#40;PRow&#41;&#41;;
    GetMem&#40;ACol, theBitmap.Height * SizeOf&#40;TRGBTriple&#41;&#41;;
    
    //record the location of the bitmap data&#58;
    for Row&#58;= 0 to theBitmap.Height - 1 do
      theRows&#91;Row&#93;&#58;= theBitmap.Scanline&#91;Row&#93;;
    
    //blur each row&#58;
    P&#58;= AllocMem&#40;theBitmap.Width*SizeOf&#40;TRGBTriple&#41;&#41;;
    for Row&#58;= 0 to theBitmap.Height - 1 do
      BlurRow&#40;Slice&#40;theRows&#91;Row&#93;^, theBitmap.Width&#41;, K, P&#41;;
    
    //now blur each column
    ReAllocMem&#40;P, theBitmap.Height*SizeOf&#40;TRGBTriple&#41;&#41;;
    for Col&#58;= 0 to theBitmap.Width - 1 do
    begin
    //- first read the column into a TRow&#58;
      for Row&#58;= 0 to theBitmap.Height - 1 do
         ACol&#91;Row&#93;&#58;= theRows&#91;Row&#93;&#91;Col&#93;;
    
    
      BlurRow&#40;Slice&#40;ACol^, theBitmap.Height&#41;, K, P&#41;;
    
    //now put that row, um, column back into the data&#58;
      for Row&#58;= 0 to theBitmap.Height - 1 do
         theRows&#91;Row&#93;&#91;Col&#93;&#58;= ACol&#91;Row&#93;;
    end;
    
    FreeMem&#40;theRows&#41;;
    FreeMem&#40;ACol&#41;;
    ReAllocMem&#40;P, 0&#41;;
    end;
    
    end.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  7. #7

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    Quote Originally Posted by Delfi
    Code:
    &#123;

    Ohh thx =)

    anything about glow ?
    From brazil (:

    Pascal pownz!

  8. #8

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    glow is a variant of blur.. but i have nothing to work with that.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  9. #9

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    If you look in the Jedi-SDL library, there's a Kernel effect system which I ported from some C code a long time ago, it's still there though.

    I think that what you need for Glow is Gradual Outlines at progressively darker colours.

    Basically, you create a surface to contain your glow,
    fill it with black
    and you draw an outline of your sprite on it. So your new surface contains a nice outline of your sprite.

    Then using a slightly darker colour, on the same surface, you draw an outline around your previous outline..

    (You'll have to check for the colour of the previous outline or you'll end up bleeding colour where you don't want it)

    Continue doing this until you're using a dark enough colour.

    then, Additive Blit your new glow to the screen.

    The technique is quite processor intensive, so you'll probably want to cache your results.

  10. #10

    Glow, Blur in 2d with no opengl or d3d ? possible ?

    Quote Originally Posted by jasonf
    If you look in the Jedi-SDL library, there's a Kernel effect system which I ported from some C code a long time ago, it's still there though.

    I think that what you need for Glow is Gradual Outlines at progressively darker colours.

    Basically, you create a surface to contain your glow,
    fill it with black
    and you draw an outline of your sprite on it. So your new surface contains a nice outline of your sprite.

    Then using a slightly darker colour, on the same surface, you draw an outline around your previous outline..

    (You'll have to check for the colour of the previous outline or you'll end up bleeding colour where you don't want it)

    Continue doing this until you're using a dark enough colour.

    then, Additive Blit your new glow to the screen.

    The technique is quite processor intensive, so you'll probably want to cache your results.
    Any ready code ?
    From brazil (:

    Pascal pownz!

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •