Convert HEX vector to image

Post Reply
Gary Woodley
Posts: 28
Joined: Mon Apr 27, 2009 3:37 pm
Location: Oxford UK

Convert HEX vector to image

Post by Gary Woodley »

I have an image that has been stored as in HEX vector pairs format. An example is:-

Code: Select all

83B483B489B294AA97A49C999E8C9B8595818C7C8279777872776A7762775B775979577A567D5680578461866
B867D82847E867C887B8A7B8C7B8C7BFFFF.
The code i have been supplied as an example is in PHP, has anybody any idea how I can covert this to run in my application and save the converted image to a file.

Code: Select all

if ($sig != "")
{                              
   $im = @ImageCreate (256, 256)
   or die ("Cannot Initialize new GD image stream");
   $background_color = ImageColorAllocate ($im, 255, 255, 255);
   $text_color = ImageColorAllocate ($im, 0, 0, 0);
   
   for( $i=0 ; $i<strlen($sig) ; )
   {
           if(substr($sig,$i,4) == "FFFF" || substr($sig,$i+4,4) == "FFFF")
                   $i += 4;
           else
           {
                   if(substr($sig,$i,2) != "FF")
                           $x1 = base_convert(substr($sig,$i,2),16,10);
                   if(substr($sig,$i+2,2) != "FF")
                           $y1 = base_convert(substr($sig,$i+2,2),16,10);
                   if(substr($sig,$i+4,2) != "FF")
                           $x2 = base_convert(substr($sig,$i+4,2),16,10);
                   if(substr($sig,$i+6,2) != "FF")
                           $y2 = base_convert(substr($sig,$i+6,2),16,10);
                   imageline ($im,$x1,$y1,$x2,$y2,$text_color);
                   $i+=4;
           }
   }
   
   ImagePng ($im,"temp.png");
   echo "<img src='temp.png'>";
}
 
Thanks in advance

Gary
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Convert HEX vector to image

Post by Antonio Linares »

Gary,

This is a quick try. I don't know how the image should look like, so please test it and lets see :-)

Code: Select all

#include "FiveWin.ch"

function Main()

   local oWnd

   DEFINE WINDOW oWnd

   ACTIVATE WINDOW oWnd ;
      ON PAINT DrawVectors( hDC )

return nil

function DrawVectors( hDC )

   local cSig := "83B483B489B294AA97A49C999E8C9B8595818C7C8279777872776A7762775B775979577A567D5680578461866" + ;
                 "B867D82847E867C887B8A7B8C7B8C7BFFFF"
   local n, nX1, nY1, nX2, nY2 
   
   for n = 1 to Len( cSig )
      if substr( cSig, n, 4 ) == "FFFF" .or. substr( cSig, n + 4, 4 ) == "FFFF"
         n += 4
      else
         if substr( cSig, n, 2 ) != "FF"
            nX1 = nHex( substr( cSig, n, 2 ) )
         endif   
         if substr( cSig, n + 2, 2 ) != "FF"
            nY1 = nHex( substr( cSig, n + 2, 2 ) )
         endif   
         if substr( cSig, n + 4, 2 ) != "FF"
            nX2 = nHex( substr( cSig, n + 4, 2 ) )
         endif   
         if substr( cSig, n + 6, 2 ) != "FF"
            nY2 = nHex( substr( cSig, n + 6, 2 ) )
         endif
         MoveTo( hDC, nX1, nY1 )
         LineTo( hDC, nX2, nY2 )
         n += 4
      endif
   next         

return nil
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
xProgrammer
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia

Re: Convert HEX vector to image

Post by xProgrammer »

Hi Gary, Antonio

Nice translation of the original code. It might be worth looking at the logic of it because it may produce funny results if code meets "FF" in one of the tests - the corresponding value of nX1 or nX2 or nY1 or nY2 will not be set and will either be the value from the last FOR loop (would that be correct?) or nil presumably if this is the first such iteration (which will cause a run-time error I assume). n += 4 is evaluated regardless of the condition so it could be made a step value. If indeed it is an "error" to have "FF" on an even boundary then some additional logic is required and the individual tests for "FF" would make the two tests for "FFFF" redundant.

I would question the logic further. It seems that all lines have to be connected with this code, that is nX2 nY2 will become nX1 nY1 on the next iteration. Is that correct or should the second n += 4 actually be an n += 8?

I am unfamiliar with this format, but the above are questions and issues I would be looking into, especially if you get some unexpected results.

Regards
xProgrammer
Gary Woodley
Posts: 28
Joined: Mon Apr 27, 2009 3:37 pm
Location: Oxford UK

Re: Convert HEX vector to image

Post by Gary Woodley »

Hi,

I just tested the conversion using

Code: Select all

