private void drawImage(Image source, int x, int y, String mapName) {
        if (images == null) {
            images = new HashMap<String, byte[]>();
        }
        byte[] image = images.get(mapName);
        
        //FIXME this is not accounting for color space.
        //Texture Atlas should linearize the data if the source image isSRGB        
        if (image == null) {
            image = new byte[atlasWidth * atlasHeight * 4];
            images.put(mapName, image);
        }
        //TODO: all buffers?
        ByteBuffer sourceData = source.getData(0);
        int height = source.getHeight();
        int width = source.getWidth();
        Image newImage = null;
        for (int yPos = 0; yPos < height; yPos++) {
            for (int xPos = 0; xPos < width; xPos++) {
                int i = ((xPos + x) + (yPos + y) * atlasWidth) * 4;
                if (source.getFormat() == Format.ABGR8) {
                    int j = (xPos + yPos * width) * 4;
                    image[i] = sourceData.get(j); //a
                    image[i + 1] = sourceData.get(j + 1); //b
                    image[i + 2] = sourceData.get(j + 2); //g
                    image[i + 3] = sourceData.get(j + 3); //r
                } else if (source.getFormat() == Format.BGR8) {
                    int j = (xPos + yPos * width) * 3;
                    image[i] = 1; //a
                    image[i + 1] = sourceData.get(j); //b
                    image[i + 2] = sourceData.get(j + 1); //g
                    image[i + 3] = sourceData.get(j + 2); //r
                } else if (source.getFormat() == Format.RGB8) {
                    int j = (xPos + yPos * width) * 3;
                    image[i] = 1; //a
                    image[i + 1] = sourceData.get(j + 2); //b
                    image[i + 2] = sourceData.get(j + 1); //g
                    image[i + 3] = sourceData.get(j); //r
                } else if (source.getFormat() == Format.RGBA8) {
                    int j = (xPos + yPos * width) * 4;
                    image[i] = sourceData.get(j + 3); //a
                    image[i + 1] = sourceData.get(j + 2); //b
                    image[i + 2] = sourceData.get(j + 1); //g
                    image[i + 3] = sourceData.get(j); //r
                } else if (source.getFormat() == Format.Luminance8) {
                    int j = (xPos + yPos * width) * 1;
                    image[i] = 1; //a
                    image[i + 1] = sourceData.get(j); //b
                    image[i + 2] = sourceData.get(j); //g
                    image[i + 3] = sourceData.get(j); //r
                } else if (source.getFormat() == Format.Luminance8Alpha8) {
                    int j = (xPos + yPos * width) * 2;
                    image[i] = sourceData.get(j + 1); //a
                    image[i + 1] = sourceData.get(j); //b
                    image[i + 2] = sourceData.get(j); //g
                    image[i + 3] = sourceData.get(j); //r
                } else {
                    //ImageToAwt conversion
                    if (newImage == null) {
                        newImage = convertImageToAwt(source);
                        if (newImage != null) {
                            source = newImage;
                            sourceData = source.getData(0);
                            int j = (xPos + yPos * width) * 4;
                            image[i] = sourceData.get(j); //a
                            image[i + 1] = sourceData.get(j + 1); //b
                            image[i + 2] = sourceData.get(j + 2); //g
                            image[i + 3] = sourceData.get(j + 3); //r
                        }else{
                            throw new UnsupportedOperationException("Cannot draw or convert textures with format " + source.getFormat());
                        }
                    } else {
                        throw new UnsupportedOperationException("Cannot draw textures with format " + source.getFormat());
                    }
                }
            }
        }
    }
Add a texture for a specific map name at the location of another existing texture (on the master map).
@param texture A texture to add to the atlas.
@param mapName A freely chosen map name that can be later retrieved as a Texture.
@param sourceTextureName Name of the master map used for the location.
jme3-core/src/tools/java/jme3tools/optimize/TextureAtlas.java