-
Notifications
You must be signed in to change notification settings - Fork 3
/
widget_imageconv.py
261 lines (218 loc) · 11.6 KB
/
widget_imageconv.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
259
260
261
import arduboy.image
import constants
import gui_common
import gui_utils
import widgets_common
import debug_actions
import logging
from PyQt6.QtWidgets import QVBoxLayout, QWidget, QPushButton, QGraphicsView, QGraphicsScene, QGroupBox, QMessageBox
from PyQt6.QtWidgets import QGraphicsPixmapItem, QFileDialog, QHBoxLayout, QPlainTextEdit, QCheckBox, QLineEdit
from PyQt6.QtGui import QPixmap, QPen, QRegularExpressionValidator
from PyQt6.QtCore import QRectF, Qt, QRegularExpression
from PIL import Image
class ImageConvertWidget(QWidget):
def __init__(self):
super().__init__()
self.rects = []
self.pilimage = None
self.setAcceptDrops(True)
full_layout = QVBoxLayout()
# Image display + config portion is hbox layout
top_widget = QGroupBox("Image converter")
top_layout = QHBoxLayout()
# top_layout.setSpacing(20)
top_widget.setLayout(top_layout)
# Image display by itself is another vbox
# ---------------------------------------
image_widget = QWidget()
image_layout = QVBoxLayout()
image_layout.setContentsMargins(5, 0, 5, 0)
image_widget.setLayout(image_layout)
# You put stuff in the scene. We go ahead and add our base image item
self.image_scene = QGraphicsScene()
self.image_item = QGraphicsPixmapItem()
self.image_scene.addItem(self.image_item)
# The view is a window into a scene, this is what you put into the layout?
self.image_view = widgets_common.CustomGraphicsView()
self.image_view.setScene(self.image_scene)
self.image_view.set_zoom(4.0)
self.image_view.setStyleSheet(f"background-color: {gui_common.SUBDUEDCOLOR}")
self.image_view.setToolTip("Preview image with tiling. Note: PyQt doesn't let you drag+drop here (you can drag+drop anywhere else to load an image)")
image_layout.addWidget(self.image_view)
# Config display another vbox
# ---------------------------------------
config_widget = QWidget()
config_layout = QVBoxLayout()
config_layout.setContentsMargins(5,0,5,0)
config_widget.setLayout(config_layout)
self.select_image_button = QPushButton("Select Image")
self.select_image_button.clicked.connect(self.do_load_image)
config_layout.addWidget(self.select_image_button)
self.tilesize = widgets_common.WidthHeightWidget()
tilesize_container, self.tilesize_cb = gui_utils.make_toggleable_element("Tiled image", self.tilesize, nostretch=True)
self.tilesize_cb.setToolTip("Rearranges output data so tile pixels are grouped together; see help (F1)")
config_layout.addWidget(tilesize_container)
self.tilesize.onchange.connect(self.recalculate_rects)
self.tilesize_cb.stateChanged.connect(self.recalculate_rects)
self.spacing_number = widgets_common.NumberOnlyLineEdit()
self.spacing_number.setText("0")
spacing_container, self.spacing_cb = gui_utils.make_toggleable_element("Tile spacing", self.spacing_number, nostretch=True)
self.spacing_cb.setToolTip("Trims space between tiles in the case of sparse tilesheets; see help (F1)")
config_layout.addWidget(spacing_container)
self.spacing_number.textChanged.connect(self.recalculate_rects)
self.spacing_cb.stateChanged.connect(self.recalculate_rects)
self.sepmask_cb = QCheckBox("Separate mask variable")
self.sepmask_cb.setToolTip("Make mask generate in a separate variable rather than interleaved with data, but NOT for fx data!")
self.sepmask_cb.stateChanged.connect(self.recalculate_rects)
mask_container, self.mask_cb = gui_utils.make_toggleable_element("Generate mask from transparency", self.sepmask_cb) #, toggled=True)
self.mask_cb.setToolTip("Note: if checked, mask data always included, even if no transparency found!")
# self.mask_cb = QCheckBox("Use transparency to make mask")
# self.mask_cb.setChecked(True)
config_layout.addWidget(mask_container) # self.mask_cb)
self.mask_cb.stateChanged.connect(self.recalculate_rects)
self.generate_dims = QCheckBox("Add dimensions to array")
self.generate_dims.setChecked(True)
config_layout.addWidget(self.generate_dims)
self.image_name = QLineEdit("MyImage")
validator = QRegularExpressionValidator(QRegularExpression(r"[a-zA-Z_][a-zA-Z0-9_]*"), self)
self.image_name.setValidator(validator)
self.image_name.setToolTip("Exported image name (required)")
config_layout.addWidget(self.image_name)
# Lower controls for config
# ---------------------------------------
controls_widget = QWidget()
controls_layout = QHBoxLayout()
controls_layout.setContentsMargins(0, 0, 0, 0)
controls_widget.setLayout(controls_layout)
self.convert_button = QPushButton("Convert ↓")
self.convert_button.setToolTip("Convert directly to code and place in the textbox below")
self.convert_button.clicked.connect(self.do_convert)
controls_layout.addWidget(self.convert_button)
self.convert_file_button = QPushButton("Convert to .h")
self.convert_file_button.setToolTip("Convert to code and save to a .h file with appropriate boilerplate")
self.convert_file_button.clicked.connect(self.do_convert_file)
controls_layout.addWidget(self.convert_file_button)
self.convert_fx_button = QPushButton("Convert to fxdata")
self.convert_fx_button.setToolTip("Convert to a binary blob usable with the FX libraries and save to a .bin file")
self.convert_fx_button.clicked.connect(self.do_convert_fx)
controls_layout.addWidget(self.convert_fx_button)
config_layout.addWidget(controls_widget)
config_layout.setStretchFactor(controls_widget, 99)
# Setup the output box
# ---------------------------------------
self.output_box = QPlainTextEdit()
self.output_box.setReadOnly(True)
self.output_box.setPlaceholderText("Converted image code goes here")
self.output_box.setToolTip("Converted image code goes here")
# Put all the junk together
# ---------------------------------------
top_layout.addWidget(image_widget)
top_layout.addWidget(config_widget)
# top_layout.setStretchFactor(image_widget, 2)
# top_layout.setStretchFactor(config_widget, 3)
top_layout.setStretchFactor(image_widget, 1)
top_layout.setStretchFactor(config_widget, 0)
full_layout.addWidget(top_widget)
full_layout.addWidget(self.output_box)
full_layout.setStretchFactor(top_widget, 3)
full_layout.setStretchFactor(self.output_box, 2)
self.setLayout(full_layout)
def get_tileconfig(self):
result = arduboy.image.TileConfig()
if self.spacing_cb.isChecked():
try:
result.spacing = int(self.spacing_number.text())
except:
logging.warning(f"Bad spacing value: {self.spacing_number.text()}, not setting spacing!")
if self.tilesize_cb.isChecked():
try:
result.width, result.height = self.tilesize.get_values()
except:
logging.warning(f"Bad width/height values, not setting tiling!")
result.add_dimensions = self.generate_dims.isChecked()
result.use_mask = self.mask_cb.isChecked()
result.separate_header_mask = self.sepmask_cb.isChecked()
return result
def recalculate_rects(self):
if not self.pilimage:
logging.error("No image set in image converter widget, can't recalculate rectangles!")
return
# gather all the relevant values
tileconfig = self.get_tileconfig()
# Clear out the old rects
for r in self.rects:
self.image_scene.removeItem(r)
self.rects = []
pen = QPen(Qt.GlobalColor.red, 0.2, Qt.PenStyle.SolidLine)
# Now let's figure out where all the rects should go!
spriteWidth, spriteHeight, hframes, vframes = arduboy.image.expand_tileconfig(tileconfig, self.pilimage)
spacing = tileconfig.spacing
fy = spacing
for _ in range(vframes):
fx = spacing
for _ in range(hframes):
rect = QRectF(fx, fy, spriteWidth, spriteHeight)
self.rects.append(self.image_scene.addRect(rect, pen=pen))
fx += spriteWidth + spacing
fy += spriteHeight + spacing
def validate_inputs(self):
if not self.pilimage:
raise Exception("No image selected!")
if not self.image_name.text():
raise Exception("You must provide a name!")
def convert_self_fx(self):
self.validate_inputs()
config = self.get_tileconfig()
arduboy.image.validate_tileconfig_fx(config, self.pilimage)
_, fx = arduboy.image.convert_image(self.pilimage, self.image_name.text(), config)
return fx
def convert_self_code(self):
self.validate_inputs()
config = self.get_tileconfig()
arduboy.image.validate_tileconfig_code(config, self.pilimage)
code, _ = arduboy.image.convert_image(self.pilimage, self.image_name.text(), config)
return code
def load_image(self, file_path):
if file_path:
pixmap = QPixmap(file_path)
self.image_item.setPixmap(pixmap)
if self.pilimage:
self.pilimage.close()
self.pilimage = Image.open(file_path)
rect = pixmap.rect()
self.image_view.setSceneRect(0, 0, rect.width(), rect.height())
debug_actions.global_debug.add_action_str(f"Loaded image into converter: {file_path}")
self.recalculate_rects()
else:
logging.warning("No image provided to load_image!")
def do_load_image(self):
file_path, _ = QFileDialog.getOpenFileName(self, "Open Image", "", constants.IMAGE_FILEFILTER)
self.load_image(file_path)
def do_convert(self):
self.output_box.setPlainText(self.convert_self_code())
def do_convert_file(self):
self.validate_inputs()
# Unfortunately, in order for the dialog to remember the last location, you must pass in nothing as the default filename?
filepath, _ = QFileDialog.getSaveFileName(self, "Save image header", "", constants.HEADER_FILEFILTER)
if filepath:
code = self.convert_self_code()
with open(filepath, "w") as f:
f.write(arduboy.image.IMAGEHEADER_PREAMBLE + code)
def do_convert_fx(self):
self.validate_inputs()
if self.mask_cb.isChecked() and self.sepmask_cb.isChecked():
QMessageBox.information(self, "Incompatible settings", "FX binaries with masks are always stored interleaved. The 'Separate mask' setting will be ignored")
filepath, _ = QFileDialog.getSaveFileName(self, "Save image fx binary", "", constants.BIN_FILEFILTER)
if filepath:
binary = self.convert_self_fx()
with open(filepath, "wb") as f:
f.write(binary)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
url = event.mimeData().urls()[0]
self.load_image(url.toLocalFile())