564B564B574A58495948594759455944584357415641554054404F404C404940434040423E433D453A473A49384C384
E35533358325B325D325F32613264366739693C6A3F6C426D457049724A724B724F72517254715570556F556E556C55
6953685267526651654D644C634B634A634A6249614A614E60505F515E535E545E545D565D565C575B5B5B615A645A6
45AFFFF6C456C456C466C486C4A6D4E6E55715C74627666786B796E7A707C737C747E767F78817A81788171806B806
680628060805E815E8261876689688B6C8C6C8D6B8E6A8E698F6891689265926093589451964B974797449743974297
3E973A973597329730972F982F9A2F9A2FFFFF
 
This should be a handwritten GW but it seems to return random lines. Supposedly the php code is actually used to convert successfully though I have never seen it actually work.

Regards

Gary
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Convert HEX vector to image

Post by Antonio Linares »

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Convert HEX vector to image

Post by Antonio Linares »

Solved! :-)

Code: Select all

#include "FiveWin.ch"

function Main()

   local oWnd

   DEFINE WINDOW oWnd

   ACTIVATE WINDOW oWnd ;
      ON PAINT DrawVectors( hDC )

return nil

function DrawVectors( hDC )

   local cSig := "564B564B574A58495948594759455944584357415641554054404F404C404940434040423E433D453A473A49384C384" + ;
"E35533358325B325D325F32613264366739693C6A3F6C426D457049724A724B724F72517254715570556F556E556C55" + ;
"6953685267526651654D644C634B634A634A6249614A614E60505F515E535E545E545D565D565C575B5B5B615A645A6" + ;
"45AFFFF6C456C456C466C486C4A6D4E6E55715C74627666786B796E7A707C737C747E767F78817A81788171806B806" + ;
"680628060805E815E8261876689688B6C8C6C8D6B8E6A8E698F6891689265926093589451964B974797449743974297" + ;
"3E973A973597329730972F982F9A2F9A2FFFFF"

   local n, nX1, nY1, nX2, nY2 
   
   for n = 1 to Len( cSig ) step 4
      if substr( cSig, n, 4 ) == "FFFF" .or. substr( cSig, n + 4, 4 ) == "FFFF"
         n += 4
      else
         if substr( cSig, n, 2 ) != "FF"
            nX1 = nHex( substr( cSig, n, 2 ) )
         endif   
         if substr( cSig, n + 2, 2 ) != "FF"
            nY1 = nHex( substr( cSig, n + 2, 2 ) )
         endif   
         if substr( cSig, n + 4, 2 ) != "FF"
            nX2 = nHex( substr( cSig, n + 4, 2 ) )
         endif   
         if substr( cSig, n + 6, 2 ) != "FF"
            nY2 = nHex( substr( cSig, n + 6, 2 ) )
         endif
         MoveTo( hDC, nX1, nY1 )
         LineTo( hDC, nX2, nY2 )
      endif
   next         

return nil
 
Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
Gary Woodley
Posts: 28
Joined: Mon Apr 27, 2009 3:37 pm
Location: Oxford UK

Re: Convert HEX vector to image

Post by Gary Woodley »

Works perfectly thanks!

Next question, How do I save the image to a file?

Thanks

Gary
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Convert HEX vector to image

Post by Antonio Linares »

Gary,

oWnd:SaveToBmp( cFileName )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
fraxzi
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines
Contact:

Re: Convert HEX vector to image

Post by fraxzi »

Dear Mr. Antonio,

Similar to the subject..

How about cStr := BmpToStr( oImg:hBitmap ) to be use with this construct..

Code: Select all

..
"Update sql_table Set thisfield = '"+cStr+"' where field_id = 'abc' "    //not saving..  thisfield type is blob or binary
..

Hope you can help.

Regards,
FAP
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Convert HEX vector to image

Post by Antonio Linares »

Frances,

Do you mean that SQL errors with such string ?

Maybe it has to be encoded so the bytes are not higher than some certain value. What database engine are you using ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
fraxzi
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines
Contact:

Re: Convert HEX vector to image

Post by fraxzi »

Antonio Linares wrote:Frances,

Do you mean that SQL errors with such string ?

Maybe it has to be encoded so the bytes are not higher than some certain value. What database engine are you using ?

Dear Mr. Antonio,

I'm using ADS 9.1

There's no error but not saving cStr := Bmp2Str(oImg:hBitmap).. if using cStr := '{1231412-41234123-123423}' ..it saves the example string in BLOB field as binary..


Regards,
FAP
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Convert HEX vector to image

Post by Antonio Linares »

Frances,

I am afraid that I don' know how to help you on that issue,

Probably an ADS user may be able to help you. We use MySQL mostly here.
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply