Jump to content


Photo

Conversión de C++ a Delphi


  • Please log in to reply
16 replies to this topic

#1 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 18 September 2011 - 11:48 AM

Hola Amigos
Buenas tardes

Alguno de ustedes me pudiera por favor ayudar a convertir esta funcion de C++ en Endlphi, se los voy a agradecer mucho. Gracias

[cpp]
int WINAPI BII_Uncompress_User_Record_MT(unsigned char *record, int whichEnrollment,
                                                      int reserved, int *nx, int *ny, unsigned char *outarray);

[/cpp]




Retoco el título. escafandra.
  • 0

#2 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 18 September 2011 - 03:31 PM

La definición de esa función sería, de forma literal, esto:



delphi
  1. function BII_Uncompress_User_Record_MT(rec: PCHAR; whichEnrollment, reserved: integer; nx, ny: PInteger; outarray: PCHAR): integer; stdcall;




Saludos.


  • 0

#3 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 04 October 2011 - 07:07 PM

Muchas Gracias, por la ayuda,
me pudieran por favor ayudar a convertir este codigo en Delphi, ya lo intente pero me falta como intercambiar la imagen de un puntero a una Imagen en el.
Se los voy a agradecer mucho. Gracias de Antemando
Este es el codigo:
[cpp]
int CBIITemplate::Template2Bitmap(BOOL bPass_)
{
unsigned char *unpackedimage = NULL;
int size, retVal = 0;
if(m_hDIBTemplate != NULL)
{
  m_hDIBTemplate = (HDIB) GlobalFree(m_hDIBTemplate);
}
try
{
  if ((size = BII_Uncompress_User_Record_MT(m_TemplateData, m_WhichEnrollment, 0, &m_nx, &m_ny, NULL)) > 0)
  {
  unpackedimage = (unsigned char *) malloc(size);

  if ((size = BII_Uncompress_User_Record_MT(m_TemplateData, m_WhichEnrollment, 0, &m_nx, &m_ny,unpackedimage)) < 0)
    retVal =  -1;
  }
  else
  {
  if ( bPass_ || BII_Get_Template_Nx_Ny_MT((BII_Template *)m_TemplateData, &m_nx, &m_ny) == TRUE)
  {
    m_nx=96;
    m_ny=96;
    unpackedimage = (unsigned char *) malloc(m_nx*m_ny);
    BII_Uncompress_MT((unsigned char *)m_TemplateData, unpackedimage);
  }
  else
  {
    // Mshk: the BII_Get_Template_Nx_Ny is in Compression.lib, for which the source code is gone.
    // For templates the subsampled image size is always 96x96, so it is hard coded here
    m_nx = 96;
    m_ny = 96;
    unpackedimage = (unsigned char *) malloc(m_nx * m_ny);   

    for (int i = 0; i < m_nx * m_ny; i++)
    unpackedimage = 127;
  }
  }
}
catch (...)
{
  m_nx = 96;
  m_ny = 96;
  unpackedimage = (unsigned char *) malloc(m_nx * m_ny);   

  for (int i = 0; i < m_nx * m_ny; i++)
  unpackedimage = 127;
}
/* Create a DIB from the template */
/* NX and NY are now reversed (not important if symetric) */
m_hDIBTemplate = CreateDIB(unpackedimage, m_nx, m_ny);
if (unpackedimage)
  free (unpackedimage);
/* If the DIB creation failed return -1 */
if(m_hDIBTemplate == NULL)
  return -1;
return retVal;
}


[/cpp]
  • 0

#4 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14448 posts
  • LocationMéxico

Posted 04 October 2011 - 07:30 PM

Yo me aprovecho también del hilo :D

Cual sería la equivalencia de esto en Delphi

[csharp]
    customer.@operator = StringFieldOperator.@Not;
[/csharp]

Salud OS
  • 0

#5 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 06 October 2011 - 05:56 AM

....
me pudieran por favor ayudar a convertir este codigo en Delphi, ya lo intente pero me falta como intercambiar la imagen de un puntero a una Imagen en el.
Se los voy a agradecer mucho. Gracias de Antemando
Este es el codigo:
[cpp]
/...................[/cpp]


