查看: 2673|回复: 1

[原创发布] 位图模糊

[复制链接]
梦石
0
星屑
6483
在线时间
119 小时
注册时间
2020-1-8
回帖
205
发表于 2022-4-16 15:45:22 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
边学边练,直接上代码

  1. class Bitmap
  2.   dll = "Blur.dll"
  3.   
  4.   GetValue = Win32API.new(dll, 'GetValue', 'LLLLL', 'L')
  5.   Blur = Win32API.new(dll, 'bitmapBlur', 'LL', 'V')
  6.   ExpBlur = Win32API.new(dll, 'bitmapExpBlur', 'LLLL', 'V')
  7.   GaussBlur = Win32API.new(dll, 'bitmapGaussBlur', 'LL', 'V')
  8.   
  9.   def to_int
  10.     self.object_id
  11.   end
  12.   
  13.   def hBitmap
  14.     @_hBitmap ||= GetValue.call(self, 16, 8, 44, 0)
  15.   end
  16.   
  17.   def pBits
  18.     @_pBits ||= GetValue.call(self, 16, 8, 16, 0)
  19.   end
  20.   
  21.   def pScan0
  22.     @_pScan0 ||= GetValue.call(self, 16, 8, 12, 0)
  23.   end
  24.   
  25.   def pIHr
  26.     @_pIHr ||= GetValue.call(self, 16, 8, 8, 0)
  27.   end
  28.   
  29.   # 简单快速的模糊,“第一行”与“最后一行”未处理;简单的看作半径1的模糊
  30.   # 具体计算用SSE的pavgb实现
  31.   # width >= 8 && height > 3
  32.   def main_blur!(exchg_times = 0)
  33.     Blur.call(self, exchg_times)
  34.   end
  35.   
  36.   # 网上的算法,效率与半径无关,纯C实现
  37.   def expBlur!(radius = 1, aprec = 16, zprec = 7)
  38.     ExpBlur.call(self, radius, aprec, zprec)
  39.   end
  40.   
  41.   # 网上的算法,效率与半径无关,具体计算用内嵌汇编SSE实现
  42.   # C函数半径参数为单精度,为了避免转换类型,写成百分比数  250 => 2.5
  43.   def gaussBlur!(radius_percent = 100)
  44.     GaussBlur.call(self, radius_percent)
  45.   end
  46.   
  47.   
  48.   BlurEffectGuid = [0x633C80A4, 0x482B1843, 0x28BEF29E, 0xD4FDC534]
  49.   BrightnessContrastEffectGuid = [0xD3A1DBE1,0x4C178EC4,0x97EA4C9F,0x3D341CAD]
  50.   ColorBalanceEffectGuid = [0x537E597D, 0x48DA251E, 0xCA296496, 0xF8706B49]
  51.   ColorCurveEffectGuid = [0xDD6A0022, 0x4A6758E4, 0x8ED49B9D, 0x3DA581B8]
  52.   ColorLUTEffectGuid = [0xA7CE72A9, 0x40D70F7F, 0xC0D0CCB3, 0x12325C2D]
  53.   ColorLookupTableEffectGuid = ColorLUTEffectGuid
  54.   ColorMatrixEffectGuid = [0x718F2615, 0x40E37933, 0x685F11A5, 0x74DD14FE]
  55.   HSLEffectGuid = [0x8B2DD6C3, 0x4D87EB07, 0x0871F0A5, 0x5F9C6AE2]
  56.   HueSaturationLightnessEffectGuid =  HSLEffectGuid   
  57.   LevelsEffectGuid = [0x99C354EC, 0x4F3A2A31, 0xA817348C, 0x253AB303]
  58.   RECEffectGuid = [0x74D29D05, 0x426669A4, 0xC53C4995, 0x32B63628]
  59.   RedEyeCorrectionEffectGuid = RECEffectGuid
  60.   SharpenEffectGuid = [0x63CBF3EE, 0x402CC526, 0xC562718F, 0x4251BF40]
  61.   TintEffectGuid = [0x1077AF00, 0x44412848, 0xAD448994, 0x2C7A2D4C]
  62.   
  63.   #PixelFormat32bppARGB = 0x26200A
  64.   #ImageLockModeRead = 1
  65.   #ImageLockModeWrite = 2
  66.   #ImageLockModeUserInputBuf = 4
  67.   
  68.   gdip = "gdiplus.dll"
  69.   
  70.   GdiplusStartup = Win32API.new(gdip, 'GdiplusStartup', 'PPP', 'I')
  71.   GdiplusShutdown = Win32API.new(gdip, 'GdiplusShutdown', 'L', 'V')
  72.   GdipStartupInput = [1, 0, 0, 0].pack('L4')
  73.   
  74.   CreateBitmapFromHBITMAP =
  75.     Win32API.new(gdip, 'GdipCreateBitmapFromHBITMAP', 'LLP', 'I')
  76.    
  77.   DisposeImage = Win32API.new(gdip, 'GdipDisposeImage', 'L', 'I')

  78.   # CreateEffect = Win32API.new(gdip, 'GdipCreateEffect', 'PP', 'I')
  79.   # 该API第一个参数类型Guid,可以看作一个16bytes结构,
  80.   # 需要其所有成员完全进栈,而不是其地址
  81.   # RMXP中没有此结构,所以拓展参数
  82.   CreateEffectGuidP = Win32API.new(gdip, 'GdipCreateEffect', 'LLLLP', 'I')
  83.   CreateEffect = lambda {|guid, buf|
  84.     CreateEffectGuidP.call(guid[0], guid[1], guid[2], guid[3], buf)
  85.   }
  86.   
  87.   BitmapApplyEffect = Win32API.new(gdip, 'GdipBitmapApplyEffect','LLPIPP','I')  
  88.   DeleteEffect = Win32API.new(gdip, 'GdipDeleteEffect', 'L', 'I')
  89.   SetEffectParams = Win32API.new(gdip,'GdipSetEffectParameters', 'LPL', 'I')
  90.   
  91.   BitmapLockBits = Win32API.new(gdip, 'GdipBitmapLockBits', 'LPLLP', 'I')
  92.   BitmapUnlockBits = Win32API.new(gdip, 'GdipBitmapUnlockBits', 'LP', 'I')
  93.   
  94.   
  95.   # 半径越大,效率越低
  96.   def gdiplus_blur!(radius, rx = 0, ry = 0, rw = nil, rh = nil)
  97.     buf = "\0" * 4
  98.    
  99.     GdiplusStartup.call(buf, GdipStartupInput, nil)
  100.     token = buf.unpack('L')[0]
  101.    
  102.     CreateBitmapFromHBITMAP.call(self.hBitmap, 0, buf)
  103.     pBitmap = buf.unpack('L')[0]
  104.    
  105.     CreateEffect.call(BlurEffectGuid, buf)
  106.     pBlurEffect = buf.unpack('L')[0]
  107.    
  108.     # gdiplus 不同效果的效果参数结构不同,自行百度
  109.     blurParams = [radius, 0].pack('fI')
  110.     SetEffectParams.call(pBlurEffect, blurParams, blurParams.size)
  111.    
  112.     w, h = self.width, self.height
  113.     rw, rh = rw || w, rh || h
  114.    
  115.     rect = [rx, ry, rx + rw, ry + rh].pack('iiII')
  116.     BitmapApplyEffect.call(pBitmap, pBlurEffect, rect, 0, nil, nil)
  117.    
  118.     #PixelFormat32bppARGB => 0x26200A
  119.     stride = w * -4
  120.     scan0 = self.pScan0 + rx * 4 + ry * stride
  121.     lockedBmpData = [rw, rh, stride, 0x26200A, scan0, 0].pack('IIiI3')
  122.    
  123.     #ImageLockModeUserInputBuf | ImageLockModeRead => 5
  124.     BitmapLockBits.call(pBitmap, rect, 5, 0x26200A, lockedBmpData)
  125.     BitmapUnlockBits.call(pBitmap, lockedBmpData)
  126.    
  127.     DisposeImage.call(pBitmap)
  128.     DeleteEffect.call(pBlurEffect)
  129.     GdiplusShutdown.call(token)
  130.   end   
  131. end
