Skip to content

Commit

Permalink
Bitmap: Reduce fake array size for 32 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Sep 3, 2020
1 parent 27a6cd9 commit 5ebea42
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (bmp *Bitmap) ToImage() (*image.RGBA, error) {

func (bmp *Bitmap) hasTransparency() (bool, error) {
if bmp.transparencyStatus == transparencyUnknown {
if err := bmp.withPixels(func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[2 << 30]dibPixel, pixelsLen int) error {
if err := bmp.withPixels(func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[maxPixels]bgraPixel, pixelsLen int) error {
for i := 0; i < pixelsLen; i++ {
if pixels[i].A == 0x00 {
bmp.transparencyStatus = transparencyTransparent
Expand All @@ -318,7 +318,7 @@ func (bmp *Bitmap) hasTransparency() (bool, error) {
}

func (bmp *Bitmap) postProcess() error {
return bmp.withPixels(func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[2 << 30]dibPixel, pixelsLen int) error {
return bmp.withPixels(func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[maxPixels]bgraPixel, pixelsLen int) error {
for i := 0; i < pixelsLen; i++ {
switch pixels[i].A {
case 0x00:
Expand All @@ -340,14 +340,16 @@ func (bmp *Bitmap) postProcess() error {
})
}

type dibPixel struct {
type bgraPixel struct {
B byte
G byte
R byte
A byte
}

func (bmp *Bitmap) withPixels(f func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[2 << 30]dibPixel, pixelsLen int) error) error {
const maxPixels = 2 << 27

func (bmp *Bitmap) withPixels(f func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[maxPixels]bgraPixel, pixelsLen int) error) error {
var bi win.BITMAPINFO
bi.BmiHeader.BiSize = uint32(unsafe.Sizeof(bi.BmiHeader))

Expand All @@ -363,7 +365,7 @@ func (bmp *Bitmap) withPixels(f func(bi *win.BITMAPINFO, hdc win.HDC, pixels *[2

hPixels := win.GlobalAlloc(win.GHND, uintptr(bi.BmiHeader.BiSizeImage))
defer win.GlobalFree(hPixels)
pixels := (*[2 << 30]dibPixel)(win.GlobalLock(hPixels))
pixels := (*[maxPixels]bgraPixel)(win.GlobalLock(hPixels))
defer win.GlobalUnlock(hPixels)

bi.BmiHeader.BiCompression = win.BI_RGB
Expand Down

0 comments on commit 5ebea42

Please sign in to comment.