Es una función de una clase que desconozco, el código es un poco extenso y con conversiones de tipos privados. Deberías concretar mas tu duda.


...Cual sería la equivalencia de esto en Delphi

[csharp]
    customer.@operator = StringFieldOperator.@Not;
[/csharp]


¿Eso es literal?.
El problema es que delphi no admite la sobrecarga de operadores y tendrás que adaptar el código ...


Saludos.
  • 0

#6 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14448 posts
  • LocationMéxico

Posted 06 October 2011 - 08:03 AM



...Cual sería la equivalencia de esto en Delphi

[csharp]
    customer.@operator = StringFieldOperator.@Not;
[/csharp]


¿Eso es literal?.
El problema es que delphi no admite la sobrecarga de operadores y tendrás que adaptar el código ...

Saludos.


Pues si, si es literal, viendo los tipos de datos y las clases, logré entender el concepto, en delphi se hace así:



delphi
  1.     customer.&operator := StringFieldOperator(0);



Gracias amigo.  :)

Salud OS
  • 0

#7 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 09 October 2011 - 07:47 PM

Hola Escafandra
Basicamente es como puedo pasar un array de bytes a un bitmpa o bmp
como lo ilustro en lo siguiente:
[cpp]
m_nx = 96;
  m_ny = 96;
  unpackedimage = (unsigned char *) malloc(m_nx * m_ny);   

  for (int i = 0; i < m_nx * m_ny; i++)
  unpackedimage = 127;
}
/* Create a DIB from the template */
/* NX and NY are now reversed (not important if symetric) */
m_hDIBTemplate = CreateDIB(unpackedimage, m_nx, m_ny);
if (unpackedimage)
  free (unpackedimage);
/* If the DIB creation failed return -1 */
if(m_hDIBTemplate == NULL)
  return -1;

[/cpp]
  • 0

#8 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 10 October 2011 - 12:19 AM

Antes de poder responderte, es necesario conocer el tipo de la variable m_hDIBTemplate. La función CreateDIB no pertenece a la API de windows aunque si CreateDIBitmap y ImageAllocator.CreateDIB (DirectShow). Ninguna se corresponde con los parámetros que se le pasa en el código que pretendes traducir. Bien la tienes implementada en otro sitio o el código inicial es Visual C.

En cualquier caso para adaptar el código a delphi debemos pasar por la API y tipos básicos de ésta. En caso de que m_hDIBTemplate sea un HBITMAP será mas sencillo pero me da la sensación que es un  objeto de una clase específica. En ese caso deberás usar HBITMAP o un TBitmap de delphi.

Saludos.
  • 0

#9 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 10 October 2011 - 06:48 AM

Voy a poner un ejemplo de como rellenar el bloque de memoria de un Bitmap Creado con la API. Asumo que el bitmap es de 24 bits, para prescindir de la paleta.

En el ejemplo relleno del valor 127 que es el que muestras en el código:


delphi
  1. var
  2.   hBitmap: Cardinal;
  3.   unpackedimage: Pointer;
  4.   BitmapInfo: TBITMAPINFO;
  5.   m_nx, m_ny: Cardinal;
  6.  
  7. begin
  8.   m_nx:= 96;
  9.   m_ny:= 96;
  10.   unpackedimage:= nil;
  11.   with BitmapInfo.bmiHeader do
  12.   begin
  13.     biSize := SizeOf(BitmapInfo.bmiHeader);
  14.     biWidth := m_nx;
  15.     biHeight := m_ny;
  16.     biPlanes := 1;
  17.     biBitCount := 24;
  18.     biCompression := BI_RGB;
  19.     biSizeImage := (((biWidth * (biBitCount div 8)) + 3) and $FFFFFFFC) * biHeight;
  20.     hBitmap:= CreateDIBSection(0, BitmapInfo, DIB_RGB_COLORS, unpackedimage, 0, 0);
  21.    
  22.     // unpackedimage se asigna automáticamente al usar la API CreateDIBSection.
  23.     // Ahora lo relleno.
  24.     FillMemory(unpackedimage, biSizeImage, 127);
  25.     // unpackedimage no se libera hasta no destruir hBitmap:  DeleteObject(hBitmap)
  26.  
  27.   end;
  28. end;




