Contering C++ code to FreeBASIC

General FreeBASIC programming questions.
Gablea
Posts: 1049
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Contering C++ code to FreeBASIC

Postby Gablea » Nov 17, 2015 10:31

Hi All can someone please help me by converting this code I have been trying for the last 2 weeks. (C++ code is not my weakpoint is my death)

Thanks

Code: Select all

[Code=cpp file=Untitled.bas]
//************************************************//
//Function: AntiRotateBmp90D //
// Utility: Transform bmp format bit image into printer processable data //
// Parameter: pBmpData--- Pointer to source data //
// nPixelsOfWidth----bit image width(dots) //
// nPixelsOfHeight---bit image height(dots) //
// pBmpDataRotated---Pointer to target data //
//Back value: 1:data transformation success 0: data error //
//************************************************//
int AntiRotateBmp90D(
 char *pBmpData,
 const int nPixelsOfWidth,
 const int nPixelsOfHeight,
 char *pBmpDataRotated
 )
{
 // Define process variables
 int nBytesOfWidth = 0,nBytesOfHeight = 0;
 int i=0,col=0,row=0,index = 0,colbyte = 0;
 char* midData;
 unsigned char tempdata = 0,colnum = 0,rownum = 0;
 unsigned char temp[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
 // Comparing parameters
 if (pBmpData == NULL || pBmpDataRotated == NULL) return 0;
 if (nPixelsOfWidth <= 0 || nPixelsOfHeight <= 0) return 0;
 if ((nPixelsOfWidth % 8) != 0 || (nPixelsOfHeight % 8) != 0) return 0;
 // get real image dimension
 nBytesOfWidth = (nPixelsOfWidth+31)/32*4;
 nBytesOfHeight = nPixelsOfHeight / 8;
 // White/black reverse,the value 1 in BMP data is for white which is contrary to the printer definition.
 midData = (char*)malloc(nBytesOfWidth*nPixelsOfHeight+1);
 for(i=0;i<nBytesOfWidth*nPixelsOfHeight;i++){midData[i] = 0xff-pBmpData[i];}
 // Rotation. BMP data is arranged in line data type while data downloaded to printer are arranged in column data type.
 for (row = 0; row <nPixelsOfWidth; row++){
 for (colbyte = 0; colbyte < nBytesOfHeight; colbyte++){
 index = row * nBytesOfHeight + colbyte;
 pBmpDataRotated[index] = 0x00;
 for (col = 0; col < 8; col++){
 colnum = col % 8;
 rownum = row % 8;
 if(colnum >= rownum)
 tempdata = temp[col] & (midData[(nPixelsOfHeight-1-colbyte*8-col)
* nBytesOfWidth + row / 8] >> (colnum-rownum));
 else
 tempdata = temp[col] & (midData[(nPixelsOfHeight-1-colbyte*8-col)
* nBytesOfWidth + row / 8] << (rownum-colnum));
 pBmpDataRotated[index] |= tempdata;
 }
 }
 }
 // release interim buffers
 free(midData);
 return 1;
} [/Code]
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: Contering C++ code to FreeBASIC

Postby RockTheSchock » Nov 17, 2015 11:15

I translated the code. It does compile fine, but i am not sure if I did all boundaries right. So be prepared for crashes because of memory violations at runtime.

Code: Select all

'//************************************************//
'//Function: AntiRotateBmp90D //
'// Utility: Transform bmp format bit image into printer processable data //
'// Parameter: pBmpData--- Pointer to source data //
'// nPixelsOfWidth----bit image width(dots) //
'// nPixelsOfHeight---bit image height(dots) //
'// pBmpDataRotated---Pointer to target data //
'//Back value: 1:data transformation success 0: data error //
'//************************************************//

'Int AntiRotateBmp90D(char *pBmpData,Const int nPixelsOfWidth,Const int nPixelsOfHeight,char *pBmpDataRotated)
Function AntiRotateBmp90D( pBmpData As UByte Ptr, nPixelsOfWidth As Const Integer, nPixelsOfHeight As Const Integer, pBmpDataRotated As UByte Ptr) As Integer

'    // Define process variables
'    int nBytesOfWidth = 0,nBytesOfHeight = 0;
'    int i=0,col=0,row=0,index = 0,colbyte = 0;
'    char* midData;
'    unsigned char tempdata = 0,colnum = 0,rownum = 0;
'    unsigned char temp[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
Dim As Integer   nBytesOfWidth = 0, nBytesOfHeight = 0
Dim As Integer   i=0, col=0, row=0, index = 0, colbyte = 0
Dim As UByte Ptr midData
Dim As UByte     tempdata = 0, colnum = 0, rownum = 0
Dim As UByte     temp(0 To 7) => {&h80,&h40,&h20,&h10,&h08,&h04,&h02,&h01}


'// Comparing parameters
'if (pBmpData == NULL <PIPE><PIPE> pBmpDataRotated == NULL) return 0;
'if (nPixelsOfWidth <= 0 <PIPE><PIPE> nPixelsOfHeight <= 0) return 0;
'if ((nPixelsOfWidth % 8) != 0 <PIPE><PIPE> (nPixelsOfHeight % 8) != 0) return 0;
If (pBmpData = 0 Or pBmpDataRotated = 0) Then Return 0
if (nPixelsOfWidth <= 0 Or nPixelsOfHeight <= 0) Then  return 0
if ((nPixelsOfWidth mod 8) <> 0 or (nPixelsOfHeight mod 8) <> 0) Then return 0
   
   
'// get real image dimension
'nBytesOfWidth = (nPixelsOfWidth+31)/32*4;
'nBytesOfHeight = nPixelsOfHeight / 8;
'// White/black reverse,the value 1 in BMP data is for white which is contrary to the printer definition.
'midData = (char*)malloc(nBytesOfWidth*nPixelsOfHeight+1);
nBytesOfWidth = (nPixelsOfWidth+31)/32*4
nBytesOfHeight = nPixelsOfHeight / 8
midData = New UByte[nBytesOfWidth*nPixelsOfHeight+1]

'for(i=0;i<nBytesOfWidth*nPixelsOfHeight;i++){midData[i] = 0xff-pBmpData[i];}
For i=0 To nBytesOfWidth*nPixelsOfHeight-1
   midData[i] = &hff-pBmpData[i]
Next

'// Rotation. BMP data is arranged in line data type while data downloaded to printer are arranged in column data type.
'for (row = 0; row <nPixelsOfWidth; row++){
'    for (colbyte = 0; colbyte < nBytesOfHeight; colbyte++){
For row = 0 To nPixelsOfWidth-1
   For colbyte = 0 To nBytesOfHeight-1
   
      'index = row * nBytesOfHeight + colbyte;
      'pBmpDataRotated[index] = 0x00;
      'For (col = 0; col < 8; col++){   
      index = row * nBytesOfHeight + colbyte
      pBmpDataRotated[index] = 0
      for col = 0 To 7
   
         'colnum = col % 8;
         'rownum = row % 8;
         'if(colnum >= rownum)
         'tempdata = temp[col] & (midData[(nPixelsOfHeight-1-colbyte*8-col)
         '* nBytesOfWidth + row / 8] >> (colnum-rownum));
         'else
         'tempdata = temp[col] & (midData[(nPixelsOfHeight-1-colbyte*8-col)
         '* nBytesOfWidth + row / 8] << (rownum-colnum));
         'pBmpDataRotated[index] <PIPE>= tempdata;
         colnum = col Mod 8
         rownum = row Mod 8
         If (colnum >= rownum) Then
            tempdata = temp(col) And (midData[(nPixelsOfHeight-1-colbyte*8-col)_
             * nBytesOfWidth + row / 8] SHR (colnum-rownum))
         Else
            tempdata = temp(col) And (midData[(nPixelsOfHeight-1-colbyte*8-col)_
              * nBytesOfWidth + row / 8] Shl (rownum-colnum))
            pBmpDataRotated[index] = pBmpDataRotated[index] Or tempdata   
         EndIf
      Next
   Next
Next

' // release interim buffers
 'free(midData);
 'return 1;

Delete midData
Return 1

End Function
Gablea
Posts: 1049
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Contering C++ code to FreeBASIC

Postby Gablea » Nov 17, 2015 12:00

@RockTheSchock,

Thanks for that the code should convert a bmp image into a file that can be uploaded to my Recipt Printer I hope it works as I have been trying to do this for years

i shall try that and let you know :)
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: Contering C++ code to FreeBASIC

Postby RockTheSchock » Nov 17, 2015 13:32

There is also some space to optimize the algorithm / code. I estimate speed improvements by 30% maybe even more. Now you read byte by byte and write byte by byte. But you could read 8 bytes at once and write one byte at a time ( or other way aorund read single bytes and then write 8 bytes in one chunk). But that isn't really needed if slow file io is involved.

Maybe for games it would be nice to have a faster routine to rotate sprites. But then you would use opengl anyway:
glTranslatef, glRotatef

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests