From d897822e9e350148f9455232c020dae6ee4a264b Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Thu, 12 Nov 2026 22:54:41 +0100 Subject: [PATCH] removing the imagefloat thing... and trying to solve the filter in integer only --- configuration.cc | 18 +++--- configuration.h | 2 +- convert.cc | 58 +++--------------- convert.h | 2 +- debayer.cc | 68 +++------------------ debayer.h | 14 ++--- main.cc | 18 +++--- miniwebcam.h | 5 +- video.h | 2 +- videodevice_dump.cc | 4 +- videodevice_dump.h | 2 +- videodevice_v4l2.cc | 16 +++-- videodevice_v4l2.h | 2 +- videoframe.cc | 145 +++++++------------------------------------- videoframe.h | 26 +------- webserver.cc | 4 +- www/index.html | 30 +++++---- 17 files changed, 105 insertions(+), 311 deletions(-) diff --git a/configuration.cc b/configuration.cc index 0921665..3e57722 100644 --- a/configuration.cc +++ b/configuration.cc @@ -46,9 +46,9 @@ Configuration::Configuration() { web_imagerefresh = 250; debug = 0; - filter_ratio = 0.5; + filter_weight = 50; auto_brightness = 0; - auto_brightness_target = 0.5; + auto_brightness_target = 0.5f; }; Configuration::~Configuration() { @@ -292,9 +292,9 @@ void Configuration::Help() { void Configuration::SetValue(std::string name, std::string value) { - if (name.compare("filter_ratio") == 0) { - filter_ratio = atof(value.c_str()); - debug ("Filter Ration set to : %g", filter_ratio); + if (name.compare("filter_weight") == 0) { + filter_weight = atof(value.c_str()); + debug ("Filter Weight set to : %g", filter_weight); } if (name.compare("auto_brightness") == 0) { auto_brightness = atol(value.c_str()); @@ -302,6 +302,8 @@ void Configuration::SetValue(std::string name, std::string value) { } if (name.compare("auto_brightness_target") == 0) { auto_brightness_target = atof(value.c_str()); + if (auto_brightness_target < 0.1f) auto_brightness_target = 0.1f; + if (auto_brightness_target > 1.0f) auto_brightness_target = 1.0f; debug ("Auto Brightness Target set to : %g", auto_brightness_target); } }; @@ -318,9 +320,9 @@ std::string Configuration::GetValues() { // jpe.Clear(); - jpe.AddObject("name", "filter_ratio"); - jpe.AddObject("value", filter_ratio); - jpe.AddObject("desc", "Ratio between new to old image (1.0 = new image)"); + jpe.AddObject("name", "filter_weight"); + jpe.AddObject("value", filter_weight); + jpe.AddObject("desc", "Weight for the filtered image (0-100)"); je.SetAddArrayEscaped("variables", jpe.ToString()); // diff --git a/configuration.h b/configuration.h index 68d8e90..d0fc2c5 100644 --- a/configuration.h +++ b/configuration.h @@ -50,7 +50,7 @@ public: unsigned int auto_brightness; float auto_brightness_target; - float filter_ratio; + int filter_weight; Configuration(); ~Configuration(); diff --git a/convert.cc b/convert.cc index 6626c6f..446c74a 100644 --- a/convert.cc +++ b/convert.cc @@ -105,13 +105,12 @@ int ConvertStop(ConvertData *cdata, uint32_t pixelformat) { /* * converts the video from input type to RGB24 type - 24Bit */ -int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsigned char *ptrsrc, int srcsize, uint32_t pixelformat, int srcw, int srch) { +int Convert (ConvertData *cdata, VideoFrame *dest, unsigned char *ptrsrc, int srcsize, uint32_t pixelformat, int srcw, int srch) { int xs, ys; int xd, yd; unsigned char r,g,b; unsigned char cb, cr, y1; unsigned char *ptrdst = NULL; - float *ptrdstf = NULL; // for float data scaled to 0...1.0 // debug ("srcsize:%d pixfmt:%s size: %dx%d", srcsize, convert_from_pixelformat(pixelformat).c_str(), srcw, srch); if (config.vdev_dumppath.length() > 0) dumpframe(ptrsrc, srcsize, pixelformat, srcw, srch); @@ -121,17 +120,12 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig // check if there is a destination and that the destination is large to keep // the full image - if ((dest == NULL && destf == NULL) || ptrsrc == NULL) - return 0; + if (dest == NULL || ptrsrc == NULL) return 0; if (dest) { dest->SetSize(srcw, srch); ptrdst = dest->GetPixBuf(); } - if (destf) { - destf->SetSize(srcw, srch); - ptrdstf = destf->GetPixBuf(); - } switch (pixelformat) { case (V4L2_PIX_FMT_RGB32): @@ -152,11 +146,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig *(ptrdst++) = g; *(ptrdst++) = b; } - if (ptrdstf) { - *(ptrdstf++) = (float)r/255.0; - *(ptrdstf++) = (float)g/255.0; - *(ptrdstf++) = (float)b/255.0; - } xd++; } } @@ -164,7 +153,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig /* if the source image is too small ignore the other places.. */ if (xd < srcw) { if (ptrdst) ptrdst += 3 * (srcw - xd); - if (ptrdstf) ptrdstf += 3 *(srcw - xd); } yd++; } @@ -187,11 +175,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig *(ptrdst++) = g; *(ptrdst++) = b; } - if (ptrdstf) { - *(ptrdstf++) = (float)r/255.0; - *(ptrdstf++) = (float)g/255.0; - *(ptrdstf++) = (float)b/255.0; - } xd++; } } @@ -199,7 +182,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig /* if the source image is too small ignore the other places.. */ if (xd < srcw) { if (ptrdst) ptrdst += 3 * (srcw - xd); - if (ptrdstf) ptrdstf += 3 *(srcw - xd); } yd++; } @@ -220,11 +202,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig *(ptrdst++) = g; *(ptrdst++) = b; } - if (ptrdstf) { - *(ptrdstf++) = (float)r/255.0; - *(ptrdstf++) = (float)g/255.0; - *(ptrdstf++) = (float)b/255.0; - } xd++; } } @@ -232,7 +209,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig /* if the source image is too small ignore the other places.. */ if (xd < srcw) { if (ptrdst) ptrdst += 3 * (srcw - xd); - if (ptrdstf) ptrdstf += 3 *(srcw - xd); } yd++; } @@ -254,11 +230,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig *(ptrdst++) = g; *(ptrdst++) = b; } - if (ptrdstf) { - *(ptrdstf++) = (float)r/255.0; - *(ptrdstf++) = (float)g/255.0; - *(ptrdstf++) = (float)b/255.0; - } xd++; } } @@ -266,34 +237,33 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig /* if the source image is too small ignore the other places.. */ if (xd < srcw) { if (ptrdst) ptrdst += 3 * (srcw - xd); - if (ptrdstf) ptrdstf += 3 *(srcw - xd); } yd++; } break; case (V4L2_PIX_FMT_SGRBG16): - debayer_grbg16 ((uint16_t *)ptrsrc, srcw, srch, ptrdst, ptrdstf, srcw, srch); + debayer_grbg16 ((uint16_t *)ptrsrc, srcw, srch, ptrdst, srcw, srch); break; case (V4L2_PIX_FMT_SRGGB10P): - debayer_rggb10packet ((uint8_t *)ptrsrc, srcw, srch, ptrdst, ptrdstf, srcw, srch); + debayer_rggb10packet ((uint8_t *)ptrsrc, srcw, srch, ptrdst, srcw, srch); break; case (V4L2_PIX_FMT_SGBRG10): - debayer_gbrg10 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, ptrdstf, srcw, srch); + debayer_gbrg10 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, srcw, srch); break; case (V4L2_PIX_FMT_SRGGB10): - debayer_rggb10 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, ptrdstf, srcw, srch); + debayer_rggb10 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, srcw, srch); break; case (V4L2_PIX_FMT_SRGGB8): - debayer_rggb8 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, ptrdstf, srcw, srch); + debayer_rggb8 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, srcw, srch); break; case (V4L2_PIX_FMT_SGRBG8): - debayer_grbg8 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, ptrdstf, srcw, srch); + debayer_grbg8 ((uint8_t *)ptrsrc, srcw, srch, ptrdst, srcw, srch); break; case (V4L2_PIX_FMT_UYVY): @@ -321,11 +291,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig *(ptrdst++) = g; *(ptrdst++) = b; } - if (ptrdstf) { - *(ptrdstf++) = (float)r/255.0; - *(ptrdstf++) = (float)g/255.0; - *(ptrdstf++) = (float)b/255.0; - } xd++; } } @@ -333,7 +298,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig /* if the source image is too small ignore the other places.. */ if (xd < srcw) { if (ptrdst) ptrdst += 3 * (srcw - xd); - if (ptrdstf) ptrdstf += 3 *(srcw - xd); } yd++; } @@ -364,11 +328,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig *(ptrdst++) = g; *(ptrdst++) = b; } - if (ptrdstf) { - *(ptrdstf++) = (float)r/255.0; - *(ptrdstf++) = (float)g/255.0; - *(ptrdstf++) = (float)b/255.0; - } xd++; } } @@ -376,7 +335,6 @@ int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsig /* if the source image is too small ignore the other places.. */ if (xd < srcw) { if (ptrdst) ptrdst += 3 * (srcw - xd); - if (ptrdstf) ptrdstf += 3 *(srcw - xd); } yd++; } diff --git a/convert.h b/convert.h index f198470..f1bb80d 100644 --- a/convert.h +++ b/convert.h @@ -18,7 +18,7 @@ struct { } typedef ConvertData; -int Convert (ConvertData *cdata, VideoFrame *dest, VideoFrameFloat *destf, unsigned char *ptrsrc, int srcsize, uint32_t pixelformat, int srcw, int srch); +int Convert (ConvertData *cdata, VideoFrame *dest, unsigned char *ptrsrc, int srcsize, uint32_t pixelformat, int srcw, int srch); int ConvertStart(ConvertData *cdata, uint32_t pixelformat); int ConvertStop(ConvertData *cdata, uint32_t pixelformat); diff --git a/debayer.cc b/debayer.cc index 3a6386c..58757f2 100644 --- a/debayer.cc +++ b/debayer.cc @@ -24,8 +24,6 @@ std::string toBits(unsigned int i, int maxbits); #define BITCONV(d) ((d>>8) & 0xff) #define STORE if (dst) { *(dst++) = BITCONV(r); *(dst++) = BITCONV(g); *(dst++) = BITCONV(b); src++; } #define STORE8 if (dst) { *(dst++) = (r & 0xff); *(dst++) = (g & 0xff); *(dst++) = (b & 0xff); src++; } -#define STOREF16 if (dstf) { *(dstf++) = ((float)r/65535.0); *(dstf++) = ((float)g/65535.0); *(dstf++) = ((float)b/65535.0); } -#define STOREF8 if (dstf) { *(dstf++) = ((float)r/255.0); *(dstf++) = ((float)g/255.0); *(dstf++) = ((float)b/255.0); } /* @@ -33,7 +31,7 @@ std::string toBits(unsigned int i, int maxbits); RGB image by bilinear interpolation. */ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, - uint8_t * dst, float * dstf, int dst_w, int dst_h) { + uint8_t * dst, int dst_w, int dst_h) { // GG RR GG RR // BB GG BB GG @@ -47,7 +45,6 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = CE; b = DN; STORE; - STOREF16; // upper first line, starts with RR GG RR ... for (xs = 1; xs < src_w - 1; xs+=2) { @@ -56,13 +53,11 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = (LE + RI + DN) / 3; b = (DNLE + DNRI) / 2; STORE; - STOREF16; // green pixel r = (LE + RI) / 2; g = CE; b = DN; STORE; - STOREF16; } // upper right pixel (red) @@ -70,7 +65,6 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = (DN + LE) / 2; b = DNLE; STORE; - STOREF16; // go through the "body" of the image for (ys = 1; ys < src_h - 1; ys+=2) { @@ -82,27 +76,23 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = (UP + DN + RI) / 3; b = CE; STORE; - STOREF16; for (xs = 1; xs < src_w - 1; xs+=2) { // green pixel r = (UP + DN) / 2; g = CE; b = (LE + RI) / 2; STORE; - STOREF16; // blue pixel r = (UPLE + UPRI + DNLE + DNRI) / 4; g = (LE + RI + UP + DN) / 4; b = CE; STORE; - STOREF16; } // last pixel in line (green) r = (UP + DN) / 2; g = CE; b = LE; STORE; - STOREF16; // every second line with GG RR GG RR ... (start at 3rd line) @@ -111,27 +101,23 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = CE; b = (UP + DN) / 2; STORE; - STOREF16; for (xs = 1; xs < src_w - 1; xs+=2) { // red pixel r = CE; g = (LE + RI + UP + DN) / 4; b = (UPLE + UPRI + DNLE + DNRI) / 4; STORE; - STOREF16; // green pixel r = (LE + RI) / 2; g = CE; b = (UP + DN) / 2; STORE; - STOREF16; } // last pixel in line (red) r = CE; g = (UP + DN + LE) / 3; b = (UPLE + DNLE) / 2; STORE; - STOREF16; } // bottom left pixel @@ -139,7 +125,6 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = (UP + RI) / 2; b = CE; STORE; - STOREF16; // last line starting with GG BB GG ... for (xs = 1; xs < src_w - 1; xs+=2) { @@ -148,13 +133,11 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = CE; b = (LE + RI) / 2; STORE; - STOREF16; // blue pixel r = (UPLE + UPRI) / 2; g = (LE + UP + RI) / 2; b = CE; STORE; - STOREF16; } // bottom right pixel (green) @@ -162,22 +145,21 @@ void debayer_grbg16 (uint16_t * src, int src_w, int src_h, g = CE; b = LE; STORE; - STOREF16; } /* The function converts a 8bit GRBG 2x2 CFA coded image into an 8bit RGB image by bilinear interpolation. */ -void debayer_gbrg8 (uint8_t * src, int src_w, int src_h, uint8_t *dst, float *dstf, int dst_w, int dst_h) { - debayer_grbg8 (src, src_w, src_h, dst, dstf, dst_w, dst_h); +void debayer_gbrg8 (uint8_t * src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h) { + debayer_grbg8 (src, src_w, src_h, dst, dst_w, dst_h); }; -void debayer_rggb8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *dstf, int dst_w, int dst_h) { - debayer_grbg8 (src, src_w, src_h, dst, dstf, dst_w, dst_h); +void debayer_rggb8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) { + debayer_grbg8 (src, src_w, src_h, dst, dst_w, dst_h); }; -void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *dstf, int dst_w, int dst_h) { +void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) { // GG RR GG RR // BB GG BB GG @@ -191,7 +173,6 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = CE; b = DN; STORE8; - STOREF8; // upper first line, starts with RR GG RR ... for (xs = 1; xs < src_w - 1; xs+=2) { @@ -200,13 +181,11 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = (LE + RI + DN) / 3; b = (DNLE + DNRI) / 2; STORE8; - STOREF8; // green pixel r = (LE + RI) / 2; g = CE; b = DN; STORE8; - STOREF8; } // upper right pixel (red) @@ -214,7 +193,6 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = (DN + LE) / 2; b = DNLE; STORE8; - STOREF8; // go through the "body" of the image for (ys = 1; ys < src_h - 1; ys+=2) { @@ -226,27 +204,23 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = (UP + DN + RI) / 3; b = CE; STORE8; - STOREF8; for (xs = 1; xs < src_w - 1; xs+=2) { // green pixel r = (UP + DN) / 2; g = CE; b = (LE + RI) / 2; STORE8; - STOREF8; // blue pixel r = (UPLE + UPRI + DNLE + DNRI) / 4; g = (LE + RI + UP + DN) / 4; b = CE; STORE8; - STOREF8; } // last pixel in line (green) r = (UP + DN) / 2; g = CE; b = LE; STORE8; - STOREF8; // every second line with GG RR GG RR ... (start at 3rd line) @@ -255,27 +229,23 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = CE; b = (UP + DN) / 2; STORE8; - STOREF8; for (xs = 1; xs < src_w - 1; xs+=2) { // red pixel r = CE; g = (LE + RI + UP + DN) / 4; b = (UPLE + UPRI + DNLE + DNRI) / 4; STORE8; - STOREF8; // green pixel r = (LE + RI) / 2; g = CE; b = (UP + DN) / 2; STORE8; - STOREF8; } // last pixel in line (red) r = CE; g = (UP + DN + LE) / 3; b = (UPLE + DNLE) / 2; STORE8; - STOREF8; } // bottom left pixel @@ -283,7 +253,6 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = (UP + RI) / 2; b = CE; STORE8; - STOREF8; // last line starting with GG BB GG ... for (xs = 1; xs < src_w - 1; xs+=2) { @@ -292,13 +261,11 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = CE; b = (LE + RI) / 2; STORE8; - STOREF8; // blue pixel r = (UPLE + UPRI) / 2; g = (LE + UP + RI) / 2; b = CE; STORE8; - STOREF8; } // bottom right pixel (green) @@ -306,7 +273,6 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d g = CE; b = LE; STORE8; - STOREF8; } #define MAX(_a_,_m_) ( _a_ > _m_ ? _m_ : _a_) @@ -319,7 +285,7 @@ void debayer_grbg8 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float *d #define PRGGB10P_DL ((uint16_t)(*(psrc-2-src_w*2*10/8))) #define PRGGB10P_DR ((uint16_t)(*(psrc+2+src_w*2*10/8))) void debayer_rggb10packet (uint8_t *src, int src_w, int src_h, - uint8_t *dst, float *dstf, int dst_w, int dst_h) { + uint8_t *dst, int dst_w, int dst_h) { debug ("not yep implemented"); debug ("src size:%dx%d dst size:%dx%d", src_w, src_h, dst_w, dst_h); @@ -355,7 +321,7 @@ inline int swap16(uint16_t i) { #define PRGGB10_UR (unsigned int)(*(psrc+1-src_w)) #define PRGGB10_DL (unsigned int)(*(psrc-1+src_w)) #define PRGGB10_DR (unsigned int)(*(psrc+1+src_w)) -void debayer_gbrg10 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float * dstf, int dst_w, int dst_h) { +void debayer_gbrg10 (uint8_t * src, int src_w, int src_h, uint8_t * dst, int dst_w, int dst_h) { // GG BB GG BB // RR GG RR GG // GG BB GG BB @@ -371,7 +337,6 @@ void debayer_gbrg10 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float int max_b = 0; pdst = dst; - pdstf = dstf; for (ys = 0, yd = 0, psrc = (uint16_t*)src; ys < dst_h; ys++, yd++) { r = 0; g = 0; @@ -413,19 +378,12 @@ void debayer_gbrg10 (uint8_t * src, int src_w, int src_h, uint8_t * dst, float pdst += 3; } - // copy pixel to destination float - if (pdstf) { - pdstf[0] = ((float)r / 1023.0); - pdstf[1] = ((float)g / 1023.0); - pdstf[2] = ((float)b / 1023.0); - pdstf += 3; - } psrc += 1; } } }; -void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float *dstf, int dst_w, int dst_h) { +void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h) { // RR GG RR GG // GG BB GG BB // RR GG RR GG @@ -441,7 +399,6 @@ void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float *ds int max_b = 0; pdst = dst; - pdstf = dstf; for (ys = 0, yd = 0, psrc = (uint16_t*)src; ys < dst_h; ys++, yd++) { r = 0; g = 0; @@ -482,13 +439,6 @@ void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float *ds pdst += 3; } - // copy pixel to destination float - if (pdstf) { - pdstf[0] = ((float)r / 1023.0); - pdstf[1] = ((float)g / 1023.0); - pdstf[2] = ((float)b / 1023.0); - pdstf += 3; - } psrc += 1; } } diff --git a/debayer.h b/debayer.h index 98803b8..99055dc 100644 --- a/debayer.h +++ b/debayer.h @@ -3,14 +3,14 @@ #include -void debayer_grbg16 (uint16_t *src, int src_w, int src_h, uint8_t *dst, float*dstf, int dst_w, int dst_h); +void debayer_grbg16 (uint16_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); -void debayer_grbg8 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float*dstf, int dst_w, int dst_h); -void debayer_gbrg8 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float*dstf, int dst_w, int dst_h); -void debayer_rggb8 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float*dstf, int dst_w, int dst_h); +void debayer_grbg8 (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); +void debayer_gbrg8 (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); +void debayer_rggb8 (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); -void debayer_rggb10packet (uint8_t *src, int src_w, int src_h, uint8_t *dst, float *dstf, int dst_w, int dst_h); -void debayer_gbrg10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float *dstf, int dst_w, int dst_h); -void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, float *dstf, int dst_w, int dst_h); +void debayer_rggb10packet (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); +void debayer_gbrg10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); +void debayer_rggb10 (uint8_t *src, int src_w, int src_h, uint8_t *dst, int dst_w, int dst_h); #endif diff --git a/main.cc b/main.cc index 9028fe4..376601a 100644 --- a/main.cc +++ b/main.cc @@ -23,10 +23,9 @@ std::string GetDefaultConfig(std::string name); int timedeiff_ms(timeval *t1); VideoFrame inputimage; -VideoFrameFloat inputfloat; -VideoFrameFloat inputfloatfilter; VideoFrame currentimage; -VideoFrame currentimagefloat; +VideoFrame filteredimage; +VideoFrame currentfilteredimage; VideoDevice *vdev= NULL; double framespersecond = -1; // 60secs average frames per second @@ -36,6 +35,7 @@ static void *thread_webserver(void *ignored_argument) { WebCamServer webserver; webserver.SetupPorts(config.http_port, config.https_port); webserver.SetupSSL(config.ssl_key, config.ssl_cert); + webserver.SetupMaxClients(16); webserver.Start(); while (running) { @@ -57,7 +57,7 @@ static void *thread_video(void *ignored_argument) { float brightness_error_sum = 0.0f; float brightness_error_integral = 0.0f; int brightness_sample_count = 0; - const int brightness_settle_frames = 5; + const int brightness_settle_frames = VDEV_INBUFFERS; const float brightness_deadband = 0.01f; const float brightness_kp = 0.10f; const float brightness_ki = 0.01f; @@ -91,8 +91,7 @@ static void *thread_video(void *ignored_argument) { currentimage.SetSize (config.web_width, config.web_height); while (running) { -// if (vdev->GetFrame(&inputimage, &inputfloat) == 0) { - if (vdev->GetFrame(&inputimage, NULL) == 0) { + if (vdev->GetFrame(&inputimage) == 0) { vdev->Stop(); vdev->Start(); } @@ -109,9 +108,8 @@ static void *thread_video(void *ignored_argument) { Lock(); current_brightness = 0.0f; inputimage.CopyTo(¤timage, config.web_width, config.web_height, ¤t_brightness); - if (inputfloatfilter.AddScaledImage(&inputfloat, config.filter_ratio) == 0) - inputfloat.CopyTo(&inputfloatfilter); - inputfloatfilter.CopyTo(¤timagefloat); + inputimage.CopyToFiltered(&filteredimage, config.filter_weight); + filteredimage.CopyTo(¤tfilteredimage); UnLock(); // calculate and set new brightness value if auto_brightness is enabled @@ -131,7 +129,7 @@ static void *thread_video(void *ignored_argument) { config.auto_brightness = 0; // disable auto brightness if control not found } if (found) { - if (config.auto_brightness_target < 0.0f) config.auto_brightness_target = 0.0f; + if (config.auto_brightness_target < 0.1f) config.auto_brightness_target = 0.1f; if (config.auto_brightness_target > 1.0f) config.auto_brightness_target = 1.0f; brightness_error_sum += config.auto_brightness_target - current_brightness; diff --git a/miniwebcam.h b/miniwebcam.h index 565b13a..55b0dde 100644 --- a/miniwebcam.h +++ b/miniwebcam.h @@ -29,9 +29,8 @@ int isfile(std::string fname); extern VideoFrame inputimage; extern VideoFrame currentimage; - -extern VideoFrameFloat inputfloat; -extern VideoFrame currentimagefloat; +extern VideoFrame filteredimage; +extern VideoFrame currentfilteredimage; extern int running; #endif diff --git a/video.h b/video.h index d16c63c..f23a958 100644 --- a/video.h +++ b/video.h @@ -84,7 +84,7 @@ class VideoDevice { virtual int SetIOMode(int newiomode) { debug (""); return 0; }; virtual int Start() { debug (""); return 0; }; virtual int Stop() { debug (""); return 0; }; - virtual int GetFrame (VideoFrame *destframe, VideoFrameFloat *destfloat) { debug (""); return 0; }; + virtual int GetFrame (VideoFrame *destframe) { debug (""); return 0; }; virtual int SetDevCtrl(unsigned int id, int value) { debug (""); return 0; }; virtual int GetDevCtrl(unsigned int id, int *value) { debug (""); return 0; }; virtual std::list GetDevCtrls() { return vidctrls; }; diff --git a/videodevice_dump.cc b/videodevice_dump.cc index 62eb93a..c147136 100644 --- a/videodevice_dump.cc +++ b/videodevice_dump.cc @@ -248,7 +248,7 @@ int VideoDevice_Dump::Stop() { // #define SIZE_FRAMEHEADER 8 #define SIZE_DUMPHEADER 12 -int VideoDevice_Dump::GetFrame(VideoFrame *destframe, VideoFrameFloat *destfloat) { +int VideoDevice_Dump::GetFrame(VideoFrame *destframe) { struct timeval curtv; unsigned int diff = 0; std::list::iterator ctrl; @@ -267,7 +267,7 @@ int VideoDevice_Dump::GetFrame(VideoFrame *destframe, VideoFrameFloat *destfloat } while (diff < inframe_nexttime); ReadFrame(); - Convert(&cdata, destframe, destfloat, inframe, inframe_size, + Convert(&cdata, destframe, inframe, inframe_size, conf_videofmt, w, h); ctrl = vidctrls.begin(); diff --git a/videodevice_dump.h b/videodevice_dump.h index 529beba..794c202 100644 --- a/videodevice_dump.h +++ b/videodevice_dump.h @@ -34,7 +34,7 @@ class VideoDevice_Dump : public VideoDevice { int SetIOMode(int newiomode) { return 1; }; int Start(); int Stop(); - int GetFrame (VideoFrame *destframe, VideoFrameFloat *destfloat); + int GetFrame (VideoFrame *destframe); int SetDevCtrl(unsigned int id, int value); int GetDevCtrl(unsigned int id, int *value); }; diff --git a/videodevice_v4l2.cc b/videodevice_v4l2.cc index 67bacf4..3eb8d61 100644 --- a/videodevice_v4l2.cc +++ b/videodevice_v4l2.cc @@ -401,11 +401,11 @@ int VideoDevice_V4L2::Stop() { }; -int VideoDevice_V4L2::GetFrame(VideoFrame *destframe, VideoFrameFloat *destfloat) { +int VideoDevice_V4L2::GetFrame(VideoFrame *destframe) { struct v4l2_buffer buf; int len, ret; - if (destframe == NULL && destfloat == NULL) return 0; + if (destframe == NULL) return 0; switch (conf_iomode) { case IOMODE_READ: @@ -424,8 +424,10 @@ int VideoDevice_V4L2::GetFrame(VideoFrame *destframe, VideoFrameFloat *destfloat } } else { - Convert(&cdata, destframe, destfloat, inbuffer[0].data, len, - fmt.fmt.pix.pixelformat, fmt.fmt.pix.width, fmt.fmt.pix.height); + if (Convert(&cdata, destframe, inbuffer[0].data, len, + fmt.fmt.pix.pixelformat, fmt.fmt.pix.width, fmt.fmt.pix.height) == 0) { + return 0; + } } break; @@ -452,8 +454,10 @@ int VideoDevice_V4L2::GetFrame(VideoFrame *destframe, VideoFrameFloat *destfloat } if (buf.index >= 0 && buf.index < VDEV_INBUFFERS) { - Convert(&cdata, destframe, destfloat, inbuffer[buf.index].data, buf.bytesused, - fmt.fmt.pix.pixelformat, fmt.fmt.pix.width, fmt.fmt.pix.height); + if (Convert(&cdata, destframe, inbuffer[buf.index].data, buf.bytesused, + fmt.fmt.pix.pixelformat, fmt.fmt.pix.width, fmt.fmt.pix.height) == 0) { + return 0; + } } ret = xioctl(fd, VIDIOC_QBUF, &buf); diff --git a/videodevice_v4l2.h b/videodevice_v4l2.h index 11bd794..cdce491 100644 --- a/videodevice_v4l2.h +++ b/videodevice_v4l2.h @@ -30,7 +30,7 @@ class VideoDevice_V4L2 : public VideoDevice { int SetIOMode(int newiomode); int Start(); int Stop(); - int GetFrame (VideoFrame *destframe, VideoFrameFloat *destfloat); + int GetFrame (VideoFrame *destframe); int SetDevCtrl(unsigned int id, int value); int GetDevCtrl(unsigned int id, int *value); void RefreshCtrls(); diff --git a/videoframe.cc b/videoframe.cc index 6741110..766a313 100644 --- a/videoframe.cc +++ b/videoframe.cc @@ -177,139 +177,36 @@ int VideoFrame::CopyTo(VideoFrame *dest, int destw, int desth, float *brightness return 1; }; -/*********************************************************************/ -VideoFrameFloat::VideoFrameFloat() { - memf = NULL; - memf_allocated = 0; - width = 0; - height = 0; -}; - - -VideoFrameFloat::~VideoFrameFloat() { - FreeFrame(); -}; - - -void VideoFrameFloat::FreeFrame() { - if (memf != NULL) free(memf); - memf = NULL; - memf_allocated = 0; -}; - - -void VideoFrameFloat::AllocateFrame() { - int memnewsize = width * height * 3 * sizeof(float); - if (memnewsize <= memf_allocated) return; - else if (memnewsize == 0) { - FreeFrame(); - return; - } - - memf = (float *) realloc (memf, memnewsize); - memf_allocated = memnewsize; - if (memf == NULL) { - debug ("Error on allocation new frame\n"); - exit (1); - } -}; - - - -int VideoFrameFloat::TestScreen(int w, int h) { - int x, y; - float r, g, b; - - SetSize (w, h); - - r = g = b = 0.0; - for (x = 0; x < w; x++) { - for (g = 0, y = 0; y < h; y++) { - if (g ) - b = 0.5*(r + g); - - memf[3*(x + y * width) + 0] = r; - memf[3*(x + y * width) + 1] = g; - memf[3*(x + y * width) + 2] = b; - - g += 0.01; - } - r += 0.01; - } - - return 1; -} - - -int VideoFrameFloat::SetSize(int w, int h) { - if (w < 0 || h < 0) return 0; - if (width == w && height == h) return 1; - - width = w; - height = h; - AllocateFrame(); - - return 1; -}; - - -int VideoFrameFloat::ConvertToJpeg(InMemoryFile *imf, int quality) { - VideoFrame vf; - - if (CopyTo (&vf) == 0) return 0; - return vf.ConvertToJpeg(imf, quality); -}; - - -int VideoFrameFloat::CopyTo(VideoFrame *dest) { - int x, y; - unsigned char *dptr; - float *sptr; - - if (dest == NULL || memf == NULL || width == 0 || height == 0) return 0; - dest->SetSize(width, height); - - for (sptr = memf, dptr = dest->GetPixBuf(), y = 0; y < height; y++) - for (x = 0; x < width*3; x++) { - *dptr = (unsigned char) (sqrtf(*sptr) * 255.0); - dptr++; - sptr++; - } - - return 1; -}; - - -int VideoFrameFloat::CopyTo(VideoFrameFloat *dest) { - if (memf == NULL || height == 0 || width == 0) return 0; - - if (dest->SetSize(width, height) == 0) return 0; - memcpy (dest->memf, memf, 3 * height * width * sizeof(float)); +int VideoFrame::CopyTo(VideoFrame *dest) { + if (dest == NULL) return 0; + if (dest->GetWidth() != width || dest->GetHeight() != height) + dest->SetSize(width, height); + memcpy(dest->GetPixBuf(), mem, width * height * 3); return 1; }; -int VideoFrameFloat::AddScaledImage(VideoFrameFloat *vf, float ratio, float *brightness) { - float *sptr, *dptr; - int x, y; - - if (vf->height != height || vf->width != width || memf == NULL || vf->memf == NULL) return 0; +int VideoFrame::CopyToFiltered(VideoFrame *dest, int filter_weight) { + // check if destination is equal to source size, if not resize destination and blindly copy image data + if (dest == NULL) return 0; + if (dest->GetWidth() != width || dest->GetHeight() != height) { + dest->SetSize(width, height); + memcpy(dest->GetPixBuf(), mem, width * height * 3); + return 1; + } - for (dptr = memf, sptr = vf->GetPixBuf(), y = 0; y < height; y++) - for (x = 0; x < width*3; x++) { - *dptr = (1.0-ratio) * (*dptr) + ratio * (*sptr); - if (brightness != NULL) *brightness += *dptr; - dptr++; - sptr++; + // merge the destination image with the source image using the old and the new pixel values averaging them out with the filter_weight + unsigned char *destptr = dest->GetPixBuf(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int idx = 3 * (y * width + x); + destptr[idx + 0] = (destptr[idx + 0] * (100 - filter_weight) + mem[idx + 0] * filter_weight) / 100; + destptr[idx + 1] = (destptr[idx + 1] * (100 - filter_weight) + mem[idx + 1] * filter_weight) / 100; + destptr[idx + 2] = (destptr[idx + 2] * (100 - filter_weight) + mem[idx + 2] * filter_weight) / 100; } - - if (brightness != NULL) { - *brightness /= (width * height * 3); - *brightness = sqrtf(*brightness); } return 1; }; - diff --git a/videoframe.h b/videoframe.h index db4e05c..acaff60 100644 --- a/videoframe.h +++ b/videoframe.h @@ -25,31 +25,9 @@ class VideoFrame { int ConvertToJpeg(InMemoryFile *imf, int quality); int TestScreen(int w, int h); int CopyTo(VideoFrame *dest, int destw, int desth, float *brightness = NULL); - int CopyFrom(int sw, int sh, int ssize, unsigned char *sdata); -}; - - -class VideoFrameFloat { - private: - protected: - void AllocateFrame(); - void FreeFrame(); - int height; - int width; - int memf_allocated; - float *memf; - public: - VideoFrameFloat(); - ~VideoFrameFloat(); - - int SetSize(int w, int h); - int TestScreen(int w, int h); - float *GetPixBuf() { return memf; }; - int ConvertToJpeg(InMemoryFile *imf, int quality); int CopyTo(VideoFrame *dest); - int CopyTo(VideoFrameFloat *dest); - - int AddScaledImage(VideoFrameFloat *vf, float ratio, float *brightness = NULL); + int CopyToFiltered(VideoFrame *dest, int filter_weight); + int CopyFrom(int sw, int sh, int ssize, unsigned char *sdata); }; diff --git a/webserver.cc b/webserver.cc index 178a089..f173ab6 100644 --- a/webserver.cc +++ b/webserver.cc @@ -33,10 +33,10 @@ int WebCamServer::HandleRequest (WebRequestBuffer *requestbuffer, WebServerClien (void*) jpgfile.mem, jpgfile.memsize) != 1) return 0; } - else if (request.find("/snapshot-float.jpg") != std::string::npos) { + else if (request.find("/snapshot-filtered.jpg") != std::string::npos) { InMemoryFile jpgfile; Lock(); - currentimagefloat.ConvertToJpeg(&jpgfile, 99); + currentfilteredimage.ConvertToJpeg(&jpgfile, 99); UnLock(); if (webclient->SendResponseFileFromMemory(requestbuffer, request, "", (void*) jpgfile.mem, diff --git a/www/index.html b/www/index.html index 0bb0f76..e3cdb7a 100644 --- a/www/index.html +++ b/www/index.html @@ -109,6 +109,7 @@ const notice = document.getElementById("notice"); const statusText = document.getElementById("status-text"); const connectionStatus = document.getElementById("connection-status"); + let imageLoading = !image.complete; let controls = []; let filterEnabled = false; let refreshCount = 0; @@ -141,13 +142,16 @@ } function reloadImage() { - const imageName = filterEnabled ? "snapshot-float.jpg" : "snapshot.jpg"; - image.src = imageName + "?" + Date.now(); + const imageName = filterEnabled ? "snapshot-filtered.jpg" : "snapshot.jpg"; document.getElementById("preview-label").textContent = filterEnabled ? "Processed image" : "Standard image"; + if (imageLoading) return; + imageLoading = true; + image.src = imageName + "?" + Date.now(); } image.addEventListener("load", function() { + imageLoading = false; setPreviewAspectRatio(); document.getElementById("preview-error").hidden = true; document.getElementById("last-updated").textContent = @@ -155,6 +159,7 @@ }); image.addEventListener("error", function() { + imageLoading = false; document.getElementById("preview-error").hidden = false; }); @@ -327,8 +332,8 @@ const editor = document.createElement("div"); editor.className = "setting-editor"; const isControlId = variable.name === "auto_brightness"; - const isRatio = variable.name === "filter_ratio" || - variable.name === "auto_brightness_target"; + const isWeight = variable.name === "filter_weight"; + const isBrightnessTarget = variable.name === "auto_brightness_target"; const input = document.createElement(isControlId ? "select" : "input"); input.className = "variable-value"; input.dataset.name = variable.name; @@ -355,21 +360,24 @@ input.appendChild(option); }); input.value = currentId; - } else if (isRatio) { + } else if (isWeight || isBrightnessTarget) { + const min = isBrightnessTarget ? "0.1" : "0"; + const max = isBrightnessTarget ? "1" : "100"; + const step = isBrightnessTarget ? "0.01" : "1"; const slider = document.createElement("input"); slider.className = "control-slider variable-slider"; slider.type = "range"; - slider.min = "0"; - slider.max = "1"; - slider.step = "0.01"; + slider.min = min; + slider.max = max; + slider.step = step; slider.value = variable.value; slider.dataset.name = variable.name; slider.setAttribute("aria-label", variable.name + " slider"); input.type = "number"; - input.min = "0"; - input.max = "1"; - input.step = "0.01"; + input.min = min; + input.max = max; + input.step = step; input.value = variable.value; input.setAttribute("aria-label", variable.name + " value");