-
Notifications
You must be signed in to change notification settings - Fork 0
/
uno_image.py
executable file
·258 lines (200 loc) · 8.44 KB
/
uno_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
Example usage of UNO, graphic objects and networking in LO extension
"""
import errno
import logging
import socket
import struct
import uno
import unohelper
import uuid
from com.sun.star.beans import PropertyValue
from com.sun.star.task import XJobExecutor
from PIL import Image
# Network functions
def create_socket(server_addr, server_port):
"""Creates socket, connects to server. Returns socket"""
logging.info("Server address: {}:{}".format(server_addr, server_port))
logging.info("Creating socket...")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
logging.error("Socket creation failed with error {}".format(err))
return None
logging.info("Connecting...")
try:
sock.connect((server_addr, server_port))
except socket.error as err:
logging.error("Connecting failed with error {}".format(err))
return None
return sock
def send_image(sock, image_size, image_bytes):
"""Sends image to open and set socket"""
logging.info("Image size in bytes: {}".format(image_size))
logging.info("Sending data...")
sz_bytes = struct.pack("i", image_size)
try:
sock.send(sz_bytes)
except socket.error as err:
logging.error("Error sending data: {}".format(err))
return None
try:
sock.send(image_bytes)
except socket.error as err:
logging.error("Error sending data: {}".format(err))
return None
return 1
def recv_bytes(sock, data_sz):
"""Receive exactly sz bytes from sock"""
data = ""
while len(data) < data_sz:
try:
data += sock.recv(data_sz, socket.MSG_WAITALL)
except socket.error as err:
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
logging.info("No data, trying again")
continue
else:
logging.error("Socket error: {}".format(err))
break
if len(data) < data_sz:
logging.warning("Server send no data")
return None
return data
def recv_image(sock):
"""Receive image data from server socket"""
data_sz_bytes = recv_bytes(sock, 4)
if data_sz_bytes is None:
logging.warning("Server send no data")
return None
data_sz = struct.unpack("i", data_sz_bytes)[0]
logging.info("Will accept {} bytes of data".format(data_sz))
img_data = recv_bytes(sock, data_sz)
logging.info("img sz: {}".format(len(img_data)))
if img_data is None:
logging.warning("Server send lesser data than needed")
return None
return img_data
# Misc functions
def generate_tmp_path():
"""Generates unique temporary path to save image"""
return "/tmp/" + str(uuid.uuid4()) + ".png"
class ImageExample(unohelper.Base, XJobExecutor):
'''Class that implements the service registered in LibreOffice'''
def __init__(self, context):
self.context = context
self.desktop = self.createUnoService("com.sun.star.frame.Desktop")
self.graphics = self.createUnoService("com.sun.star.graphic.GraphicProvider")
logging.basicConfig(filename="opencl_uno_example.log",
level=logging.WARNING)
def createUnoService(self, name):
return self.context.ServiceManager.createInstanceWithContext(name, self.context)
def show_warning(self, title, msg):
"""Shows warning message box"""
frame = self.desktop.ActiveFrame
window = frame.ContainerWindow
window.Toolkit.createMessageBox(
window,
uno.Enum('com.sun.star.awt.MessageBoxType', 'WARNINGBOX'),
uno.getConstantByName("com.sun.star.awt.MessageBoxButtons.BUTTONS_OK"),
title,
msg).execute()
def export_graphic(self, graphic, path):
"""Exports graphic to path"""
logging.debug("exporting graphic {} to {}".format(graphic, path))
url = unohelper.systemPathToFileUrl(path)
props = ((PropertyValue("URL", 0, url, 0),
PropertyValue("MimeType", 0, "image/" + path[-3:], 0),))
try:
self.graphics.storeGraphic(graphic, props)
except IOError as err:
self.show_warning("I/O exception", "I/O exception: {}".format(err))
def import_graphic(self, graphic, path, props):
"""Imports graphic from path by setting url in props"""
logging.debug("importing graphic {} from {}".format(graphic, path))
url = unohelper.systemPathToFileUrl(path)
props.setPropertyValue("GraphicURL", url)
def make_grayscale(self, graphic, props, sock):
"""
Makes graphic grayscale
This methods pass graphic to server socket sock and receive processed
image. Image then imported to LO and new url sets in props.
"""
path = generate_tmp_path()
logging.debug("making grayscale image {}".format(path))
self.export_graphic(graphic, path)
try:
image = Image.open(path)
except IOError as err:
logging.warning("Cannot open image {}".format(path))
self.show_warning("Image error", "Cannot open {}".format(path))
return
raw_data = image.tobytes()
if send_image(sock, len(raw_data), raw_data) is None:
logging.warning("Cannot send image {}".format(path))
self.show_warning("Network error", "Cannot send image")
return
processed_image_raw = recv_image(sock)
if processed_image_raw is None:
logging.warning("Failed to receive image. Exiting")
self.show_warning("Network error", "Cannot receive image.")
return
try:
processed_image = Image.frombytes(image.mode, image.size,
processed_image_raw)
except ValueError as err:
self.show_warning("Value error",
"Cannot save image: {}.".format(err))
return
path = generate_tmp_path()
processed_image.save(path)
self.import_graphic(graphic, path, props)
def trigger(self, args):
"""This method provides options for ImageExample service"""
if args == 'show_warning':
self.show_warning("Warning", "Warning description here")
elif args == 'export_images':
component = self.desktop.CurrentComponent
graphics = component.getGraphicObjects()
if not graphics.hasElements():
self.show_warning("No images",
"This document contains no images. Nothing to do.")
return
for name in graphics.getElementNames():
props = graphics.getByName(name)
url = props.getPropertyValue("GraphicURL")
props = ((PropertyValue("URL", 0, url, 0)), )
graphic = self.graphics.queryGraphic(props)
self.export_graphic(graphic, generate_tmp_path())
elif args == "grayscale_images":
component = self.desktop.CurrentComponent
try:
graphics = component.getGraphicObjects()
except AttributeError:
self.show_warning("Attribute Error",
"This LO component is not supported")
return
if not graphics.hasElements():
self.show_warning("No images",
"This document contains no images. Nothing to do.")
return
# TODO: create some config for this
server_addr = "192.168.1.241"
server_port = 9000
for name in graphics.getElementNames():
props = graphics.getByName(name)
url = props.getPropertyValue("GraphicURL")
props_url = ((PropertyValue("URL", 0, url, 0)), )
graphic = self.graphics.queryGraphic(props_url)
sock = create_socket(server_addr, server_port)
if sock is None:
logging.error("Cannot create or set up socket. Exiting")
self.show_warning("Network error", "Cannot create socket.")
return
self.make_grayscale(graphic, props, sock)
sock.close()
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
ImageExample,
'org.libreoffice.imageexample.ImageExample',
('com.sun.star.task.JobExecutor',))