复制代码



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

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


  6. count, t = 0, Time.now
  7. loop do
  8.   dst.main_blur!(1)
  9.   break if Time.now - t >= 1.0
  10.   count += 1
  11. end
  12. str = "blur => #{count}次/秒\n"  # 195次/秒……

  13. count, t = 0, Time.now
  14. loop do
  15.   dst.expBlur!(2, 16, 7)
  16.   break if Time.now - t >= 1.0
  17.   count += 1
  18. end
  19. str << "expblur => #{count}次/秒\n"  # 25次/秒……

  20. count, t = 0, Time.now
  21. loop do
  22.   dst.gaussBlur!(200)
  23.   break if Time.now - t >= 1.0
  24.   count += 1
  25. end
  26. str << "gaussblur => #{count}次/秒\n"  # 47次/秒……

  27. count, t = 0, Time.now
  28. loop do
  29.   dst.gdiplus_blur!(5)
  30.   break if Time.now - t >= 1.0
  31.   count += 1
  32. end
  33. str << "gdiplus_blur => #{count}次/秒"  # 8次/秒……

  34. print str


  35. loop do
  36.   Graphics.update
  37.   Input.update
  38.   if Input.trigger?(Input::B)
  39.     dst.clear
  40.     dst.blt(0, 0, src, src.rect)
  41.   end
  42.   
  43.   if Input.trigger?(Input::C)
  44.     dst.main_blur!(1)
  45.   end
  46.   
  47.   if Input.trigger?(Input::L)
  48.     dst.gaussBlur!(200)
  49.   end
  50.   
  51.   if Input.trigger?(Input::R)
  52.     dst.expBlur!(2, 16, 7)
  53.   end
  54.   
  55.   if Input.trigger?(Input::RIGHT)
  56.     dst.gdiplus_blur!(5)
  57.   end
  58. end
复制代码

Blur.zip

5.01 KB, 下载次数: 13

Blur.dll

评分

参与人数 1赞 +1 收起 理由
guoxiaomi + 1 塞糖

查看全部评分

梦石
1
星屑
24088
在线时间
3339 小时
注册时间
2011-7-8
回帖
3862

开拓者

发表于 2022-4-16 19:23:09 | 显示全部楼层
截个图算了
Screenshot 2022-04-16 192310.png
熟悉rgss和ruby,xp区版主~
正在填坑:《膜拜组传奇》讲述膜拜组和学霸们的故事。
已上steam:与TXBD合作的Reformers《变革者》
* 战斗调用公共事件 *
* RGSOS 网络脚本 *
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部