;
; Œ ªà®áë ¤«ï ¤¥ª®¤¨à®¢ ­¨ï ¨§®¡à ¦¥­¨© ç१ ä㭪樨 ¡¨¡«¨®â¥ª¨ libimg.
;
; (1) ¬ ªà®á load_image_file:
;
; à®¢¥àï¥â à §¬¥à ¯®«ã祭­®£® ä ©«  á ¨§®¡à ¦¥­¨¥¬.
; ‚뤥«ï¥â ¯®¤ ­¥£® ¯ ¬ïâì ¨ § £à㦠¥â â㤠 ä ©«.
; Ž¯à¥¤¥«ï¥â à §¬¥à ¯ ¬ï⨠­¥®¡å®¤¨¬®© ¤«ï à á¯ ª®¢ª¨ ¨§®¡à ¦¥­¨ï.
; ¥à¥¢ë¤¥«ï¥â ¯ ¬ïâì, ¨ à á¯ ª®¢ë¢ ¥â ¨§®¡à ¦¥­¨¥ ¨§ ä®à¬ â®¢ jpg, png, ...
;    ¢ ä®à¬ â rgb ª®â®àë© ¬®¦­® ¢ë¢®¤¨âì ­  íªà ­ ç¥à¥§ á¨áâ. äã­ªæ¨î.
; à¨ ­¥®¡å®¤¨¬®á⨠¬®¦­® â ª¦¥ ¯®«ãç¨âì à §¬¥àë ®âªà뢠¥¬®£® ¨§®¡à ¦¥­¨ï ¤«ï
;    ¨å ¤ «ì­¥©è¥£® ¨á¯®«ì§®¢ ­¨ï ¢ ¯à®£à ¬¬¥.
;
; ‚ ¨á¯®«ì§ãî饩 ¯à®£à ¬¬¥ ¤®«¦­  ¡ëâì ¯®¤ª«î祭  ¡¨¡«¨®â¥ª  libimg,
;    ®¡ê¥­  ¯¥à¥¬¥­­ ï file_name à §¬¥à®¬ ®ª®«® 4096 ¡ ©â,
;      â ª¦¥ áâàãªâãà  run_file_70 ⨯  FileInfoBlock.
;
; (2) ¬ ªà®á include_image_file:
;
; ‚­¥¤àï¥â ¨§®¡à ¦¥­¨¥ ¢ ä ©« ¯à®£à ¬¬ë.
; Ž¯à¥¤¥«ï¥â à §¬¥à ¯ ¬ï⨠­¥®¡å®¤¨¬®© ¤«ï à á¯ ª®¢ª¨ ¨§®¡à ¦¥­¨ï.
; ‚뤥«ï¥â ¯ ¬ïâì, ¨ à á¯ ª®¢ë¢ ¥â ¨§®¡à ¦¥­¨¥ ¨§ ä®à¬ â®¢ jpg, png, ...
;    ¢ ä®à¬ â rgb ª®â®àë© ¬®¦­® ¢ë¢®¤¨âì ­  íªà ­ ç¥à¥§ á¨áâ. äã­ªæ¨î.
; à¨ ­¥®¡å®¤¨¬®á⨠¬®¦­® â ª¦¥ ¯®«ãç¨âì à §¬¥àë ¢­¥¤à¥­­®£® ¨§®¡à ¦¥­¨ï ¤«ï
;    ¨å ¤ «ì­¥©è¥£® ¨á¯®«ì§®¢ ­¨ï ¢ ¯à®£à ¬¬¥.

; ¯®¤ª«î祭¨¥ ­¥ª®â®àëå ­¥®¡å®¤¨¬ëå ä ©«®¢:
include 'dll.inc'
include 'develop/libraries/box_lib/load_lib.mac'
include 'develop/libraries/libs-dev/libio/libio.inc'

align 4
open_b rb 560

txt_err_img_file:
if lang eq ru
	db 'Žè¨¡ª  N'
.n: db '* ¯à¨ ®âªàë⨨ ä ©«  '
else
	db 'Error N'
.n: db '*. Can',39,'t open file '
end if
.f: dd 0,0

