Skip to content

Commit 3afd881

Browse files
committed
Add mipmap options
1 parent 1c52727 commit 3afd881

File tree

3 files changed

+84
-25
lines changed

3 files changed

+84
-25
lines changed

file-mat/file-mat.py

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
COPYRIGHT = AUTHOR
1919
COPYRIGHT_YEAR = '2019'
2020

21-
22-
2321
EDITOR_PROC = 'ijim-mat-export-dialog'
2422
LOAD_PROC = 'file-ijim-mat-load'
2523
LOAD_THUMB_PROC = 'file-ijim-mat-load-thumb'
2624
SAVE_PROC = 'file-ijim-mat-save'
2725

28-
DEBUG_MODE = False
29-
DISPLAY_LOD_TEXTURES = False
26+
DEBUG_MODE = True
27+
DISPLAY_MIPMAP_TEXTURES = False
28+
29+
DEFAULT_MAX_MIPMAP_LEVEL = 4
30+
DEFAULT_MIN_MIPMAP_SIZE = 16
31+
INPUT_MAX_MIPMAP_LEVEL = 16
32+
INPUT_MAX_MIN_MIPMAP_SIZE = 128
3033

3134

3235

@@ -47,7 +50,7 @@ def thumbnail_mat(file_path, thumb_size):
4750

4851
def load_mat(file_path, raw_filename):
4952
try:
50-
mat = MAT(DISPLAY_LOD_TEXTURES)
53+
mat = MAT(DISPLAY_MIPMAP_TEXTURES)
5154
mat.load_from_file(file_path)
5255
last_idx = len(mat.images) - 1
5356
if last_idx < 0:
@@ -100,6 +103,9 @@ def __init__(self):
100103
self.set_name(EDITOR_PROC)
101104
self.connect('response', self.on_response)
102105
self.connect("destroy", self.on_destroy)
106+
107+
self.mm_max_level = DEFAULT_MAX_MIPMAP_LEVEL
108+
self.mm_min_size = DEFAULT_MIN_MIPMAP_SIZE
103109

104110
export_opt_box = self.make_export_options_box()
105111
self.img_view_frame = self.make_images_view(reversed(gimp.image_list()))
@@ -117,29 +123,81 @@ def __init__(self):
117123
self.set_resizable(False)
118124

119125
def make_export_options_box(self):
126+
# Color depth
120127
self.rb_color_16bit = gtk.RadioButton(label="16 bit")
121128
self.rb_color_32bit = gtk.RadioButton(group=self.rb_color_16bit, label="32 bit")
122129

123130
box = gtk.VBox(True, 5)
124131
box.pack_start(self.rb_color_16bit, False, False)
125132
box.pack_start(self.rb_color_32bit, False, False)
126133

127-
frame = gimpui.Frame("Color depth:")
128-
frame.set_shadow_type(gtk.SHADOW_IN)
129-
frame.add(box)
130-
#frame.add(self.rb_32bit)
131-
134+
cdo_frame = gimpui.Frame("Color depth:")
135+
cdo_frame.set_shadow_type(gtk.SHADOW_IN)
136+
cdo_frame.add(box)
137+
138+
# Min MM size
139+
sb_mm_min_size = gtk.SpinButton(\
140+
gtk.Adjustment(self.mm_min_size, 2, INPUT_MAX_MIN_MIPMAP_SIZE, 1, 1, 0), climb_rate=1)
141+
sb_mm_min_size.set_tooltip_text(_('Min mipmap texture size'))
142+
sb_mm_min_size.set_has_frame(False)
143+
sb_mm_min_size.set_numeric(True)
144+
sb_mm_min_size.set_update_policy(gtk.UPDATE_IF_VALID)
145+
146+
def sb_mm_min_size_changed(sp):
147+
val = sp.get_value_as_int()
148+
if val != self.mm_min_size:
149+
if val > self.mm_min_size:
150+
self.mm_min_size = self.mm_min_size << 1
151+
else:
152+
self.mm_min_size = self.mm_min_size >> 1
153+
sp.set_value(self.mm_min_size)
154+
sb_mm_min_size.connect("changed", sb_mm_min_size_changed)
155+
156+
t_mm_min_size = gtk.Table(1, 2 ,False)
157+
t_mm_min_size.attach(gtk.Label("Min size: "), 0,1,0,1)
158+
t_mm_min_size.attach(sb_mm_min_size, 1,2,0,1)
159+
160+
# Max MM level
161+
sb_mm_level_count = gtk.SpinButton(\
162+
gtk.Adjustment(self.mm_max_level, 1, INPUT_MAX_MIPMAP_LEVEL, 1, 1, ), climb_rate=1)
163+
sb_mm_level_count.set_tooltip_text(_('Max mipmap level'))
164+
sb_mm_level_count.set_has_frame(False)
165+
sb_mm_level_count.set_numeric(True)
166+
sb_mm_level_count.set_update_policy(gtk.UPDATE_IF_VALID)
167+
168+
def sb_mm_max_level_changed(sp):
169+
self.mm_max_level = sp.get_value_as_int()
170+
sb_mm_level_count.connect("changed", sb_mm_max_level_changed)
171+
172+
t_mm_level_count = gtk.Table(1, 2 ,False)
173+
t_mm_level_count.attach(gtk.Label("Max level: "), 0,1,0,1)
174+
t_mm_level_count.attach(sb_mm_level_count, 1,2,0,1)
175+
176+
# Mipmap option frame
177+
box = gtk.VBox(True, 5)
178+
box.pack_start(t_mm_min_size, False, False)
179+
box.pack_start(t_mm_level_count, False, False)
180+
mmo_frame = gimpui.Frame("Mipmap Options:")
181+
mmo_frame.set_shadow_type(gtk.SHADOW_IN)
182+
mmo_frame.add(box)
183+
184+
# Main option frame
185+
o_box = gtk.VBox()
186+
o_box.set_size_request(70, -1)
187+
o_box.pack_start(cdo_frame, False, False, 10)
188+
o_box.pack_start(mmo_frame, False, False, 10)
189+
132190
box = gtk.VBox()
133-
box.set_size_request(70, -1)
134-
box.pack_start(frame, True, False, 50)
191+
box.set_size_request(90, -1)
192+
box.pack_start(o_box, True, False)
135193

