搜索附件  

位图模糊: Blur.zip

 

位图模糊:
边学边练,直接上代码

RUBY 代码
  1. class Bitmap
  2.   dll = "Blur.dll"
  3.   GetValue = Win32API.new(dll, 'GetValue', 'LLLLL', 'L')
  4.   Blur = Win32API.new(dll, 'bitmapBlur', 'LL', 'V')
  5.   ExpBlur = Win32API.new(dll, 'bitmapExpBlur', 'LLLL', 'V')
  6.   GaussBlur = Win32API.new(dll, 'bitmapGaussBlur', 'LL', 'V')
  7.   def to_int
  8.     self.object_id
  9.   end
  10.   def hBitmap
  11.     @_hBitmap ||= GetValue.call(self, 16, 8, 44, 0)
  12.   end
  13.   def pBits
  14.     @_pBits ||= GetValue.call(self, 16, 8, 16, 0)
  15.   end
  16.   def pScan0
  17.     @_pScan0 ||= GetValue.call(self, 16, 8, 12, 0)
  18.   end
  19.   def pIHr
  20.     @_pIHr ||= GetValue.call(self, 16, 8, 8, 0)
  21.   end
  22.   # 简单快速的模糊,“第一行”与“最后一行”未处理;简单的看作半径1的模糊
  23.   # 具体计算用SSE的pavgb实现
  24.   # width >= 8 && height > 3
  25.   def main_blur!(exchg_times = 0)
  26.     Blur.call(self, exchg_times)
  27.   end
  28.   # 网上的算法,效率与半径无关,纯C实现
  29.   def expBlur!(radius = 1, aprec = 16, zprec = 7)
  30.     ExpBlur.call(self, radius, aprec, zprec)
  31.   end
  32.   # 网上的算法,效率与半径无关,具体计算用内嵌汇编SSE实现
  33.   # C函数半径参数为单精度,为了避免转换类型,写成百分比数  250 => 2.5
  34.   def gaussBlur!(radius_percent = 100)
  35.     GaussBlur.call(self, radius_percent)
  36.   end
  37.   BlurEffectGuid = [0x633C80A4, 0x482B1843, 0x28BEF29E, 0xD4FDC534]
  38.   BrightnessContrastEffectGuid = [0xD3A1DBE1,0x4C178EC4,0x97EA4C9F,0x3D341CAD]
  39.   ColorBalanceEffectGuid = [0x537E597D, 0x48DA251E, 0xCA296496, 0xF8706B49]
  40.   ColorCurveEffectGuid = [0xDD6A0022, 0x4A6758E4, 0x8ED49B9D, 0x3DA581B8]
  41.   ColorLUTEffectGuid = [0xA7CE72A9, 0x40D70F7F, 0xC0D0CCB3, 0x12325C2D]
  42.   ColorLookupTableEffectGuid = ColorLUTEffectGuid
  43.   ColorMatrixEffectGuid = [0x718F2615, 0x40E37933, 0x685F11A5, 0x74DD14FE]
  44.   HSLEffectGuid = [0x8B2DD6C3, 0x4D87EB07, 0x0871F0A5, 0x5F9C6AE2]
  45.   HueSaturationLightnessEffectGuid =  HSLEffectGuid   
  46.   LevelsEffectGuid = [0x99C354EC, 0x4F3A2A31, 0xA817348C, 0x253AB303]
  47.   RECEffectGuid = [0x74D29D05, 0x426669A4, 0xC53C4995, 0x32B63628]
  48.   RedEyeCorrectionEffectGuid = RECEffectGuid
  49.   SharpenEffectGuid = [0x63CBF3EE, 0x402CC526, 0xC562718F, 0x4251BF40]
  50.   TintEffectGuid = [0x1077AF00, 0x44412848, 0xAD448994, 0x2C7A2D4C]
  51.   #PixelFormat32bppARGB = 0x26200A
  52.   #ImageLockModeRead = 1
  53.   #ImageLockModeWrite = 2
  54.   #ImageLockModeUserInputBuf = 4
  55.   gdip = "gdiplus.dll"
  56.   GdiplusStartup = Win32API.new(gdip, 'GdiplusStartup', 'PPP', 'I')
  57.   GdiplusShutdown = Win32API.new(gdip, 'GdiplusShutdown', 'L', 'V')
  58.   GdipStartupInput = [1, 0, 0, 0].pack('L4')
  59.   CreateBitmapFromHBITMAP =
  60.     Win32API.new(gdip, 'GdipCreateBitmapFromHBITMAP', 'LLP', 'I')
  61.   DisposeImage = Win32API.new(gdip, 'GdipDisposeImage', 'L', 'I')
  62.   # CreateEffect = Win32API.new(gdip, 'GdipCreateEffect', 'PP', 'I')
  63.   # 该API第一个参数类型Guid,可以看作一个16bytes结构,
  64.   # 需要其所有成员完全进栈,而不是其地址
  65.   # RMXP中没有此结构,所以拓展参数
  66.   CreateEffectGuidP = Win32API.new(gdip, 'GdipCreateEffect', 'LLLLP', 'I')
  67.   CreateEffect = lambda{|guid, buf|
  68.     CreateEffectGuidP.call(guid[0], guid[1], guid[2], guid[3], buf)
  69.   }
  70.   BitmapApplyEffect = Win32API.new(gdip, 'GdipBitmapApplyEffect','LLPIPP','I')  
  71.   DeleteEffect = Win32API.new(gdip, 'GdipDeleteEffect', 'L', 'I')
  72.   SetEffectParams = Win32API.new(gdip,'GdipSetEffectParameters', 'LPL', 'I')
  73.   BitmapLockBits = Win32API.new(gdip, 'GdipBitmapLockBits', 'LPLLP', 'I')
  74.   BitmapUnlockBits = Win32API.new(gdip, 'GdipBitmapUnlockBits', 'LP', 'I')
  75.   # 半径越大,效率越低
  76.   def gdiplus_blur!(radius, rx = 0, ry = 0, rw = nil, rh = nil)
  77.     buf = "\0" * 4
  78.     GdiplusStartup.call(buf, GdipStartupInput, nil)
  79.     token = buf.unpack('L')[0]
  80.     CreateBitmapFromHBITMAP.call(self.hBitmap, 0, buf)
  81.     pBitmap = buf.unpack('L')[0]
  82.     CreateEffect.call(BlurEffectGuid, buf)
  83.     pBlurEffect = buf.unpack('L')[0]
  84.     # gdiplus 不同效果的效果参数结构不同,自行百度
  85.     blurParams = [radius, 0].pack('fI')
  86.     SetEffectParams.call(pBlurEffect, blurParams, blurParams.size)
  87.     w, h = self.width, self.height
  88.     rw, rh = rw || w, rh || h
  89.     rect = [rx, ry, rx + rw, ry + rh].pack('iiII')
  90.     BitmapApplyEffect.call(pBitmap, pBlurEffect, rect, 0, nil, nil)
  91.     #PixelFormat32bppARGB => 0x26200A
  92.     stride = w * -4
  93.     scan0 = self.pScan0 + rx * 4 + ry * stride
  94.     lockedBmpData = [rw, rh, stride, 0x26200A, scan0, 0].pack('IIiI3')
  95.     #ImageLockModeUserInputBuf | ImageLockModeRead => 5
  96.     BitmapLockBits.call(pBitmap, rect, 5, 0x26200A, lockedBmpData)
  97.     BitmapUnlockBits.call(pBitmap, lockedBmpData)
  98.     DisposeImage.call(pBitmap)
  99.     DeleteEffect.call(pBlurEffect)
  100.     GdiplusShutdown.call(token)
  101.   end   
  102. end
复制代码



简单的测试,希望可以回复一下次数

RUBY 代码
[code]src = Bitmap.new(640, 480)  # 观看效果的话,换成图片,注释掉下面一行代码
src.fill_rect(src.rect, Color.new(0, 255, 255))
dst = src.clone
sprite = Sprite.new
sprite.bitmap = dst


count, t = 0, Time.now
loopdo
  dst.main_blur!(1)
  breakifTime.now - t >= 1.0
  count += 1
end
str = "blur => #{count}次/秒\n"  # 195次/秒……

count, t = 0, Time.now
loopdo
  dst.expBlur!(2, 16, 7)
  breakifTime.now - t >= 1.0
  count += 1
end
str = 1.0
  count += 1
end
str = 1.0
  count += 1
end
str
Loading...

QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

GMT+8, 2024-11-22 04:37 , Processed in 0.051865 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

返回顶部