# Path: cuwo/qmo.py
# class QubicleFile:
#     def __init__(self, reader=None):
#         self.models = []
#         if reader is None:
#             return
#         if read_string(reader) != MAGIC:
#             raise NotImplementedError('invalid magic')
#         elif read_string(reader) != VERSION:
#             raise NotImplementedError('unsupported version')
#         for _ in range(reader.read_uint32()):
#             self.models.append(QubicleModel(reader))
#         self.junk = reader.read()
#         if len(self.junk) != 108:
#             raise NotImplementedError
# 
#     def write(self, writer):
#         write_string(writer, MAGIC)
#         write_string(writer, VERSION)
#         writer.write_uint32(len(self.models))
#         for model in self.models:
#             model.write(writer)
#         writer.write(b'\x00' * 108)
# 
# class QubicleModel:
#     def __init__(self, reader=None):
#         self.blocks = {}
#         if reader is None:
#             self.x_size = self.y_size = self.z_size = 0
#             self.name = 'Empty'
#             self.x_offset = self.y_offset = self.z_offset = 0
#             self.hidden = 0
#             return
#         self.x_size = reader.read_uint32()
#         self.y_size = reader.read_uint32()
#         self.z_size = reader.read_uint32()
#         self.name = read_string(reader)
#         self.x_offset = reader.read_int32()
#         self.y_offset = reader.read_int32()
#         self.z_offset = reader.read_int32()
#         self.hidden = reader.read_uint8()
#         rle_count = reader.read_uint32()
#         i = 0
#         while rle_count:
#             value = reader.read_uint32()
#             rle_count -= 1
#             if value == 2:  # repetition
#                 times = reader.read_uint32()
#                 color = reader.read_uint32()
#                 rle_count -= 2
#             else:
#                 times = 1
#                 color = value
#             if color & 0xFF000000 == 0:
#                 i += times
#                 continue
#             r = int(color & 0x0000FF)
#             g = int((color & 0x00FF00) >> 8)
#             b = int((color & 0xFF0000) >> 16)
#             for _ in range(times):
#                 x = i / (self.y_size * self.z_size)
#                 y = (i % (self.y_size * self.z_size)) / self.z_size
#                 z = i % self.z_size
#                 self.blocks[(int(x), int(y), int(z))] = (r, g, b)
#                 i += 1
# 
#     def write(self, writer):
#         writer.write_uint32(self.x_size)
#         writer.write_uint32(self.y_size)
#         writer.write_uint32(self.z_size)
#         write_string(writer, self.name)
#         writer.write_int32(self.x_offset)
#         writer.write_int32(self.y_offset)
#         writer.write_int32(self.z_offset)
#         writer.write_uint8(self.hidden)
#         writer.write_uint32(self.x_size * self.y_size * self.z_size)
#         for x in range(self.x_size):
#             for y in range(self.y_size):
#                 for z in range(self.z_size):
#                     c = self.blocks.get((x, y, z), None)
#                     if c is None:
#                         writer.write_uint32(0)
#                         continue
#                     r, g, b = c
#                     c2 = r | (g << 8) | (b << 16) | (0x7F << 24)
#                     writer.write_uint32(c2)
# 
# Path: cuwo/cub.py
# class CubModel:
#     def __init__(self, reader=None):
#         self.blocks = {}
#         if reader is None:
#             return
#         self.x_size = reader.read_uint32()
#         self.y_size = reader.read_uint32()
#         self.z_size = reader.read_uint32()
#         for z in range(self.z_size):
#             for y in range(self.y_size):
#                 for x in range(self.x_size):
#                     r = reader.read_uint8()
#                     g = reader.read_uint8()
#                     b = reader.read_uint8()
#                     if r == 0 and g == 0 and b == 0:
#                         continue
#                     self.blocks[(x, y, z)] = (r, g, b)
# 
#     def write(self, writer):
#         writer.write_uint32(self.x_size)
#         writer.write_uint32(self.y_size)
#         writer.write_uint32(self.z_size)
#         for z in range(self.z_size):
#             for y in range(self.y_size):
#                 for x in range(self.x_size):
#                     r, g, b = self.blocks.get((x, y, z), (0, 0, 0))
#                     writer.write_uint8(r)
#                     writer.write_uint8(g)
#                     writer.write_uint8(b)


import os
import sys
import argparse
import os
from cuwo.bytes import ByteReader, ByteWriter
from cuwo.qmo import QubicleFile, QubicleModel
from cuwo.cub import CubModel



def switch_axes(x, y, z):
    return x, z, y


def to_qmo(in_file, out_file):
    cub = CubModel(ByteReader(open(in_file, 'rb').read()))
    qmo_file = QubicleFile()
    qmo_model = QubicleModel()
    x_size, y_size, z_size = switch_axes(cub.x_size, cub.y_size, cub.z_size)
    qmo_model.x_size = x_size
    qmo_model.y_size = y_size
    qmo_model.z_size = z_size
    qmo_model.x_offset = -x_size / 2
    qmo_model.y_offset = 0
    qmo_model.z_offset = -z_size / 2
    for k, v in cub.blocks.items():
        x, y, z = k
        x2, y2, z2 = switch_axes(x, y, z)
        qmo_model.blocks[x2, y2, z2] = v
    qmo_file.models.append(qmo_model)
    writer = ByteWriter()
    qmo_file.write(writer)
    with open(out_file, 'wb') as fp:
        fp.write(writer.get())


def to_cub(in_file, out_file):

