// File: olegk0[P]readAVI/player.cpp
// Function: UnpackPNG(unsigned char*, unsigned int, unsigned int, unsigned char*)
int UnpackPNG(unsigned char *src_buf, unsigned src_length, unsigned bpp, unsigned char *dst_buf)
{
    int ret = 0;
    png_infop info_ptr = NULL, end_info = NULL;
    png_structp png_ptr = NULL;
    png_byte header[8];
    png_byte *image_data = NULL;
    png_bytep *row_pointers = NULL;

    // read the header
    memcpy(header, src_buf, 8);

    if (png_sig_cmp(header, 0, 8)) {
	fprintf(stderr, "error: is not a PNG.\n");
	return -1;
    }

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr) {
	fprintf(stderr, "error: png_create_read_struct returned 0.\n");
	return -1;
    }

    try {
	// create png info struct
	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
	    fprintf(stderr, "error: png_create_info_struct returned 0.\n");
	    throw 1;
	}

	// create png info struct
	end_info = png_create_info_struct(png_ptr);
	if (!end_info) {
	    fprintf(stderr, "error: png_create_info_struct returned 0.\n");
	    throw 1;
	}

	// the code in this if statement gets called if libpng encounters an error
	if (setjmp(png_jmpbuf(png_ptr))) {
	    fprintf(stderr, "error from libpng\n");
	    throw 1;
	}

	// init png reading
	//png_init_io(png_ptr, fp);
	read_buf_str_t read_buf_str;
	read_buf_str.buf = src_buf;
	read_buf_str.len = src_length;
	read_buf_str.pos = 8;

	png_set_read_fn(png_ptr, &read_buf_str, ReadBuf);

	// let libpng know you already read the first 8 bytes
	png_set_sig_bytes(png_ptr, 8);

	// read all the info up to the image data
	png_read_info(png_ptr, info_ptr);

	// variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 temp_width, temp_height;

	// get info about png
	png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, NULL, NULL, NULL);

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// glTexImage2d requires rows to be 4-byte aligned
	rowbytes += 3 - ((rowbytes - 1) % 4);

	// Allocate the image_data as a big block, to be given to opengl
	image_data = (png_byte *) malloc(rowbytes * temp_height * sizeof(png_byte) + 15);
	if (image_data == NULL) {
	    fprintf(stderr, "error: could not allocate memory for PNG image data\n");
	    throw 1;
	}

	// row_pointers is for pointing to image_data for reading the png with libpng
	row_pointers = (png_bytep *) malloc(temp_height * sizeof(png_bytep));
	if (row_pointers == NULL) {
	    fprintf(stderr, "error: could not allocate memory for PNG row pointers\n");
	    throw 1;
	}

	// set the individual row_pointers to point at the correct offsets of image_data
	unsigned i;
	for (i = 0; i < temp_height; i++) {
//	    row_pointers[temp_height - 1 - i] = image_data + i * rowbytes;
	    row_pointers[i] = image_data + i * rowbytes;
	}

	// read the png into image_data through row_pointers
	png_read_image(png_ptr, row_pointers);

	memcpy(dst_buf, image_data, temp_width * temp_height * bpp / 8);

	// clean up
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	free(image_data);
	free(row_pointers);

    } catch (...) {
	if (png_ptr)
	    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	if (image_data)
	    free(image_data);
	if (row_pointers)
	    free(row_pointers);
	ret = -1;
    }

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
callq 0x2190
cmpl $0x0, %eax
je 0x264e
movq 0x699f(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x39c9(%rip), %rsi      # 0x6004
movb $0x0, %al
callq 0x2360
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2acf
leaq 0x39c5(%rip), %rdi      # 0x601a
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x2080
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2693
movq 0x695a(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x39a1(%rip), %rsi      # 0x6021
movb $0x0, %al
callq 0x2360
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2acf
movq -0x40(%rbp), %rdi
callq 0x2400
movq %rax, -0x98(%rbp)
jmp 0x26a5
movq -0x98(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x273d
movq 0x690e(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x3980(%rip), %rsi      # 0x604c
xorl %eax, %eax
movl %eax, -0x9c(%rbp)
callq 0x2360
movl $0x4, %edi
callq 0x2120
movq %rax, %rdi
movl -0x9c(%rbp), %eax
movl $0x1, (%rdi)
movq 0x68af(%rip), %rsi      # 0x8fa8
movl %eax, %edx
callq 0x23e0
jmp 0x2aec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rdi
callq 0x2110
cmpq $0x0, -0x40(%rbp)
je 0x2a99
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x2160
jmp 0x2738
jmp 0x2a99
movq -0x40(%rbp), %rdi
callq 0x2400
movq %rax, -0xa8(%rbp)
jmp 0x274f
movq -0xa8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x27ab
movq 0x6868(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x38da(%rip), %rsi      # 0x604c
xorl %eax, %eax
movl %eax, -0xac(%rbp)
callq 0x2360
movl $0x4, %edi
callq 0x2120
movq %rax, %rdi
movl -0xac(%rbp), %eax
movl $0x1, (%rdi)
movq 0x6809(%rip), %rsi      # 0x8fa8
movl %eax, %edx
callq 0x23e0
jmp 0x2aec
movq -0x40(%rbp), %rdi
movq 0x6802(%rip), %rsi      # 0x8fb8
movl $0xc8, %edx
callq 0x2250
movq %rax, -0xb8(%rbp)
jmp 0x27c9
movq -0xb8(%rbp), %rdi
callq 0x2380
cmpl $0x0, %eax
je 0x2824
movq 0x67ef(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x388c(%rip), %rsi      # 0x6077
xorl %eax, %eax
movl %eax, -0xbc(%rbp)
callq 0x2360
movl $0x4, %edi
callq 0x2120
movq %rax, %rdi
movl -0xbc(%rbp), %eax
movl $0x1, (%rdi)
movq 0x6790(%rip), %rsi      # 0x8fa8
movl %eax, %edx
callq 0x23e0
jmp 0x2aec
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x70(%rbp)
movl $0x8, -0x6c(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x2e4(%rip), %rdx      # 0x2560
leaq -0x78(%rbp), %rsi
callq 0x2130
jmp 0x284f
movq -0x40(%rbp), %rdi
movl $0x8, %esi
callq 0x20b0
jmp 0x285f
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x21b0
jmp 0x286e
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq %rsp, %rax
movq $0x0, 0x10(%rax)
movq $0x0, 0x8(%rax)
movq $0x0, (%rax)
leaq -0x84(%rbp), %rdx
leaq -0x88(%rbp), %rcx
leaq -0x7c(%rbp), %r8
leaq -0x80(%rbp), %r9
callq 0x2370
jmp 0x28ad
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2050
jmp 0x28bc
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2090
movq %rax, -0xc8(%rbp)
jmp 0x28d2
movq -0xc8(%rbp), %rax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
subl $0x1, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl $0x3, %eax
subl %edx, %eax
addl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
imull -0x88(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x0, %rdi
addq $0xf, %rdi
callq 0x22f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x2976
movq 0x669d(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x374d(%rip), %rsi      # 0x608a
xorl %eax, %eax
movl %eax, -0xcc(%rbp)
callq 0x2360
movl $0x4, %edi
callq 0x2120
movq %rax, %rdi
movl -0xcc(%rbp), %eax
movl $0x1, (%rdi)
movq 0x663e(%rip), %rsi      # 0x8fa8
movl %eax, %edx
callq 0x23e0
jmp 0x2aec
movl -0x88(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x22f0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x29dc
movq 0x6637(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x371c(%rip), %rsi      # 0x60bf
xorl %eax, %eax
movl %eax, -0xd0(%rbp)
callq 0x2360
movl $0x4, %edi
callq 0x2120
movq %rax, %rdi
movl -0xd0(%rbp), %eax
movl $0x1, (%rdi)
movq 0x65d8(%rip), %rsi      # 0x8fa8
movl %eax, %edx
callq 0x23e0
jmp 0x2aec
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x88(%rbp), %eax
jae 0x2a29
movq -0x50(%rbp), %rdx
movl -0x90(%rbp), %eax
imull -0x8c(%rbp), %eax
movl %eax, %eax
addq %rax, %rdx
movq -0x58(%rbp), %rax
movl -0x90(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x29e6
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x20f0
jmp 0x2a38
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x84(%rbp), %eax
movl -0x88(%rbp), %ecx
imull %ecx, %eax
movl -0x18(%rbp), %ecx
imull %ecx, %eax
shrl $0x3, %eax
movl %eax, %edx
callq 0x21d0
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x2160
jmp 0x2a72
movq -0x50(%rbp), %rdi
callq 0x22a0
movq -0x58(%rbp), %rdi
callq 0x22a0
jmp 0x2ac5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x23d0
jmp 0x2acd
cmpq $0x0, -0x50(%rbp)
je 0x2aa9
movq -0x50(%rbp), %rdi
callq 0x22a0
cmpq $0x0, -0x58(%rbp)
je 0x2ab9
movq -0x58(%rbp), %rdi
callq 0x22a0
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
callq 0x23d0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2acf
jmp 0x2adb
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2410
movq %rax, %rdi
callq 0x2e30
nopl (%rax)