Esto se puede hacer también usando TBitmap.ScanLine desde delphi, todo depende del nivel al que estés trabajando...

Es por este motivo la razón de mi anterior mensaje.

Saludos.
  • 0

#10 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 10 October 2011 - 09:47 AM

Hola Escafandra,
Te anexo el codigo completo en c++, en donde m_hDIBTemplate lo manejan como null
[cpp]
// BIITemplate.cpp: implementation of the CBIITemplate class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BIITemplate.h"
#include "Dibapi.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBIITemplate::CBIITemplate()
{
m_hDIBTemplate = NULL;
m_TemplateData = NULL;
m_WhichEnrollment = 0;
}
CBIITemplate::~CBIITemplate()
{
if(m_hDIBTemplate)
  m_hDIBTemplate = (HDIB) GlobalFree(m_hDIBTemplate);
}
void CBIITemplate::Paint(CPaintDC& TheDC, CRect frame)
{
CRect  templateDIBrect;
CPalette Palette;
HPALETTE hPal = NULL;
//To Do:  Add code the free's up memory used for the DIB
if (m_hDIBTemplate)
{
  /* Area of the Template DIB to display */
  templateDIBrect.left = 0;
  templateDIBrect.top = 0;
  templateDIBrect.right = m_nx;
  templateDIBrect.bottom = m_ny;
  /* CREATE THE PALLETTE AND DISPLAY THE TEMPLATE */
  CreateDIBPalette(m_hDIBTemplate, &Palette);
  hPal = (HPALETTE) Palette.m_hObject;
  PaintDIB(TheDC.m_hDC, &frame, m_hDIBTemplate, &templateDIBrect, &Palette);
}
}

int CBIITemplate::Template2Bitmap(BOOL bPass_)
{
unsigned char *unpackedimage = NULL;
int size, retVal = 0;
if(m_hDIBTemplate != NULL)
{
  m_hDIBTemplate = (HDIB) GlobalFree(m_hDIBTemplate);
}
try
{
  if ((size = BII_Uncompress_User_Record_MT(m_TemplateData, m_WhichEnrollment, 0, &m_nx, &m_ny, NULL)) > 0)
  {
  unpackedimage = (unsigned char *) malloc(size);

  if ((size = BII_Uncompress_User_Record_MT(m_TemplateData, m_WhichEnrollment, 0, &m_nx, &m_ny,unpackedimage)) < 0)
    retVal =  -1;
  }
  else
  {
  if ( bPass_ || BII_Get_Template_Nx_Ny_MT((BII_Template *)m_TemplateData, &m_nx, &m_ny) == TRUE)
  {
    m_nx=96;
    m_ny=96;
    unpackedimage = (unsigned char *) malloc(m_nx*m_ny);
    BII_Uncompress_MT((unsigned char *)m_TemplateData, unpackedimage);
  }
  else
  {
    // Mshk: the BII_Get_Template_Nx_Ny is in Compression.lib, for which the source code is gone.
    // For templates the subsampled image size is always 96x96, so it is hard coded here
    m_nx = 96;
    m_ny = 96;
    unpackedimage = (unsigned char *) malloc(m_nx * m_ny);   

    for (int i = 0; i < m_nx * m_ny; i++)
    unpackedimage = 127;
  }
  }
}
catch (...)
{
  m_nx = 96;
  m_ny = 96;
  unpackedimage = (unsigned char *) malloc(m_nx * m_ny);   

  for (int i = 0; i < m_nx * m_ny; i++)
  unpackedimage = 127;
}
/* Create a DIB from the template */
/* NX and NY are now reversed (not important if symetric) */
m_hDIBTemplate = CreateDIB(unpackedimage, m_nx, m_ny);
if (unpackedimage)
  free (unpackedimage);
/* If the DIB creation failed return -1 */
if(m_hDIBTemplate == NULL)
  return -1;
return retVal;
}


