Skip to content

Commit

Permalink
use screenBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
seanbudd committed Aug 2, 2021
1 parent 5f756a4 commit b7692c5
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions source/visionEnhancementProviders/screenCurtain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A part of NonVisual Desktop Access (NVDA)
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
# Copyright (C) 2018-2019 NV Access Limited, Babbage B.V., Leonard de Ruijter
# Copyright (C) 2018-2021 NV Access Limited, Babbage B.V., Leonard de Ruijter

"""Screen curtain implementation based on the windows magnification API.
The Magnification API has been marked by MS as unsupported for WOW64 applications such as NVDA. (#12491)
Expand All @@ -22,6 +22,8 @@
from typing import Optional, Type
import nvwave
import globalVars
from screenBitmap import ScreenBitmap
from itertools import chain


class MAGCOLOREFFECT(Structure):
Expand Down Expand Up @@ -302,35 +304,16 @@ def confirmInitWithUser(self) -> bool:

def isScreenFullyBlack() -> bool:
"""
Uses wx to check that the screen is currently fully black by taking a screen capture and checking:
- there is only one colour used in the image
- the first pixel of the screen capture is black
Uses ScreenBitmap to check that the screen is currently fully black by taking a screen capture and
checking the colour of each pixel.
"""
screen = wx.ScreenDC()
screenSize = screen.GetSize()
bmp: wx.Bitmap = wx.Bitmap(screenSize[0], screenSize[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, screenSize[0], screenSize[1], screen, 0, 0) # copy screen over to bmp
del mem # Release bitmap
img: wx.Image = bmp.ConvertToImage()
# https://docs.wxwidgets.org/3.0/classwx_image.html#a7c9d557cd7ad577ed76e4337b1fd843a
hist = wx.ImageHistogram()
numberOfColours = img.ComputeHistogram(hist)
# Due to wxImageHistogram not fully supported in wxPython, the histogram is not subscriptable,
# and thus the key value cannot be accessed for colours.
# https://github.com/wxWidgets/Phoenix/issues/1991
# This function could be improved in the future using the following logic:
# numberOfBlackPixelsKey = hist.MakeKey(255, 255, 255)
# numberOfBlackPixels = hist[numberOfBlackPixelsKey]
# numberOfDisplayPixels = screenSize[0] * screenSize[1]
# log.debug(f"""Screen Capture:
# - number of colours: {numberOfColours}
# - number of black pixels: {numberOfBlackPixels}
# - number of total pixels: {numberOfDisplayPixels}
# """)
# return numberOfColours == 1 and numberOfBlackPixels == numberOfDisplayPixels
firstPixelIsBlack = img.GetRed(0, 0) == 0 and img.GetBlue(0, 0) == 0 and img.GetGreen(0, 0) == 0
return numberOfColours == 1 and firstPixelIsBlack
screenCapture = ScreenBitmap(screenSize[0], screenSize[1]).captureImage(0, 0, screenSize[0], screenSize[1])
for pixel in chain.from_iterable(screenCapture):
if not (pixel.rgbRed == 0 and pixel.rgbGreen == 0 and pixel.rgbBlue == 0):
return False
return True


class ScreenCurtainInitializationError(RuntimeError):
Expand Down

0 comments on commit b7692c5

Please sign in to comment.