; path - ¬®¦¥â ¡ëâì ¯¥à¥¬¥­­®© ¨«¨ áâப®¢ë¬ ¯ à ¬¥â஬
; buf - ¯¥à¥¬¥­­ ï ªã¤  ¡ã¤¥â § ¯¨á ­ 㪠§ â¥«ì ­  ¨§®¡à ¦¥­¨¥ ¢ ä®à¬ â¥ rgb,
;    ¢ á«ãç ¥ ¥á«¨ ä ©« ­¥ ®âªà®¥âáï â® ¡ã¤¥â § ¯¨á ­ 0
; img_w, img_h - ¯¥à¥¬¥­­ë¥ ªã¤  ¡ã¤ãâ § ¯¨á ­ë à §¬¥àë ®âªà뢠¥¬®£®
;    ¨§®¡à ¦¥­¨ï, ­¥ ®¡ï§ â¥«ì­ë¥ ¯ à ¬¥âàë
; bytes_p_p - ᪮«ìª® ¡ ©â ¯ ¬ï⨠¡à âì ­  ¯¨ªá¥«ì, ­¥ ®¡ï§ â¥«ì­ë© ¯ à ¬¥âà
;    ¥á«¨ ­¥ 㪠§ ­ ¡¥à¥âáï 3. Œ¨­¨¬ «ì­®¥ §­ ç¥­¨¥ ¤®«¦­® ¡ëâì ­¥ ¬¥­ìè¥ 3.
macro load_image_file path, buf, img_w, img_h, bytes_p_p
{
local .err_open
local .end_open

if path eqtype '' ;¯à®¢¥à塞 § ¤ ­ «¨ áâப®© ¯ à ¬¥âà path
	local .path_str
	jmp @f
		.path_str db path ;ä®à¬¨à㥬 «®ª «ì­ãî ¯¥à¥¬¥­­ãî
		db 0
	@@:
	;32 - áâ ­¤ àâ­ë©  ¤à¥á ¯® ª®â®à®¬ã ¤®«¦¥­ ¡ëâì ¡ãä¥à á á¨á⥬­ë¬ ¯ã⥬
	copy_path .path_str,[32],file_name,0
else
	copy_path path,[32],file_name,0 ;ä®à¬¨à㥬 ¯®«­ë© ¯ãâì ª ä ©«ã ¨§®¡à ¦¥­¨ï, ¯®¤à §ã¬¥¢ ¥¬ çâ® ®­ ¢ ®¤­®© ¯ ¯ª¥ á ¯à®£à ¬¬®©
end if
	mov dword[buf],0

	mov [run_file_70.Function], SSF_GET_INFO
	mov [run_file_70.Position], 0
	mov [run_file_70.Flags], 0
	mov dword[run_file_70.Count], 0
	mov dword[run_file_70.Buffer], open_b
	mov byte[run_file_70+20], 0
	mov dword[run_file_70.FileName], file_name
	mcall SF_FILE,run_file_70
	or eax,eax
	jnz .err_open

		mov ecx,dword[open_b+32] ;+32 qword: à §¬¥à ä ©«  ¢ ¡ ©â å
		stdcall mem.Alloc,ecx ;¢ë¤¥«ï¥¬ ¯ ¬ïâì ¤«ï ¨§®¡à ¦¥­¨ï
		mov [buf],eax
		mov [run_file_70.Function], SSF_READ_FILE
		mov [run_file_70.Position], 0
		mov [run_file_70.Flags], 0
		mov [run_file_70.Count], ecx
		mov [run_file_70.Buffer], eax
		mov byte[run_file_70+20], 0
		mov [run_file_70.FileName], file_name
		mcall SF_FILE,run_file_70 ;§ £à㦠¥¬ ä ©« ¨§®¡à ¦¥­¨ï
		test eax,eax
		jnz .err_open
		cmp ebx,0xffffffff
		je .end_open
			;®¯à¥¤¥«ï¥¬ ¢¨¤ ¨§®¡à ¦¥­¨ï ¨ ¯¨è¥¬ ¥£® ¯ à ¬¥âàë
			stdcall [img_decode], [buf],ebx,0
			mov ebx,eax
			;®¯à¥¤¥«ï¥¬ à §¬¥à ¤¥ª®¤¨à®¢ ­­®£® ¨§®¡à ¦¥­¨ï
			mov ecx,[eax+4] ;+4 = image width
if img_w eq
else
			mov dword[img_w],ecx
end if
if img_h eq
			imul ecx,[eax+8] ;+8 = image height
else
			mov eax,[eax+8] ;+8 = image height
			mov dword[img_h],eax
			imul ecx,eax
end if
if bytes_p_p eq
			imul ecx,3 ;need for r,g,b
else
			imul ecx,bytes_p_p
end if
			stdcall mem.ReAlloc,[buf],ecx ;¨§¬¥­ï¥¬ à §¬¥à ¤«ï ¡ãä¥à 
			mov [buf],eax
			stdcall [img_to_rgb2], ebx,[buf] ;¯à¥®¡à §ã¥¬ ¨§®¡à ¦¥­¨¥ ª ä®à¬ âã rgb
			stdcall [img_destroy], ebx ;㤠«ï¥¬ ¢à¥¬¥­­ë© ¡ãä¥à á ¯ à ¬¥âà ¬¨ ¨§®¡à ¦¥­¨ï
			jmp .end_open
	.err_open:
		add al,'0'
		mov byte[txt_err_img_file.n],al
if path eqtype ''
		mov eax,dword[.path_str]
		mov dword[txt_err_img_file.f],eax
		mov eax,dword[.path_str+4]
else
		mov eax,dword[path]
		mov dword[txt_err_img_file.f],eax
		mov eax,dword[path+4]
end if
		mov dword[txt_err_img_file.f+4],eax
		mov byte[txt_err_img_file.f+7],0
		notify_window_run txt_err_img_file
	.end_open:
}



