Conversión de C++ a Delphi
#1
Posted 18 September 2011 - 11:48 AM
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.
#2
Posted 18 September 2011 - 03:31 PM
function BII_Uncompress_User_Record_MT(rec: PCHAR; whichEnrollment, reserved: integer; nx, ny: PInteger; outarray: PCHAR): integer; stdcall;
Saludos.
#3
Posted 04 October 2011 - 07:07 PM
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]
#4
Posted 04 October 2011 - 07:30 PM
Cual sería la equivalencia de esto en Delphi
[csharp]
customer.@operator = StringFieldOperator.@Not;
[/csharp]
Salud OS
#5
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.
#6
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í:
customer.&operator := StringFieldOperator(0);
Gracias amigo.
Salud OS
#7
Posted 09 October 2011 - 07:47 PM
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]
#8
Posted 10 October 2011 - 12:19 AM
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.
#9
Posted 10 October 2011 - 06:48 AM
En el ejemplo relleno del valor 127 que es el que muestras en el código:
var hBitmap: Cardinal; unpackedimage: Pointer; BitmapInfo: TBITMAPINFO; m_nx, m_ny: Cardinal; begin m_nx:= 96; m_ny:= 96; unpackedimage:= nil; with BitmapInfo.bmiHeader do begin biSize := SizeOf(BitmapInfo.bmiHeader); biWidth := m_nx; biHeight := m_ny; biPlanes := 1; biBitCount := 24; biCompression := BI_RGB; biSizeImage := (((biWidth * (biBitCount div 8)) + 3) and $FFFFFFFC) * biHeight; hBitmap:= CreateDIBSection(0, BitmapInfo, DIB_RGB_COLORS, unpackedimage, 0, 0); // unpackedimage se asigna automáticamente al usar la API CreateDIBSection. // Ahora lo relleno. FillMemory(unpackedimage, biSizeImage, 127); // unpackedimage no se libera hasta no destruir hBitmap: DeleteObject(hBitmap) end; 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.
#10
Posted 10 October 2011 - 09:47 AM
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]
#11
Posted 10 October 2011 - 09:48 AM
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]
#12
Posted 10 October 2011 - 10:31 AM
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.
#13
Posted 10 October 2011 - 04:23 PM
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
#14
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:
// Crear DibBitmap de 8 bits function CreateDibBitmap(Data: PBYTE; Width, Height: integer): HBITMAP; var LineWidth: integer; Buffer: Pointer; BitmapInfo: ^TBITMAPINFO; i, j: Cardinal; begin Buffer:= nil; LineWidth:= ((Width + 3) div 4 ) * 4; GetMem(BitmapInfo, sizeof(TBITMAPINFOHEADER) + sizeof(RGBQUAD) * 256); with BitmapInfo.bmiHeader do begin biSize := SizeOf(BitmapInfo.bmiHeader); biWidth := Width; biHeight := Height; biPlanes := 1; biBitCount := 8; biCompression := BI_RGB; biSizeImage := 0; end; // en BitmapInfo.bmiColors rellenamos la paleta... for i:= 0 to 255 do begin BitmapInfo.bmiColors[i].rgbRed:= i; BitmapInfo.bmiColors.rgbGreen:= i; BitmapInfo.bmiColors.rgbBlue:= i; end; Result:= CreateDIBSection(0, BitmapInfo^, DIB_RGB_COLORS, Buffer, 0, 0); FreeMem(BitmapInfo); for i:= 0 to Height-1 do begin CopyMemory(Buffer, Data, Width); inc(PCHAR(Buffer), Width); ZeroMemory(Buffer, LineWidth - Width); inc(PCHAR(Buffer), LineWidth - Width); end; end;
Un ejemplo de uso integrado en delphi:
procedure TForm1.Button4Click(Sender: TObject); var unpackedimage: PBYTE; begin GetMem(unpackedimage, 96*96); FillMemory(unpackedimage, 96*96, 127); Image1.Picture.Bitmap.Handle:= CreateDibBitmap(unpackedimage, 96, 96); FreeMem(unpackedimage); end;
Espero que te aclare las ideas.
Saludos.
#15
Posted 10 October 2011 - 06:49 PM
Dejame realizo la prueba y te comento.
SAludos
#16
Posted 10 October 2011 - 07:30 PM
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.
var miimagen : array[0..9216] of byte; unpakedimage : pbyte; begin unpaekdimage:= @miimagen; end;
#17
Posted 11 October 2011 - 12:00 AM
var miimagen : array[0..9216] of byte; i: integer; begin for i:= 0 to 9215 do miimagen[i]:= 127; Image1.Picture.Bitmap.Handle:= CreateDibBitmap(@miimagen, 96, 96); end;
Saludos.