Add libmegapixels

This commit is contained in:
Martijn Braam
2023-07-11 23:43:23 +02:00
parent b5161db18e
commit 3ee314a68d
25 changed files with 540 additions and 2509 deletions

View File

@@ -1,6 +1,5 @@
#include "process_pipeline.h"
#include "config.h"
#include "gles2_debayer.h"
#include "io_pipeline.h"
#include "main.h"
@@ -10,15 +9,20 @@
#include <gtk/gtk.h>
#include <math.h>
#include <tiffio.h>
#ifndef SYSCONFDIR
#include "config.h"
#endif
#include "dcp.h"
#include "gl_util.h"
#include <sys/mman.h>
#define TIFFTAG_FORWARDMATRIX1 50964
#define TIFFTAG_FORWARDMATRIX2 50965
static const float colormatrix_srgb[] = { 3.2409, -1.5373, -0.4986, -0.9692, 1.8759,
0.0415, 0.0556, -0.2039, 1.0569 };
static const float colormatrix_srgb[] = { 3.2409f, -1.5373f, -0.4986f,
-0.9692f, 1.8759f, 0.0415f,
0.0556f, -0.2039f, 1.0569f };
static MPPipeline *pipeline;
@@ -28,10 +32,10 @@ static volatile bool is_capturing = false;
static volatile int frames_processed = 0;
static volatile int frames_received = 0;
static const struct mp_camera_config *camera;
static int camera_rotation;
libmegapixels_camera *pr_camera;
libmegapixels_mode *mode;
static MPMode mode;
static int camera_rotation;
static int burst_length;
static int captures_remaining = 0;
@@ -281,17 +285,19 @@ mp_process_pipeline_buffer_get_texture_id(MPProcessPipelineBuffer *buf)
}
static void
repack_image_sequencial(const uint8_t *src_buf, uint8_t *dst_buf, MPMode *mode)
repack_image_sequencial(const uint8_t *src_buf,
uint8_t *dst_buf,
libmegapixels_mode *mode)
{
uint16_t pixels[4];
uint32_t row_length =
mp_pixel_format_width_to_bytes(mode->pixel_format, mode->width);
libmegapixels_mode_width_to_bytes(mode->format, mode->width);
uint32_t padding_bytes =
mp_pixel_format_width_to_padding(mode->pixel_format, mode->width);
libmegapixels_mode_width_to_padding(mode->format, mode->width);
size_t si = 0;
// Image data must be 10-bit packed
assert(mp_pixel_format_bits_per_pixel(mode->pixel_format) == 10);
assert(libmegapixels_format_bits_per_pixel(mode->format) == 10);
/*
* Repack 40 bits stored in sensor format into sequencial format
@@ -439,10 +445,10 @@ process_image_for_preview(const uint8_t *image)
glTexImage2D(GL_TEXTURE_2D,
0,
GL_LUMINANCE,
mp_pixel_format_width_to_bytes(mode.pixel_format, mode.width) +
mp_pixel_format_width_to_padding(mode.pixel_format,
mode.width),
mode.height,
libmegapixels_mode_width_to_bytes(mode->format, mode->width) +
libmegapixels_mode_width_to_padding(mode->format,
mode->width),
mode->height,
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
@@ -535,26 +541,26 @@ process_image_for_capture(const uint8_t *image, int count)
// Define TIFF thumbnail
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, mode.width >> 4);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, mode.height >> 4);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, mode->width >> 4);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, mode->height >> 4);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_MAKE, mp_get_device_make());
TIFFSetField(tif, TIFFTAG_MODEL, mp_get_device_model());
TIFFSetField(tif, TIFFTAG_MAKE, "MAKE"); // TODO: fix
TIFFSetField(tif, TIFFTAG_MODEL, "MODEL");
uint16_t orientation;
if (camera_rotation == 0) {
orientation = camera->mirrored ? ORIENTATION_TOPRIGHT :
ORIENTATION_TOPLEFT;
orientation =
mode->mirrored ? ORIENTATION_TOPRIGHT : ORIENTATION_TOPLEFT;
} else if (camera_rotation == 90) {
orientation = camera->mirrored ? ORIENTATION_RIGHTBOT :
ORIENTATION_LEFTBOT;
orientation =
mode->mirrored ? ORIENTATION_RIGHTBOT : ORIENTATION_LEFTBOT;
} else if (camera_rotation == 180) {
orientation = camera->mirrored ? ORIENTATION_BOTLEFT :
ORIENTATION_BOTRIGHT;
orientation =
mode->mirrored ? ORIENTATION_BOTLEFT : ORIENTATION_BOTRIGHT;
} else {
orientation = camera->mirrored ? ORIENTATION_LEFTTOP :
ORIENTATION_RIGHTTOP;
orientation =
mode->mirrored ? ORIENTATION_LEFTTOP : ORIENTATION_RIGHTTOP;
}
TIFFSetField(tif, TIFFTAG_ORIENTATION, orientation);
TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
@@ -568,96 +574,98 @@ process_image_for_capture(const uint8_t *image, int count)
char uniquecameramodel[255];
sprintf(uniquecameramodel,
"%s %s",
mp_get_device_make(),
mp_get_device_model());
"MAKE", // TODO: fix
"MODEL");
TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, uniquecameramodel);
// Color matrices
if (camera->calibration.color_matrix_1[0]) {
/*
if (pr_camera->calibration.color_matrix_1[0]) {
TIFFSetField(tif,
TIFFTAG_COLORMATRIX1,
9,
camera->calibration.color_matrix_1);
} else if (camera->colormatrix[0]) {
TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, camera->colormatrix);
pr_camera->calibration.color_matrix_1);
} else if (pr_camera->colormatrix[0]) {
TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, pr_camera->colormatrix);
} else {
TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colormatrix_srgb);
}
if (camera->calibration.color_matrix_2[0]) {
if (pr_camera->calibration.color_matrix_2[0]) {
TIFFSetField(tif,
TIFFTAG_COLORMATRIX2,
9,
camera->calibration.color_matrix_2);
pr_camera->calibration.color_matrix_2);
}
if (camera->calibration.forward_matrix_1[0]) {
if (pr_camera->calibration.forward_matrix_1[0]) {
TIFFSetField(tif,
TIFFTAG_FORWARDMATRIX1,
9,
camera->calibration.forward_matrix_1);
} else if (camera->forwardmatrix[0]) {
TIFFSetField(tif, TIFFTAG_FORWARDMATRIX1, 9, camera->forwardmatrix);
pr_camera->calibration.forward_matrix_1);
} else if (pr_camera->forwardmatrix[0]) {
TIFFSetField(tif, TIFFTAG_FORWARDMATRIX1, 9,
pr_camera->forwardmatrix);
}
if (camera->calibration.forward_matrix_2[0]) {
if (pr_camera->calibration.forward_matrix_2[0]) {
TIFFSetField(tif,
TIFFTAG_FORWARDMATRIX2,
9,
camera->calibration.forward_matrix_2);
pr_camera->calibration.forward_matrix_2);
}
static const float neutral[] = { 1.0, 1.0, 1.0 };
TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
if (camera->calibration.illuminant_1) {
if (pr_camera->calibration.illuminant_1) {
TIFFSetField(tif,
TIFFTAG_CALIBRATIONILLUMINANT1,
camera->calibration.illuminant_1);
pr_camera->calibration.illuminant_1);
} else {
TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);
}
if (camera->calibration.illuminant_2) {
if (pr_camera->calibration.illuminant_2) {
TIFFSetField(tif,
TIFFTAG_CALIBRATIONILLUMINANT2,
camera->calibration.illuminant_2);
pr_camera->calibration.illuminant_2);
}
if (camera->calibration.tone_curve_length) {
if (pr_camera->calibration.tone_curve_length) {
TIFFSetField(tif,
DCPTAG_PROFILE_TONE_CURVE,
camera->calibration.tone_curve_length,
camera->calibration.tone_curve);
pr_camera->calibration.tone_curve_length,
pr_camera->calibration.tone_curve);
}
if (camera->calibration.hue_sat_map_dims[0]) {
if (pr_camera->calibration.hue_sat_map_dims[0]) {
TIFFSetField(tif,
DCPTAG_PROFILE_HUE_SAT_MAP_DIMS,
3,
camera->calibration.hue_sat_map_dims);
pr_camera->calibration.hue_sat_map_dims);
TIFFSetField(tif,
DCPTAG_PROFILE_HUE_SAT_MAP_DATA_1,
camera->calibration.hue_sat_map_dims[0] *
camera->calibration.hue_sat_map_dims[1] *
camera->calibration.hue_sat_map_dims[2] * 3,
camera->calibration.hue_sat_map_data_1);
if (camera->calibration.hue_sat_map_data_2 != NULL) {
pr_camera->calibration.hue_sat_map_dims[0] *
pr_camera->calibration.hue_sat_map_dims[1] *
pr_camera->calibration.hue_sat_map_dims[2] * 3,
pr_camera->calibration.hue_sat_map_data_1);
if (pr_camera->calibration.hue_sat_map_data_2 != NULL) {
TIFFSetField(
tif,
DCPTAG_PROFILE_HUE_SAT_MAP_DATA_2,
camera->calibration.hue_sat_map_dims[0] *
camera->calibration.hue_sat_map_dims[1] *
camera->calibration.hue_sat_map_dims[2] * 3,
camera->calibration.hue_sat_map_data_2);
pr_camera->calibration.hue_sat_map_dims[0] *
pr_camera->calibration.hue_sat_map_dims[1] *
pr_camera->calibration.hue_sat_map_dims[2] *
3, pr_camera->calibration.hue_sat_map_data_2);
}
}
*/
TIFFSetField(tif, TIFFTAG_ANALOGBALANCE, 3, balance);
// Write black thumbnail, only windows uses this
{
unsigned char *buf =
(unsigned char *)calloc(1, (mode.width >> 4) * 3);
for (int row = 0; row < (mode.height >> 4); row++) {
(unsigned char *)calloc(1, (mode->width >> 4) * 3);
for (int row = 0; row < (mode->height >> 4); row++) {
TIFFWriteScanline(tif, buf, row, 0);
}
free(buf);
@@ -666,11 +674,11 @@ process_image_for_capture(const uint8_t *image, int count)
// Define main photo
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, mode.width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, mode.height);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, mode->width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, mode->height);
TIFFSetField(tif,
TIFFTAG_BITSPERSAMPLE,
mp_pixel_format_bits_per_pixel(mode.pixel_format));
libmegapixels_format_bits_per_pixel(mode->format));
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
@@ -684,41 +692,33 @@ process_image_for_capture(const uint8_t *image, int count)
TIFFSetField(tif,
TIFFTAG_CFAPATTERN,
4,
mp_pixel_format_cfa_pattern(mode.pixel_format));
libmegapixels_format_cfa_pattern(mode->format));
#endif
printf("TIFF version %d\n", TIFFLIB_VERSION);
int whitelevel = camera->whitelevel;
if (!whitelevel) {
whitelevel =
(1 << mp_pixel_format_pixel_depth(mode.pixel_format)) - 1;
}
int whitelevel =
(1 << libmegapixels_format_bits_per_pixel(mode->format)) - 1;
TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &whitelevel);
if (camera->blacklevel) {
const float blacklevel = camera->blacklevel;
TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &blacklevel);
}
TIFFCheckpointDirectory(tif);
printf("Writing frame to %s\n", fname);
uint8_t *output_image = (uint8_t *)image;
// Repack 10-bit image from sensor format into a sequencial format
if (mp_pixel_format_bits_per_pixel(mode.pixel_format) == 10) {
output_image = malloc(mp_pixel_format_width_to_bytes(
mode.pixel_format, mode.width) *
mode.height);
if (libmegapixels_format_bits_per_pixel(mode->format) == 10) {
output_image = malloc(libmegapixels_mode_width_to_bytes(
mode->format, mode->width) *
mode->height);
repack_image_sequencial(image, output_image, &mode);
repack_image_sequencial(image, output_image, mode);
}
for (int row = 0; row < mode.height; row++) {
TIFFWriteScanline(
tif,
(void *)output_image +
(row * mp_pixel_format_width_to_bytes(
mode.pixel_format, mode.width)),
row,
0);
for (int row = 0; row < mode->height; row++) {
TIFFWriteScanline(tif,
(void *)output_image +
(row * libmegapixels_mode_width_to_bytes(
mode->format, mode->width)),
row,
0);
}
TIFFWriteDirectory(tif);
@@ -734,17 +734,20 @@ process_image_for_capture(const uint8_t *image, int count)
TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 1);
}
/*
TIFFSetField(tif,
EXIFTAG_EXPOSURETIME,
(mode.frame_interval.numerator /
(float)mode.frame_interval.denominator) /
((float)mode.height / (float)exposure));
if (camera->iso_min && camera->iso_max) {
if (pr_camera->iso_min && pr_camera->iso_max) {
uint16_t isospeed = remap(
gain - 1, 0, gain_max, camera->iso_min, camera->iso_max);
TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, &isospeed);
gain - 1, 0, gain_max, pr_camera->iso_min,
pr_camera->iso_max); TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1,
&isospeed);
}
if (!camera->has_flash) {
if (!pr_camera->has_flash) {
// No flash function
TIFFSetField(tif, EXIFTAG_FLASH, 0x20);
} else if (flash_enabled) {
@@ -754,20 +757,24 @@ process_image_for_capture(const uint8_t *image, int count)
// Flash present but not fired
TIFFSetField(tif, EXIFTAG_FLASH, 0x0);
}
*/
TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
if (camera->fnumber) {
TIFFSetField(tif, EXIFTAG_FNUMBER, camera->fnumber);
/*
if (pr_camera->fnumber) {
TIFFSetField(tif, EXIFTAG_FNUMBER, pr_camera->fnumber);
}
if (camera->focallength) {
TIFFSetField(tif, EXIFTAG_FOCALLENGTH, camera->focallength);
if (pr_camera->focallength) {
TIFFSetField(tif, EXIFTAG_FOCALLENGTH, pr_camera->focallength);
}
if (camera->focallength && camera->cropfactor) {
if (pr_camera->focallength && pr_camera->cropfactor) {
TIFFSetField(tif,
EXIFTAG_FOCALLENGTHIN35MMFILM,
(short)(camera->focallength * camera->cropfactor));
(short)(pr_camera->focallength *
pr_camera->cropfactor));
}
*/
uint64_t exif_offset = 0;
TIFFWriteCustomDirectory(tif, &exif_offset);
TIFFFreeDirectory(tif);
@@ -870,19 +877,19 @@ process_image(MPPipeline *pipeline, const MPBuffer *buffer)
#endif
size_t size =
(mp_pixel_format_width_to_bytes(mode.pixel_format, mode.width) +
mp_pixel_format_width_to_padding(mode.pixel_format, mode.width)) *
mode.height;
(libmegapixels_mode_width_to_bytes(mode->format, mode->width) +
libmegapixels_mode_width_to_padding(mode->format, mode->width)) *
mode->height;
uint8_t *image = malloc(size);
memcpy(image, buffer->data, size);
mp_io_pipeline_release_buffer(buffer->index);
MPZBarImage *zbar_image = mp_zbar_image_new(image,
mode.pixel_format,
mode.width,
mode.height,
mode->format,
mode->width,
mode->height,
camera_rotation,
camera->mirrored);
mode->mirrored);
mp_zbar_pipeline_process_image(mp_zbar_image_ref(zbar_image));
#ifdef PROFILE_PROCESS
@@ -968,10 +975,10 @@ mp_process_pipeline_capture()
static void
on_output_changed(bool format_changed)
{
output_buffer_width = mode.width / 2;
output_buffer_height = mode.height / 2;
output_buffer_width = mode->width / 2;
output_buffer_height = mode->height / 2;
if (camera->rotate != 0 || camera->rotate != 180) {
if (mode->rotation != 0 || mode->rotation != 180) {
int tmp = output_buffer_width;
output_buffer_width = output_buffer_height;
output_buffer_height = tmp;
@@ -997,22 +1004,21 @@ on_output_changed(bool format_changed)
if (gles2_debayer)
gles2_debayer_free(gles2_debayer);
gles2_debayer = gles2_debayer_new(mode.pixel_format);
gles2_debayer = gles2_debayer_new(mode->format);
check_gl();
gles2_debayer_use(gles2_debayer);
}
gles2_debayer_configure(
gles2_debayer,
output_buffer_width,
output_buffer_height,
mode.width,
mode.height,
camera->rotate,
camera->mirrored,
camera->previewmatrix[0] == 0 ? NULL : camera->previewmatrix,
camera->blacklevel);
gles2_debayer_configure(gles2_debayer,
output_buffer_width,
output_buffer_height,
mode->width,
mode->height,
mode->rotation,
0,
NULL,
0);
}
static int
@@ -1025,15 +1031,24 @@ mod(int a, int b)
static void
update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state)
{
const bool output_changed = !mp_mode_is_equivalent(&mode, &state->mode) ||
preview_width != state->preview_width ||
preview_height != state->preview_height ||
device_rotation != state->device_rotation;
const bool output_changed =
!libmegapixels_mode_equals(mode, state->camera->current_mode) ||
preview_width != state->preview_width ||
preview_height != state->preview_height ||
device_rotation != state->device_rotation;
const bool format_changed = mode.pixel_format != state->mode.pixel_format;
bool format_changed = mode == NULL;
camera = state->camera;
mode = state->mode;
if (!format_changed &&
mode->v4l_pixfmt != state->camera->current_mode->v4l_pixfmt) {
format_changed = true;
}
if (mode == NULL) {
mode = state->camera->current_mode;
}
pr_camera = state->camera;
mode = state->camera->current_mode;
preview_width = state->preview_width;
preview_height = state->preview_height;
@@ -1052,16 +1067,14 @@ update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state
exposure = state->exposure;
if (output_changed) {
camera_rotation = mod(camera->rotate - device_rotation, 360);
camera_rotation = mod(mode->rotation - device_rotation, 360);
on_output_changed(format_changed);
}
struct mp_main_state main_state = {
.camera = camera,
.camera = pr_camera,
.mode = mode,
.image_width = output_buffer_width,
.image_height = output_buffer_height,
.gain_is_manual = state->gain_is_manual,
.gain = gain,
.gain_max = gain_max,