/tags/rel-0-1-0/FreeSpeech/video_blocks/src/SquareSum.cc
# · C++ · 100 lines · 51 code · 27 blank · 22 comment · 7 complexity · a21e106aaf48de7d828be02b6e460a7e MD5 · raw file
- // Copyright (C) 2000 Dominic Letourneau (doumdi@yahoo.com)
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2, or (at your option)
- // any later version.
- //
- // This program is distributed in the hope that it will be useful, but
- // WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this file. If not, write to the Free Software Foundation,
- // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- #include "SquareSum.h"
- #include "Object.h"
- #include "ObjectRef.h"
- #include "Exception.h"
- #include "Kernel.h"
- #include <math.h>
- using namespace std;
- NODE_INFO(SquareSum,"Image", "IMAGE1_IN:IMAGE2_IN", "IMAGE_OUT", "")
- SquareSum::SquareSum(string nodeName, ParameterSet params)
- : Node(nodeName, params)
- {
- // Add the node's input
- m_IMAGE1_ID = addInput("IMAGE1_IN");
- m_IMAGE2_ID = addInput("IMAGE2_IN");
-
- // Add the node's output
- addOutput("IMAGE_OUT");
-
- }
- SquareSum::~SquareSum()
- {
- return;
- }
- ObjectRef SquareSum::getOutput (int output_id, int count) {
- if (!hasOutput(output_id)) throw new NodeException (this, "Cannot getOutput id",__FILE__,__LINE__);
-
- if (count != processCount) {
- //We are updating our output only if needed
-
- try {
-
- //getting all data from our inputs.
- int OutputID = inputs[m_IMAGE1_ID].outputID;
- GreyScale8Image &im1 = object_cast<GreyScale8Image>(inputs[m_IMAGE1_ID].node->getOutput(OutputID, count));
-
- OutputID = inputs[m_IMAGE2_ID].outputID;
- GreyScale8Image &im2 = object_cast<GreyScale8Image>(inputs[m_IMAGE2_ID].node->getOutput(OutputID,count));
-
- if (im1.get_width() != im2.get_width() ||
- im1.get_height() != im2.get_height()) {
- throw new NodeException (this, "Images must be the same widths and heights",__FILE__,__LINE__);
- }
-
- int width = im1.get_width();
- int height = im2.get_height();
-
- GreyScale8Image *im_ptr = new GreyScale8Image(width,height);
-
- unsigned char *im1_ptr = im1.get_data();
- unsigned char *im2_ptr = im2.get_data();
- unsigned char *data = im_ptr->get_data();
- for (int i = 0; i < height * width; i++) {
- *data++ = (unsigned char) sqrt(pow(*im1_ptr++,2) + pow(*im2_ptr++,2));
- }
- m_output = ObjectRef(im_ptr);
- } //end of try block
- catch (BaseException *e) {
- //Something weird happened
- //e->print();
- throw e->add(new NodeException (this,
- string("error caught in SquareSum::getOutput(int,int)"),
- __FILE__,__LINE__));
- }
-
- //updating processCount
- processCount = count;
- }
-
-
- return (m_output);
-
- }