Skip to content

Commit

Permalink
Use rawprepare dimensions
Browse files Browse the repository at this point in the history
Currently we use the cropping **defaults** for calculation of actually used sensor dimensions / ratio
instead of what rawprepare actually does, there we have the (hidden by default) sliders defining the
real crops.
Although unlikely it is a perfectly valid usecase to change those either by presets or UI.

This pr ensures the actually used p_width & p_height in dev->image_storage are
1. correctly initialized while loading an image
2. updated while committing parameters in rawprepare
3. used wherever we want them, examples are lens, masks, crop & ashift if we want to keep sensor ratio.

For most images this won't do anything, expect subtle improvements if rawprepare does something different than
default crops, also code is more clear.
  • Loading branch information
jenshannoschwalm committed Jul 12, 2024
1 parent e4aa0e8 commit bb06d97
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 51 deletions.
9 changes: 3 additions & 6 deletions src/common/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1072,10 +1072,7 @@ void dt_image_flip(const dt_imgid_t imgid, const int32_t cw)
*/
float dt_image_get_sensor_ratio(const struct dt_image_t *img)
{
if(img->p_height >0)
return (double)img->p_width / (double)img->p_height;

return (double)img->width / (double)img->height;
return (float)img->p_width / (float)img->p_height;
}

void dt_image_set_raw_aspect_ratio(const dt_imgid_t imgid)
Expand All @@ -1085,9 +1082,9 @@ void dt_image_set_raw_aspect_ratio(const dt_imgid_t imgid)

/* set image aspect ratio */
if(image->orientation < ORIENTATION_SWAP_XY)
image->aspect_ratio = (float )image->width / (float )image->height;
image->aspect_ratio = (float )image->p_width / (float )image->p_height;
else
image->aspect_ratio = (float )image->height / (float )image->width;
image->aspect_ratio = (float )image->p_height / (float )image->p_width;

