X Tutup
Skip to content

Commit 1843a0a

Browse files
committed
Cleanup in panda3d_.py example.
PaintBuffer.GetString() supports conversion to RGBA mode, an example of conversion of browser image to PNG is in panda3d_.py > ClientHandler > _saveImage(). "include_cython" directory renamed to "cython_includes". Flipping the picture in PaintBuffer is now written in c++, see cpp_utils/.
1 parent 556e658 commit 1843a0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+670
-107
lines changed

cefpython/browser.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ cdef class PyBrowser:
497497
cdef PaintBuffer GetImage_Windows(self,
498498
PaintElementType paintElementType, int width, int height):
499499
if not self.imageBuffer:
500-
print("Browser.imageBuffer = malloc(%d)" % (width*height*4))
501500
self.imageBuffer = <void*>malloc(width*height*4)
502501
cdef cpp_bool ret = self.GetCefBrowser().get().GetImage(
503502
paintElementType, width, height, self.imageBuffer)

cefpython/cef1/windows/binaries/panda3d_.py

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Panda3D off-screen rendering example
1+
# CEF off-screen rendering using the Panda3D game engine:
22
# http://www.panda3d.org/
33

4-
# You need panda3D runtime compatible with python 2.7, the stable version (1.7.2)
5-
# comes with python 2.6, so you will have to compile it yourself
6-
# or download 1.8.0 sdk (unstable) which comes with python 2.7.
4+
# You need panda3D runtime that is compatible with python 2.7,
5+
# version 1.8.0 comes by default with python 2.7.
76

8-
# In panda3D version 1.8.0 api has changed, this code works with version 1.8.0,
9-
# to make it work with 1.7.2 slight changes will be required.
10-
11-
# To use custom python (not the one provided with the SDK) create a "panda.pth"
12-
# file inside your copy of python, containing the path of the panda directory
13-
# and the bin directory within it on separate lines, for example:
7+
# To use custom python (not the one provided with the SDK)
8+
# create a "panda.pth" file inside your copy of python, in
9+
# this file put paths to panda & bin directory on separate,
10+
# for example:
11+
#
1412
# c:\Panda3D-1.8.0
1513
# c:\Panda3D-1.8.0\bin
14+
#
1615
# This will enable your copy of python to find the panda libraries.
1716

1817
import platform
1918
if platform.architecture()[0] != "32bit":
20-
raise Exception("Architecture not supported: %s" % platform.architecture()[0])
19+
raise Exception("Unsupported architecture: %s" % (
20+
platform.architecture()[0]))
2121

2222
import sys
2323
if sys.hexversion >= 0x02070000 and sys.hexversion < 0x03000000:
@@ -32,11 +32,11 @@
3232
from direct.showbase.DirectObject import DirectObject
3333
from direct.task import Task
3434
from math import pi, sin, cos
35-
from direct.gui.OnscreenImage import OnscreenImage
3635

3736
class World(DirectObject):
3837

3938
browser = None
39+
texture = None
4040

4141
def __init__(self):
4242

@@ -48,98 +48,69 @@ def __init__(self):
4848

4949
windowHandle = base.win.getWindowHandle().getIntHandle()
5050

