Page 1 of 1

Swap colour channels via API

Posted: 2015-02-15T23:31:56-07:00
by PBert
Hi Folks,

I'm unsure how to swap colour channels using the api. Searching Google brings up command line -swap based topics, but nothing using the API (can't find any swap() function?). Reason is for rearranging order of RGBA to ARGB. Don't need any pixel colour changes, just colour channel swaps.

EDIT: just a thought, is it possible to arrange the colour channels from the get go? So when images are loaded, their loaded in ARGB format?

Cheers,

PB.

Re: Swap colour channels via API

Posted: 2015-02-16T01:50:04-07:00
by dlemstra
Why do you need to rearrange the channels? Are you writing them to your own output format?

Re: Swap colour channels via API

Posted: 2015-02-16T02:46:29-07:00
by PBert
Hi there dlemstra,

nope. I'm trying to point another api's image object reference to the IM pixel cache. They both use the same data type. The other object isn't trying to take over ownership either - it just copies the container data in the order it's given.

I can see thus far that it's 'working'. For instance, I can see the image in my picture viewer which is a display of the other api's image object. But the blue channel has no data - and this data looks like what the alpha values would be. Another reason why I think it might be the channel order is that the other api uses image flags with "..._ARGB_..." in it, which might be a sign of channel ordering too. So, two loose reasons why I was looking into the channel ordering.

I don't have any control over the other api's image channel orders - it simply isn't an image processing package. It comes with enough to display an image, and do some basic tasks. But IM really needs to be the initial image processing engine. I'm also trying to avoid time costly for loops iterating over pixel-by-pixel as the other api's pixel-by-pixel routines, I've been told, are slow.

Of course, I could be wrong and it might be something else - but thought the pixel channel ordering might be worth a shot given what I can see at this stage.

Cheers,

PB.

EDIT: spelling/grammar update

Re: Swap colour channels via API

Posted: 2015-02-16T03:28:19-07:00
by snibgo
To test your theory about swapping channels, you can easily create an image with channels changed. For example:

Code: Select all

convert xc:rgba(10%,20%,30%,0.5) -channel RGBA -separate -insert 0 -combine txt:
This creates a 1x1 image, puts the alpha channel in the first position, and writes the image in text form. You could do this for a real image:

Code: Select all

convert in.png -channel RGBA -separate -insert 0 -combine out.png
I expect you could do this in an API, but I don't know how.

Re: Swap colour channels via API

Posted: 2015-02-16T04:07:25-07:00
by PBert
Hi snibgo,

those code snippets look familiar - probably like what I've seen in other topics/searches. Like you though, I'm not sure how to transfer the command line into an api function call(s)!

PB.

Re: Swap colour channels via API

Posted: 2015-02-18T05:02:06-07:00
by PBert
Just a thought for anyone who might know - is it possible to do an iter_swap (or something similar) on any of the data containers that might act as a channel swap? Preferably on the pixel cache?

PB.

Re: Swap colour channels via API

Posted: 2015-02-18T10:29:19-07:00
by snibgo
You haven't said what API you want to use. It's easy at the MagickCore level. I don't use MagickWand, but I think that rearranging the list in a wand means creating a new wand, and inserting that into the existing wand.

Re: Swap colour channels via API

Posted: 2015-02-18T20:35:14-07:00
by PBert
Using c++ API - namespace Magick, version looks like 6.8.5, released on the 24-4-2013. Package name ImageMagick.

Re: Swap colour channels via API

Posted: 2015-02-18T21:29:59-07:00
by snibgo
Magick++\lib\Magick++\Image.h contains separate().

wand\magick-image.h contains MagickCombineImages.

For "-insert 0", at the MagickCore level I expect this could be done with RemoveLastImageFromList() and PrependImageToList(). (See magick\list.c, also see wand\mogrify.c)

Offhand, I don't know how these would translate into C++.