the-mars-rover / rsa_identification

A dart package for retrieving identification information from South African smart ID's and Driver's Licenses.
MIT License
9 stars 8 forks source link

Decode Drivers License Image Data #2

Open the-mars-rover opened 4 years ago

the-mars-rover commented 4 years ago

The image in the driver's license barcode seems to be encoded as based on the Coreldraw Wavelet image format at 8bit grayscale. But, little else is known about how to decode this image.

pjdupreez commented 3 years ago

Hi there. Just wondering if there is any progress on this? Would like to assist where I can.

the-mars-rover commented 3 years ago

Hi there. Just wondering if there is any progress on this? Would like to assist where I can.

For the moment this has stagnated - any contributions are of course welcome but personally I'm struggling to find time for this right now. If you have any additional info surrounding how the image data may have been encoded adding that to this issue may help move things along :-)

pjdupreez commented 3 years ago

Apart from it being a some wavelet algorithm that was used, unfortunately not. Apparently it is a custom algorithm that was used, but the source was lost when the company tanked in '99 πŸ˜’

Do you the range of the bytes that make up the photo?

the-mars-rover commented 3 years ago

Hi @pjdupreez :)

In terms of the range of bytes, the only insight I have is as per https://github.com/ugommirikwe/sa-license-decoder/blob/master/SPEC.md

Just as a note for everyone who happens to stumble on this issue, the following links are all useful for gaining context:

In addition, a Corrie Burger also emailed me personally and had the following to add with regards to decoding the image: "W.r.t. the image. I found a colour wavelet image online and inspected the header and found the first couple of bytes to be similar. See the first 20 characters of a license ans the colour iamage I found.

License Image b'W' b'I' b'\x04' # Number of image size bytes - 4 b'\x00' b'\xfa' # Height in pixels - 250 b'\x00' b'\xc8' # Width in pixels - 200 b'C' b'(' b'(' b'@' b'\x00' b'0' b'\x02' b'\x8c' b'*' b'%' b'\x92' b'Y' b'\xa3' 57490400fa00c8432828400030028c2a259259a3

Colour wavelet image b'W' b'I' b'\x04' # Number of image size bytes - 4 b'\x05' b'\x00' # Height in pixels - 1280 b'\x03' b'\xc0' # Width in pixels - 960 b'F' b'\x0f' b'H' b'\xc0' b'\x00' b' ' b'\x80' b'\x00' b'\x02' b'\x80' b'x' b'\x00' b'\x80' 574904050003c0460f48c0002080000280780080

Maybe the above info will help."

Hopefully at some point we have the collective brains to solve this :)

Xtr102 commented 3 years ago

Hi There, I have done some digging around this issue and can contribute the following.

There is a swi32.dll (Summus Wavelet Image) assembly that was written in C++. This is presumably the SDK assembly that was supplied by the now defunct company.

I have stumbled on a header file for this, though that does not provide much info on usage. This MyBroadBand user recently posted a screen recording of using this assembly to decode the image, though it does not seem as straight forward as passing the correct bytes to the assembly.

I have managed to decompress the image bytes using this assembly, but that is where I am stuck. The decompressed bytes does not seem to be in a known format, or some additional processing is needed on the data. If I pass 597 compressed image bytes to the decompress method on this assembly I get back around 853 decompressed bytes, which does not seem correct.

If anyone has more insight into this it will be greatly appreciated.

Apologies if any of this was general knowledge already.

pjdupreez commented 3 years ago

I found this: https://pedump.me/7198b62559fb8155733a8b5c3b573e03/

I'd really like to try and see if I can reverse engineer it in order to get it working in either Dart, or Java (Android), or C#, but I have too much on my plate at the moment. Maybe someone else with more time could have a crack at it?

Xtr102 commented 3 years ago

I found this: https://pedump.me/7198b62559fb8155733a8b5c3b573e03/

