-2

我正在写一个ImageEncoderTGA图像的东西。我已经能够成功地编写文件,但是我得到TGA的不是最终得到的是相关代码:[RRRRRGGGGGBBBBBA][RGBBBBBA]

int lastRow = minY + height;
for (int row = minY; row < lastRow; row += 8) {
    int rows = Math.min(8, lastRow - row);
    int size = rows * width * numBands;

    // Grab the pixels
    Raster src = im.getData(new Rectangle(minX, row, width, rows));
    src.getPixels(minX, row, width, rows, pixels);

    for (int i = 0; i < size; i++) {
        //output.write(pixels[i] & 0xFF);
        //corrected
        //before conversion (source image) pixel in RGBA8888 format.
        int o = (int) pixels[i];
        //Need to convert here...
        short converted = ?;
        //need to write out RGB5551
        output.write(converted);
    }
}

为了澄清我要完成的工作......我有一个 png 格式的源图像,颜色深度是 RGBA8888。我需要将此图像转换为颜色深度为 RGBA5551 的 tga 格式。上面的 for 循环是我访问单个像素的地方。所以我要问的是:如何正确读取 32 位整数(RGBA8888)并将其转换为 16 位短整数(RGBA5551)?

4

2 回答 2

1

不幸的是,没有人能够帮助我解决这个问题,但我想与社区分享答案。

 private void writeoutFromPNGFile(int width, int numBands, int minY, int height,         RenderedImage im, int minX) throws IOException {
    System.out.println("Writing from PNG data.");
    //convertTo2DWithoutUsingGetRGB(im);
    //now start writing the file...
    int[] pixels = new int[8 * width * numBands];

    int count = 0;

    // Process 8 rows at a time so all but the last will have
    // a multiple of 8 pixels.  This simplifies PBM_RAW encoding.
    int lastRow = minY + height;
    for (int row = minY; row < lastRow; row += 8) {
        int rows = Math.min(8, lastRow - row);
        int size = rows * width * numBands;

        // Grab the pixels
        Raster src = im.getData(new Rectangle(minX, row, width, rows));
        src.getPixels(minX, row, width, rows, pixels);

        for (int i = 0; i < size; i += 4) {
            //output.write(pixels[i] & 0xFF);
            int red = pixels[i];
            int green = pixels[i + 1];
            int blue = pixels[i + 2];
            int alpha = pixels[i + 3];
            //System.out.println("Pixel Value: " + o);
            convertTo5551(red, green, blue, alpha);
        }

    }
}


int convertTo5551(int r, int g, int b, int a) throws IOException {
    int r5 = r * 31 / 255;
    int g5 = (int) g * 31 / 255;
    int b5 = (int) b * 31 / 255;
    int a1 = (a > 0) ? 0 : -1;
    int rShift = (int) r5 << 11;
    int bShift = (int) g5 << 6;
    int gShift = (int) b5 << 1;
    // Combine and return
    int abgr5551 = (int) (bShift | gShift | rShift | a1);
    output.write(new BigDecimal((abgr5551) & 0xFF).byteValue());
    output.write(new BigDecimal((abgr5551 >> 8) & 0xFF).byteValue());
    return abgr5551;
}

颜色是使用线性数学转换的——对于好的结果来说不是一个好主意。真正应该使用 ImageMagick 之类的程序来减少颜色。

于 2013-10-01T12:36:01.370 回答
0

由于 Raster 知道它自己的来源(矩形),我希望阅读src.getPixels( 0, 0, width,rows,pixels). 不过没试过。

于 2013-09-29T09:13:16.517 回答