# Path: cuwo/qmo.py
# class QubicleFile:
#     def __init__(self, reader=None):
#         self.models = []
#         if reader is None:
#             return
#         if read_string(reader) != MAGIC:
#             raise NotImplementedError('invalid magic')
#         elif read_string(reader) != VERSION:
#             raise NotImplementedError('unsupported version')
#         for _ in range(reader.read_uint32()):
#             self.models.append(QubicleModel(reader))
#         self.junk = reader.read()
#         if len(self.junk) != 108:
#             raise NotImplementedError
# 
#     def write(self, writer):
#         write_string(writer, MAGIC)
#         write_string(writer, VERSION)
#         writer.write_uint32(len(self.models))
#         for model in self.models:
#             model.write(writer)
#         writer.write(b'\x00' * 108)
# 
# class QubicleModel:
#     def __init__(self, reader=None):
#         self.blocks = {}
#         if reader is None:
#             self.x_size = self.y_size = self.z_size = 0
#             self.name = 'Empty'
#             self.x_offset = self.y_offset = self.z_offset = 0
#             self.hidden = 0
#             return
#         self.x_size = reader.read_uint32()
#         self.y_size = reader.read_uint32()
#         self.z_size = reader.read_uint32()
#         self.name = read_string(reader)
#         self.x_offset = reader.read_int32()
#         self.y_offset = reader.read_int32()
#         self.z_offset = reader.read_int32()
#         self.hidden = reader.read_uint8()
#         rle_count = reader.read_uint32()
#         i = 0
#         while rle_count:
#             value = reader.read_uint32()
#             rle_count -= 1
#             if value == 2:  # repetition
#                 times = reader.read_uint32()
#                 color = reader.read_uint32()
#                 rle_count -= 2
#             else:
#                 times = 1
#                 color = value
#             if color & 0xFF000000 == 0:
#                 i += times
#                 continue
#             r = int(color & 0x0000FF)
#             g = int((color & 0x00FF00) >> 8)
#             b = int((color & 0xFF0000) >> 16)
#             for _ in range(times):
#                 x = i / (self.y_size * self.z_size)
#                 y = (i % (self.y_size * self.z_size)) / self.z_size
#                 z = i % self.z_size
#                 self.blocks[(int(x), int(y), int(z))] = (r, g, b)
#                 i += 1
# 
#     def write(self, writer):
#         writer.write_uint32(self.x_size)
#         writer.write_uint32(self.y_size)
#         writer.write_uint32(self.z_size)
#         write_string(writer, self.name)
#         writer.write_int32(self.x_offset)
#         writer.write_int32(self.y_offset)
#         writer.write_int32(self.z_offset)
#         writer.write_uint8(self.hidden)
#         writer.write_uint32(self.x_size * self.y_size * self.z_size)
#         for x in range(self.x_size):
#             for y in range(self.y_size):
#                 for z in range(self.z_size):
#                     c = self.blocks.get((x, y, z), None)
#                     if c is None:
#                         writer.write_uint32(0)
#                         continue
#                     r, g, b = c
#                     c2 = r | (g << 8) | (b << 16) | (0x7F << 24)
#                     writer.write_uint32(c2)
# 
# Path: cuwo/cub.py
# class CubModel:
#     def __init__(self, reader=None):
#         self.blocks = {}
#         if reader is None:
#             return
#         self.x_size = reader.read_uint32()
#         self.y_size = reader.read_uint32()
#         self.z_size = reader.read_uint32()
#         for z in range(self.z_size):
#             for y in range(self.y_size):
#                 for x in range(self.x_size):
#                     r = reader.read_uint8()
#                     g = reader.read_uint8()
#                     b = reader.read_uint8()
#                     if r == 0 and g == 0 and b == 0:
#                         continue
#                     self.blocks[(x, y, z)] = (r, g, b)
# 
#     def write(self, writer):
#         writer.write_uint32(self.x_size)
#         writer.write_uint32(self.y_size)
#         writer.write_uint32(self.z_size)
#         for z in range(self.z_size):
#             for y in range(self.y_size):
#                 for x in range(self.x_size):
#                     r, g, b = self.blocks.get((x, y, z), (0, 0, 0))
#                     writer.write_uint8(r)
#                     writer.write_uint8(g)
#                     writer.write_uint8(b)


# Path: tools/convertqmo.py
import os
import sys
import argparse
import os
from cuwo.bytes import ByteReader, ByteWriter
from cuwo.qmo import QubicleFile, QubicleModel
from cuwo.cub import CubModel




def switch_axes(x, y, z):
    return x, z, y


def to_qmo(in_file, out_file):
    cub = CubModel(ByteReader(open(in_file, 'rb').read()))
    qmo_file = QubicleFile()
    qmo_model = QubicleModel()
    x_size, y_size, z_size = switch_axes(cub.x_size, cub.y_size, cub.z_size)
    qmo_model.x_size = x_size
    qmo_model.y_size = y_size
    qmo_model.z_size = z_size
    qmo_model.x_offset = -x_size / 2
    qmo_model.y_offset = 0
    qmo_model.z_offset = -z_size / 2
    for k, v in cub.blocks.items():
        x, y, z = k
        x2, y2, z2 = switch_axes(x, y, z)
        qmo_model.blocks[x2, y2, z2] = v
    qmo_file.models.append(qmo_model)
    writer = ByteWriter()
    qmo_file.write(writer)
    with open(out_file, 'wb') as fp:
        fp.write(writer.get())


def to_cub(in_file, out_file):

    qmo_file = QubicleFile(ByteReader(open(in_file, 'rb').read()))