I'd really like to try and see if I can reverse engineer it in order to get it working in either Dart, or Java (Android), or C#, but I have too much on my plate at the moment. Maybe someone else with more time could have a crack at it?

This is where I got the dll from. I wrote a C# wrapper for it. Seems to be working apart from the decompressed data as mentioned in my previous post.

pjdupreez commented 3 years ago

Mmm. Yeah the bytes you get out seems to be too little... the photo's should come back as 200Γ—250 pixels and should be around 50kb in size.

Have you tried doing it the other way round? Taking a photo and compressing it?

Just a crazy thought, might not work, but have you tried writing the bytes out to a bitmap?

Xtr102 commented 3 years ago

Mmm. Yeah the bytes you get out seems to be too little... the photo's should come back as 200Γ—250 pixels and should be around 50kb in size.

Have you tried doing it the other way round? Taking a photo and compressing it?

Just a crazy thought, might not work, but have you tried writing the bytes out to a bitmap?

Yes on both questions. Compressing seems to work if you fiddle with the compression quality etc. I have however not tried to take a known image and compress and decompress it again to see if it survives. Will try that next. I might also not be decrypting the last block correctly, though the other data is correct. Writing the decompressed bytes out to an image just produces garbage in the image.

Also, a decoded image from an online SADL decoding service results in a byte array of about 8272 bytes, so it seems that either the result of the decompress method does not need to return a 50k odd byte array or this is the size of the jpeg after some processing.

pjdupreez commented 3 years ago

Is it a 'garbage' image when you write to a bitmap, or is there garbage in the image? Can you make out a face, in other words?

Keep one thing in mind when compressing the image: have a look at the bytes it spits out, and see if the amount of compressed bytes are equal to the block that you are trying to decompress.

So in other words, if you feed it a 200Γ—250 image of 50kb and it compresses it to an X amount of bytes, see if that size matches whats in the block. Dont know if the dll resizes the image, as that may also have an effect on the output size πŸ€·β€β™‚οΈ. I dont know, I am just speculating, but I'd think that no matter what image (regardless of size and dimensions) to feed into it, it should spit out the same amount of compressed bytes each time?

Xtr102 commented 3 years ago

It's just noise in the image, no pattern or blurry face etc, just random pixels. The wavelet compression does not seem to generate the same amount of compressed bytes on subsequent runs. I just took a known decoded image and ran it through the compression at 30% quality, first run produced 943 compressed bytes and the second run produced 1564 bytes.

Trying to decompress that image passes the dll, though no way to determine the resulting size apart from the width and height. Reading this directly from memory results in another random garbage image.

pjdupreez commented 3 years ago

I have an hour open a bit later today. If you dont mind sending me a copy of what you've done, I'd like to play around with it?

Xtr102 commented 3 years ago

Yeah, I need to get back to my day job as well. I'll see if I can create a branch on this repo for testing. My current code is a very messy console app.

On second thought I'll make a repo available, this is written in Dart, so would take more to add my C# code in here.

pjdupreez commented 3 years ago

Messy console apps is where breakthroughs are made πŸ’ͺ

Xtr102 commented 3 years ago

Here is the simplified wrapper code. I still have a few things to test and play around with though.

In your test project, add the swi32.dll from PEDump and set the output to always copy. Configure the project for x86 as well.

using System;
using System.Runtime.InteropServices;

namespace SADL
{
    internal static class SWIWrapper
    {
        // Progressive
        public const int PR_NONE = 0;
        public const int PR_NORMAL = 1;
        public const int PR_FAST = 2;

        // Encoders
        public const int E_SLOW = 0;
        public const int E_NORMAL = 1;
        public const int E_FAST = 2;
        public const int E_FASTEST = 3;

        // EncodePaths
        public const int P_PATH1 = 0;
        public const int P_PATH2 = 1;
        public const int P_PATH3 = 2;

        // Split Block-Size Specifications
        public const int SP_OFF = 0;
        public const int SP_AUTO = -1;
        public const int SP_USER = -2;

