DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bitmap.cpp Source File

Bitmap.cpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #include <algorithm>
00034 #include "Bitmap.hpp"
00035 
00036 static const int minWidthHeight = 1;
00037 static const int pixelsPerSegment = 8;
00038 
00039 template <typename T>
00040 static T divideRoundUp(const T dividend, const T divisor) {
00041   return (dividend / divisor) + (((dividend % divisor) == 0) ? 0 : 1);
00042 }
00043 
00044 static int calculateSegmentsPerLine(int width) {
00045   return divideRoundUp(width, pixelsPerSegment);
00046 }
00047 
00048 static int calculateHeight(size_t size, int width) {
00049   return divideRoundUp<size_t>(size, calculateSegmentsPerLine(width));
00050 }
00051 
00052 static int calculateSegmentIndex(int x, int y, int width) {
00053   return y * calculateSegmentsPerLine(width) + x / pixelsPerSegment;
00054 }
00055 
00056 static unsigned int calculatePixelMask(int x) {
00057   return 1 << (pixelsPerSegment - 1 - (x % pixelsPerSegment));
00058 }
00059 
00060 Bitmap::Bitmap(int width, int height)
00061     : width_(std::max(width, minWidthHeight)),
00062       height_(std::max(height, minWidthHeight)),
00063       data_(calculateSegmentsPerLine(width_) * height_, 0x00) {}
00064 
00065 Bitmap::Bitmap(const uint8_t * data, size_t size, int width)
00066     : width_(std::max(width, minWidthHeight)),
00067       height_(std::max(calculateHeight(size, width), minWidthHeight)),
00068       data_(calculateSegmentsPerLine(width_) * height_, 0x00) {
00069   std::copy(data, data + size, data_.begin());
00070 }
00071 
00072 bool Bitmap::pixelEnabled(int x, int y) const {
00073   bool enabled = false;
00074   if ((x >= 0) && (x < width_) && (y >= 0) && (y < height_)) {
00075     enabled =
00076         data_[calculateSegmentIndex(x, y, width_)] & calculatePixelMask(x);
00077   }
00078   return enabled;
00079 }
00080 
00081 void Bitmap::setPixelEnabled(int x, int y, bool enabled) {
00082   if ((x >= 0) && (x < width_) && (y >= 0) && (y < height_)) {
00083     uint8_t & dataSegment = data_[calculateSegmentIndex(x, y, width_)];
00084     const unsigned int dataMask = calculatePixelMask(x);
00085     if (enabled) {
00086       dataSegment |= dataMask;
00087     } else {
00088       dataSegment &= ~dataMask;
00089     }
00090   }
00091 }
00092 
00093 void Bitmap::overlay(int x, int y, const uint8_t * data, size_t size,
00094                      int width) {
00095   if (width < minWidthHeight) {
00096     return;
00097   }
00098 
00099   const int segmentsPerLine = calculateSegmentsPerLine(width);
00100   for (size_t segment = 0; segment < size; ++segment) {
00101     const int curY = segment / segmentsPerLine;
00102     if (!((y + curY) < height_)) {
00103       break;
00104     }
00105     for (int pixel = 0; pixel < pixelsPerSegment; ++pixel) {
00106       const int curX = (segment % segmentsPerLine) * pixelsPerSegment + pixel;
00107       if (!(((x + curX) < width_) && (curX < width))) {
00108         break;
00109       }
00110       setPixelEnabled(x + curX, y + curY,
00111                       data[segment] & (1 << (pixelsPerSegment - 1 - pixel)));
00112     }
00113   }
00114 }
00115 
00116 void Bitmap::overlay(int x, int y, const Bitmap & src) {
00117   overlay(x, y, &src.data_[0], src.data_.size(), src.width_);
00118 }
00119 
00120 void Bitmap::clear() { std::fill(data_.begin(), data_.end(), 0); }
00121 
00122 void Bitmap::clear(int x, int y, int width, int height) {
00123   if (!((x >= 0) && (x < width_) && (y >= 0) && (y < height_))) {
00124     return;
00125   }
00126   if ((x + width) > width_) {
00127     width = width_ - x;
00128   }
00129   if ((y + height) > height_) {
00130     height = height_ - y;
00131   }
00132 
00133   const int startX = x;
00134   const int startY = y;
00135   while (y < (startY + height)) {
00136     x = startX;
00137     while (x < (startX + width)) {
00138       if (((x % pixelsPerSegment) == 0) &&
00139           ((x + pixelsPerSegment) < (startX + width))) {
00140         data_[calculateSegmentIndex(x, y, width_)] = 0;
00141         x += pixelsPerSegment;
00142       } else {
00143         setPixelEnabled(x, y, false);
00144         ++x;
00145       }
00146     }
00147     ++y;
00148   }
00149 }