Raster Palette Manipulations Example
From fmepedia
| Table of contents |
Introduction
Currently (mid-Summer of 2009, during FME 2010 development phase), we don't have any good tool for manipulating raster palettes. However, sometimes there can be a need in accessing and editing them.
We have two transformers that allow us extract and add palettes to/from an attribute - RasterPaletteExtractor and RasterPaletteAdder.
Let's take a simple raster with several distinguishable colors:
After RasterPaletteExtractor we get an attribute that looks as follows:
or, if we copy it into a text editor:
RGBA32 0 0,0,0,0 1 127,127,127,255 2 25,25,25,255 3 0,0,127,255 4 0,0,255,255 5 0,127,255,255 6 0,255,255,255 7 0,255,0,255 8 127,255,0,255 9 127,255,127,255 10 255,0,255,255 11 255,127,255,255 12 255,0,0,255 13 255,127,0,255 14 255,127,127,255 15 229,229,229,255
The first line tells the interpretation of the raster palette, in this case, it's RGBA24, and all other lines start with key value followed by the color expressed according to interpretation, that is, first three numbers define red, green, and blue, and the fourth number controls alpha values and sets transparency.
Now imagine, we would like to replace pure blue color 0,0,255 (located in second row from the bottom, third column) with pure yellow, which is 255,255,0. In our palette, it is the key number 4 (bold).
There are two ways we can manipulate it. First, we can write the attribute to a text file, change it there, and apply a new version of the palette to the raster. Or, second way, we can try to work with the attribute directly within Workbench.
Editing Palette in Text Editor
This scenario requires two workspaces. The first one extracts the palette (RasterPaletteExtractor) and saves the attribute to a text file (AttributeFileWriter):
The text file can be opened in a text editor, and the line 6 should should be changed from 4 0,0,255,255 to 4 255,255,0,255.
The last step is to apply our modified palette to the source raster. To do so, with the second workspace, we read the palette from text file to an attribute (AttributeFileReader), remove the old palette (RasterPaletteRemover) and add the new palette from the attribute (RasterPaletteAdder):
This gives us a new raster looking as the old one with a single color changed from blue to yellow:
Changing Palette with Workbench
The previous solution does not look very nice - we have to make two workspaces or make changes to a single workspace between steps:
image '
We can try to modify the attribute directly in the workspace. With [[AttributeSplitter]] we can get a list of separate palette key entries:
_list{0} = RGBA32
_list{1} = 0 0,0,0,0
_list{2} = 1 127,127,127,255
_list{3} = 2 25,25,25,255
_list{4} = 3 0,0,127,255
_list{5} = 4 0,0,255,255
........
In order to get an access to individual list elements, we have to expose them - right click on the list attribute of AtributeSplitter -> Expose Elements:
In the dialog, we have to enter the number of elements we would like to see - in our case it should be 6. This will expose list elements up to _list{5}, which corresponds with the palette key 4.
Now we can directly set the new value for the attribute _list{5} to 4 255,255,0,255 with AttributeSetter.
With the next step, we combine with ListConcatenator list elements back into a single string attribute:
RGBA32 0 0,0,0,0 1 127,127,127,255 2 25,25,25,255 3 0,0,127,255 4 255,255,0,255 5 0,127,255,255...
The rest is the same as in the first scenario - RasterPaletteRemover helps to get rid from the old palette, and RasterPaletteAdder applies our new palette to the raster:
Conclusion
Which way is better depends on your needs in palette editing. Complex, conditional changes to big palettes may be quite difficult within workbench - visual aid of a text editor might be a better option than a long chain of transformers. On the other hand, regular production tasks may be worth placing all the modification into a single process.
Besides, you may find that adding a separate transformer allowing direct manipulations with palettes would be useful. If so, let us know what you think, we always like to hear from you. You can contact me directly at mailto:dmitri.bagh@safe.com
Workspaces and Data
Workspace 1 to extract and save a palette to text
Workspace 2 to load and apply modified palette
