Swap colour channels via API

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
PBert
Posts: 11
Joined: 2014-03-06T04:34:54-07:00
Authentication code: 6789

Swap colour channels via API

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Swap colour channels via API

Post by dlemstra »

Why do you need to rearrange the channels? Are you writing them to your own output format?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
PBert
Posts: 11
Joined: 2014-03-06T04:34:54-07:00
Authentication code: 6789

Re: Swap colour channels via API

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Swap colour channels via API

Post 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.
snibgo's IM pages: im.snibgo.com
PBert
Posts: 11
Joined: 2014-03-06T04:34:54-07:00
Authentication code: 6789

Re: Swap colour channels via API

Post 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.
PBert
Posts: 11
Joined: 2014-03-06T04:34:54-07:00
Authentication code: 6789

Re: Swap colour channels via API

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Swap colour channels via API

Post 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.
snibgo's IM pages: im.snibgo.com
PBert
Posts: 11
Joined: 2014-03-06T04:34:54-07:00
Authentication code: 6789

Re: Swap colour channels via API

Post by PBert »

Using c++ API - namespace Magick, version looks like 6.8.5, released on the 24-4-2013. Package name ImageMagick.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Swap colour channels via API

Post 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++.
snibgo's IM pages: im.snibgo.com
Post Reply