convert_image.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #!/usr/bin/python
  2. # Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  3. # All rights reserved.
  4. # Code licensed under the BSD License.
  5. # http://www.anki3d.org/LICENSE
  6. import optparse
  7. import subprocess
  8. import re
  9. import os
  10. import struct
  11. import copy
  12. import tempfile
  13. import shutil
  14. #
  15. # Config
  16. #
  17. class Config:
  18. in_files = []
  19. out_file = ""
  20. fast = False
  21. type = 0
  22. normal = False
  23. convert_path = ""
  24. no_alpha = False
  25. store_compressed = False
  26. store_uncompressed = True
  27. to_linear_rgb = False
  28. tmp_dir = ""
  29. #
  30. # AnKi texture
  31. #
  32. # Texture type
  33. TT_NONE = 0
  34. TT_2D = 1
  35. TT_CUBE = 2
  36. TT_3D = 3
  37. TT_2D_ARRAY = 4
  38. # Color format
  39. CF_NONE = 0
  40. CF_RGB8 = 1
  41. CF_RGBA8 = 2
  42. # Data compression
  43. DC_NONE = 0
  44. DC_RAW = 1 << 0
  45. DC_ETC2 = 1 << 1
  46. DC_S3TC = 1 << 2
  47. # Texture filtering
  48. TF_DEFAULT = 0
  49. TF_LINEAR = 1
  50. TF_NEAREST = 2
  51. #
  52. # DDS
  53. #
  54. # dwFlags of DDSURFACEDESC2
  55. DDSD_CAPS = 0x00000001
  56. DDSD_HEIGHT = 0x00000002
  57. DDSD_WIDTH = 0x00000004
  58. DDSD_PITCH = 0x00000008
  59. DDSD_PIXELFORMAT = 0x00001000
  60. DDSD_MIPMAPCOUNT = 0x00020000
  61. DDSD_LINEARSIZE = 0x00080000
  62. DDSD_DEPTH = 0x00800000
  63. # ddpfPixelFormat of DDSURFACEDESC2
  64. DDPF_ALPHAPIXELS = 0x00000001
  65. DDPF_FOURCC = 0x00000004
  66. DDPF_RGB = 0x00000040
  67. # dwCaps1 of DDSCAPS2
  68. DDSCAPS_COMPLEX = 0x00000008
  69. DDSCAPS_TEXTURE = 0x00001000
  70. DDSCAPS_MIPMAP = 0x00400000
  71. # dwCaps2 of DDSCAPS2
  72. DDSCAPS2_CUBEMAP = 0x00000200
  73. DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400
  74. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800
  75. DDSCAPS2_CUBEMAP_POSITIVEY = 0x00001000
  76. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000
  77. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000
  78. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000
  79. DDSCAPS2_VOLUME = 0x00200000
  80. class DdsHeader:
  81. """ The header of a dds file """
  82. _fields = [
  83. ('dwMagic', '4s'),
  84. ('dwSize', 'I'),
  85. ('dwFlags', 'I'),
  86. ('dwHeight', 'I'),
  87. ('dwWidth', 'I'),
  88. ('dwPitchOrLinearSize', 'I'),
  89. ('dwDepth', 'I'),
  90. ('dwMipMapCount', 'I'),
  91. ('dwReserved1', '44s'),
  92. # Pixel format
  93. ('dwSize', 'I'),
  94. ('dwFlags', 'I'),
  95. ('dwFourCC', '4s'),
  96. ('dwRGBBitCount', 'I'),
  97. ('dwRBitMask', 'I'),
  98. ('dwGBitMask', 'I'),
  99. ('dwBBitMask', 'I'),
  100. ('dwRGBAlphaBitMask', 'I'),
  101. ('dwCaps1', 'I'),
  102. ('dwCaps2', 'I'),
  103. ('dwCapsReserved', '8s'),
  104. ('dwReserved2', 'I')]
  105. def __init__(self, buff):
  106. buff_format = self.get_format()
  107. items = struct.unpack(buff_format, buff)
  108. for field, value in map(None, self._fields, items):
  109. setattr(self, field[0], value)
  110. @classmethod
  111. def get_format(cls):
  112. return '<' + ''.join([f[1] for f in cls._fields])
  113. @classmethod
  114. def get_size(cls):
  115. return struct.calcsize(cls.get_format())
  116. #
  117. # ETC2
  118. #
  119. class PkmHeader:
  120. """ The header of a pkm file """
  121. _fields = [
  122. ("magic", "6s"),
  123. ("type", "H"),
  124. ("width", "H"),
  125. ("height", "H"),
  126. ("origWidth", "H"),
  127. ("origHeight", "H")]
  128. def __init__(self, buff):
  129. buff_format = self.get_format()
  130. items = struct.unpack(buff_format, buff)
  131. for field, value in map(None, self._fields, items):
  132. setattr(self, field[0], value)
  133. @classmethod
  134. def get_format(cls):
  135. return ">" + "".join([f[1] for f in cls._fields])
  136. @classmethod
  137. def get_size(cls):
  138. return struct.calcsize(cls.get_format())
  139. #
  140. # Functions
  141. #
  142. def printi(s):
  143. print("[I] %s" % s)
  144. def printw(s):
  145. print("[W] %s" % s)
  146. def is_power2(num):
  147. """ Returns true if a number is a power of two """
  148. return num != 0 and ((num & (num - 1)) == 0)
  149. def get_base_fname(path):
  150. """ From path/to/a/file.ext return the "file" """
  151. return os.path.splitext(os.path.basename(path))[0]
  152. def parse_commandline():
  153. """ Parse the command line arguments """
  154. parser = optparse.OptionParser(usage = "usage: %prog [options]", \
  155. description = "This program converts a single image or a number " \
  156. "of images (for 3D and 2DArray textures) to AnKi texture format." \
  157. " It requires 4 different applications/executables to " \
  158. "operate: convert, identify, nvcompress and etcpack. These " \
  159. "applications should be in PATH except the convert where you " \
  160. "need to define the executable explicitly")
  161. parser.add_option("-i", "--input", dest = "inp",
  162. type = "string", help = "specify the image(s) to convert. " \
  163. "Seperate with :")
  164. parser.add_option("-o", "--output", dest = "out",
  165. type = "string", help = "specify output AnKi image. ")
  166. parser.add_option("-t", "--type", dest = "type",
  167. type = "string", default = "2D",
  168. help = "type of the image (2D or cube or 3D or 2DArray)")
  169. parser.add_option("-f", "--fast", dest = "fast",
  170. type = "int", action = "store", default = 0,
  171. help = "run the fast version of the converters")
  172. parser.add_option("-n", "--normal", dest = "normal",
  173. type = "int", action = "store", default = 0,
  174. help = "assume the texture is normal")
  175. parser.add_option("-c", "--convert-path", dest = "convert_path",
  176. type = "string", default = "/usr/bin/convert",
  177. help = "the executable where convert tool is " \
  178. "located. Stupid etcpack cannot get it from PATH")
  179. parser.add_option("--no-alpha", dest = "no_alpha",
  180. type = "int", action = "store", default = 0,
  181. help = "remove alpha channel")
  182. parser.add_option("--store-uncompressed", dest = "store_uncompressed",
  183. type = "int", action = "store", default = 0,
  184. help = "store or not uncompressed data")
  185. parser.add_option("--store-compressed", dest = "store_compressed",
  186. type = "int", action = "store", default = 1,
  187. help = "store or not compressed data")
  188. parser.add_option("--to-linear-rgb", dest = "to_linear_rgb",
  189. type = "int", action = "store", default = 0,
  190. help = "assume the input textures are sRGB. If this option is " \
  191. "true then convert them to linear RGB")
  192. parser.add_option("--filter", dest = "filter", type = "string",
  193. default = "default", help = "texture filtering. Can be: " \
  194. "default, linear, nearest")
  195. parser.add_option("--mips_count", dest = "mips_count", type = "int",
  196. default = "0xFFFF", help = "Max number of mipmaps")
  197. # Add the default value on each option when printing help
  198. for option in parser.option_list:
  199. if option.default != ("NO", "DEFAULT"):
  200. option.help += (" " if option.help else "") + "[default: %default]"
  201. (options, args) = parser.parse_args()
  202. if not options.inp or not options.out or not options.convert_path:
  203. parser.error("argument is missing")
  204. if options.type == "2D":
  205. typ = TT_2D
  206. elif options.type == "cube":
  207. typ = TT_CUBE
  208. elif options.type == "3D":
  209. typ = TT_3D
  210. elif options.type == "2DArray":
  211. typ = TT_2D_ARRAY
  212. else:
  213. parser.error("Unrecognized type: " + options.type)
  214. if options.filter == "default":
  215. filter = TF_DEFAULT
  216. elif options.filter == "linear":
  217. filter = TF_LINEAR
  218. elif options.filter == "nearest":
  219. filter = TF_NEAREST
  220. else:
  221. parser.error("Unrecognized type: " + options.filter)
  222. if not options.store_uncompressed \
  223. and not options.store_compressed:
  224. parser.error("One of --store-compressed and --store-uncompressed "\
  225. "should be True")
  226. if int(options.mips_count) <= 0:
  227. parser.error("Wrong number of mipmaps")
  228. config = Config()
  229. config.in_files = options.inp.split(":")
  230. config.out_file = options.out
  231. config.fast = options.fast
  232. config.type = typ
  233. config.normal = options.normal
  234. config.convert_path = options.convert_path
  235. config.no_alpha = options.no_alpha
  236. config.store_uncompressed = options.store_uncompressed
  237. config.store_compressed = options.store_compressed
  238. config.to_linear_rgb = options.to_linear_rgb
  239. config.filter = filter
  240. config.mips_count = int(options.mips_count)
  241. return config
  242. def identify_image(in_file):
  243. """ Return the size of the input image and the internal format """
  244. color_format = CF_NONE
  245. width = 0
  246. height = 0
  247. proc = subprocess.Popen(["identify", "-verbose" , in_file],
  248. stdout=subprocess.PIPE)
  249. stdout_str = proc.stdout.read()
  250. # Make sure the colorspace is what we want
  251. """reg = re.search(r"Colorspace: (.*)", stdout_str)
  252. if not reg or reg.group(1) != "RGB":
  253. raise Exception("Something is wrong with the colorspace")"""
  254. # Get the size of the iamge
  255. reg = re.search(r"Geometry: ([0-9]*)x([0-9]*)\+", stdout_str)
  256. if not reg:
  257. raise Exception("Cannot extract size")
  258. # Identify the color space
  259. """if not re.search(r"red: 8-bit", stdout_str) \
  260. or not re.search(r"green: 8-bit", stdout_str) \
  261. or not re.search(r"blue: 8-bit", stdout_str): \
  262. raise Exception("Incorrect channel depths")"""
  263. if re.search(r"alpha: 8-bit", stdout_str):
  264. color_format = CF_RGBA8
  265. color_format_str = "RGBA"
  266. else:
  267. color_format = CF_RGB8
  268. color_format_str = "RGB"
  269. # print some stuff and return
  270. printi("width: %s, height: %s color format: %s" % \
  271. (reg.group(1), reg.group(2), color_format_str))
  272. return (color_format, int(reg.group(1)), int(reg.group(2)))
  273. def create_mipmaps(in_file, tmp_dir, width_, height_, color_format, \
  274. to_linear_rgb, max_mip_count):
  275. """ Create a number of images for all mipmaps """
  276. printi("Generate mipmaps")
  277. width = width_
  278. height = height_
  279. mips_fnames = []
  280. while width >= 4 and height >= 4:
  281. size_str = "%dx%d" % (width, height)
  282. out_file_str = os.path.join(tmp_dir, get_base_fname(in_file)) \
  283. + "." + size_str
  284. printi(" %s.tga" % out_file_str)
  285. mips_fnames.append(out_file_str)
  286. args = ["convert", in_file]
  287. # to linear
  288. if to_linear_rgb:
  289. if color_format != CF_RGB8:
  290. raise Exception("to linear RGB only supported for RGB textures")
  291. args.append("-set")
  292. args.append("colorspace")
  293. args.append("sRGB")
  294. args.append("-colorspace")
  295. args.append("RGB")
  296. # resize
  297. args.append("-resize")
  298. args.append(size_str)
  299. # alpha
  300. args.append("-alpha")
  301. if color_format == CF_RGB8:
  302. args.append("deactivate")
  303. else:
  304. args.append("activate")
  305. args.append(out_file_str + ".tga")
  306. subprocess.check_call(args)
  307. if(len(mips_fnames) == max_mip_count):
  308. break;
  309. width = width / 2
  310. height = height / 2
  311. return mips_fnames
  312. def create_etc_images(mips_fnames, tmp_dir, fast, color_format, convert_path):
  313. """ Create the etc files """
  314. printi("Creating ETC images")
  315. # Copy the convert tool to the working dir so that etcpack will see it
  316. shutil.copy2(convert_path, \
  317. os.path.join(tmp_dir, os.path.basename(convert_path)))
  318. for fname in mips_fnames:
  319. # Unfortunately we need to flip the image. Use convert again
  320. in_fname = fname + ".tga"
  321. flipped_fname = fname + "_flip.tga"
  322. args = ["convert", in_fname, "-flip", flipped_fname]
  323. subprocess.check_call(args)
  324. in_fname = flipped_fname
  325. printi(" %s" % in_fname)
  326. args = ["etcpack", in_fname, tmp_dir, "-c", "etc2"]
  327. if fast:
  328. args.append("-s")
  329. args.append("fast")
  330. args.append("-f")
  331. if color_format == CF_RGB8:
  332. args.append("RGB")
  333. else:
  334. args.append("RGBA")
  335. # Call the executable AND change the working directory so that etcpack
  336. # will find convert
  337. subprocess.check_call(args, stdout = subprocess.PIPE, cwd = tmp_dir)
  338. def create_dds_images(mips_fnames, tmp_dir, fast, color_format, normal):
  339. """ Create the dds files """
  340. printi("Creating DDS images")
  341. for fname in mips_fnames:
  342. # Unfortunately we need to flip the image. Use convert again
  343. in_fname = fname + ".tga"
  344. flipped_fname = fname + "_flip.tga"
  345. args = ["convert", in_fname, "-flip", flipped_fname]
  346. subprocess.check_call(args)
  347. in_fname = flipped_fname
  348. # Continue
  349. out_fname = os.path.join(tmp_dir, os.path.basename(fname) + ".dds")
  350. printi(" %s" % out_fname)
  351. args = ["nvcompress", "-silent", "-nomips"]
  352. if fast:
  353. args.append("-fast")
  354. if color_format == CF_RGB8:
  355. if not normal:
  356. args.append("-bc1")
  357. else:
  358. args.append("-bc1n")
  359. elif color_format == CF_RGBA8:
  360. args.append("-alpha")
  361. if not normal:
  362. args.append("-bc3")
  363. else:
  364. args.append("-bc3n")
  365. args.append(in_fname)
  366. args.append(out_fname)
  367. subprocess.check_call(args, stdout = subprocess.PIPE)
  368. def write_raw(tex_file, fname, width, height, color_format):
  369. """ Append raw data to the AnKi texture file """
  370. printi(" Appending %s" % fname)
  371. # Read and check the header
  372. uncompressed_tga_header = struct.pack("BBBBBBBBBBBB", \
  373. 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  374. in_file = open(fname, "rb")
  375. tga_header = in_file.read(12)
  376. if len(tga_header) != 12:
  377. raise Exception("Failed reading TGA header")
  378. if uncompressed_tga_header != tga_header:
  379. raise Exception("Incorrect TGA header")
  380. # Read the size and bpp
  381. header6_buff = in_file.read(6)
  382. if len(header6_buff) != 6:
  383. raise Exception("Failed reading TGA header #2")
  384. header6 = struct.unpack("BBBBBB", header6_buff)
  385. img_width = header6[1] * 256 + header6[0]
  386. img_height = header6[3] * 256 + header6[2]
  387. img_bpp = header6[4];
  388. if (color_format != CF_RGB8 or img_bpp != 24) \
  389. and (color_format != CF_RGBA8 or img_bpp != 32):
  390. raise Exception("Unexpected bpp")
  391. if img_width != width or img_height != height:
  392. raise Exception("Unexpected width or height")
  393. # Read the data
  394. data_size = width * height
  395. if color_format == CF_RGB8:
  396. data_size *= 3
  397. else:
  398. data_size *= 4
  399. data = bytearray(in_file.read(data_size))
  400. if len(data) != data_size:
  401. raise Exception("Failed to read all data")
  402. tmp = in_file.read(128)
  403. if len(tmp) != 0:
  404. printw(" File shouldn't contain more data")
  405. # Swap colors
  406. bpp = img_bpp / 8
  407. for i in xrange(0, data_size, bpp):
  408. temp = data[i];
  409. data[i] = data[i + 2];
  410. data[i + 2] = temp;
  411. # Write data to tex_file
  412. tex_file.write(data)
  413. def write_s3tc(out_file, fname, width, height, color_format):
  414. """ Append s3tc data to the AnKi texture file """
  415. # Read header
  416. printi(" Appending %s" % fname)
  417. in_file = open(fname, "rb")
  418. header = in_file.read(DdsHeader.get_size())
  419. if len(header) != DdsHeader.get_size():
  420. raise Exception("Failed to read DDS header")
  421. dds_header = DdsHeader(header)
  422. if dds_header.dwWidth != width or dds_header.dwHeight != height:
  423. raise Exception("Incorrect width")
  424. if color_format == CF_RGB8 \
  425. and dds_header.dwFourCC != "DXT1":
  426. raise Exception("Incorrect format. Expecting DXT1")
  427. if color_format == CF_RGBA8 \
  428. and dds_header.dwFourCC != "DXT5":
  429. raise Exception("Incorrect format. Expecting DXT5")
  430. # Read and write the data
  431. if color_format == CF_RGB8:
  432. block_size = 8
  433. else:
  434. block_size = 16
  435. data_size = (width / 4) * (height / 4) * block_size
  436. data = in_file.read(data_size)
  437. if len(data) != data_size:
  438. raise Exception("Failed to read DDS data")
  439. # Make sure that the file doesn't contain any more data
  440. tmp = in_file.read(1)
  441. if len(tmp) != 0:
  442. printw(" File shouldn't contain more data")
  443. out_file.write(data)
  444. def write_etc(out_file, fname, width, height, color_format):
  445. """ Append etc2 data to the AnKi texture file """
  446. printi(" Appending %s" % fname)
  447. # Read header
  448. in_file = open(fname, "rb")
  449. header = in_file.read(PkmHeader.get_size())
  450. if len(header) != PkmHeader.get_size():
  451. raise Exception("Failed to read PKM header")
  452. pkm_header = PkmHeader(header)
  453. if pkm_header.magic != "PKM 20":
  454. raise Exception("Incorrect PKM header")
  455. if width != pkm_header.width or height != pkm_header.height:
  456. raise Exception("Incorrect PKM width or height")
  457. # Read and write the data
  458. data_size = (pkm_header.width / 4) * (pkm_header.height / 4) * 8
  459. data = in_file.read(data_size)
  460. if len(data) != data_size:
  461. raise Exception("Failed to read PKM data")
  462. # Make sure that the file doesn't contain any more data
  463. tmp = in_file.read(1)
  464. if len(tmp) != 0:
  465. printw(" File shouldn't contain more data")
  466. out_file.write(data)
  467. def convert(config):
  468. """ This is the function that does all the work """
  469. # Invoke app named "identify" to get internal format and width and height
  470. (color_format, width, height) = identify_image(config.in_files[0])
  471. if not is_power2(width) or not is_power2(height):
  472. raise Exception("Image width and height should power of 2")
  473. if color_format == CF_RGBA8 and config.normal:
  474. raise Exception("RGBA image and normal does not make much sense")
  475. for i in range(1, len(config.in_files)):
  476. (color_format_2, width_2, height_2) = identify_image(config.in_files[i])
  477. if width != width_2 or height != height_2 \
  478. or color_format != color_format_2:
  479. raise Exception("Images are not same size and color space")
  480. if config.no_alpha:
  481. color_format = CF_RGB8
  482. # Create images
  483. for in_file in config.in_files:
  484. mips_fnames = create_mipmaps(in_file, config.tmp_dir, width, height, \
  485. color_format, config.to_linear_rgb, config.mips_count)
  486. # Create etc images
  487. create_etc_images(mips_fnames, config.tmp_dir, config.fast, \
  488. color_format, config.convert_path)
  489. # Create dds images
  490. create_dds_images(mips_fnames, config.tmp_dir, config.fast, \
  491. color_format, config.normal)
  492. # Open file
  493. fname = config.out_file
  494. printi("Writing %s" % fname)
  495. tex_file = open(fname, "wb")
  496. # Write header
  497. ak_format = "8sIIIIIIII"
  498. data_compression = 0
  499. if config.store_compressed:
  500. data_compression = data_compression | DC_S3TC | DC_ETC2
  501. if config.store_uncompressed:
  502. data_compression = data_compression | DC_RAW
  503. buff = struct.pack(ak_format,
  504. b"ANKITEX1",
  505. width,
  506. height,
  507. len(config.in_files),
  508. config.type,
  509. color_format,
  510. data_compression,
  511. config.normal,
  512. len(mips_fnames))
  513. tex_file.write(buff)
  514. # Write header padding
  515. header_padding_size = 128 - struct.calcsize(ak_format)
  516. if header_padding_size != 88:
  517. raise Exception("Check the header")
  518. for i in range(0, header_padding_size):
  519. tex_file.write('\0')
  520. # For each compression
  521. for compression in range(0, 3):
  522. tmp_width = width
  523. tmp_height = height
  524. # For each level
  525. for i in range(0, len(mips_fnames)):
  526. # For each image
  527. for in_file in config.in_files:
  528. size_str = "%dx%d" % (tmp_width, tmp_height)
  529. in_base_fname = os.path.join(config.tmp_dir, \
  530. get_base_fname(in_file)) + "." + size_str
  531. # Write RAW
  532. if compression == 0 and config.store_uncompressed:
  533. write_raw(tex_file, in_base_fname + ".tga", \
  534. tmp_width, tmp_height, color_format)
  535. # Write S3TC
  536. elif compression == 1 and config.store_compressed:
  537. write_s3tc(tex_file, in_base_fname + ".dds", \
  538. tmp_width, tmp_height, color_format)
  539. # Write ETC
  540. elif compression == 2 and config.store_compressed:
  541. write_etc(tex_file, in_base_fname + "_flip.pkm", \
  542. tmp_width, tmp_height, color_format)
  543. tmp_width = tmp_width / 2
  544. tmp_height = tmp_height / 2
  545. def main():
  546. """ The main """
  547. # Parse cmd line args
  548. config = parse_commandline();
  549. if config.type == TT_CUBE and len(config.in_files) != 6:
  550. raise Exception("Not enough images for cube generation")
  551. if (config.type == TT_3D or config.type == TT_2D_ARRAY) \
  552. and len(config.in_files) < 2:
  553. #raise Exception("Not enough images for 2DArray/3D texture")
  554. printw("Not enough images for 2DArray/3D texture")
  555. if config.type == TT_2D and len(config.in_files) != 1:
  556. raise Exception("Only one image for 2D textures needed")
  557. if not os.path.isfile(config.convert_path):
  558. raise Exception("Tool convert not found: " + config.convert_path)
  559. # Setup the temp dir
  560. config.tmp_dir = tempfile.mkdtemp("_ankitex")
  561. # Do the work
  562. try:
  563. convert(config)
  564. finally:
  565. shutil.rmtree(config.tmp_dir)
  566. # Done
  567. printi("Done!")
  568. if __name__ == "__main__":
  569. main()