我正在将C ++程序翻译成Java。有一段代码编写一个BMP文件。而且我无法理解fwrite(&outPixels[0], infoHeader.biSizeImage, 1, file);
行。如果仅使用vector的第0个元素(该行不在任何循环中),它将如何写入整个BMP文件?
typedef std::array<uint8, 3> TPixelBGRU8;
std::vector<TPixelBGRU8> outPixels;
outPixels.resize(c_numPixels);
for (size_t i = 0; i < c_numPixels; ++i) {
const TPixelRGBF32& src = g_pixels[i];
TPixelBGRU8& dest = outPixels[i];
dest[0] = uint8(Clamp(correctedPixel[2] * 255.0f, 0.0f, 255.0f));
dest[1] = uint8(Clamp(correctedPixel[1] * 255.0f, 0.0f, 255.0f));
dest[2] = uint8(Clamp(correctedPixel[0] * 255.0f, 0.0f, 255.0f));
}
//
// setting a header info
//
FILE *file;
fopen_s(&file, fileName, "wb");
fwrite(&outPixels[0], infoHeader.biSizeImage, 1, file);
这就是我所做的:
// Vector is my own class like array<uint8, 3>
List<Vector> outPixel = new ArrayList<>(numPixels);
for (int i = 0; i < numPixels; i++) {
Vector src = pixels.get(i);
Vector dest = outPixel.get(i);
dest.setX((int) clamp(correctedPixel.getZ() * 255.0, 0.0, 255.0));
dest.setY((int) clamp(correctedPixel.getY() * 255.0, 0.0, 255.0));
dest.setZ((int) clamp(correctedPixel.getX() * 255.0, 0.0, 255.0));
}
BufferedImage img = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_BGR);
// I suppose I did it wrong:
for (int i = 0; i < imageWidth; i++) {
for (int j = 0; j < imageHeight; j++) {
img.setRGB(i, j, (int) outPixel.get(i * j));
}
}
答案 0 :(得分:2)
&outPixels[0]
是说outPixels.data()
的较差方法。它是指向vector
缓冲区内容的指针。