        // Image Documentation Spec
        public const int SP_BKGRND = 0;
        public const int SP_LAST = -1;
        public const int SP_ALL = -2;

        // Compression Ratio Specifications
        public const int CR_QUALITY = 0;
        public const int CR_CLOSEST = 1;
        public const int CR_FLOOR = 2;

        public delegate int ByteIOFunc(IntPtr param0, ref int param1);
        public delegate int ScanlineIOFunc(IntPtr param0, ref WiImageBlock param1, int param2, ref IntPtr param3);
        public delegate int ExtensionIOFunc(IntPtr param0, int param1, ref IntPtr param2, ref int param3);

        [StructLayout(LayoutKind.Sequential)]
        public struct WiBox
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct WiImageBlock
        {
            public int Page;
            public int PageLeft;
            public int PageTop;
            public int PageWidth;
            public int PageHeight;

            public int Block;
            public int BlockLeft;
            public int BlockTop;
            public int BlockWidth;
            public int BlockHeight;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct WiRawImage
        {
            public IntPtr Raw;
            public int Height;
            public int Width;
            public int BitsPerPixel;
            public int Color;

            public int LevelHeight;
            public int LevelWidth;
            public IntPtr AppData;

            [MarshalAs(UnmanagedType.LPStr)]
            public string Comment;
            public int CommentLength;

            [MarshalAs(UnmanagedType.LPStr)]
            public string AppExtension;
            public int AppExtensionLength;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct WiCmpImage
        {
            public IntPtr CmpData;
            public int Size;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct WiCmpOptions
        {
            public float Quality;
            public float CmpRatio;
            public int CmpControl;

            public int Encoder;
            public int EncodePath;
            public int Progressive;

            public ByteIOFunc WriteNextByte;
            public IntPtr WriteParam;

            public int Magnification;
            public int EdgeEnhancement;
            public int ContrastEnhancement;

            public int FocusWeight;
            public IntPtr FocusBoxes;
            public int nBoxes;

            public int HighColorQuality;

            public ScanlineIOFunc ReadScanline;
            public IntPtr ReadScanlineParam;

            public int BlockSize;
            public IntPtr Blocks;
            public int nBlocks;
        }

        [StructLayout(LayoutKind.Sequential)]
        struct WiDecmpOptions
        {
            public int Smoothing;
            public int Fast;

            public ByteIOFunc ReadNextByte;
            public IntPtr ReadParam;

            public int Sharpening;

            public ScanlineIOFunc WriteScanLine;
            public IntPtr WriteScanLineParam;

            public WiImageBlock SubImage;

            public int Magnification;

            public ExtensionIOFunc WriteAppExtension;
            public IntPtr WriteAppExtensionParam;

        }

        internal static void Test(byte[] imageData)
        {
            var rawImagePointer = WiCreateRawImage();
            var cmpImagePointer = WiCreateCmpImage();
            var decmpOptionsPointer = WiCreateDecmpOptions();

            try
            {
                var cmpImage = new WiCmpImage();
                cmpImage.CmpData = Marshal.AllocHGlobal(imageData.Length);
                Marshal.Copy(imageData, 0, cmpImage.CmpData, imageData.Length);
                cmpImage.Size = imageData.Length;
                Marshal.StructureToPtr(cmpImage, cmpImagePointer, false);

                var result = WiDecompress(decmpOptionsPointer, rawImagePointer, cmpImagePointer);
                var rawImageResult = Marshal.PtrToStructure<WiRawImage>(rawImagePointer);

                var decompressedData = new byte[200 * 250];

                for (int offset = 0; offset < decompressedData.Length; offset++)
                {
                    decompressedData[offset] = Marshal.ReadByte(rawImageResult.Raw, offset);
                }

                var base64Image = Convert.ToBase64String(decompressedData);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                WiDestroyRawImage(rawImagePointer);
                WiDestroyCmpImage(cmpImagePointer);
                WiDestroyDecmpOptions(decmpOptionsPointer);
            }
        }

        internal static void TestCompressionOfKnownImage()
        {
            var rawImagePointer = WiCreateRawImage();
            var cmpImagePointer = WiCreateCmpImage();
            var decmpOptionsPointer = WiCreateDecmpOptions();
            var cmpOptionsPointer = WiCreateCmpOptions();

            try
            {
                // Known image
                var rawBuffer = Convert.FromBase64String("/9j/4AAQSkZJRgABAQIAJgAmAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QE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KP0W2UpH0AhZmabTnne7ekJZaD1SppJB/DEI07Q6KmbEsmjyQaPVsS6Np/DGINFo2meDbFJP/ALJr/wCsOGiWzbco0l2Vt+msrTjapuUbSR8iBH//2Q==");
                var rawImage = new WiRawImage();
                rawImage.BitsPerPixel = 8;
                rawImage.Width = 200;
                rawImage.Height = 250;
                rawImage.LevelWidth = 200;
                rawImage.LevelHeight = 250;
                rawImage.Raw = Marshal.AllocHGlobal(rawBuffer.Length);
                Marshal.Copy(rawBuffer, 0, rawImage.Raw, rawBuffer.Length);
                Marshal.StructureToPtr(rawImage, rawImagePointer, false);

                var cmpOptions = new WiCmpOptions();
                cmpOptions.Quality = 0.25f;
                Marshal.StructureToPtr(cmpOptions, cmpOptionsPointer, false);

                var compressResult = WiCompress(cmpOptionsPointer, rawImagePointer, cmpImagePointer);
                var resultImage = Marshal.PtrToStructure<WiCmpImage>(cmpImagePointer);

                var compressedData = new byte[resultImage.Size];

                for (int offset = 0; offset < resultImage.Size; offset++)
                {
                    compressedData[offset] = Marshal.ReadByte(resultImage.CmpData, offset);
                }

                var cmpImage = new WiCmpImage();
                cmpImage.CmpData = Marshal.AllocHGlobal(compressedData.Length);
                Marshal.Copy(compressedData, 0, cmpImage.CmpData, compressedData.Length);
                cmpImage.Size = compressedData.Length;
                Marshal.StructureToPtr(cmpImage, cmpImagePointer, false);

                var decompressResult = WiDecompress(decmpOptionsPointer, rawImagePointer, cmpImagePointer);
                var rawImageResult = Marshal.PtrToStructure<WiRawImage>(rawImagePointer);

                var decompressedData = new byte[200 * 250];

                for (int offset = 0; offset < decompressedData.Length; offset++)
                {
                    decompressedData[offset] = Marshal.ReadByte(rawImageResult.Raw, offset);
                }

                var base64RawImage = Convert.ToBase64String(decompressedData);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                WiDestroyRawImage(rawImagePointer);
                WiDestroyCmpImage(cmpImagePointer);
                WiDestroyCmpOptions(cmpOptionsPointer);
                WiDestroyDecmpOptions(decmpOptionsPointer);
            }
        }

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr WiCreateRawImage();

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr WiCreateCmpImage();

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr WiCreateDecmpOptions();

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr WiCreateCmpOptions();

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void WiDestroyRawImage(IntPtr RawImage);

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void WiDestroyCmpImage(IntPtr CmpImage);

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void WiDestroyCmpOptions(IntPtr CmpOptions);

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void WiDestroyDecmpOptions(IntPtr DecmpOptions);

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int WiDecompress(IntPtr WiDecmpOptions, IntPtr WiRawImage, IntPtr WiCmpImage);

        [DllImport("SWI32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int WiCompress(IntPtr WiCmpOptions, IntPtr WiRawImage, IntPtr WiCmpImage);
    }
}
Xtr102 commented 3 years ago

And here is the original header contents:

Apologies for pasting, github does not allow .h or .cs files.

/*=========================================================================
  swi32.h
  Header file for Wavelet Image 32-bit SDK

  Version: 4.00

  Created:  Dec 20, 1994
  Revised:  Jan 17, 1997

  Copyright (c) 1994-1997 by Summus, Ltd. All rights reserved.
==========================================================================*/

#ifndef _SWI32_H_
#define _SWI32_H_

/*
** Defines the calling conventions for different platforms. 
*/
#ifndef CALLSPEC
#  if (defined __WIN32__)||(defined _WIN32)||(defined __NT__)||\
      (defined __WINDOWS__)||(defined _Windows)
     /* For MS-Windows Platforms */
#    include <windows.h>
#    define CALLSPEC PASCAL
#  elif (defined __OS2__)                /* For OS/2 Platform */
#    include <os2.h>
#    define CALLSPEC APIENTRY
#  else                                  /* Other platforms(UNIX, MAC...) */
#    define CALLSPEC
#  endif
#endif /* CALLSPEC */

/*
** variable argument list styles
*/
#ifndef ARGSPEC
#  if (defined FUNCPROTO)||(__STDC__)||(defined __cplusplus)||\
      (defined __TURBOC__)     /* ANSI C, C++ */
#    define ARGSPEC(alist) alist
#  else                                                      /* C Classic */
#    define ARGSPEC(alist) ()
#  endif
#endif /* ARGSPEC */

/*
** Progressive
*/
#define PR_NONE   0
#define PR_NORMAL 1
#define PR_FAST   2

/*
** Encoders
*/
#define E_SLOW    0
#define E_NORMAL  1
#define E_FAST    2
#define E_FASTEST 3

/*
** EncodePaths
*/
#define P_PATH1   0
#define P_PATH2   1
#define P_PATH3   2

/*
** Split Block-Size Specifications
*/
#define SP_OFF    0
#define SP_AUTO   (-1)
#define SP_USER   (-2)

/*
** Image Documentation Spec
*/
#define SP_BKGRND  (0)
#define SP_LAST   (-1)
#define SP_ALL    (-2)

/*
** Compression Ratio Specifications
*/
#define CR_QUALITY        0
#define CR_CLOSEST        1
#define CR_FLOOR          2

/*
** Box structure (for focusing box)
*/
typedef struct
{
    int              Left;
    int              Top;
    int              Right;
    int              Bottom;
}WiBox;

/*
** Block structure
*/
typedef struct
{
    int              Page;
    int              PageLeft;
    int              PageTop;
    int              PageWidth;
    int              PageHeight;

    int              Block;
    int              BlockLeft;
    int              BlockTop;
    int              BlockWidth;
    int              BlockHeight;
}WiImageBlock;

/*
** Raw Image
*/
typedef struct
{
    unsigned char    *Raw;
    int              Height;
    int              Width;
    int              BitsPerPixel;
    int              Color;

    int              LevelHeight;
    int              LevelWidth;
    void             *AppData;

    unsigned char    *Comment;
    int              CommentLength;

    unsigned char    *AppExtension;
    int              AppExtensionLength;
}WiRawImage;

/*
** Compressed Image
*/
typedef struct
{
    unsigned char    *CmpData;
    int              Size;
}WiCmpImage;

/*
** byte I/O function pointer
*/
typedef int (*ByteIOFunc)(void*, int*);

/*
** image row I/O function pointer
*/
typedef int (*ScanlineIOFunc)(void*, WiImageBlock*, int, unsigned char**);

/*
** image extension I/O function pointer
*/
typedef int (*ExtensionIOFunc)(void*, int, unsigned char**, int*);

/*
** Compression options
*/
typedef struct{
    float            Quality;
    float            CmpRatio;
    int              CmpControl;

    int              Encoder;
    int              EncodePath;
    int              Progressive;

    ByteIOFunc       WriteNextByte;
    void             *WriteParam;

    int              Magnification;
    int              EdgeEnhancement;
    int              ContrastEnhancement;

    int              FocusWeight;
    WiBox            *FocusBoxes;
    int              nBoxes;

    int              HighColorQuality;

    ScanlineIOFunc   ReadScanline;
    void             *ReadScanlineParam;

    int              BlockSize;
    WiBox            *Blocks;
    int              nBlocks;
}WiCmpOptions;

/*
** Decompression Options
*/
typedef struct
{
    int              Smoothing;
    int              Fast;

    ByteIOFunc       ReadNextByte;
    void             *ReadParam;

    int              Sharpening;

    ScanlineIOFunc   WriteScanline;
    void             *WriteScanlineParam;

    WiImageBlock     SubImage;

    int              Magnification;

    ExtensionIOFunc  WriteAppExtension;
    void             *WriteAppExtensionParam;
}WiDecmpOptions;

/*
** Obsolete data structures. They will be removed in future releases.
*/
typedef struct
{
    int              left;
    int              top;
    int              right;
    int              bottom;
}SiBox;

typedef struct
{
    unsigned char    *Raw;
    int              Height;
    int              Width;
    int              BitsPerPixel;
    int              color;

    int              LevelHeight;
    int              LevelWidth;
    void             *AppData;
}SiImageInfo;

typedef struct
{
    unsigned char    *CmpData;
    int              size;
}SiCmpDataInfo;

typedef struct{
    float            Quality;
    float            CmpRatio;
    int              AutoRatio;

    int              Encoder;
    int              EncodePath;
    int              Progressive;

    ByteIOFunc       WriteNextByte;
    void             *WriteParam;

    int              Magnification;
    int              EdgeEnhancement;
    int              ContrastEnhancement;

    int              FocusWeight;
    SiBox            *FocusBoxes;
    int              nBoxes;
}SiCmpOption;

typedef struct
{
    int              Smoothing;
    int              Fast;

    ByteIOFunc       ReadNextByte;
    void             *ReadParam;

    int              Sharpening;

    ScanlineIOFunc   WriteScanline;
    void             *WriteScanlineParam;
}SiDecmpOption;

/*
** entry points
*/
#ifdef __cplusplus
extern "C"{
#endif

#if (defined __MC68K__)||(defined __POWERPC__)
#  if (__MC68K__)||(__POWERPC__) /* For mac platforms */
#    pragma export on
#  endif
#endif

WiRawImage* CALLSPEC WiCreateRawImage ARGSPEC((
        void
    ));

WiCmpImage* CALLSPEC WiCreateCmpImage ARGSPEC((
        void
    ));

WiCmpOptions* CALLSPEC WiCreateCmpOptions ARGSPEC((
        void
    ));

WiDecmpOptions* CALLSPEC WiCreateDecmpOptions ARGSPEC((
        void
    ));

void CALLSPEC WiDestroyRawImage ARGSPEC((
        WiRawImage        *RawImage
    ));

void CALLSPEC WiDestroyCmpImage ARGSPEC((
        WiCmpImage        *CmpImage
    ));

void CALLSPEC WiDestroyCmpOptions ARGSPEC((
        WiCmpOptions      *CmpOptions
    ));

void CALLSPEC WiDestroyDecmpOptions ARGSPEC((
        WiDecmpOptions    *DecompOptions
    ));

int CALLSPEC WiCompress ARGSPEC((
        WiCmpOptions      *CmpOptions,
        WiRawImage        *RawImage,
        WiCmpImage        *CmpImage
    ));

int CALLSPEC WiDecompress ARGSPEC((
        WiDecmpOptions    *DecmpOptions,
        WiRawImage        *RawImage,
        WiCmpImage        *CmpImage
    ));

int CALLSPEC WiBeginDecompress ARGSPEC((
        WiDecmpOptions    *DecmpOptions,
        WiRawImage        *RawImage,
        WiCmpImage        *CmpImage
    ));

void CALLSPEC WiEndDecompress ARGSPEC((
        WiDecmpOptions    *DecmpOptions,
        WiRawImage        *RawImage,
        WiCmpImage        *CmpImage
    ));

int CALLSPEC WiDecompressSubHeader ARGSPEC((
        WiDecmpOptions    *DecmpOptions,
        WiRawImage        *RawImage,
        WiCmpImage        *CmpImage
    ));

int CALLSPEC WiDecompressSubImage ARGSPEC((
        WiDecmpOptions    *DecmpOptions,
        WiRawImage        *RawImage,
        WiCmpImage        *CmpImage
    ));

int CALLSPEC WiGetProgressiveImage ARGSPEC((
        WiRawImage        *RawImage
    ));

void CALLSPEC WiFreeRawImageData ARGSPEC((
        WiRawImage        *RawImage
    ));

void CALLSPEC WiFreeCmpImageData ARGSPEC((
        WiCmpImage        *CmpImage
    ));

int CALLSPEC WiInsertCmpImage ARGSPEC((
        WiImageBlock      *BlockIndex,
        WiCmpImage        *SrcCmpImage,
        WiCmpImage        *InsCmpImage,
        WiCmpImage        *DesCmpImage
    ));

/*
** Obsolete functions. They will be removed in future releases.
*/

int CALLSPEC SiCompress ARGSPEC((
        SiImageInfo       *RawImage,
        SiCmpOption       *CmpOptions,
        SiCmpDataInfo     *CmpImage
    ));

int CALLSPEC SiDecompress ARGSPEC((
        SiCmpDataInfo     *CmpImage,
        SiDecmpOption     *DecmpOptions,
        SiImageInfo       *RawImage
    ));

int CALLSPEC SiGetImageInfo ARGSPEC((
        SiImageInfo       *ImageInfo
    ));

void CALLSPEC SiFreeImageInfo ARGSPEC((
        SiImageInfo       *ImageInfo
    ));

void CALLSPEC SiFreeCmpDataInfo ARGSPEC((
        SiCmpDataInfo     *CmpDataInfo
    ));

#if (defined __MC68K__)||(defined __POWERPC__)
#  if (__MC68K__)||(__POWERPC__)
#    pragma export off
#  endif
#endif

#ifdef __cplusplus
}
#endif

#endif /* _SWI32_H_ */
pjdupreez commented 3 years ago

Great stuff, thanks. I'll hack away at this.

pjdupreez commented 3 years ago

I had a look at the code and played around a bit. Specifically feeding it a photo, compressing it, and then decompressing it to see if I can get the same image out.

No luck.

What I have seen, though, is if you set the size of the byte[] for the decompressed to the same size as the input image byte[], the resultant bytes seem very close to the original. I cant figure out where (or if) decompress options should be used.

I also used a dll decompiler on the swi32.dll to get to the C code. My idea is to try and see how the compression/decompression is done, then there should be no need for the file. But I'll have to do this some other time.

If you are feeling adventurous, download Snowman: https://derevenets.com/ and feed it the swi32.dll

Xtr102 commented 3 years ago

I have not tried reading the input size from the decompressed array, I have tried a few different variations on the structures, ultimately ended up using pointers and reading the memory directly.

When compressing an image it provides a nice size and pointer to work with. Decompressing would be more complex as one could feed it an image using different bit depths etc.

I actually found the header file on an old Nasa repo that is open on a university server. Looks like they used Summus to compress images taken by a rover. I did not find any examples of where they actually consume the decompression though.

There are decompress options, but I have not been able to spend time on that. I played around with the WriteScanLine callback. Seems like we should be able to either intercept where each scanline point to in memory, or it expects us to override the logic.

Re-writing the logic would be first prize. I also tried to decompile the dll, but the closest I got was getting assembler code using dumpbin, seemed like a lot to read through...

I will definitely give Snowman a go, never heard of it, but if it can spit out C code we should be good to go.

Thanks for your help, I'm sure we can figure this out.

pjdupreez commented 3 years ago

Awesome. Yeah, Snowman was the only decompiler that give C/C++ output, albeit a bit difficult to read without providing propper names for variables and methods. I just quickly had a look at it. Hope it may be of use.

Yip. We'll get this eventually

gertdreyer commented 2 years ago

This has been a pet project of mine for a while now too. I have started to reverse engineer the DLL with Ghidra but its matrix maths is done with basic instructions.

I got the DLL to work on Linux with a shim library and wrote a Rust wrapper for it. https://github.com/gertdreyer/wi_decode

pjdupreez commented 2 years ago

I'd like to give it a try at some point. Well done!

Now, the next thing is, how the heck would one get it to be a 64 bit library πŸ˜…

Wackymax commented 2 years ago

Hi, was there any more progress made on this? I'd like to help out if I can

the-mars-rover commented 2 years ago

Hi Hennie :-)

Thanks so much for reaching out. Unfortunately, I don’t have capacity available to continue working on this. If you’d like, I can transfer the repo to you?

On 31 Aug 2022, at 12:42, Hennie Brink @.***> wrote:

Hi, was there any more progress made on this? I'd like to help out if I can

β€” Reply to this email directly, view it on GitHub https://github.com/the-mars-rover/rsa_identification/issues/2#issuecomment-1232771594, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANRVYXDRACCVWC35DYL7JBDV34ZHXANCNFSM4TOCB3GA. You are receiving this because you authored the thread.

gertdreyer commented 2 years ago

Hi Hennie,

If you are brave, I can give you access to a private repo where I have started decompiling the DLL with Ghidra and fixing the decompiled C that it compiles again and works when my functions replace the internal DLL function calls with hooks. I am using taviso/loadlibrary for hooking the DLL. I am not actively working on this at the moment, however, I suspect it has about half of the functions needed to do a build without hooks...

If you just want to decode the image on a server see gertdreyer/wi_decode.

Wackymax commented 2 years ago

@the-mars-rover I'm not sure I want to take on that responsibility right now πŸ˜…. I will try and make pull requests if I do manage to figure out something.

@gertdreyer Yeah I am just looking to decode the image and it seems like a server side approach would be the easiest to go with right now. Thanks for the link, I will have a look at that.

pjdupreez commented 2 years ago

I am keen to have a go. I have 2 project that use your library to decode and get the info. I have forked it as I am looking at getting it upgraded to Flutter 3 for my projects.

Regards,

On Wed, 31 Aug 2022, 13:10 Hennie Brink, @.***> wrote:

@the-mars-rover https://github.com/the-mars-rover I'm not sure I want to take on that responsibility right now πŸ˜…. I will try and make pull requests if I do manage to figure out something.

@gertdreyer https://github.com/gertdreyer Yeah I am just looking to decode the image and it seems like a server side approach would be the easiest to go with right now. Thanks for the link, I will have a look at that.

β€” Reply to this email directly, view it on GitHub https://github.com/the-mars-rover/rsa_identification/issues/2#issuecomment-1232796400, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH4NCDTMB4GQXBVLOEEUDDV344R3ANCNFSM4TOCB3GA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

saguran commented 1 year ago

Hi, I have a project where we need to decode the image in the barcode (seems like everyone is having that issue πŸ˜‚). Willing to help wherever I can. Let me know.

Going to try your package @the-mars-rover in Flutter, as it's already a good starting point for us. The other packages we have tried struggle with the driver's license specifically.

Businessware commented 1 year ago

Hi, thanks for all the contributions so far. I have provided a C# codec and encode/decode apps that wraps the Summus SWI32.dll. The codec works fine for encoding followed by decoding known images, but still fails when decoding the SADL WI image. The codec and interop code implements the new "Wi" prefixed methods API, as well as the obsolete "Si" methods API - both API's seem to work the same (was hoping that the old API would magically decode the SADL image, but sadly -no!) The source code and further comments are here:

https://github.com/Businessware/Swi

Please feel free to use the code any which way.

Best regards, Willem Fourie