Skip to content

Commit

Permalink
Added dof values to layer, pencildef and added to cameraprop-dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlamhauge committed Mar 1, 2024
1 parent ab2063f commit da15d2d
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 3 deletions.
127 changes: 124 additions & 3 deletions app/ui/camerapropertiesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<rect>
<x>0</x>
<y>0</y>
<width>246</width>
<height>105</height>
<width>253</width>
<height>269</height>
</rect>
</property>
<property name="windowTitle">
<string>Camera Properties</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="nameLayout">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="nameLabel">
<property name="text">
Expand Down Expand Up @@ -59,6 +59,127 @@
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Camera Settings</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="labAperture">
<property name="text">
<string>Aperture</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="apertureBox">
<property name="currentIndex">
<number>4</number>
</property>
<item>
<property name="text">
<string>2</string>
</property>
</item>
<item>
<property name="text">
<string>2,8</string>
</property>
</item>
<item>
<property name="text">
<string>4</string>
</property>
</item>
<item>
<property name="text">
<string>5,6</string>
</property>
</item>
<item>
<property name="text">
<string>8</string>
</property>
</item>
<item>
<property name="text">
<string>11</string>
</property>
</item>
<item>
<property name="text">
<string>16</string>
</property>
</item>
<item>
<property name="text">
<string>22</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="labDistance">
<property name="text">
<string>Distance</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="distanceSpinBox">
<property name="suffix">
<string> m.</string>
</property>
<property name="prefix">
<string/>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>50000</number>
</property>
<property name="value">
<number>10</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QPushButton" name="btnResetSettings">
<property name="text">
<string>Reset settings</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
Expand Down
1 change: 1 addition & 0 deletions core_lib/core_lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ HEADERS += \
src/tool/strokemanager.h \
src/tool/stroketool.h \
src/util/blitrect.h \
src/util/blurutils.h \
src/util/cameraeasingtype.h \
src/util/camerafieldoption.h \
src/util/colordictionary.h \
Expand Down
14 changes: 14 additions & 0 deletions core_lib/src/structure/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ class Camera : public KeyFrame
void setPathControlPointMoved(bool pathMoved);
bool pathControlPointMoved() const { return mPathControlPointMoved; }

void setDistance(qreal dist) { mDistance = dist; }
qreal getDistance() { return mDistance; }
void setAperture(qreal aperture) { mAperture = aperture; }
qreal getAperture() { return mAperture; }

signals:

void settingsChanged(qreal mDistance, qreal mAperture);

private:

// values used for depth of field
qreal mDistance = 10.0f; // standard distance setting on camera
qreal mAperture = 8.0f; // standard aperture setting on camera

QTransform mView;
QPointF mTranslate;
qreal mRotate = 0.;
Expand Down
8 changes: 8 additions & 0 deletions core_lib/src/structure/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GNU General Public License for more details.
#include <QPainter>
#include <QDomElement>
#include "keyframe.h"
#include "blurutils.h"

// Used to sort the selected frames list
bool sortAsc(int left, int right)
Expand Down Expand Up @@ -676,6 +677,13 @@ bool Layer::isPaintable() const
return (type() == BITMAP || type() == VECTOR);
}

void Layer::updateFarNearDistance(qreal distance, qreal aperture)
{
qreal hyperFocalDist = getHyperfocalDistance(50, aperture);
mNearDistance = getDistanceNear(hyperFocalDist, distance, 50);
mFarDistance = getDistanceFar(hyperFocalDist, distance, 50);
}

