public Map groupColors1(BufferedImage image, int max, int mask)
            throws ImageWriteException
    {
        Map color_map = new HashMap();

        int width = image.getWidth();
        int height = image.getHeight();

        int row[] = new int[width];
        for (int y = 0; y < height; y++)
        {
            image.getRGB(0, y, width, 1, row, 0, width);
            for (int x = 0; x < width; x++)
            {
                int argb = row[x];

                if (ignoreAlpha)
                    argb &= 0xffffff;
                argb &= mask;

                ColorCount color = (ColorCount) color_map
                        .get(new Integer(argb));
                if (color == null)
                {
                    color = new ColorCount(argb);
                    color_map.put(new Integer(argb), color);
                    if (color_map.keySet().size() > max)
                        return null;
                }
                color.count++;
            }
        }

        return color_map;
    }