Como reverter um retângulo para um hash de face picasa
-
18-09-2019 - |
Pergunta
Aqui estão os detalhes do que as lojas do Picasa como hash. Ele os armazena assim:
faces=rect64(54391dc9b6a76c2b),4cd643f64b715489
[DSC_2289.jpg]
faces=rect64(1680000a5c26c82),76bc8d8d518750bc
Informações sobre a web dizem o seguinte:
O número envolto no RECT64 () é um número hexadecimal de 64 bits.
- Divida isso em quatro números de 16 bits.
- Divida cada um pelo número máximo de 16 bits não assinado (65535) e você terá quatro números entre 0 e 1.
- Os quatro números restantes oferecem coordenadas relativas para o retângulo da face: (esquerda, superior, direita, inferior).
- Se você deseja acabar com coordenadas absolutas, múltipla esquerda e direita pela largura da imagem e pela parte superior e inferior pela altura da imagem.
Portanto, meu código para transformar isso em um retângulo funciona bem (apenas mantendo as coordenadas relativas):
public static RectangleF GetRectangle(string hashstr)
{
UInt64 hash = UInt64.Parse(hashstr, System.Globalization.NumberStyles.HexNumber);
byte[] bytes = BitConverter.GetBytes(hash);
UInt16 l16 = BitConverter.ToUInt16(bytes, 6);
UInt16 t16 = BitConverter.ToUInt16(bytes, 4);
UInt16 r16 = BitConverter.ToUInt16(bytes, 2);
UInt16 b16 = BitConverter.ToUInt16(bytes, 0);
float left = l16 / 65535.0F;
float top = t16 / 65535.0F;
float right = r16 / 65535.0F;
float bottom = b16 / 65535.0F;
return new RectangleF(left, top, right - left, bottom - top);
}
Agora eu tenho um retângulo e quero transformá -lo novamente no hash mencionado acima. Não consigo descobrir isso. Parece que o Picasa usa 2 bytes, incluindo precisão, no entanto, um flutuador em C# é 8 bytes, e até o bitconverter.tosingle é 4 bytes.
Qualquer ajuda apreciada.
Edit: aqui está o que tenho agora
public static string HashFromRectangle(RectangleCoordinates rect)
{
Console.WriteLine("{0} {1} {2} {3}", rect.Left, rect.Top, rect.Right, rect.Bottom);
UInt16 left = Convert.ToUInt16((float)rect.Left * 65535.0F);
UInt16 top = Convert.ToUInt16((float)rect.Top * 65535.0F);
UInt16 right = Convert.ToUInt16((float)rect.Right * 65535.0F);
UInt16 bottom = Convert.ToUInt16((float)rect.Bottom * 65535.0F);
byte[] lb = BitConverter.GetBytes(left);
byte[] tb = BitConverter.GetBytes(top);
byte[] rb = BitConverter.GetBytes(right);
byte[] bb = BitConverter.GetBytes(bottom);
byte[] barray = new byte[8];
barray[0] = lb[0];
barray[1] = lb[1];
barray[2] = tb[0];
barray[3] = tb[1];
barray[4] = rb[0];
barray[5] = rb[1];
barray[6] = bb[0];
barray[7] = bb[1];
return BitConverter.ToString(barray).Replace("-", "").ToLower();
}
Solução
Seu código atual está trocando os bytes de cada coordenada. Isso ocorre porque o BitConverter fornece os bytes em Little Endian Order (ou seja, o primeiro byte da matriz é o byte menos significativo). A troca de suas tarefas da seguinte forma faz com que a decodificação e a renúncia devolvam o hash original.
barray[0] = lb[1];
barray[1] = lb[0];
barray[2] = tb[1];
barray[3] = tb[0];
barray[4] = rb[1];
barray[5] = rb[0];
barray[6] = bb[1];
barray[7] = bb[0];
Dito isto, acho mais claro fazer a conversão usando multiplicações e acrescenta simples. Você pode fazer uma coisa semelhante com a decodificação da string de hash se você a ler como um único ulong
e subtrair/dividir. por exemplo, para a codificação:
public static ushort ToUShort(double coordinate)
{
double ratio = Math.Max(0, Math.Min(Math.Round(coordinate * 65535), 65535));
return (ushort)ratio;
}
public static string HashFromRectangle(Rect rect)
{
ulong left = ToUShort(rect.Left);
ulong top = ToUShort(rect.Top);
ulong right = ToUShort(rect.Right);
ulong bottom = ToUShort(rect.Bottom);
ulong hash = (((left * 65536) + top) * 65536 + right) * 65536 + bottom;
return hash.ToString("x");
}
Outras dicas
Parece que você precisa retirar os tipos de flutuação de hashfromRectangle (ret) como assim:
UInt16 left = (UInt16)( rect.Left * 65535.0F);
UInt16 top =(UInt16) (rect.Top * 65535.0F);
UInt16 right = (UInt16) (rect.Right * 65535.0F);
UInt16 bottom = (UInt16) (rect.Bottom * 65535.0F);
Além disso, pode ser mais legível usar isso para preencher a matriz:
Array.Copy(lb, 0, barray, 0, 2);
Array.Copy(tb, 0, barray, 2, 2);
Array.Copy(rb, 0, barray, 4, 2);
Array.Copy(bb, 0, barray, 6, 2);
Deixe-me saber se isso funciona!
Aaron