--Funciones auxiliares de PSP Explorer

--Función recursiva
function copylist(ruta)
local lista=files.list(ruta)
if lista!=nil then
	if #lista>0 then
		for i=1,#lista do
			if lista[i].directory then
				table.insert(folders,lista[i].name)
				copylist(lista[i].name)
			else
				table.insert(files2,lista[i].name)
			end
		end
	end
end
end

--Abrir archivo a copiar
function opencopy(file,dest,varios,index)


local a=io.open(file,"r")
local size=a:seek("end")

local b=io.open(dest,"w+")

local d=0

for i=1,size,1024 do
	a:seek("set",i)
	datos=a:read(1024)
	if datos==nil then break end
	b:write(datos)
	datos=nil
	d=d+1
	if d==5 then
		imgcall:blend(0,0,100)
		draw.fillrect(0,60,480,152,color.new(0,0,255,60))
		draw.rect(0,60,480,152,negro)
		screen.print(240,70,"Copiando",0.9,blanco,0,"center")

		draw.fillrect(160,100,160,30,blanco_d)
		draw.gradrect(160,100,160/size*i,30,verde,verde,verde,blanco_d)
		draw.rect(160,100,160,30,negro)

		if varios then
			screen.print(240,160,"Archivo "..index.. " de "..#files2,0.7,blanco,0,"center")
		else
			screen.print(240,160,"1 Archivo ("..math.floor(i*100/size).. "%)",0.7,blanco,0,"center")
		end
		screen.print(240,175,"Tamaño: "..math.floor(size/1048576).." MB",0.7,blanco,0,"center")
		screen.print(240,195,"Pulsa círculo para cancelar",0.7,blanco,0,"center")
		controls.read()
		if controls.circle() then
			a:close()
			b:close()
			return true
		end

		screen.flip()
		d=0
	end
end

a:close()
b:close()
end

--Copiando
function newcopy(file,dest)

imgcall=screen.toimage()

try=files.list(file)

if try!=nil then
	dest=files.nofile(dest)
	folders={}
	files2={}
	copylist(file)
	tam=#files.nofile(file)
	files.mkdir(dest.."/"..files.nopath(file))
	for i=1,#folders do
		files.mkdir(dest..folders[i]:sub(tam,#folders[i]))
	end
	for i=1,#files2 do
		if opencopy(files2[i],dest..files2[i]:sub(tam,#files2[i]),true,i) then
			os.message("Copiado cancelado")
			imgcall:free()
			imgcall=nil
			return
		end
	end
else
	opencopy(file,dest,false)
end

imgcall:free()
imgcall=nil

end


function ReadWORD(str, offset)
	local loByte = str:byte(offset);
	local hiByte = str:byte(offset+1);
	return hiByte*256 + loByte;
end

function ReadDWORD(str, offset)
	local loWord = ReadWORD(str, offset);
	local hiWord = ReadWORD(str, offset+2);
	return hiWord*65536 + loWord;
end


function imageload(ruta)

	local f = io.open(ruta, "rb");
	local bytecode = f:read("*a");
	f:close();
	
	a=io.open("a.txt","w+")
	a:write(bytecode)
	a:close()
	
	-------------------------
	-- Parse BITMAPFILEHEADER
	-------------------------
	local offset = 1;
	local bfType = ReadWORD(bytecode, offset);
	if(bfType ~= 0x4D42) then
		os.message("Imagen corrupta. Imposible de cargar")
		return image.create(0,0)
	end
	local bfOffBits = ReadWORD(bytecode, offset+10);

	-------------------------
	-- Parse BITMAPINFOHEADER
	-------------------------
	offset = 15; -- BITMAPFILEHEADER is 14 bytes long
	local biWidth = ReadDWORD(bytecode, offset+4);
	local biHeight = ReadDWORD(bytecode, offset+8);
	local biBitCount = ReadWORD(bytecode, offset+14);
	local biCompression = ReadDWORD(bytecode, offset+16);
	if(biBitCount ~= 24) then
		os.message("Imagen no soportada (Bits por píxel: "..bpp..")\nSolo 24-bit")
		return image.create(0,0)
	end
	if(biCompression ~= 0) then
		os.message("Solo se soportan imágenes sin compresión.")
		return image.create(0,0)
	end

	---------------------
	-- Parse bitmap image
	---------------------
	img=image.create(biWidth,biHeight)
	for y = biHeight-1, 0, -1 do
		offset = bfOffBits + (biWidth*biBitCount/8)*y + 1;
		for x = 0, biWidth-1 do
			local b = bytecode:byte(offset);
			local g = bytecode:byte(offset+1);
			local r = bytecode:byte(offset+2);
			offset = offset + 3;

			img:pixel(x, biHeight-y-1,color.new( r, g, b))
		end
	end
	return img
end


oldload=image.load

function image.load(ruta)
if files.ext(ruta)=="bmp" then
	return imageload(ruta)
else
	return oldload(ruta)
end
end

function hexadec(word)
  local hexa = ""
  local result = 0
  local number = 0
  local n = 0
  
     for i = 4 , 1 , -1 do
      hexa = hexa..string.format("%X",word:byte(i))
    end
  for i = #hexa , 1 , -1 do
    if hexa:sub(i,i) == "A" then
      number = 10
    elseif hexa:sub(i,i) == "B" then
      number = 11
    elseif hexa:sub(i,i) == "C" then
      number = 12
    elseif hexa:sub(i,i) == "D" then
      number = 13
    elseif hexa:sub(i,i) == "E" then
      number = 14
    elseif hexa:sub(i,i) == "F" then
      number = 15
    else
      number = hexa:sub(i,i)
    end
    result = result + number*math.pow(n,16)
    n = n + 1
  end
  return result
end

function extractpbp(ruta)
local offset={}
local a=io.open(ruta,"rb")
if a==nil then
	return 6
end
a:seek("set",12)
offset[1]=hexadec(a:read(4))
a:seek("set",16)
offset[2]=hexadec(a:read(4))


if offset[1]==offset[2] then
	a:close()
	return 6
else
	a:seek("set",offset[1])
	dat=a:read(offset[2]-offset[1])
	a:close()
	table.insert(icons,image.loadfrommemory(dat,"PNG"))
	icons[#icons]:resize(55,30)
	return #icons
end
end


