2

I have image source in XML format (the image size is variable therefore I will need to change the image placeholder size). The image source is individual pixel data written in HEX format as string. It is grayscale BMP image data (without header). I would like to know how to work with the data in order to be able to convert the text HEX pixel data into an image to be displayed in MacOS app using Xcode and Swift.

I have achieved similar thing years back using Action Script for AIR app.

The image source can look like:

<Signature t="8209">000200010000000203435A4B03000000C8010205410....

So first thing is to convert the text HEX string into actual binary data, in Action Script I have used this code (part of cycle to read the whole known length of image data):

var pixelData:uint = parseInt(imgdata.substr((j-1)*2,2),16);

In this example, part of the "signature" XML key is also information about the actual image size, so I would like to be able to change the image size based on that data and be safe not to over/under run the byte stream length needed for the actual image.

So the whole looping through the image pixel data and constructing byte array with alpha, red, green, blue values looked like:

var bx2:ByteArray = new ByteArray();
if(img2_w!=0){
  for (var j:Number = 0; j <imgdata.length / 2; j++)
  {
    var pixelData:uint = parseInt(imgdata.substr((j-1)*2,2),16);
    bx2.writeByte(255);
    bx2.writeByte(pixelData);
    bx2.writeByte(pixelData);
    bx2.writeByte(pixelData);
  }
}

then it was about linking the byte array image data to the image itself:

var b_image1:ByteArray = new ByteArray();
var bmd_image1:BitmapData = new BitmapData(img1_w,img1_h, false, 0xFFCC00);

//getting the parsed XML data off selected item in the displayed grid - P6Grid
b_image1 = sigData[P6Grid.selectedIndex].sig1;
b_image1.position=0;

bmd_image1.setPixels(bmd_image1.rect,b_image1);

//reference to the visual component placed on the UI
img_sn1.source = bmd_image1;

Would anyone know how to do this using Swift and Xcode on MacOS app. Currently I am using sotory boards and figured out how to handle the file drop, check if that is XML document and parse the key values, so I can get to the XML data, but don't know how to turn them into the image.

Many thanks for your thoughts,

JendaDH

JendaDH
  • 21
  • 2
  • You might be interested in: https://stackoverflow.com/a/24139701/3141234 – Alexander Nov 04 '21 at 19:06
  • You'll want to take this string, hex-decode it into `Data`, and do your operations on that. Don't use `String` for the look-ups, because they're built for unicode/human correctness and not performance. Reading into a specific integer index of a string is a linear time operation, whereas Data does it in constant time. It'll probably be easiest to just generate a bitmap header file, prepend it to the contents, and use conventional bitmap parsing tools to make it into an NSImage/CGImage – Alexander Nov 04 '21 at 19:07
  • Hi Alexander, thanks for the hint, I will look into it. The BMP header can indeed be prepended to the data, will check this. – JendaDH Nov 05 '21 at 11:34

0 Answers0