; path - ¨¬ï ¢ª«îç ¥¬®£® ä ©« 
; buf - ¯¥à¥¬¥­­ ï ªã¤  ¡ã¤¥â § ¯¨á ­ 㪠§ â¥«ì ­  ¨§®¡à ¦¥­¨¥ ¢ ä®à¬ â¥ rgb
; img_w, img_h - ¯¥à¥¬¥­­ë¥ ªã¤  ¡ã¤ãâ § ¯¨á ­ë à §¬¥àë ¨§®¡à ¦¥­¨ï,
;    ­¥ ®¡ï§ â¥«ì­ë¥ ¯ à ¬¥âàë
; bytes_p_p - ᪮«ìª® ¡ ©â ¯ ¬ï⨠¡à âì ­  ¯¨ªá¥«ì, ­¥ ®¡ï§ â¥«ì­ë© ¯ à ¬¥âà
;    ¥á«¨ ­¥ 㪠§ ­ ¡¥à¥âáï 3. Œ¨­¨¬ «ì­®¥ §­ ç¥­¨¥ ¤®«¦­® ¡ëâì ­¥ ¬¥­ìè¥ 3.
macro include_image_file path, buf, img_w, img_h, bytes_p_p
{
local .beg_file
local .end_file

jmp .end_file
align 4
.beg_file:
	file path
.end_file:

	;®¯à¥¤¥«ï¥¬ ¢¨¤ ¨§®¡à ¦¥­¨ï ¨ ¯¨è¥¬ ¥£® ¯ à ¬¥âàë
	stdcall [img_decode], .beg_file,.end_file-.beg_file,0
	mov ebx,eax
	;®¯à¥¤¥«ï¥¬ à §¬¥à ¤¥ª®¤¨à®¢ ­­®£® ¨§®¡à ¦¥­¨ï
	mov ecx,[eax+4] ;+4 = image width
if img_w eq
else
	mov dword[img_w],ecx
end if
if img_h eq
	imul ecx,[eax+8] ;+8 = image height
else
	mov eax,[eax+8] ;+8 = image height
	mov dword[img_h],eax
	imul ecx,eax
end if
if bytes_p_p eq
	imul ecx,3 ;need for r,g,b
else
	imul ecx,bytes_p_p
end if
	stdcall mem.Alloc,ecx ;¨§¬¥­ï¥¬ à §¬¥à ¤«ï ¡ãä¥à 
	mov [buf],eax
	stdcall [img_to_rgb2], ebx,[buf] ;¯à¥®¡à §ã¥¬ ¨§®¡à ¦¥­¨¥ ª ä®à¬ âã rgb
	stdcall [img_destroy], ebx ;㤠«ï¥¬ ¢à¥¬¥­­ë© ¡ãä¥à á ¯ à ¬¥âà ¬¨ ¨§®¡à ¦¥­¨ï
}