bool Layer::keyExistsWhichCovers(int frameNumber)
{
return getKeyFrameWhichCovers(frameNumber) != nullptr;
Expand Down
14 changes: 14 additions & 0 deletions core_lib/src/structure/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ class Layer : public QObject
/** Clear the list of dirty keyframes */
void clearDirtyFrames() { mDirtyFrames.clear(); }

// for depth of field
void setDistance(qreal dist) { mDistance = dist; }
qreal getDistance() { return mDistance; }
void setNearDistance(qreal dist) { mNearDistance = dist; }
qreal getNearDistance() { return mNearDistance; }
void setFarDistance(qreal dist) { mFarDistance = dist; }
qreal getFarDistance() { return mFarDistance; }
void updateFarNearDistance(qreal near, qreal far);

protected:
virtual KeyFrame* createKeyFrame(int position) = 0;
bool loadKey(KeyFrame*);
Expand All @@ -176,6 +185,11 @@ class Layer : public QObject
bool mVisible = true;
QString mName;

// vars for depth of field
qreal mDistance = 10.0f; // standard distance from camera to layer
qreal mNearDistance = 10.0f; // nearest distance, where the object will be i "acceptable" focus
qreal mFarDistance = 10.0f; // farthest distance, where the object will be i "acceptable" focus

std::map<int, KeyFrame*, std::greater<int>> mKeyFrames;

// We need to keep track of selected frames ordered by last selected
Expand Down
5 changes: 5 additions & 0 deletions core_lib/src/structure/layerbitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ QRect LayerBitmap::getFrameBounds(int frame)
return image->bounds();
}

void LayerBitmap::updateDistanceNearFar()
{

}