51-
# image = OnscreenImage("wxpython.png", pos = Vec3(0,0,0))
52-
# image.setImage("panda3d_.jpg")
53-
54-
# from panda3d.core import PTAUchar
55-
56-
# tex = Texture()
57-
# tex.setup2dTexture(512, 512, Texture.CMOff, Texture.FRgba)
58-
# tex.setRamImage(tex2.getRamImage())
59-
60-
# or:
61-
62-
# tex = Texture()
63-
# tex.setup2dTexture()
64-
# tex.load()...?
65-
# tex.read("wxpython.png")
66-
67-
self.tex = Texture()
68-
self.tex.setup2dTexture(400, 300, Texture.CMOff,
51+
self.texture = Texture()
52+
self.texture.setup2dTexture(400, 300, Texture.CMOff,
6953
Texture.FLuminanceAlpha)
7054

71-
cm = CardMaker("browser2d")
72-
cm.setFrame(-0.75,.75,-0.75,0.75)
73-
card = render2d.attachNewNode(cm.generate())
74-
card.setTexture(self.tex)
55+
cardMaker = CardMaker("browser2d")
56+
cardMaker.setFrame(-0.75,.75,-0.75,0.75)
57+
card = render2d.attachNewNode(cardMaker.generate())
58+
card.setTexture(self.texture)
7559
card.setHpr(0, 0, 5)
7660

7761
windowInfo = cefpython.WindowInfo()
7862
windowInfo.SetAsOffscreen(windowHandle)
7963
self.browser = cefpython.CreateBrowserSync(
8064
windowInfo, browserSettings={}, navigateURL="cefsimple.html")
81-
self.browser.SetClientHandler(ClientHandler(self.browser, self.tex))
65+
self.browser.SetClientHandler(
66+
ClientHandler(self.browser, self.texture))
8267
self.browser.SetSize(cefpython.PET_VIEW, 400, 300);
8368

8469
taskMgr.add(self.messageLoop, "CefMessageLoop")
8570

86-
# python: bytearray(), memoryview() (buffer in python < 3)
87-
# mode = "L", "RGBX", "RGBA", and "CMYK"
88-
# image = Image.frombuffer(mode="RGBA", size=(w,h), buffer, "raw", "RGBA", 0, 1)
89-
# image.save("panda3D_buffer.png", "PNG")
90-
9171
def messageLoop(self, task):
92-
# print("messageLoop()")
9372
cefpython.SingleMessageLoop()
94-
# Force more calls to OnPaint():
95-
# self.browser.GetMainFrame().ExecuteJavascript("document.body.innerHTML += '<b>asd</b>'")
9673
return Task.cont
9774

9875
def spinCameraTask(self, task):
99-
10076
angleDegrees = task.time * 6.0
10177
angleRadians = angleDegrees * (pi / 180.0)
10278
camera.setPos(20 * sin(angleRadians), -20.0 * cos(angleRadians), 3)
10379
camera.setHpr(angleDegrees, 0, 0)
10480
return Task.cont
10581

10682
class ClientHandler:
107-
10883
browser = None
109-
tex = None
84+
texture = None
11085

111-
def __init__(self, browser, tex):
86+
def __init__(self, browser, texture):
11287
self.browser = browser
113-
self.tex = tex
88+
self.texture = texture
11489

11590
def OnPaint(self, browser, paintElementType, dirtyRects, buffer):
116-
print("OnPaint()")
117-
print("paintElementType: %s" % paintElementType)
118-
print("dirtyRects: %s" % dirtyRects)
119-
12091
(width, height) = self.browser.GetSize(paintElementType)
121-
122-
print("buffer.GetIntPointer(): %s" % buffer.GetIntPointer())
123-
print("width, height = %d, %d" % (width, height))
124-
125-
print("len(buffer.GetString()): %s" % len(buffer.GetString()))
126-
print("type: %s" % type(buffer.GetString()))
127-
128-
img = self.tex.modifyRamImage()
129-
img.setData(buffer.GetString(origin="bottom-left", mode="bgra"))
130-
131-
#arrayBuffer = PTAUchar.emptyArray(0)
132-
#arrayBuffer.setData(buffer.GetString())
133-
#self.tex.setRamImage(CPTAUchar(arrayBuffer), Texture.CMOff)
134-
135-
#image = Image.fromstring("BGRA", (width, height),
136-
# buffer.GetString(), "raw", "BGRA")
137-
138-
#self.tex.setRamMipmapPointerFromInt(
139-
# buffer.GetIntPointer(), 0, width*height*4)
92+
img = self.texture.modifyRamImage()
93+
img.setData(buffer.GetString(mode="bgra", origin="bottom-left"))
94+
95+
def OnLoadEnd(self, browser, frame, httpStatusCode):
96+
return
97+
self._saveImage()
98+
99+
def _saveImage(self):
100+
try:
101+
from PIL import Image
102+
except:
103+
print("PIL library not available, can't save image")
104+
return
105+
(width, height) = self.browser.GetSize(cefpython.PET_VIEW)
106+
buffer = self.browser.GetImage(cefpython.PET_VIEW, width, height)
107+
image = Image.fromstring(
108+
"RGBA", (width,height),
109+
buffer.GetString(mode="rgba", origin="top-left"),
110+
"raw", "RGBA", 0, 1)
111+
image.save("panda3d_image.png", "PNG")
140112

141113
if __name__ == "__main__":
142-
143114
sys.excepthook = cefpython.ExceptHook
144115
settings = {
145116
"log_file": cefpython.GetRealPath("debug.log"),

cefpython/cef1/windows/setup/setup.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Stop on first error, otherwise hundreds of errors appear in the console.
99
Options.fast_fail = True
1010

11-
# Written to include_cython/compile_time_constants.pxi
11+
# Written to cython_includes/compile_time_constants.pxi
1212
CEF_VERSION = 1
1313

1414
"""
@@ -34,8 +34,8 @@
3434

3535
def CompileTimeConstants():
3636

37-
print("Generating: include_cython/compile_time_constants.pxi")
38-
with open("./../../../include_cython/compile_time_constants.pxi", "w") as fd:
37+
print("Generating: cython_includes/compile_time_constants.pxi")
38+
with open("./../../../cython_includes/compile_time_constants.pxi", "w") as fd:
3939
fd.write('# This file was generated by setup.py\n')
4040
# A way around Python 3.2 bug: UNAME_SYSNAME is not set.
4141
fd.write('DEF UNAME_SYSNAME = "%s"\n' % platform.uname()[0])
@@ -49,14 +49,15 @@ def CompileTimeConstants():
4949
["cefpython.pyx"],
5050

5151
language='c++',
52-
include_dirs=[r'./../', r'./../../', r'./../../../', r'./../../../include_cython/'],
52+
include_dirs=[r'./../', r'./../../', r'./../../../', r'./../../../cython_includes/'],
5353

5454
library_dirs=[
5555
r'./',
5656
r"c:/Program Files (x86)/Windows Kits/8.0/Lib/win8/um/x86/",
5757
r'./../../http_authentication/Release/',
5858
r'./../../v8function_handler/Release_py%s/' % PYTHON_VERSION,
59-
r'./../../client_handler/Release_py%s/' % PYTHON_VERSION
59+
r'./../../client_handler/Release_py%s/' % PYTHON_VERSION,
60+
r'./../../../cpp_utils/Release/cpp_utils.lib',
6061
],
6162

6263
libraries=[

cefpython/cef3/windows/setup/setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Stop on first error, otherwise hundreds of errors appear in the console.
99
Options.fast_fail = True
1010

11-
# Written to include_cython/compile_time_constants.pxi
11+
# Written to cython_includes/compile_time_constants.pxi
1212
CEF_VERSION = 3
1313

1414
"""
@@ -34,8 +34,8 @@
3434

3535
def CompileTimeConstants():
3636

37-
print("Generating: include_cython/compile_time_constants.pxi")
38-
with open("./../../../include_cython/compile_time_constants.pxi", "w") as fd:
37+
print("Generating: cython_includes/compile_time_constants.pxi")
38+
with open("./../../../cython_includes/compile_time_constants.pxi", "w") as fd:
3939
fd.write('# This file was generated by setup.py\n')
4040
# A way around Python 3.2 bug: UNAME_SYSNAME is not set.
4141
fd.write('DEF UNAME_SYSNAME = "%s"\n' % platform.uname()[0])
@@ -49,7 +49,7 @@ def CompileTimeConstants():
4949
["cefpython.pyx"],
5050

5151
language='c++',
52-
include_dirs=[r'./../', r'./../../', r'./../../../', r'./../../../include_cython/'],
52+
include_dirs=[r'./../', r'./../../', r'./../../../', r'./../../../cython_includes/'],
5353

5454
library_dirs=[
5555
r'./',

cefpython/cefpython.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ g_applicationSettings = {}
3636

3737
# All .pyx files need to be included here.
3838

39-
include "include_cython/compile_time_constants.pxi"
39+
include "cython_includes/compile_time_constants.pxi"
4040
include "imports.pyx"
4141

4242
include "utils.pyx"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "PaintBuffer.h"

cefpython/cpp_utils/PaintBuffer.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2012 CefPython Authors. All rights reserved.
2+
// License: New BSD License.
3+
// Website: http://code.google.com/p/cefpython/
4+
5+
#pragma once
6+
7+
#include "windows.h"
8+
#include "stdio.h"
9+
10+
void FlipBufferUpsideDown(void* _dest, void* _src, int width, int height) {
11+
// In CEF the buffer passed to Browser.GetImage() & RenderHandler.OnPaint()
12+
// has upper-left origin, but some libraries like Panda3D require
13+
// bottom-left origin.
14+
__int32* dest = (__int32*)_dest;
15+
__int32* src = (__int32*)_src;
16+
unsigned int tb;
17+
int length = width*height;
18+
for (int y = 0; y < height; y++) {
19+
tb = length - ((y+1)*width);
20+
memcpy(&dest[tb], &src[y*width], width*4);
21+
}
22+
}
23+
24+
void SwapBufferFromBgraToRgba(void* _dest, void* _src, int width, int height) {
25+
__int32* dest = (__int32*)_dest;
26+
__int32* src = (__int32*)_src;
27+
__int32 rgba;
28+
__int32 bgra;
29+
int length = width*height;
30+
for (int i = 0; i < length; i++) {
31+
bgra = src[i];
32+
rgba = (bgra & 0x00ff0000) >> 16
33+
| (bgra & 0xff00ff00)
34+
| (bgra & 0x000000ff) << 16;
35+
dest[i] = rgba;
36+
}
37+
}

cefpython/cpp_utils/cpp_utils.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 10.00
3+
# Visual Studio 2008
4+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cpp_utils", "cpp_utils.vcproj", "{4BC60266-2D0B-4C83-8CEC-B243A6952A1A}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Win32 = Debug|Win32
9+
Release|Win32 = Release|Win32
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{4BC60266-2D0B-4C83-8CEC-B243A6952A1A}.Debug|Win32.ActiveCfg = Debug|Win32
13+
{4BC60266-2D0B-4C83-8CEC-B243A6952A1A}.Debug|Win32.Build.0 = Debug|Win32
14+
{4BC60266-2D0B-4C83-8CEC-B243A6952A1A}.Release|Win32.ActiveCfg = Release|Win32
15+
{4BC60266-2D0B-4C83-8CEC-B243A6952A1A}.Release|Win32.Build.0 = Release|Win32
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

0 commit comments

Comments
 (0)
X Tutup