From 233457ad3b0867eaec688df77d8412ea0e82e88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Vald=C3=A9s?= Date: Wed, 3 Jul 2024 18:13:35 +0200 Subject: [PATCH] operator for RGB distance added; to be replaced by a version for a new data type Picture1024 --- Algebras/Picture/PictureAlgebra.h | 7 +++ Algebras/Picture/PictureAlgebra.spec | 1 + Algebras/Picture/PictureAlgebra_base.cpp | 51 ++++++++++++++++++++ Algebras/Picture/PictureAlgebra_graphops.cpp | 21 +++++++- 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/Algebras/Picture/PictureAlgebra.h b/Algebras/Picture/PictureAlgebra.h index d04d11388c..b59600de51 100644 --- a/Algebras/Picture/PictureAlgebra.h +++ b/Algebras/Picture/PictureAlgebra.h @@ -496,6 +496,13 @@ horizontally otherwise. /* +Returns the L1 norm of two RGB vectors (given as Picture objects). + +*/ + double DistanceRGB(const Picture& pic) const; + +/* + 4.8 ~Histogram~ and comparison operations As the comparison operations are based on histograms, they go into the same diff --git a/Algebras/Picture/PictureAlgebra.spec b/Algebras/Picture/PictureAlgebra.spec index e88d6d112a..73fa25f9ae 100644 --- a/Algebras/Picture/PictureAlgebra.spec +++ b/Algebras/Picture/PictureAlgebra.spec @@ -18,6 +18,7 @@ operator display alias DISPLAY pattern _ op operator export alias EXPORT pattern _ op [ _ ] operator distance alias DISTANCE pattern op(_,_) +operator distanceRGB alias DISTANCERGB pattern op(_,_) operator getHistHsv8 alias GETHISTHSV8 pattern op(_) operator getHistHsv16 alias GETHISTHSV16 pattern op(_) operator getHistHsv32 alias GETHISTHSV32 pattern op(_) diff --git a/Algebras/Picture/PictureAlgebra_base.cpp b/Algebras/Picture/PictureAlgebra_base.cpp index 22ebb5cca6..844025333e 100644 --- a/Algebras/Picture/PictureAlgebra_base.cpp +++ b/Algebras/Picture/PictureAlgebra_base.cpp @@ -37,6 +37,7 @@ SECONDO types ~picture~ and ~histogram~ are located in other modules. #include "StringUtils.h" #include "hist_hsv.h" #include "JPEGPicture.h" +#include "../FText/FTextAlgebra.h" using namespace std; @@ -721,6 +722,55 @@ Operator distanceOp( +ListExpr distanceRGBTM(ListExpr args) { + if (PA_DEBUG) { + cerr << "distanceRGBTM() called" << endl; + } + if (!nl->HasLength(args, 2)) { + return listutils::typeError(" (wrong number of args)"); + } + if ((!nl->IsEqual(nl->First(args), FText::BasicType()) || + !nl->IsEqual(nl->Second(args), FText::BasicType())) && + (!nl->IsEqual(nl->First(args), Picture::BasicType()) || + !nl->IsEqual(nl->Second(args), Picture::BasicType()))) { + return listutils::typeError(" expected either 'text x text' or 'picture x " + "picture' as arguments"); + } + return nl->SymbolAtom(CcReal::BasicType()); +} + +int distanceRGBVM(Word* args, Word& result, int message, Word& local, + Supplier s) { + Picture* p1 = (Picture*)args[0].addr; + Picture* p2 = (Picture*)args[1].addr; + result = qp->ResultStorage(s); + CcReal* res = (CcReal*)result.addr; + if (!p1->IsDefined() || !p2->IsDefined()) { + res->SetDefined(false); + } + else { + res->Set(true, p1->DistanceRGB(*p2)); + } + return 0; +} + +OperatorSpec distanceRGBSpec( + "picture x picture -> real", + "distanceRGB(_,_)", + "computes the L1 norm of the two RGB vectors in the two png files", + "query distanceRGB(theater, theater)" +); + +Operator distanceRGBOp( + "distanceRGB", + distanceRGBSpec.getStr(), + distanceRGBVM, + SimpleSelect, + distanceRGBTM +); + + + template ListExpr getHistHsvTM(ListExpr args){ string err = "picture expected"; @@ -1138,6 +1188,7 @@ class PictureAlgebra: public Algebra { AddOperator(&exportop); AddOperator(&distanceOp); + // AddOperator(&distanceRGBOp); AddOperator(&getHistHsv8Op); AddOperator(&getHistHsv16Op); AddOperator(&getHistHsv32Op); diff --git a/Algebras/Picture/PictureAlgebra_graphops.cpp b/Algebras/Picture/PictureAlgebra_graphops.cpp index 213f20cb1c..d90e449c64 100644 --- a/Algebras/Picture/PictureAlgebra_graphops.cpp +++ b/Algebras/Picture/PictureAlgebra_graphops.cpp @@ -19,7 +19,8 @@ See the documentation of ~PictureAlgebra.h~ for a general introduction to the Picture algebra. This module contains SECONDO operators on ~picture~, which -deal with graphical operations like scale, cut, mirror and flipleft. +deal with graphical operations like scale, cut, mirror, flipleft, +and distanceRGB. 2 Includes and other preparations @@ -201,6 +202,24 @@ void Picture::Mirror(Picture *pic, bool dir) { delete[] buf; } +double Picture::DistanceRGB(const Picture& pic) const { + Picture pic1 = *this; + Picture pic2 = pic; + Picture *p1 = new Picture(true); + Picture *p2 = new Picture(true); + pic1.Scale(p1, 32, 32); + pic2.Scale(p2, 32, 32); + unsigned long size1, size2; + char* p1data = p1->GetJPEGData(size1); + char* p2data = p2->GetJPEGData(size2); + cout << "files " << p1->GetFilename() << " and " << p2->GetFilename() << endl; + cout << "sizes " << size1 << " and " << size2 << endl; + for (int i = 0; i < 1000; i++) { + cout << "[" << p1data[i] << " " << p2data[i] << "] "; + } + return 0.09; +} + /* 4 Type mapping functions