void LayerBitmap::loadImageAtFrame(QString path, QPoint topLeft, int frameNumber, qreal opacity)
{
BitmapImage* pKeyFrame = new BitmapImage(topLeft, path);
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/structure/layerbitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class LayerBitmap : public Layer
void repositionFrame(QPoint point, int frame);
QRect getFrameBounds(int frame);

void updateDistanceNearFar();

protected:
Status saveKeyFrameFile(KeyFrame*, QString strPath) override;
KeyFrame* createKeyFrame(int position) override;
Expand Down
124 changes: 124 additions & 0 deletions core_lib/src/util/blurutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#ifndef BLURUTILS_H
#define BLURUTILS_H

#include <QtMath>
#include <vector>
#include <QRect>
#include <QDebug>

using namespace std;

/** Get the nearest distance, where the object will be i "acceptable" focus
*
* \param h is the Hyperfocal distance
* \param s is the focus distance on the camera
* \param fLength in the cameras focal length, can vary from 25-300 mm (standard is 50 mm)
* @return
*/
inline qreal getDistanceNear(qreal h, qreal s, qreal fLength)
{
return (s*(h-fLength))/(h+s-2*fLength);
}

/** Get the farthest distance, where the object will be i "acceptable" focus
*
* \param h is the Hyperfocal distance
* \param s is the focus distance on the camera
* \param fLength in the cameras focal length, can vary from 25-300 mm (standard is 50 mm)
* @return
*/
inline qreal getDistanceFar(qreal h, qreal s, qreal fLength)
{
return (s*(h-fLength))/(h-s);
}

/** Get hyperfocal distance using coc, focal length and f-number.
*
* Hyperfoacal distance is a distance beyond which,
* all objects can be brought into an "acceptable" focus.
* coc is Circle of confusion (=0.03)
* \param fLength in the cameras focal length, can vary from 30-300 mm (standard is 50 mm)
* \param aperture is aperture and can be 2.8 4 5.6 8 11 and 22
* \return The cameras hyperfocal distance in mm
*/
inline qreal getHyperfocalDistance(qreal fLength, qreal aperture)
{
return ((fLength*fLength)/(0.03*aperture)) + fLength;
}

/**
* @brief boxesForGauss
* @param sigma
* @param n
* @return
*/
inline vector<int> boxesForGauss(float sigma, int n) // standard deviation, number of boxes
{
auto wIdeal = sqrt((12 * sigma*sigma / n) + 1); // Ideal averaging filter width
int wl = floor(wIdeal);
if (wl % 2 == 0)
wl--;
int wu = wl + 2;

auto mIdeal = (12 * sigma*sigma - n * wl*wl - 4 * n*wl - 3 * n) / (-4 * wl - 4);
int m = round(mIdeal);
// var sigmaActual = Math.sqrt( (m*wl*wl + (n-m)*wu*wu - n)/12 );

vector<int> sizes(n);
for (auto i = 0; i < n; i++)
sizes[i] = i < m ? wl : wu;
return sizes;
}

inline void boxBlurH_4(vector<unsigned char>& scl, vector<unsigned char>& tcl, int w, int h, int r) {
float iarr = 1.f / (r + r + 1);
for (auto i = 0; i < h; i++) {
auto ti = i * w, li = ti, ri = ti + r;
auto fv = scl[ti], lv = scl[ti + w - 1];
auto val = (r + 1)*fv;
for (auto j = 0; j < r; j++) val += scl[ti + j];
for (auto j = 0; j <= r; j++) { val += scl[ri++] - fv; tcl[ti++] = round(val*iarr); }
for (auto j = r + 1; j < w - r; j++) { val += scl[ri++] - scl[li++]; tcl[ti++] = round(val*iarr); }
for (auto j = w - r; j < w; j++) { val += lv - scl[li++]; tcl[ti++] = round(val*iarr); }
}
}

inline void boxBlurT_4(vector<unsigned char>& scl, vector<unsigned char>& tcl, int w, int h, int r) {
float iarr = 1.f / (r + r + 1);
for (auto i = 0; i < w; i++) {
auto ti = i, li = ti, ri = ti + r * w;
auto fv = scl[ti], lv = scl[ti + w * (h - 1)];
auto val = (r + 1)*fv;
for (auto j = 0; j < r; j++) val += scl[ti + j * w];
for (auto j = 0; j <= r; j++) { val += scl[ri] - fv; tcl[ti] = round(val*iarr); ri += w; ti += w; }
for (auto j = r + 1; j < h - r; j++) { val += scl[ri] - scl[li]; tcl[ti] = round(val*iarr); li += w; ri += w; ti += w; }
for (auto j = h - r; j < h; j++) { val += lv - scl[li]; tcl[ti] = round(val*iarr); li += w; ti += w; }
}
}


inline void boxBlur_4(vector<unsigned char>& scl, vector<unsigned char>& tcl, int w, int h, int r) {
for (unsigned i = 0; i < scl.size(); i++) tcl[i] = scl[i];
boxBlurH_4(tcl, scl, w, h, r);
boxBlurT_4(scl, tcl, w, h, r);
}

/**
* @brief gaussBlur_4 The '4' because it is Algoritm 4 of fast gaussBlur:
* http://blog.ivank.net/fastest-gaussian-blur.html
* @param scl Source channel
* @param tcl Target channel
* @param w Width
* @param h Height
* @param r Radius
*/
inline void gaussBlur_4(vector<unsigned char>& scl, vector<unsigned char>& tcl, int w, int h, qreal r)
{
auto bxs = boxesForGauss(r, 3);
qDebug() << "bxs: " << bxs ;
boxBlur_4(scl, tcl, w, h, (bxs[0] - 1) / 2);
boxBlur_4(tcl, scl, w, h, (bxs[1] - 1) / 2);
boxBlur_4(scl, tcl, w, h, (bxs[2] - 1) / 2);
}

#endif // BLURUTILS_H
2 changes: 2 additions & 0 deletions core_lib/src/util/pencildef.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ const static float RotationHandleOffset = 50;
#define SETTING_FPS "Fps"
#define SETTING_FIELD_W "FieldW"
#define SETTING_FIELD_H "FieldH"
#define SETTING_APERTURE "Aperture"
#define SETTING_CAM_DISTANCE "CamDistance"
#define SETTING_FRAME_SIZE "FrameSize"
#define SETTING_TIMELINE_SIZE "TimelineSize"
#define SETTING_LABEL_FONT_SIZE "LabelFontSize"
Expand Down

0 comments on commit da15d2d

Please sign in to comment.