[/cpp]
  • 0

#11 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 10 October 2011 - 09:48 AM

Me falto comentar que tambien es de 8 Bits, como lo ilustra el siguiente codigo.
En espera de tus comentarios. SAludos y muchisimas gracias de antemano.
[cpp]
HDIB WINAPI CreateDIB(unsigned char *m_data, int m_biWidth, int m_biHeight)
{
HDIB hDIB;
LPSTR pDIB;
LPBITMAPINFO lpbi;
unsigned char *dibData;
int m_numberPalEntries = 256;
int actualWidth = m_biWidth;
if (m_biWidth % 4)
  m_biWidth = ((m_biWidth+4) / 4) * 4;

unsigned long dibSize = sizeof(BITMAPINFOHEADER) +
  (m_numberPalEntries*sizeof(RGBQUAD)) + m_biWidth * m_biHeight;
hDIB = (HDIB) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dibSize);
if (hDIB == 0)
{
  return NULL;
}
pDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
lpbi = (LPBITMAPINFO) pDIB;
dibData = (unsigned char *) pDIB + sizeof(BITMAPINFOHEADER) +
  (m_numberPalEntries*sizeof(RGBQUAD));

lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbi->bmiHeader.biWidth = m_biWidth;
lpbi->bmiHeader.biHeight = m_biHeight;
lpbi->bmiHeader.biPlanes = 1;
lpbi->bmiHeader.biBitCount = 8;
lpbi->bmiHeader.biCompression = BI_RGB;
lpbi->bmiHeader.biSizeImage =  m_biWidth*m_biHeight;
lpbi->bmiHeader.biXPelsPerMeter = 0;
lpbi->bmiHeader.biYPelsPerMeter = 0;
lpbi->bmiHeader.biClrUsed = 0;
lpbi->bmiHeader.biClrImportant = 0;
  for(int i=0; i<m_numberPalEntries; i++){
  lpbi->bmiColors.rgbRed = i;
  lpbi->bmiColors.rgbGreen = i;
  lpbi->bmiColors.rgbBlue = i;
}
for(int j=0,n=0; j<m_biHeight; j++)
  for(i=0; i<m_biWidth; i++)
  if (i < actualWidth)
    dibData[j*m_biWidth+i] = m_data[n++];
  else
    dibData[j*m_biWidth+i] = 0;

::GlobalUnlock((HGLOBAL) hDIB);
return hDIB;
}

[/cpp]
  • 0

#12 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 10 October 2011 - 10:31 AM

El código de una clase sin su definición (archivo.h) no dice demasiado. Entiendo que m_hDIBTemplate es un HDIB. Ese tipo no es estandar de la API de windows. Se refiere a un bloque de memoria donde está escrito un bitmap tal como se leería de un archivo. No es un HBITMAP.

No se que es lo que te interesa. Una traducción literal puede que no sea lo mas conveniente. Imagino que querrás asimilarlo a las clases de la VCL de delphi. En ese caso quizás sea mas recomendable usar un HBITMAP como te sugerí arriba.

Es fundamental que tengas claro el uso que le vas a dar a esa clase, para qué la quieres y como la integrarás con delphi. Antes de seguir traduciendo literalmente piensa despacio en este punto.

Saludos.
  • 0

#13 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 10 October 2011 - 04:23 PM

Hola Escafandra, en resumen
Lo que necesito es pasar el bitmpa que se encuentra en array de bits a una imagen, lo otro ya lo tengo definido, nada más me esta faltando como impoirtar de un array de bytes a un bitmap o timagen en delphi, como te lo ilustraba en los mensajes anteriores. Si me pudieras ayudar te lo voy a agradecer mucho.

En mi variable unpakedimage, ahi traigo el array de bytes.
En espera de tus comentarios. SAludos
  • 0

#14 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 10 October 2011 - 04:49 PM

