8#include "collimationoverlaytypes.h"
10#include "kstars_debug.h"
11#include "kstarsdata.h"
14#include <QImageReader>
16#include <QResizeEvent>
18#include <QSqlTableModel>
24 streamImage.reset(
new QImage());
28 for (
int i = 0; i < 256; i++)
29 grayTable[i] = qRgb(i, i, i);
32bool VideoWG::newBayerFrame(IBLOB *bp,
const BayerParams ¶ms)
34 return debayer(bp, params);
37bool VideoWG::newFrame(IBLOB *bp)
43 QString format(bp->format);
44 if (m_RawFormat != format)
47 format.remove(
"stream_");
52 if (m_RawFormatSupported)
53 rc = streamImage->loadFromData(
static_cast<uchar *
>(bp->blob), bp->size);
54 else if (
static_cast<uint32_t
>(bp->size) == totalBaseCount)
57 streamImage->setColorTable(grayTable);
58 rc = !streamImage->isNull();
60 else if (
static_cast<uint32_t
>(bp->size) == totalBaseCount * 3)
62 streamImage.reset(
new QImage(
static_cast<uchar *
>(bp->blob), streamW, streamH,
QImage::Format_RGB888));
63 rc = !streamImage->isNull();
75 emit imageChanged(streamImage);
80bool VideoWG::save(
const QString &filename,
const char *format)
82 return kPix.save(filename, format);
85void VideoWG::setSize(uint16_t w, uint16_t h)
89 totalBaseCount = w * h;
100 origin =
event->pos();
103 rubberBand->setGeometry(QRect(origin, QSize()));
109 rubberBand->setGeometry(QRect(origin,
event->pos()).normalized());
118 emit newSelection(QRect());
122 QRect rawSelection = rubberBand->geometry();
123 int pixmapX = (
width() - kPix.width()) / 2;
124 int pixmapY = (
height() - kPix.height()) / 2;
126 QRect finalSelection;
128 double scaleX =
static_cast<double>(streamImage->width()) / kPix.width();
129 double scaleY =
static_cast<double>(streamImage->height()) / kPix.height();
131 finalSelection.
setX((rawSelection.
x() - pixmapX) * scaleX);
132 finalSelection.
setY((rawSelection.
y() - pixmapY) * scaleY);
136 emit newSelection(finalSelection);
141bool VideoWG::debayer(
const IBLOB *bp,
const BayerParams ¶ms)
143 uint32_t rgb_size = streamW * streamH * 3;
144 auto * destinationBuffer =
new uint8_t[rgb_size];
146 if (destinationBuffer ==
nullptr)
148 qCCritical(KSTARS) <<
"Unable to allocate memory for temporary bayer buffer.";
152 int ds1394_height = streamH;
154 uint8_t * dc1394_source =
reinterpret_cast<uint8_t*
>(bp->blob);
155 if (params.offsetY == 1)
157 dc1394_source += streamW;
160 if (params.offsetX == 1)
164 dc1394error_t error_code = dc1394_bayer_decoding_8bit(dc1394_source, destinationBuffer, streamW, ds1394_height,
165 params.filter, params.method);
167 if (error_code != DC1394_SUCCESS)
169 qCCritical(KSTARS) <<
"Debayer failed" << error_code;
170 delete[] destinationBuffer;
175 bool rc = !streamImage->isNull();
186 emit imageChanged(streamImage);
188 delete[] destinationBuffer;
192void VideoWG::paintOverlay(
QPixmap &imagePix)
194 if (!overlayEnabled || m_EnabledOverlayElements.count() == 0)
return;
197 QPointF m_anchor (
static_cast<float>(kPix.width() / 2),
static_cast<float>(kPix.height()/2));
198 scale = (
static_cast<float>(kPix.width()) /
static_cast<float>(streamW));
201 bool foundAnchor =
false;
202 for (
auto &oneElement : m_EnabledOverlayElements) {
203 if (oneElement[
"Type"] ==
"Anchor" && !foundAnchor) {
204 m_anchor.setX(m_anchor.x() + oneElement[
"OffsetX"].toInt());
205 m_anchor.setY(m_anchor.y() + oneElement[
"OffsetY"].toInt());
210 painter->begin(&imagePix);
211 painter->translate(m_anchor);
212 painter->scale(scale, scale);
214 for (
auto ¤tElement : m_EnabledOverlayElements) {
217 QPen m_pen = QPen(QColor(currentElement[
"Colour"].
toString()));
218 m_pen.
setWidth(currentElement[
"Thickness"].toUInt());
221 painter->setPen(m_pen);
222 painter->translate(currentElement[
"OffsetX"].toFloat(), currentElement[
"OffsetY"].toFloat());
224 int m_count = currentElement[
"Count"].toUInt();
225 float m_pcd = currentElement[
"PCD"].toFloat();
228 PaintOneItem(currentElement[
"Type"].
toString(), QPointF(0.0, 0.0), currentElement[
"SizeX"].toUInt(), currentElement[
"SizeY"].toUInt(), currentElement[
"Thickness"].toUInt());
229 }
else if (m_count > 1) {
230 float slice = 360 / m_count;
231 for (
int i = 0; i < m_count; i++) {
233 painter->rotate((slice * i) + currentElement[
"Rotation"].toFloat());
234 PaintOneItem(currentElement[
"Type"].
toString(), QPointF((m_pcd / 2), 0.0), currentElement[
"SizeX"].toUInt(), currentElement[
"SizeY"].toUInt(), currentElement[
"Thickness"].toUInt());
244void VideoWG::setupOverlay ()
246 if (overlayEnabled) {
249 typeValues =
new QStringList;
250 collimationoverlaytype m_types;
251 const QMetaObject *m_metaobject = m_types.metaObject();
253 for (
int i = 0; i < m_metaEnum.
keyCount(); i++) {
254 *typeValues <<
tr(m_metaEnum.
key(i));
257 painter =
new QPainter;
261void VideoWG::initOverlayModel()
263 m_CollimationOverlayElements.clear();
265 m_CollimationOverlayElementsModel =
new QSqlTableModel(
this, userdb);
269void VideoWG::modelChanged()
271 m_CollimationOverlayElements.clear();
272 m_EnabledOverlayElements.clear();
274 for (
auto &oneElement : m_CollimationOverlayElements)
276 m_EnabledOverlayElements.append(oneElement);
279void VideoWG::PaintOneItem (
QString type,
QPointF position,
int sizeX,
int sizeY,
int thickness)
281 float m_sizeX = sizeX - (thickness / 2);
282 float m_sizeY = sizeY - (thickness / 2);
284 switch (typeValues->indexOf(type)) {
289 painter->drawEllipse(position, m_sizeX, m_sizeY);
294 QRect m_rect((position.
x() - (m_sizeX / 2)), (position.
y() - (m_sizeY / 2)), (m_sizeX - (thickness / 2)), (m_sizeY - (thickness / 2)));
295 painter->drawRect(m_rect);
300 painter->drawLine(position.
x(), position.
y(), sizeX, sizeY);
308void VideoWG::toggleOverlay()
310 if (overlayEnabled ==
false) {
311 overlayEnabled =
true;
312 if (m_CollimationOverlayElementsModel ==
nullptr) {
315 }
else if (overlayEnabled ==
true) {
316 overlayEnabled =
false;
322 delete m_CollimationOverlayElementsModel;
323 delete m_CurrentElement;
bool GetCollimationOverlayElements(QList< QVariantMap > &collimationOverlayElements)
Populate the reference passed with all collimation overlay elements.
static KStars * Instance()
KStarsData * data() const
char * toString(const EngineQuery &query)
void setPixmap(const QPixmap &)
bool contains(const AT &value) const const
virtual bool event(QEvent *e)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setCapStyle(Qt::PenCapStyle style)
void setJoinStyle(Qt::PenJoinStyle style)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
void setHeight(int height)
QSqlDatabase database(const QString &connectionName, bool open)