Skip to content

Commit

Permalink
operator for RGB distance added; to be replaced by a version for a ne…
Browse files Browse the repository at this point in the history
…w data type Picture1024
  • Loading branch information
fabioValdes9 committed Jul 3, 2024
1 parent 0d81fa4 commit 233457a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Algebras/Picture/PictureAlgebra.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Algebras/Picture/PictureAlgebra.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down
51 changes: 51 additions & 0 deletions Algebras/Picture/PictureAlgebra_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<unsigned int dim, bool lab>
ListExpr getHistHsvTM(ListExpr args){
string err = "picture expected";
Expand Down Expand Up @@ -1138,6 +1188,7 @@ class PictureAlgebra: public Algebra {
AddOperator(&exportop);

AddOperator(&distanceOp);
// AddOperator(&distanceRGBOp);
AddOperator(&getHistHsv8Op);
AddOperator(&getHistHsv16Op);
AddOperator(&getHistHsv32Op);
Expand Down
21 changes: 20 additions & 1 deletion Algebras/Picture/PictureAlgebra_graphops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 233457a

Please sign in to comment.