int numChannels = getNumChannels(format);
error = flycaptureGetCameraRegister(context, IMAGE_DATA_FORMAT, regOut);
if (error != FLYCAPTURE_OK) {
throw new Exception("flycaptureGetCameraRegister() Error " + error);
}
ByteOrder frameEndian = (regOut[0] & 0x1) != 0 ?
ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
boolean alreadySwapped = false;
boolean colorbayer = raw_image.bStippled();
boolean colorrgb = format == FLYCAPTURE_RGB8 || format == FLYCAPTURE_RGB16 ||
format == FLYCAPTURE_BGR || format == FLYCAPTURE_BGRU;
if ((depth == IPL_DEPTH_8U || frameEndian.equals(ByteOrder.nativeOrder())) &&
(colorMode == ColorMode.RAW || (colorMode == ColorMode.BGR && numChannels == 3 &&
format == FLYCAPTURE_BGR) ||
(colorMode == ColorMode.GRAY && numChannels == 1 && !colorbayer))) {
if (return_image == null) {
return_image = IplImage.createHeader(w, h, depth, numChannels);
}
return_image.widthStep(stride);
return_image.imageSize(size);
return_image.imageData(raw_image.pData());
} else {
if (return_image == null) {
return_image = IplImage.create(w, h, depth, colorMode == ColorMode.BGR ? 3 : 1);
}
if (temp_image == null) {
if (colorMode == ColorMode.GRAY && colorbayer) {
temp_image = IplImage.create(w, h, depth, 3);
} else if (colorMode == ColorMode.GRAY && colorrgb) {
temp_image = IplImage.createHeader(w, h, depth, 3);
temp_image.widthStep(stride);
temp_image.imageSize(size);
temp_image.imageData(raw_image.pData());
} else {
temp_image = return_image;
}
}
conv_image.iRowInc(temp_image.widthStep());
conv_image.pData(temp_image.imageData());
if (depth == IPL_DEPTH_8U) {
conv_image.pixelFormat(colorMode == ColorMode.RAW ? FLYCAPTURE_RAW8 :
temp_image.nChannels() == 1 ? FLYCAPTURE_MONO8 : FLYCAPTURE_BGR);
} else {
conv_image.pixelFormat(colorMode == ColorMode.RAW ? FLYCAPTURE_RAW16 :
temp_image.nChannels() == 1 ? FLYCAPTURE_MONO16 : FLYCAPTURE_RGB16);
}
if (conv_image.pixelFormat() == format && conv_image.iRowInc() == stride) {
// we just need a copy to swap bytes..
ShortBuffer in = raw_image.getByteBuffer().order(frameEndian).asShortBuffer();
ShortBuffer out = temp_image.getByteBuffer().order(ByteOrder.nativeOrder()).asShortBuffer();
out.put(in);
alreadySwapped = true;
} else if (!colorrgb) {
error = flycaptureConvertImage(context, raw_image, conv_image);
if (error != FLYCAPTURE_OK) {
throw new Exception("flycaptureConvertImage() Error " + error);
}
}
}
if (!alreadySwapped && depth != IPL_DEPTH_8U &&
!frameEndian.equals(ByteOrder.nativeOrder())) {
// ack, the camera's endianness doesn't correspond to our machine ...
// swap bytes of 16-bit images
ByteBuffer bb = temp_image.getByteBuffer();
ShortBuffer in = bb.order(frameEndian).asShortBuffer();
ShortBuffer out = bb.order(ByteOrder.nativeOrder()).asShortBuffer();