LuminanceSource.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
  2. /*
  3. * LuminanceSource.cpp
  4. * zxing
  5. *
  6. * Copyright 2008 ZXing authors All rights reserved.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include <sstream>
  21. #include <zxing/LuminanceSource.h>
  22. #include <zxing/InvertedLuminanceSource.h>
  23. #include <zxing/common/IllegalArgumentException.h>
  24. using zxing::Ref;
  25. using zxing::LuminanceSource;
  26. LuminanceSource::LuminanceSource(int width_, int height_) :width(width_), height(height_) {}
  27. LuminanceSource::~LuminanceSource() {}
  28. bool LuminanceSource::isCropSupported() const {
  29. return false;
  30. }
  31. Ref<LuminanceSource> LuminanceSource::crop(int, int, int, int) const {
  32. throw IllegalArgumentException("This luminance source does not support cropping.");
  33. }
  34. bool LuminanceSource::isRotateSupported() const {
  35. return false;
  36. }
  37. Ref<LuminanceSource> LuminanceSource::rotateCounterClockwise() const {
  38. throw IllegalArgumentException("This luminance source does not support rotation.");
  39. }
  40. LuminanceSource::operator std::string() const {
  41. ArrayRef<char> row;
  42. std::ostringstream oss;
  43. for (int y = 0; y < getHeight(); y++) {
  44. row = getRow(y, row);
  45. for (int x = 0; x < getWidth(); x++) {
  46. int luminance = row[x] & 0xFF;
  47. char c;
  48. if (luminance < 0x40) {
  49. c = '#';
  50. } else if (luminance < 0x80) {
  51. c = '+';
  52. } else if (luminance < 0xC0) {
  53. c = '.';
  54. } else {
  55. c = ' ';
  56. }
  57. oss << c;
  58. }
  59. oss << '\n';
  60. }
  61. return oss.str();
  62. }
  63. Ref<LuminanceSource> LuminanceSource::invert() const {
  64. // N.B.: this only works because we use counted objects with the
  65. // count in the object. This is _not_ how things like shared_ptr
  66. // work. They do not allow you to make a smart pointer from a native
  67. // pointer more than once. If we ever switch to (something like)
  68. // shared_ptr's, the luminace source is going to have keep a weak
  69. // pointer to itself from which it can create a strong pointer as
  70. // needed. And, FWIW, that has nasty semantics in the face of
  71. // exceptions during construction.
  72. return Ref<LuminanceSource>
  73. (new InvertedLuminanceSource(Ref<LuminanceSource>(const_cast<LuminanceSource*>(this))));
  74. }