0

For my project, I'm able to print textures on objects. As soon I use nicer textures that use a color palette higher than 256 it will turn black or invisible...

Is anyone able to help me with this issue? Right now this is my code to transfer the .png into a useable texture:

public static Background getIndexedImage(int id, File file) throws IOException {

    BufferedImage image = ImageIO.read(file);

    List<Integer> paletteList = new LinkedList<>();
    paletteList.add(0);
    int width = image.getWidth();
    int height = image.getHeight();
    byte[] pixels = new byte[width * height];
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int rgb = image.getRGB(x, y);
            int red = rgb >> 16 & 0xff;
            int green = rgb >> 8 & 0xff;
            int blue = rgb & 0xff;
            int alpha = rgb & 0xff;
            rgb = red << 16 | green << 8 | blue;
            if (alpha == 255) {
                rgb = 0;
            }
            int index = paletteList.indexOf(rgb);
            if (index == -1) {

                if (paletteList.size() < 256) {
                    index = paletteList.size();
                    paletteList.add(rgb);
                } else {
                    throw new IllegalArgumentException("The target image has more than 255 color in the palette "+id);
                }
            }
            pixels[x + y * width] = (byte) index;
        }
    }
    int[] palette = new int[paletteList.size()];

    final AtomicInteger index = new AtomicInteger(0);


    for (int pallet = 0; pallet < paletteList.size(); pallet++) {
        palette[index.getAndIncrement()] = paletteList.get(pallet);
    }
    return new Background(width, height, palette, pixels);
}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • "higher than 256" you're using 256 "locations" four times, or 4 sets of 8 bits, for a 32-bit pixel format. Why do you want values higher than 255? `255,255,255,255` == full white, and `0,0,0,255` == full black. – Rogue May 21 '21 at 00:52
  • to handle better detailed textures – Tiddo May 21 '21 at 05:42
  • I do not understand your question. Could you describe more the problem you want to solve? You can use any convention internally. In fact many programs that handle photos uses floats or 16-bit per channel colours (as working colour space). Finally you must convert back to something user can see (not many people have screens that can do 10-bit per channel [and they have problems: many programs doesn't understand such colours, but professional programs for photo/graphics/etc.). – Giacomo Catenazzi May 21 '21 at 09:40

0 Answers0