From 5c4506a8c5ee1a82268146007619b09e980a7a57 Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 2 May 2009 17:28:21 +0200 Subject: add some assert() to make tutor happy --- ue2/imgsynth2/cbitmap.cpp | 22 ++++++++++++++++++++-- ue2/imgsynth2/cpixelformat_bgr24.cpp | 3 +++ ue2/imgsynth2/cpixelformat_bgr555.cpp | 3 +++ ue2/imgsynth2/cpixelformat_indexed8.cpp | 6 +++++- ue2/imgsynth2/cpixmap.cpp | 6 ++++++ ue2/imgsynth2/cwindowsbitmap.cpp | 2 ++ 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/ue2/imgsynth2/cbitmap.cpp b/ue2/imgsynth2/cbitmap.cpp index 3d206f5..3bff857 100644 --- a/ue2/imgsynth2/cbitmap.cpp +++ b/ue2/imgsynth2/cbitmap.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "cbitmap.h" using namespace std; @@ -28,7 +29,6 @@ CBitmap::~CBitmap() m_pixelformat = NULL; /* delete colortable content */ - //map::iterator it2; map::iterator it2; for(it2 = m_colortable.begin(); it2 != m_colortable.end(); it2++) delete (*it2).second; @@ -67,6 +67,10 @@ void CBitmap::fillrect(std::list params) if (m_pixeldata == NULL || m_pixelformat == NULL) return; + assert(getHeight() > 0); + assert(getWidth() > 0); + assert(getPixelDataSize() > 0); + /* convert parameters */ uint32_t pparams[7]; int i = 0; @@ -106,7 +110,7 @@ void CBitmap::fillrect(std::list params) pixel.blue = pparams[6]; /* call setPixel for every pixel in the rectangel */ - /* NOTE: maybe use fill() here? */ + /* NOTE: maybe use std::fill() here? */ for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++) { for(uint32_t j = pparams[1]; j < pparams[3] + pparams[1]; j++) @@ -139,6 +143,10 @@ void CBitmap::invert(std::list params) if (m_pixeldata == NULL || m_pixelformat == NULL) return; + assert(getHeight() > 0); + assert(getWidth() > 0); + assert(getPixelDataSize() > 0); + CPixelFormat::RGBPIXEL pixel; CPixelFormat::RGBPIXEL max; m_pixelformat->getMaxColor(max); @@ -261,6 +269,11 @@ void CBitmap::mirror_y(std::list params) if (m_pixeldata == NULL || m_pixelformat == NULL) return; + assert(m_rowsize > 0); + assert(getHeight() > 0); + assert(getWidth() > 0); + assert(getPixelDataSize() > 0); + uint8_t *buf = new uint8_t[m_rowsize]; for(uint32_t i = 0; i < getHeight()/2; i++) { @@ -296,6 +309,11 @@ void CBitmap::mirror_x(std::list params) /* calc pixelwidth */ unsigned int pixelwidth = (hasColorTable()) ? sizeof(uint32_t) : m_pixelformat->getBitCount()/8; + assert(m_rowsize > 0); + assert(getHeight() > 0); + assert(getWidth() > 0); + assert(getPixelDataSize() > 0); + uint8_t *buf = new uint8_t[pixelwidth]; for(uint32_t i = 0; i < getHeight(); i++) { diff --git a/ue2/imgsynth2/cpixelformat_bgr24.cpp b/ue2/imgsynth2/cpixelformat_bgr24.cpp index bc95ab9..2332142 100644 --- a/ue2/imgsynth2/cpixelformat_bgr24.cpp +++ b/ue2/imgsynth2/cpixelformat_bgr24.cpp @@ -6,6 +6,7 @@ */ #include +#include #include "cpixelformat_bgr24.h" #include "cbitmap.h" @@ -15,6 +16,7 @@ void CPixelFormat_BGR24::getPixel(RGBPIXEL& pixel, uint32_t x, uint32_t y) { if (m_bitmap->getPixelData() == NULL) throw PixelFormatError("No pixelbuffer allocated."); + assert(m_bitmap->getPixelDataSize() > 0); /* if the y-coordinates are mirrored */ if (m_bitmap->isMirrored()) @@ -37,6 +39,7 @@ void CPixelFormat_BGR24::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t y) { if (m_bitmap->getPixelData() == NULL) throw PixelFormatError("No pixelbuffer allocated."); + assert(m_bitmap->getPixelDataSize() > 0); /* if the y-coordinates are mirrored */ if (m_bitmap->isMirrored()) diff --git a/ue2/imgsynth2/cpixelformat_bgr555.cpp b/ue2/imgsynth2/cpixelformat_bgr555.cpp index 657c148..ba12cb5 100644 --- a/ue2/imgsynth2/cpixelformat_bgr555.cpp +++ b/ue2/imgsynth2/cpixelformat_bgr555.cpp @@ -7,6 +7,7 @@ */ #include +#include #include "cpixelformat_bgr555.h" #include "cbitmap.h" @@ -16,6 +17,7 @@ void CPixelFormat_BGR555::getPixel(RGBPIXEL& pixel, uint32_t x, uint32_t y) { if (m_bitmap->getPixelData() == NULL) throw PixelFormatError("No pixelbuffer allocated."); + assert(m_bitmap->getPixelDataSize() > 0); /* if the y-coordinates are mirrored */ if (m_bitmap->isMirrored()) @@ -39,6 +41,7 @@ void CPixelFormat_BGR555::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t y { if (m_bitmap->getPixelData() == NULL) throw PixelFormatError("No pixelbuffer allocated."); + assert(m_bitmap->getPixelDataSize() > 0); /* if the y-coordinates are mirrored */ if (m_bitmap->isMirrored()) diff --git a/ue2/imgsynth2/cpixelformat_indexed8.cpp b/ue2/imgsynth2/cpixelformat_indexed8.cpp index 21b0988..d92ae9f 100644 --- a/ue2/imgsynth2/cpixelformat_indexed8.cpp +++ b/ue2/imgsynth2/cpixelformat_indexed8.cpp @@ -6,6 +6,7 @@ */ #include +#include #include "cpixelformat_indexed8.h" using namespace std; @@ -16,6 +17,7 @@ void CPixelFormat_Indexed8::getPixel(RGBPIXEL& pixel, uint32_t x, uint32_t y) throw PixelFormatError("No pixelbuffer allocated."); if (m_bitmap->getColorTable().size() == 0) return; + assert(m_bitmap->getPixelDataSize() > 0); uint32_t offset = y * m_bitmap->getWidth() + x; @@ -40,8 +42,10 @@ void CPixelFormat_Indexed8::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t { if (m_bitmap->getPixelData() == NULL) throw PixelFormatError("No pixelbuffer allocated."); + /* if colortable is empty there are no pixels */ if (m_bitmap->getColorTable().size() == 0) return; + assert(m_bitmap->getPixelDataSize() > 0); uint32_t offset = y * m_bitmap->getWidth() + x; @@ -60,7 +64,7 @@ void CPixelFormat_Indexed8::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t } uint32_t index = (*it).first; - /* need to get a new character for our color */ + /* need to get a new entry for our color */ if (it == m_bitmap->getColorTable().end()) { index = (*it).first + 1; diff --git a/ue2/imgsynth2/cpixmap.cpp b/ue2/imgsynth2/cpixmap.cpp index 94f0310..52f9825 100644 --- a/ue2/imgsynth2/cpixmap.cpp +++ b/ue2/imgsynth2/cpixmap.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef DEBUG # include #endif @@ -123,6 +124,7 @@ void CPixmap::read(std::ifstream& in) throw FileError("Pixmap has no imagename."); /* additional: check "{" exists */ + assert(!line.empty()); if (line[line.length() - 1] != '{') throw FileError("Pixmap array has no opening bracket."); @@ -236,6 +238,7 @@ void CPixmap::read(std::ifstream& in) for(uint32_t x = 0; x < getWidth(); x++) { character = line.substr(x * m_fileheader.nChar, m_fileheader.nChar); + assert(!character.empty()); if (colornr.find(character) == colornr.end()) throw FileError("Pixel has no reference in colortable."); @@ -265,6 +268,7 @@ void CPixmap::read(std::ifstream& in) const std::string CPixmap::getXPMColorID(unsigned int index, unsigned int length) { static const char code[] = PIXMAP_COLORCHARS; + assert(strlen(code) > 0); string str(""); for(unsigned int i = length - 1; i > 0; i--) { @@ -272,6 +276,7 @@ const std::string CPixmap::getXPMColorID(unsigned int index, unsigned int length index /= strlen(code); } str += code[index]; + assert(!str.empty()); return str; } @@ -286,6 +291,7 @@ void CPixmap::write(std::ofstream& out) out << PIXMAP_IDENTIFIER << endl; /* variables*/ + assert(!m_imagename.empty()); out << "static char * " << m_imagename << "[] = {" << endl; out << "\"" << m_fileheader.width << " " << m_fileheader.height << " " << m_fileheader.nColor << " " << m_fileheader.nChar; diff --git a/ue2/imgsynth2/cwindowsbitmap.cpp b/ue2/imgsynth2/cwindowsbitmap.cpp index d561465..83954b6 100644 --- a/ue2/imgsynth2/cwindowsbitmap.cpp +++ b/ue2/imgsynth2/cwindowsbitmap.cpp @@ -7,6 +7,7 @@ #include #include +#include #ifdef DEBUG # include #endif @@ -58,6 +59,7 @@ void CWindowsBitmap::read(std::ifstream& in) if (m_pixeldata != NULL) delete[] m_pixeldata; m_pixeldata = new uint8_t[m_infoheader.biSizeImage]; + assert(m_fileheader.bfOffBits > 0); in.seekg(m_fileheader.bfOffBits); in.read(reinterpret_cast(m_pixeldata), m_infoheader.biSizeImage); } -- cgit v1.2.3