From cb1b152452df421c011b1fb82c7bd99f68e0928d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Fri, 23 Jan 2015 16:02:50 +0100 Subject: [PATCH] Converter can now reuse IDs of deleted compositions. --- converter/converter.cc | 39 +++++++++++++++++++++++++-------------- converter/converter.hh | 1 + converter/main.cc | 8 +++++--- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/converter/converter.cc b/converter/converter.cc index d4314f6..373c73d 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -33,8 +33,14 @@ Converter::Converter(std::string level){ //Load the Level xml file + nextID.push_back(0); nextID.push_back(1); - nextID.push_back(1); + for (int i=0; i<256; i++){ + for (int j=0; j<256; j++){ + idUsed[i][j] = false; + } + } + idUsed[0][0] = true; const char* charXmlFile = xmlFile.c_str(); doc->LoadFile(charXmlFile); if (doc->ErrorID()!=0){ @@ -127,14 +133,11 @@ Converter::Converter(std::string level){ }else{ dst << src.rdbuf(); XMLElement* thisComposition = doc->FirstChildElement("composition"); - int idGreen, idBlue; for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ + int idGreen = 0, idBlue = 0; errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue)); - if(idGreen > nextID[0] || (idGreen == nextID[0] && idBlue > nextID[1])){ - nextID[0] = idGreen; - nextID[1] = idBlue; - } + idUsed[idGreen][idBlue] = true; } } } @@ -148,6 +151,17 @@ Converter::~Converter(){ std::vector Converter::newComposition(int type, float posX, float posZ){ XMLElement* newComposition = doc->NewElement("composition"); doc->InsertFirstChild(newComposition); + while(idUsed[nextID[0]][nextID[1]]){ + nextID[1] += 1; + if (nextID[1] == 256){ + nextID[1] = 0; + nextID[0] +=1; + if (nextID[0] == 256){ + std::cout << "Can not have more than 65535 compositions." << std::endl; + exit(-1); + } + } + } XMLElement* typeID = doc->NewElement("typeID"); XMLElement* idBlue = doc->NewElement("idBlue"); @@ -230,13 +244,8 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ trigger->InsertEndChild(toChangeIdBlue); trigger->InsertEndChild(toChangeObjNum); - std::vector ret = nextID; - nextID[1] += 1; - if (nextID[1] == 255){ - nextID[1] = 0; - nextID[0] +=1; - } - return ret; + idUsed[nextID[0]][nextID[1]] = true; + return nextID; } void Converter::updateComposition(int idG, int idB, float posX, float posZ){ @@ -249,6 +258,7 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){ if(idGreen == idG && idBlue == idB){ if (compositionExists){ std::cout << "An ID is used for multiple compositions in the xml." << std::endl; + exit(-1); } bool manualPos; errorCheck(thisComposition->FirstChildElement("manualPos")->QueryBoolText(&manualPos)); @@ -267,8 +277,8 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){ void Converter::deleteComposition(int idG, int idB){ XMLElement* thisComposition = doc->FirstChildElement("composition"); - int idGreen, idBlue; for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ + int idGreen = 0, idBlue = 0; errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue)); if(idGreen == idG && idBlue == idB){ @@ -304,5 +314,6 @@ void Converter::errorCheck(XMLError error){ else { printf("Unknown error.\n"); } + exit(-1); } } diff --git a/converter/converter.hh b/converter/converter.hh index bfface4..92f1c43 100644 --- a/converter/converter.hh +++ b/converter/converter.hh @@ -20,6 +20,7 @@ class Converter { private: void errorCheck(XMLError error); std::vector nextID; + bool idUsed[256][256]; std::string xmlFile; XMLDocument* doc = new XMLDocument(); XMLDocument* compositions = new XMLDocument(); diff --git a/converter/main.cc b/converter/main.cc index 248c9ec..2e881e6 100644 --- a/converter/main.cc +++ b/converter/main.cc @@ -25,6 +25,7 @@ int main( int argc, char *argv[] ){ unsigned error = lodepng::decode(image, width, height, filePath); if (error) { std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl; + exit(-1); } //iterate over all pixels of the image @@ -33,13 +34,13 @@ int main( int argc, char *argv[] ){ unsigned int pixel = (rowNum*width+columnNum)*4; //if there is a composition here, adjust the xml and image if(image[pixel]!=0 && image[pixel]!=255){ - if((image[pixel+1]==0 && image[pixel+2]==0) || (image[pixel+1]==255 && image[pixel+2]==255)){ + if(image[pixel+1]==0 && image[pixel+2]==0){//composition has no ID std::vector temp; temp = conv.newComposition(image[pixel], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); idFound[temp[0]][temp[1]] = true; image[pixel+1] = temp[0]; image[pixel+2] = temp[1]; - }else{ + }else{//composition has an ID conv.updateComposition(image[pixel+1], image[pixel+2], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); idFound[image[pixel+1]][image[pixel+2]] = true; } @@ -51,10 +52,11 @@ int main( int argc, char *argv[] ){ error = lodepng::encode(filePath, image, width, height); if(error) { std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl; + exit(-1); } //delete compositions that were not in the png anymore - for (int i=0; i<=conv.getNextID()[1]; i++){ + for (int i=0; i<256; i++){ for (int j=0; j<256; j++){ if (! idFound[i][j]){ conv.deleteComposition(i,j);