136194
if DEBUG_MODE:
137195
def on_makemip_maps(btn):
138196
img = self.get_selected_image()
139197
if img == None:
140198
return
141199
try:
142-
for mm in make_mipmaps(img):
200+
for mm in make_mipmaps(img, self.mm_min_size, self.mm_max_level):
143201
sanitize_image(mm)
144202
gimp.Display(mm)
145203
finally:
@@ -149,7 +207,7 @@ def on_makemip_maps(btn):
149207
self.btn_mkmm = gtk.Button("Show\nMipmaps")
150208
self.btn_mkmm.set_sensitive(False)
151209
self.btn_mkmm.connect("clicked", on_makemip_maps)
152-
box.pack_start(self.btn_mkmm , False, False)
210+
o_box.pack_start(self.btn_mkmm , False, False)
153211
# DEBUG_MODE
154212

155213
return box
@@ -281,8 +339,8 @@ def selection_changed(iconview):
281339
def export_selected_images(self):
282340
mat = MAT()
283341
for row in self.liststore:
284-
if row[4]:
285-
i = row[0].duplicate()
342+
if row[4]: # 4 - include in export
343+
i = row[0].duplicate() # 0 - image
286344
if len(i.layers) > 1:
287345
i.merge_visible_layers(CLIP_TO_IMAGE)
288346

@@ -294,7 +352,7 @@ def export_selected_images(self):
294352

295353
# Export images as MAT file format
296354
bpp = 16 if self.rb_color_16bit.get_active() else 32
297-
mat.save_to_file(filename, bpp)
355+
mat.save_to_file(filename, bpp, self.mm_min_size, self.mm_max_level)
298356

299357
def set_btn_export_sensitive(self, sensitive):
300358
self.get_widget_for_response(RESPONSE_EXPORT).set_sensitive(sensitive)

file-mat/mat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def load_from_file(self, file_path, max_mmaps = -1):
5050
if not self._display_lod:
5151
break
5252

53-
def save_to_file(self, file_path, bpp):
53+
def save_to_file(self, file_path, bpp, min_mipmap_size = 8, max_mipmap_level = 4): #
5454
if bpp != 16 and bpp != 32:
5555
raise ValueError("bpp argument must be 16 or 32")
5656

@@ -67,7 +67,7 @@ def save_to_file(self, file_path, bpp):
6767
MAT._write_records(f, img_count)
6868

6969
for idx, i in enumerate(self._imgs):
70-
MAT.write_mipmap(f, i, cf)
70+
MAT.write_mipmap(f, i, cf, min_mipmap_size, max_mipmap_level)
7171
gimp.progress_update(idx / float(img_count))
7272

7373
@property
@@ -342,10 +342,10 @@ def _read_mipmap(f, ci): # f: file ci: color_format
342342
return MAT.mipmap(mmh.width, mmh.height, ci, pd)
343343

344344
@staticmethod
345-
def write_mipmap(f, img, ci): # f: file img: image ci: color_format
345+
def write_mipmap(f, img, ci, min_mipmap_size, max_mipmap_level): # f:file, img:image, ci:color_format, min_mipmap_size:int, max_mipmap_level:int
346346
imgs = [img]
347347
if (is_image_mipmap(img)):
348-
imgs += make_mipmaps(img, 4) # max mipmaps texture = 4
348+
imgs += make_mipmaps(img, min_mipmap_size, max_mipmap_level)
349349

350350
mmh = MAT.mat_mipmap_header(img.width, img.height, 0, 0, 0, len(imgs))
351351
f.write( MAT.mmm_serf.pack(*mmh) )

file-mat/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ def set_image_as_mipmap(img, is_mipmap):
1111
img.attach_new_parasite("mipmap", is_mipmap, "")
1212

1313

14-
def make_mipmaps(img, max = -1):
14+
def make_mipmaps(img, min_size = 1, max_level = -1):
15+
if min_size < 1 or max_level == 0: return []
1516
mipmaps = []
1617
mip_width = img.width >> 1
1718
mip_height = img.height >> 1
18-
while(mip_width >= 16 and mip_height >= 16 and max != 0):
19+
while(mip_width >= min_size and mip_height >= min_size and max_level - 1 != 0):
1920
mip = img.duplicate()
2021
mip.scale(mip_width, mip_height)
2122
#pdb.gimp_image_scale_full(mip, mip_width, mip_height, INTERPOLATION_CUBIC)
@@ -25,7 +26,7 @@ def make_mipmaps(img, max = -1):
2526
mipmaps.append(mip)
2627
mip_width = mip_width >> 1
2728
mip_height = mip_height >> 1
28-
max -= 1
29+
max_level -= 1
2930

3031
return mipmaps
3132

@@ -36,4 +37,4 @@ def sanitize_image(img):
3637
while(not img.undo_is_enabled()):
3738
img.undo_thaw()
3839

39-
img.clean_all()
40+
img.clean_all()

0 commit comments

Comments
 (0)