//
// Created by igor on 03.06.2021.
//

#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include "Texture.cuh"

Texture::Texture(const char *path) {
    std::ifstream inp(path, std::ios::in | std::ios::binary);
    if (inp.is_open()) {
        std::string line;
        std::getline(inp, line);
        if (line != "P6") {
            std::cout << "Error. Unrecognized file format." << std::endl;
            return;
        }
        std::getline(inp, line);
        while (line[0] == '#') {
            std::getline(inp, line);
        }
        std::stringstream dimensions(line);

        try {
            dimensions >> x;
            dimensions >> y;
        } catch (std::exception &e) {
            std::cout << "Header file format error. " << e.what() << std::endl;
            return;
        }

        std::getline(inp, line);
        std::stringstream max_val(line);
        try {
            max_val >> max;
        } catch (std::exception &e) {
            std::cout << "Header file format error. " << e.what() << std::endl;
            return;
        }

        uint size = x*y;

        img = (ColorF*)malloc(sizeof(ColorF) * size);

        char aux;
        for (unsigned int i = 0; i < size; ++i) {
            inp.read(&aux, 1);
            img[i].r = (float)(unsigned char) aux;
            inp.read(&aux, 1);
            img[i].g = (float)(unsigned char) aux;
            inp.read(&aux, 1);
            img[i].b = (float)(unsigned char) aux;
        }
    } else {
        std::cout << "Error. Unable to open " << path << std::endl;
    }
    inp.close();
}

Texture::~Texture() {
//    free(img);
}

	.file	"tmpxft_002abb86_00000000-6_Texture.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3832:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3832:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.type	_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0, @function
_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0:
.LFB4581:
	.cfi_startproc
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	(%rdi), %rax
	movq	-24(%rax), %rax
	movq	240(%rdi,%rax), %rbx
	testq	%rbx, %rbx
	jne	.L3
	call	_ZSt16__throw_bad_castv@PLT
.L3:
	cmpb	$0, 56(%rbx)
	movq	%rdi, %rbp
	movq	%rsi, %r12
	je	.L4
	movb	67(%rbx), %al
	jmp	.L5
.L4:
	movq	%rbx, %rdi
	call	_ZNKSt5ctypeIcE13_M_widen_initEv@PLT
	movq	(%rbx), %rax
	movl	$10, %esi
	movq	%rbx, %rdi
	call	*48(%rax)
.L5:
	popq	%rbx
	.cfi_def_cfa_offset 24
	movq	%r12, %rsi
	movq	%rbp, %rdi
	movsbl	%al, %edx
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EES4_@PLT
	.cfi_endproc
.LFE4581:
	.size	_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0, .-_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0
	.align 2
	.globl	_ZN7TextureD2Ev
	.type	_ZN7TextureD2Ev, @function
_ZN7TextureD2Ev:
.LFB3828:
	.cfi_startproc
	endbr64
	ret
	.cfi_endproc
.LFE3828:
	.size	_ZN7TextureD2Ev, .-_ZN7TextureD2Ev
	.globl	_ZN7TextureD1Ev
	.set	_ZN7TextureD1Ev,_ZN7TextureD2Ev
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3855:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3855:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"P6"
.LC1:
	.string	"Error. Unrecognized file format."
.LC2:
	.string	"Error. Unable to open "
.LC3:
	.string	"Header file format error. "
	.text
	.align 2
	.globl	_ZN7TextureC2EPKc
	.type	_ZN7TextureC2EPKc, @function
_ZN7TextureC2EPKc:
.LFB3825:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3825
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$12, %edx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	subq	$1400, %rsp
	.cfi_def_cfa_offset 1456
	movq	%fs:40, %rax
	movq	%rax, 1384(%rsp)
	xorl	%eax, %eax
	leaq	864(%rsp), %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
.LEHB0:
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@PLT
.LEHE0:
	leaq	984(%rsp), %rdi
	call	_ZNKSt12__basic_fileIcE7is_openEv@PLT
	testb	%al, %al
	je	.L12
	leaq	48(%rsp), %rax
	movq	8(%rsp), %rdi
	movb	$0, 64(%rsp)
	movq	%rax, 16(%rsp)
	leaq	64(%rsp), %rax
	movq	16(%rsp), %rsi
	movq	%rax, 48(%rsp)
	xorl	%eax, %eax
	movq	%rax, 56(%rsp)