Hola Escafandra, en resumen...
...como impoirtar de un array de bytes a un bitmap o timagen en delphi...


Eso ya te lo apunté mas arriba con un HBITMAP de 24 bits.

Vamos a crear una función concreta para un HBITMAP de 8 bits 256 colores y paleta:


delphi
  1. // Crear DibBitmap de 8 bits
  2. function CreateDibBitmap(Data: PBYTE; Width, Height: integer): HBITMAP;
  3. var
  4.   LineWidth: integer;
  5.   Buffer: Pointer;
  6.   BitmapInfo: ^TBITMAPINFO;
  7.   i, j: Cardinal;
  8.  
  9. begin
  10.   Buffer:= nil;
  11.   LineWidth:= ((Width  + 3) div 4 ) * 4;
  12.   GetMem(BitmapInfo, sizeof(TBITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
  13.   with BitmapInfo.bmiHeader do
  14.   begin
  15.     biSize := SizeOf(BitmapInfo.bmiHeader);
  16.     biWidth := Width;
  17.     biHeight := Height;
  18.     biPlanes := 1;
  19.     biBitCount := 8;
  20.     biCompression := BI_RGB;
  21.     biSizeImage := 0;
  22.   end;
  23.   // en BitmapInfo.bmiColors rellenamos la paleta...
  24.   for i:= 0 to  255 do
  25.   begin
  26.     BitmapInfo.bmiColors[i].rgbRed:= i;
  27.     BitmapInfo.bmiColors.rgbGreen:= i;
  28.     BitmapInfo.bmiColors.rgbBlue:= i;
  29.   end;
  30.  
  31.   Result:= CreateDIBSection(0, BitmapInfo^, DIB_RGB_COLORS, Buffer, 0, 0);
  32.   FreeMem(BitmapInfo);
  33.   for i:= 0 to Height-1 do
  34.   begin
  35.     CopyMemory(Buffer, Data, Width);
  36.     inc(PCHAR(Buffer), Width);
  37.     ZeroMemory(Buffer, LineWidth - Width);
  38.     inc(PCHAR(Buffer), LineWidth - Width);
  39.   end;
  40. end;



Un ejemplo de uso integrado en delphi:


delphi
  1. procedure TForm1.Button4Click(Sender: TObject);
  2. var
  3.   unpackedimage: PBYTE;
  4. begin
  5.   GetMem(unpackedimage, 96*96);
  6.   FillMemory(unpackedimage, 96*96, 127);
  7.   Image1.Picture.Bitmap.Handle:= CreateDibBitmap(unpackedimage, 96, 96);
  8.   FreeMem(unpackedimage);
  9. end;



Espero que te aclare las ideas.


Saludos.
  • 0

#15 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 10 October 2011 - 06:49 PM

Muchisimas Gracias
Dejame realizo la prueba y te comento.
SAludos
  • 0

#16 jjmcira

jjmcira

    Member

  • Miembros
  • PipPip
  • 26 posts

Posted 10 October 2011 - 07:30 PM

Hola Escafandra
Fijate que todo el procedimiento me parece bien pero en el unpakedimage traigo un array, como puedo pasar ese arreglo al apuntador, ya intente colocandolo como indico y me marca un error de que no son compatibles, si me puedes ayudar te lo voy agradecer gracias.


delphi
  1. var
  2. miimagen : array[0..9216] of byte;
  3. unpakedimage : pbyte;
  4. begin
  5. unpaekdimage:= @miimagen;
  6. end;


  • 0

#17 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4107 posts
  • LocationMadrid - España

Posted 11 October 2011 - 12:00 AM



delphi
  1. var
  2.   miimagen : array[0..9216] of byte;
  3.   i: integer;
  4. begin
  5.   for i:= 0 to 9215 do
  6.     miimagen[i]:= 127;
  7.   Image1.Picture.Bitmap.Handle:= CreateDibBitmap(@miimagen, 96, 96);
  8. end;




Saludos.
  • 0




IP.Board spam blocked by CleanTalk.