/* store */
dt_image_cache_write_release_info(darktable.image_cache, image,
Expand Down
15 changes: 4 additions & 11 deletions src/common/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ typedef struct dt_image_t
// common stuff

// to understand this, look at comment for dt_histogram_roi_t
int32_t width, height, final_width, final_height, p_width, p_height;
int32_t width, height, final_width, final_height;
// p_width and p_height are updated by rawprepare
int32_t p_width, p_height;
// written by the image loader, data come from rawspeed; **not** changed by rawprepare
int32_t crop_x, crop_y;
int32_t crop_right, crop_bottom;
float aspect_ratio;
Expand Down Expand Up @@ -495,16 +498,6 @@ gboolean dt_image_set_history_end(const dt_imgid_t imgid,
/** get the ratio of cropped raw sensor data */
float dt_image_get_sensor_ratio(const dt_image_t *img);

/** get dimensions of image after cropping in rawprepare */
static inline int dt_image_raw_width(const dt_image_t *img)
{
return img->width - img->crop_x - img->crop_right;
}
static inline int dt_image_raw_height(const dt_image_t *img)
{
return img->height - img->crop_y - img->crop_bottom;
}

/** returns the orientation bits of the image from exif. */
static inline dt_image_orientation_t dt_image_orientation(const dt_image_t *img)
{
Expand Down
4 changes: 2 additions & 2 deletions src/common/image_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ static void _image_cache_allocate(void *data,
img->id = sqlite3_column_int(stmt, 0);
img->group_id = sqlite3_column_int(stmt, 1);
img->film_id = sqlite3_column_int(stmt, 2);
img->width = sqlite3_column_int(stmt, 3);
img->height = sqlite3_column_int(stmt, 4);
img->p_width = img->width = sqlite3_column_int(stmt, 3);
img->p_height = img->height = sqlite3_column_int(stmt, 4);
img->crop_x = img->crop_y = img->crop_right = img->crop_bottom = 0;
img->filename[0] = img->exif_maker[0] = img->exif_model[0] = img->exif_lens[0] = '\0';
dt_datetime_exif_to_img(img, "");
Expand Down
26 changes: 12 additions & 14 deletions src/develop/masks/masks.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,12 @@ static int dt_masks_legacy_params_v1_to_v2(dt_develop_t *dev, void *params)
static void dt_masks_legacy_params_v2_to_v3_transform(const dt_image_t *img,
float *points)
{
const float w = (float)img->width, h = (float)img->height;

const float cx = (float)img->crop_x, cy = (float)img->crop_y;

const float cw = dt_image_raw_width(img);
const float ch = dt_image_raw_height(img);
const float w = img->width;
const float h = img->height;
const float cx = img->crop_x;
const float cy = img->crop_y;
const float cw = img->p_width;
const float ch = img->p_height;

/*
* masks coordinates are normalized, so we need to:
Expand All @@ -618,10 +618,10 @@ static void dt_masks_legacy_params_v2_to_v3_transform_only_rescale
float *points,
const size_t points_count)
{
const float w = (float)img->width, h = (float)img->height;

const float cw = dt_image_raw_width(img);
const float ch = dt_image_raw_height(img);
const float w = img->width;
const float h = img->height;
const float cw = img->p_width;
const float ch = img->p_height;

/*
* masks coordinates are normalized, so we need to:
Expand All @@ -643,10 +643,8 @@ static int dt_masks_legacy_params_v2_to_v3(dt_develop_t *dev, void *params)

const dt_image_t *img = &(dev->image_storage);

if(img->crop_x == 0
&& img->crop_y == 0
&& img->crop_right == 0
&& img->crop_bottom == 0)
if(img->p_width == img->width
&& img->p_height == img->height)
{
// image has no "raw cropping", we're fine!
m->version = 3;
Expand Down
4 changes: 2 additions & 2 deletions src/imageio/imageio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,8 @@ dt_imageio_retval_t dt_imageio_open(dt_image_t *img,
if((ret == DT_IMAGEIO_OK) && (was_bw != dt_image_monochrome_flags(img)))
dt_imageio_update_monochrome_workflow_tag(img->id, dt_image_monochrome_flags(img));

img->p_width = dt_image_raw_width(img);
img->p_height = dt_image_raw_height(img);
img->p_width = img->width - img->crop_x - img->crop_right;
img->p_height = img->height - img->crop_y - img->crop_bottom;

return ret;
}
Expand Down
2 changes: 2 additions & 0 deletions src/imageio/imageio_libraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ dt_imageio_retval_t dt_imageio_open_libraw(dt_image_t *img,
img->crop_right = raw->rawdata.sizes.raw_width - raw->rawdata.sizes.width - raw->rawdata.sizes.left_margin;
img->crop_bottom = raw->rawdata.sizes.raw_height - raw->rawdata.sizes.height - raw->rawdata.sizes.top_margin;

img->p_width = img->width - img->crop_x - img->crop_right;
img->p_height = img->height - img->crop_y - img->crop_bottom;
// We can reuse the libraw filters property, it's already well-handled in dt.
// It contains (for CR3) the Bayer pattern, but we have to undo some LibRaw logic.
if(raw->rawdata.iparams.colors == 3)
Expand Down
2 changes: 2 additions & 0 deletions src/imageio/imageio_rawspeed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ dt_imageio_retval_t dt_imageio_open_rawspeed(dt_image_t *img,
const iPoint2D cropBR = dimUncropped - dimCropped - cropTL;
img->crop_right = cropBR.x;
img->crop_bottom = cropBR.y;
img->p_width = img->width - img->crop_x - img->crop_right;
img->p_height = img->height - img->crop_y - img->crop_bottom;

img->fuji_rotation_pos = r->metadata.fujiRotationPos;
img->pixel_aspect_ratio = (float)r->metadata.pixelAspectRatio;
Expand Down
8 changes: 3 additions & 5 deletions src/iop/crop.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,7 @@ void commit_params(struct dt_iop_module_t *self,
d->aspect = 0.0f; // freehand
if(rn == 0 && abs(rd) == 1) // original image ratio
{
// we use the rawprepare cropped dimension for original
const dt_image_t *img = &(self->dev->image_storage);
const float pratio = (float)dt_image_raw_width(img) / (float)dt_image_raw_height(img);
const float pratio = dt_image_get_sensor_ratio(&self->dev->image_storage);
d->aspect = rd > 0 ? pratio : -pratio;
}
else if(rn == 0) { }
Expand Down Expand Up @@ -528,8 +526,8 @@ static float _aspect_ratio_get(dt_iop_module_t *self, GtkWidget *combo)
if(text && !g_strcmp0(text, _("original image")))
{
const dt_image_t *img = &(self->dev->image_storage);
int wd = dt_image_raw_width(img);
int ht = dt_image_raw_height(img);
int wd = img->p_width;
int ht = img->p_height;

if(!(wd > 0 && ht > 0)) return 0.0f;

Expand Down
8 changes: 4 additions & 4 deletions src/iop/lens.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ static float _get_autoscale_lf(dt_iop_module_t *self,
if(lenslist)
{
const dt_image_t *img = &(self->dev->image_storage);
const int iwd = dt_image_raw_width(img);
const int iht = dt_image_raw_height(img);
const int iwd = img->p_width;
const int iht = img->p_height;

// create dummy modifier
const dt_iop_lens_data_t d =
Expand Down Expand Up @@ -2445,8 +2445,8 @@ static int _init_coeffs_md_v2(const dt_image_t *img,
// TODO(sgotti) Theoretically, since the distortion function should always be
// monotonic and the center is always the center of the image, we should only
// look at the the shorter image radius and 1 ignoring intermediate values
const float iwd2 = 0.5f * dt_image_raw_width(img);
const float iht2 = 0.5f * dt_image_raw_height(img);
const float iwd2 = 0.5f * img->p_width;
const float iht2 = 0.5f * img->p_height;

const float r = sqrtf(iwd2 * iwd2 + iht2 * iht2);
const float sr = MIN(iwd2, iht2);
Expand Down
15 changes: 8 additions & 7 deletions src/iop/rawprepare.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ static gboolean _image_set_rawcrops(
const int top,
const int bottom)
{
dt_image_t *img = dt_image_cache_get(darktable.image_cache, imgid, 'r');
dt_image_t *img = &self->dev->image_storage;

const gboolean cropvalid = (left >= 0) && (right >= 0) && (top >= 0) && (bottom >= 0)
&& (left+right < img->width / 2) && (top + bottom < img->height /2);
Expand All @@ -616,7 +616,6 @@ static gboolean _image_set_rawcrops(
(img->p_width == img->width - left - right)
&& (img->p_height == img->height - top - bottom);

dt_image_cache_read_release(darktable.image_cache, img);
if(testdim && cropvalid) return FALSE;

if(!cropvalid)
Expand All @@ -633,10 +632,12 @@ static gboolean _image_set_rawcrops(
else
dt_iop_set_module_trouble_message(self, NULL, NULL, NULL);

img = dt_image_cache_get(darktable.image_cache, imgid, 'w');
img->p_width = img->width - (cropvalid ? left + right : 0);
img->p_height = img->height - (cropvalid ? top + bottom : 0);
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
// we update p_width & height both in the image_storage for fast access withing the pipeline
// and the database so we can access that also via dt_image_cache_get()
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
image->p_width = img->p_width = img->width - (cropvalid ? left + right : 0);
image->p_height = img->p_height = img->height - (cropvalid ? top + bottom : 0);
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_RELAXED);

return TRUE;
}
Expand Down Expand Up @@ -749,7 +750,7 @@ void commit_params(
else
d->apply_gainmaps = FALSE;

if(_image_set_rawcrops(self, pipe->image.id, d->left, d->right, d->top, d->bottom))
if(_image_set_rawcrops(self, pipe->image.id, d->left, d->right, d->top, d->bottom))
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_METADATA_UPDATE);

if(!(dt_image_is_rawprepare_supported(&piece->pipe->image))
Expand Down

0 comments on commit bb06d97

Please sign in to comment.