.LEHB1:
	call	_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0
	cmpq	$2, 56(%rsp)
	jne	.L13
	movq	48(%rsp), %rdi
	movl	$2, %edx
	leaq	.LC0(%rip), %rsi
	call	memcmp@PLT
	testl	%eax, %eax
	je	.L18
.L13:
	leaq	.LC1(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	jmp	.L17
.L49:
	movq	48(%rsp), %rax
	cmpb	$35, (%rax)
	jne	.L48
.L18:
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
	call	_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0
	jmp	.L49
.L48:
	leaq	80(%rsp), %rax
	movq	16(%rsp), %rsi
	movl	$24, %edx
	movq	%rax, %rdi
	movq	%rax, 24(%rsp)
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEC1ERKNS_12basic_stringIcS2_S3_EESt13_Ios_Openmode@PLT
.LEHE1:
	movq	24(%rsp), %rdi
	leaq	12(%rbx), %rsi
.LEHB2:
	call	_ZNSi10_M_extractIjEERSiRT_@PLT
	movq	24(%rsp), %rdi
	leaq	16(%rbx), %rsi
	call	_ZNSi10_M_extractIjEERSiRT_@PLT
.LEHE2:
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
.LEHB3:
	call	_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE.isra.0
	leaq	472(%rsp), %rbp
	movq	16(%rsp), %rsi
	movl	$24, %edx
	movq	%rbp, %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEC1ERKNS_12basic_stringIcS2_S3_EESt13_Ios_Openmode@PLT
.LEHE3:
	leaq	8(%rbx), %rsi
	movq	%rbp, %rdi
.LEHB4:
	call	_ZNSi10_M_extractIjEERSiRT_@PLT
.LEHE4:
	movl	12(%rbx), %r12d
	imull	16(%rbx), %r12d
	xorl	%r13d, %r13d
	leaq	47(%rsp), %r14
	movl	%r12d, %edi
	imulq	$12, %rdi, %rdi
	call	malloc@PLT
	movq	%rax, (%rbx)
.L19:
	cmpl	%r12d, %r13d
	jnb	.L50
	movq	8(%rsp), %rdi
	movl	$1, %edx
	movq	%r14, %rsi
.LEHB5:
	call	_ZNSi4readEPcl@PLT
	movzbl	47(%rsp), %edx
	imulq	$12, %r13, %r15
	movq	(%rbx), %rax
	movq	%r14, %rsi
	movq	8(%rsp), %rdi
	cvtsi2ssl	%edx, %xmm0
	movl	$1, %edx
	movss	%xmm0, (%rax,%r15)
	call	_ZNSi4readEPcl@PLT
	movzbl	47(%rsp), %edx
	movq	(%rbx), %rax
	movq	%r14, %rsi
	movq	8(%rsp), %rdi
	cvtsi2ssl	%edx, %xmm0
	movl	$1, %edx
	movss	%xmm0, 4(%rax,%r15)
	call	_ZNSi4readEPcl@PLT
.LEHE5:
	movzbl	47(%rsp), %edx
	movq	(%rbx), %rax
	incq	%r13
	cvtsi2ssl	%edx, %xmm0
	movss	%xmm0, 8(%rax,%r15)
	jmp	.L19
.L50:
	movq	%rbp, %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev@PLT
	movq	24(%rsp), %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev@PLT
	movq	16(%rsp), %rdi
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv@PLT
	jmp	.L21
.L23:
	decq	%rdx
	movq	%rax, %rbx
	jne	.L25
	call	__cxa_begin_catch@PLT
	leaq	.LC3(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	movq	%rax, %rbx
.LEHB6:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rbp
	movq	(%rbx), %rax
	movq	%rbx, %rdi
	call	*16(%rax)
	movq	%rbp, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.LEHE6:
	call	__cxa_end_catch@PLT
.L26:
	movq	24(%rsp), %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev@PLT
.L17:
	movq	16(%rsp), %rdi
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv@PLT
	jmp	.L46
.L12:
	leaq	.LC2(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
.LEHB7:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	movq	%rbp, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.L21:
	movq	8(%rsp), %rdi
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEE5closeEv@PLT
.LEHE7:
.L46:
	movq	8(%rsp), %rdi
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	1384(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	jmp	.L47
.L37:
	endbr64
	movq	%rax, %rdi
	jmp	.L23
.L39:
	endbr64
	movq	%rax, %rbx
.L27:
	call	__cxa_end_catch@PLT
	jmp	.L25
.L40:
	endbr64
	movq	%rax, %rbx
.L28:
	decq	%rdx
	jne	.L30
	movq	%rax, %rdi
	call	__cxa_begin_catch@PLT
	leaq	.LC3(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	movq	%rax, %rbx
.LEHB8:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %r12
	movq	(%rbx), %rax
	movq	%rbx, %rdi
	call	*16(%rax)
	movq	%r12, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.LEHE8:
	call	__cxa_end_catch@PLT
	movq	%rbp, %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev@PLT
	jmp	.L26
.L42:
	endbr64
	movq	%rax, %rbx
.L31:
	call	__cxa_end_catch@PLT
	jmp	.L30
.L41:
	endbr64
	movq	%rax, %rbx
.L30:
	movq	%rbp, %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev@PLT
	jmp	.L25
.L38:
	endbr64
	movq	%rax, %rbx
.L25:
	movq	24(%rsp), %rdi
	call	_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev@PLT
	jmp	.L32
.L36:
	endbr64
	movq	%rax, %rbx
.L32:
	movq	16(%rsp), %rdi
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv@PLT
	jmp	.L33
.L43:
	endbr64
	movq	%rax, %rbx
.L33:
	movq	8(%rsp), %rdi
	call	_ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	1384(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L47
	movq	%rbx, %rdi
.LEHB9:
	call	_Unwind_Resume@PLT
.LEHE9:
.L47:
	call	__stack_chk_fail@PLT
.L35:
	addq	$1400, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3825:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
	.align 4
.LLSDA3825:
	.byte	0xff
	.byte	0x9b
	.uleb128 .LLSDATT3825-.LLSDATTD3825
.LLSDATTD3825:
	.byte	0x1
	.uleb128 .LLSDACSE3825-.LLSDACSB3825
.LLSDACSB3825:
	.uleb128 .LEHB0-.LFB3825
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB3825
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L36-.LFB3825
	.uleb128 0
	.uleb128 .LEHB2-.LFB3825
	.uleb128 .LEHE2-.LEHB2
	.uleb128 .L37-.LFB3825
	.uleb128 0x3
	.uleb128 .LEHB3-.LFB3825
	.uleb128 .LEHE3-.LEHB3
	.uleb128 .L38-.LFB3825
	.uleb128 0
	.uleb128 .LEHB4-.LFB3825
	.uleb128 .LEHE4-.LEHB4
	.uleb128 .L40-.LFB3825
	.uleb128 0x3
	.uleb128 .LEHB5-.LFB3825
	.uleb128 .LEHE5-.LEHB5
	.uleb128 .L41-.LFB3825
	.uleb128 0
	.uleb128 .LEHB6-.LFB3825
	.uleb128 .LEHE6-.LEHB6
	.uleb128 .L39-.LFB3825
	.uleb128 0
	.uleb128 .LEHB7-.LFB3825
	.uleb128 .LEHE7-.LEHB7
	.uleb128 .L43-.LFB3825
	.uleb128 0
	.uleb128 .LEHB8-.LFB3825
	.uleb128 .LEHE8-.LEHB8
	.uleb128 .L42-.LFB3825
	.uleb128 0
	.uleb128 .LEHB9-.LFB3825
	.uleb128 .LEHE9-.LEHB9
	.uleb128 0
	.uleb128 0
.LLSDACSE3825:
	.byte	0
	.byte	0
	.byte	0x1
	.byte	0x7d
	.align 4
	.long	DW.ref._ZTISt9exception-.
.LLSDATT3825:
	.text
	.size	_ZN7TextureC2EPKc, .-_ZN7TextureC2EPKc
	.globl	_ZN7TextureC1EPKc
	.set	_ZN7TextureC1EPKc,_ZN7TextureC2EPKc
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.hidden	DW.ref._ZTISt9exception
	.weak	DW.ref._ZTISt9exception
	.section	.data.rel.local.DW.ref._ZTISt9exception,"awG",@progbits,DW.ref._ZTISt9exception,comdat
	.align 8
	.type	DW.ref._ZTISt9exception, @object
	.size	DW.ref._ZTISt9exception, 8
DW.ref._ZTISt9exception:
	.quad	_ZTISt9exception
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

//
// Created by igor on 03.06.2021.
//

#ifndef CUDATEST_TEXTURE_CUH
#define CUDATEST_TEXTURE_CUH


#include "ColorF.cuh"

class Texture {
public:
    ColorF* img;
    uint max;
    uint x, y;
    Texture(const char* path);
    ~Texture();
};


#endif //CUDATEST_TEXTURE_CUH

	.text
	.file	"Texture.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
