/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

  1. // Copyright (C) 2000 Dominic Letourneau (doumdi@yahoo.com)
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #include "SquareSum.h"
  17. #include "Object.h"
  18. #include "ObjectRef.h"
  19. #include "Exception.h"
  20. #include "Kernel.h"
  21. #include <math.h>
  22. using namespace std;
  23. NODE_INFO(SquareSum,"Image", "IMAGE1_IN:IMAGE2_IN", "IMAGE_OUT", "")
  24. SquareSum::SquareSum(string nodeName, ParameterSet params)
  25. : Node(nodeName, params)
  26. {
  27. // Add the node's input
  28. m_IMAGE1_ID = addInput("IMAGE1_IN");
  29. m_IMAGE2_ID = addInput("IMAGE2_IN");
  30. // Add the node's output
  31. addOutput("IMAGE_OUT");
  32. }
  33. SquareSum::~SquareSum()
  34. {
  35. return;
  36. }
  37. ObjectRef SquareSum::getOutput (int output_id, int count) {
  38. if (!hasOutput(output_id)) throw new NodeException (this, "Cannot getOutput id",__FILE__,__LINE__);
  39. if (count != processCount) {
  40. //We are updating our output only if needed
  41. try {
  42. //getting all data from our inputs.
  43. int OutputID = inputs[m_IMAGE1_ID].outputID;
  44. GreyScale8Image &im1 = object_cast<GreyScale8Image>(inputs[m_IMAGE1_ID].node->getOutput(OutputID, count));
  45. OutputID = inputs[m_IMAGE2_ID].outputID;
  46. GreyScale8Image &im2 = object_cast<GreyScale8Image>(inputs[m_IMAGE2_ID].node->getOutput(OutputID,count));
  47. if (im1.get_width() != im2.get_width() ||
  48. im1.get_height() != im2.get_height()) {
  49. throw new NodeException (this, "Images must be the same widths and heights",__FILE__,__LINE__);
  50. }
  51. int width = im1.get_width();
  52. int height = im2.get_height();
  53. GreyScale8Image *im_ptr = new GreyScale8Image(width,height);
  54. unsigned char *im1_ptr = im1.get_data();
  55. unsigned char *im2_ptr = im2.get_data();
  56. unsigned char *data = im_ptr->get_data();
  57. for (int i = 0; i < height * width; i++) {
  58. *data++ = (unsigned char) sqrt(pow(*im1_ptr++,2) + pow(*im2_ptr++,2));
  59. }
  60. m_output = ObjectRef(im_ptr);
  61. } //end of try block
  62. catch (BaseException *e) {
  63. //Something weird happened
  64. //e->print();
  65. throw e->add(new NodeException (this,
  66. string("error caught in SquareSum::getOutput(int,int)"),
  67. __FILE__,__LINE__));
  68. }
  69. //updating processCount
  70. processCount = count;
  71. }
  72. return (m_output);
  73. }