// File: /workspace/github_c_makefile_star2_O2/0x3f00[P]PhotonCpp/PhotonCpp.cpp
// Function: main
int main(int argc, char * argv[])
{
	if (argc != 3)
	{
		std::cerr << "\nUsage: PngToPhoton <input-1440x2560.png> <output.photon>\n"
		"To read data from STDIN use '-' as filename.\n"
		"To write data to STDOUT use '-' as filename.\n\n"
		"White and transparent areas of the image are treated as voids.\n"
		"Check with Photon File Validator after conversion. Use output at your own risk.\n";
		return -1;
	}

#ifdef WIN32
	_setmode(_fileno(stdout), _O_BINARY);
	_setmode(_fileno(stdin), _O_BINARY);
#endif // WIN32

	std::string bufferModel = getModel();

	PhotonFileHeader photonFileHeader(bufferModel);
	PhotonFilePreview previewOne(photonFileHeader.getPreviewOneOffsetAddress(), bufferModel);
	PhotonFilePreview previewTwo(photonFileHeader.getPreviewTwoOffsetAddress(), bufferModel);
	
	PhotonFilePrintParameters photonFilePrintParameters;
	if (photonFileHeader.getVersion() > 1) 
	{
		PhotonFilePrintParameters dummy(photonFileHeader.getPrintParametersOffsetAddress(), bufferModel);
		photonFilePrintParameters = std::move(dummy);
	}

	int margin = 0;
	
	std::vector<PhotonFileLayer> layers = PhotonFileLayer::readLayers(photonFileHeader, bufferModel, margin);

//	PhotonFileLayer::calculateLayers(photonFileHeader,layers, 0);

	// load png
	std::string bufferPng;
	
	if (std::string("-") == argv[1])
	{
		std::string tmp(std::istreambuf_iterator<char>(std::cin), {});
		bufferPng = std::move(tmp);
	}
	else
	{
		std::ifstream ifPng(argv[1], std::ios_base::binary);

		if (ifPng.fail())
		{
			std::cerr << "Error opening input file.\n";
			return -1;
		}

		ifPng.seekg(0, std::ios::end);
		std::streamoff size = 0;
		size = ifPng.tellg();
		bufferPng.resize((unsigned int)size);
		ifPng.seekg(0);
		ifPng.read(&bufferPng[0], size);
		ifPng.close();
	}

	std::pair<std::string, PhotonLayer> rle = png2layer(bufferPng);
	if (!rle.first.empty())
	{
		std::cerr << rle.first << "\n";
		return -1;
	}

	PhotonLayer & layerPng = rle.second;
	int antiAliasLevel = 1;
	if (photonFileHeader.getVersion() > 1) {
		antiAliasLevel = photonFileHeader.getAntiAliasingLevel();
	}

	for (int i = 0; i < photonFileHeader.getNumberOfLayers(); i++) {
		PhotonFileLayer & layer = layers.at(i);
		layer.saveLayer(layerPng);
		if (antiAliasLevel > 1) {
			for (int a = 0; a < (antiAliasLevel - 1); a++) {
				layer.getAntiAlias(a).saveLayer(layerPng);
			}
		}
	}




	// writing is here:
	int headerPos = 0;
	int previewOnePos = headerPos + photonFileHeader.getByteSize();
	int previewTwoPos = previewOnePos + previewOne.getByteSize();
	int layerDefinitionPos = previewTwoPos + previewTwo.getByteSize();

	int parametersPos = 0;
	if (photonFileHeader.getVersion() > 1) {
		parametersPos = layerDefinitionPos;
		layerDefinitionPos = parametersPos + photonFilePrintParameters.getByteSize();
	}

	int dataPosition = layerDefinitionPos + (PhotonFileLayer::getByteSize() * photonFileHeader.getNumberOfLayers() * antiAliasLevel);


	std::ofstream ofs;
	if (std::string("-") != argv[2])
	{
		ofs.open(argv[2], std::ios_base::binary);
		if (ofs.fail())
		{
			std::cerr << "Error opening output file.\n";
			return -1;
		}
	}

	PhotonOutputStream os( std::string("-") != argv[2] ? ofs : std::cout);
	photonFileHeader.save(os, previewOnePos, previewTwoPos, layerDefinitionPos, parametersPos);
	previewOne.save(os, previewOnePos);
	previewTwo.save(os, previewTwoPos);

	if (photonFileHeader.getVersion() > 1) {
		photonFilePrintParameters.save(os);
	}

	// Optimize order for speed read on photon
	for (int i = 0; i < photonFileHeader.getNumberOfLayers(); i++) {
		PhotonFileLayer & layer = layers.at(i);
		dataPosition = layer.savePos(dataPosition);
		if (antiAliasLevel > 1) {
			for (int a = 0; a < (antiAliasLevel - 1); a++) {
				dataPosition = layer.getAntiAlias(a).savePos(dataPosition);
			}
		}
	}

	// Order for backward compatibility with photon/cbddlp version 1
	for (int i = 0; i < photonFileHeader.getNumberOfLayers(); i++) {
		layers.at(i).save(os);
	}

	if (antiAliasLevel > 1) {
		for (int a = 0; a < (antiAliasLevel - 1); a++) {
			for (int i = 0; i < photonFileHeader.getNumberOfLayers(); i++) {
				layers.at(i).getAntiAlias(a).save(os);
			}
		}
	}

	// Optimize order for speed read on photon
	for (int i = 0; i < photonFileHeader.getNumberOfLayers(); i++) {
		PhotonFileLayer & layer = layers.at(i);
		layer.saveData(os);
		if (antiAliasLevel > 1) {
			for (int a = 0; a < (antiAliasLevel - 1); a++) {
				layer.getAntiAlias(a).saveData(os);
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
cmpl $0x3, %edi
jne 0x4f11
movq %rsi, %r15
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
callq 0x4cd8
leaq -0x190(%rbp), %rdi
movq %rbx, %rsi
callq 0x8194
movl -0x154(%rbp), %esi
leaq -0x230(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x8354
movl -0x148(%rbp), %esi
leaq -0x1e0(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x8354
movaps 0x7245(%rip), %xmm0     # 0xc020
movups %xmm0, -0x120(%rbp)
movl $0x43960000, -0x110(%rbp) # imm = 0x43960000
andl $0x0, -0xfc(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x10c(%rbp)
cmpl $0x2, -0x18c(%rbp)
jl 0x4e57
movl -0x13c(%rbp), %esi
leaq -0x438(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x84a2
movups -0x438(%rbp), %xmm0
movups -0x428(%rbp), %xmm1
movups -0x418(%rbp), %xmm2
movups -0x40c(%rbp), %xmm3
movups %xmm3, -0xf4(%rbp)
movaps %xmm2, -0x100(%rbp)
movaps %xmm1, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x190(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
xorl %ecx, %ecx
callq 0x860a
leaq -0x88(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x7d54(%rip), %rsi      # 0xcbde
leaq -0x438(%rbp), %rdi
leaq -0x4e0(%rbp), %rdx
callq 0x30d0
movq 0x8(%r15), %rsi
leaq -0x438(%rbp), %rbx
movq %rbx, %rdi
callq 0x889a
movl %eax, %r14d
movq %rbx, %rdi
callq 0x30f0
testb %r14b, %r14b
je 0x4f2d
movq 0xd111(%rip), %rax      # 0x11fd8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq 0xe8(%rax,%rcx), %rsi
leaq -0x428(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x438(%rbp), %rdi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
movl %edx, %r8d
callq 0xb948
leaq -0x98(%rbp), %rdi
movq %rbx, %rsi
callq 0x3250
movq %rbx, %rdi
callq 0x30f0
jmp 0x4fe9
pushq $-0x1
popq %r14
movq 0xd0d4(%rip), %rdi      # 0x11ff0
leaq 0x7b96(%rip), %rsi      # 0xcab9
callq 0x31d0
jmp 0x53f0
movq 0x8(%r15), %rsi
leaq -0x438(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x3410
movq -0x438(%rbp), %rax
movq -0x18(%rax), %rax
movl -0x418(%rbp,%rax), %ebx
testb $0x5, %bl
je 0x4f70
movq 0xd092(%rip), %rdi      # 0x11ff0
leaq 0x7c7b(%rip), %rsi      # 0xcbe0
callq 0x31d0
pushq $-0x1
popq %r14
jmp 0x4fd4
leaq -0x438(%rbp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x32c0
leaq -0x438(%rbp), %rdi
callq 0x33e0
movq %rax, %r14
movl %r14d, %esi
leaq -0x98(%rbp), %rdi
callq 0x32e0
leaq -0x438(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3280
movq -0x98(%rbp), %rsi
leaq -0x438(%rbp), %rdi
movq %r14, %rdx
callq 0x3270
xorl %r14d, %r14d
leaq -0x438(%rbp), %rdi
callq 0x3070
leaq -0x438(%rbp), %rdi
callq 0x3090
testb $0x5, %bl
jne 0x53b7
leaq -0x4e0(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x4a52
cmpq $0x0, -0x4d8(%rbp)
je 0x5031
movq 0xcfe3(%rip), %rdi      # 0x11ff0
leaq -0x4e0(%rbp), %rsi
callq 0x31b0
leaq 0x7bd9(%rip), %rsi      # 0xcbf9
movq %rax, %rdi
callq 0x31d0
pushq $-0x1
popq %r14
jmp 0x53ab
cmpl $0x2, -0x18c(%rbp)
movq %r15, -0x48(%rbp)
jl 0x5046
movl -0x134(%rbp), %eax
jmp 0x5049
pushq $0x1
popq %rax
leaq -0x4c0(%rbp), %r14
movq %rax, -0x40(%rbp)
leal -0x1(%rax), %r15d
xorl %r12d, %r12d
movslq -0x14c(%rbp), %rbx
cmpq %rbx, %r12
jge 0x50b5
leaq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x88aa
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
callq 0x88cc
cmpl $0x2, -0x40(%rbp)
jl 0x50b0
addq $0x90, %r13
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x50b0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x88aa
movq %rax, %rdi
movq %r14, %rsi
callq 0x88cc
incq %rbx
jmp 0x5090
incq %r12
jmp 0x505b
xorl %r12d, %r12d
xorl %eax, %eax
cmpl $0x2, -0x18c(%rbp)
setge %al
movl -0x224(%rbp), %r13d
movq %rax, -0x78(%rbp)
leal 0x6c(%r13,%rax,4), %eax
movl -0x1d4(%rbp), %ecx
leal 0x40(%rcx,%rax), %edx
leal 0x7c(%rcx,%rax), %r14d
cmovll %edx, %r14d
cmovgel %edx, %r12d
leaq -0x438(%rbp), %rdi
callq 0x33d0
movq %r13, -0x70(%rbp)
movl %r12d, -0x4c(%rbp)
leaq 0x7ad8(%rip), %rsi      # 0xcbde
leaq -0xd8(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x30d0
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0xd8(%rbp), %r12
movq %r12, %rdi
callq 0x894c
movl %eax, %r13d
movq %r12, %rdi
callq 0x30f0
testb %r13b, %r13b
je 0x5185
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0x438(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x3050
movq -0x438(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x418(%rbp,%rax)
je 0x5185
movq 0xce80(%rip), %rdi      # 0x11ff0
leaq 0x7a84(%rip), %rsi      # 0xcbfb
callq 0x31d0
pushq $-0x1
popq %r14
jmp 0x539f
leaq 0x7a52(%rip), %rsi      # 0xcbde
leaq -0xd8(%rbp), %rdi
leaq -0x31(%rbp), %rdx
callq 0x30d0
movq -0x78(%rbp), %rax
leal 0x6c(,%rax,4), %r13d
movq -0x70(%rbp), %rcx
leal (%rcx,%rax,4), %r12d
addl $0x8c, %r12d
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x894c
testb %al, %al
leaq -0x438(%rbp), %rax
jne 0x51dd
movq 0xcdeb(%rip), %rax      # 0x11fc8
movq %rax, -0x30(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x30f0
movq -0x30(%rbp), %rsi
leaq -0x190(%rbp), %rdi
movl %r13d, %edx
movl %r12d, %ecx
movl %r14d, %r8d
movl -0x4c(%rbp), %r9d
callq 0x895a
movq -0x30(%rbp), %rsi
leaq -0x230(%rbp), %rdi
movl %r13d, %edx
callq 0x8ace
movq -0x30(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
movl %r12d, %edx
callq 0x8ace
cmpl $0x2, -0x18c(%rbp)
jl 0x5249
leaq -0x120(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8b92
imull -0x40(%rbp), %ebx
imull $0x24, %ebx, %eax
addl %eax, %r14d
xorl %ebx, %ebx
movslq -0x14c(%rbp), %rax
cmpq %rax, %rbx
jge 0x52b0
leaq -0x68(%rbp), %rdi
movq %rbx, %rsi
callq 0x88aa
movq %rax, %r12
movl 0x10(%rax), %r13d
addl %r14d, %r13d
movl %r14d, 0xc(%rax)
cmpl $0x2, -0x40(%rbp)
jl 0x52a8
addq $0x90, %r12
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x52a8
movq %r12, %rdi
movq %r14, %rsi
callq 0x88aa
movl %r13d, 0xc(%rax)
addl 0x10(%rax), %r13d
incq %r14
jmp 0x528b
incq %rbx
movl %r13d, %r14d
jmp 0x5255
xorl %ebx, %ebx
leaq -0x68(%rbp), %r14
leaq -0x30(%rbp), %r12
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x52e3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x88aa
movq %rax, %rdi
movq %r12, %rsi
callq 0x8c56
incq %rbx
movl -0x14c(%rbp), %eax
jmp 0x52ba
cmpl $0x2, -0x40(%rbp)
jl 0x533a
xorl %ebx, %ebx
leaq -0x68(%rbp), %r14
leaq -0x30(%rbp), %r13
cmpq %r15, %rbx
je 0x533a
xorl %r12d, %r12d
movslq %eax, %rcx
cmpq %rcx, %r12
jge 0x5335
movq %r14, %rdi
movq %r12, %rsi
callq 0x88aa
addq $0x90, %rax
movq %rax, %rdi
movq %rbx, %rsi
callq 0x88aa
movq %rax, %rdi
movq %r13, %rsi
callq 0x8c56
incq %r12
movl -0x14c(%rbp), %eax
jmp 0x52fb
incq %rbx
jmp 0x52f3
xorl %ebx, %ebx
leaq -0x30(%rbp), %r14
cltq
cmpq %rax, %rbx
jge 0x539c
leaq -0x68(%rbp), %rdi
movq %rbx, %rsi
callq 0x88aa
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x8cd2
cmpl $0x2, -0x40(%rbp)
jl 0x5391
addq $0x90, %r12
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x5391
movq %r12, %rdi
movq %r13, %rsi
callq 0x88aa
movq %rax, %rdi
movq %r14, %rsi
callq 0x8cd2
incq %r13
jmp 0x5371
incq %rbx
movl -0x14c(%rbp), %eax
jmp 0x5340
xorl %r14d, %r14d
leaq -0x438(%rbp), %rdi
callq 0x3300
leaq -0x4e0(%rbp), %rdi
callq 0x8d2e
leaq -0x98(%rbp), %rdi
callq 0x30f0
leaq -0x68(%rbp), %rdi
callq 0x8d4e
leaq -0x1e0(%rbp), %rdi
callq 0x8d7a
leaq -0x230(%rbp), %rdi
callq 0x8d7a
leaq -0xb8(%rbp), %rdi
callq 0x30f0
movl %r14d, %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5455
jmp 0x541b
jmp 0x5455
jmp 0x5455
jmp 0x5450
jmp 0x541b
jmp 0x542e
jmp 0x542e
jmp 0x5433
jmp 0x5455
jmp 0x542e
movq %rax, %rbx
leaq -0x438(%rbp), %rdi
callq 0x3090
jmp 0x5470
jmp 0x5450
movq %rax, %rbx
jmp 0x5470
movq %rax, %rbx
jmp 0x5485
movq %rax, %rbx
jmp 0x5491
jmp 0x543f
movq %rax, %rbx
jmp 0x549d
jmp 0x5455
jmp 0x5455
jmp 0x5455
jmp 0x5450
jmp 0x5455
jmp 0x5455
movq %rax, %rbx
jmp 0x5464
movq %rax, %rbx
leaq -0x438(%rbp), %rdi
callq 0x3300
leaq -0x4e0(%rbp), %rdi
callq 0x8d2e
leaq -0x98(%rbp), %rdi
callq 0x30f0
leaq -0x68(%rbp), %rdi
callq 0x8d4e
leaq -0x1e0(%rbp), %rdi
callq 0x8d7a
leaq -0x230(%rbp), %rdi
callq 0x8d7a
leaq -0xb8(%rbp), %rdi
callq 0x30f0
movq %rbx, %rdi
callq 0x33f0