Пример #1
0
bool
GIFLoad::ReadGIFImageData()
{
	unsigned char newEntry[ENTRY_COUNT];
	unsigned char codeSize;
	if (fInput->Read(&codeSize, 1) < 1)
		return false;

	if (codeSize > fPalette->size_in_bits) {
		if (debug) {
			syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - "
				"Code_size should be %d, not %d, allowing it\n",
				fCodeSize, codeSize);
		}
		if (!InitFrame(codeSize))
			return false;
	} else if (codeSize < fPalette->size_in_bits) {
		if (debug) {
			syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - "
				"Code_size should be %d, not %d\n", fCodeSize, codeSize);
		}
		return false;
	} else if (!InitFrame(fPalette->size_in_bits))
		return false;

	if (debug)
		syslog(LOG_INFO, "GIFLoad::ReadGIFImageData() - Starting LZW\n");

	while ((fNewCode = NextCode()) != -1 && fNewCode != fEndCode) {
		if (fNewCode == fClearCode) {
			ResetTable();
			fNewCode = NextCode();
			fOldCode[0] = fNewCode;
			fOldCodeLength = 1;
			if (!OutputColor(fOldCode, 1))
				goto bad_end;

			if (fNewCode == -1 || fNewCode == fEndCode) {
				if (debug) {
					syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - "
						"Premature fEndCode or error reading fNewCode\n");
				}
				goto bad_end;
			}
			continue;
		}

		// explicitly check for lack of clear code at start of file
		if (fOldCodeLength == 0) {
			fOldCode[0] = fNewCode;
			fOldCodeLength = 1;
			if (!OutputColor(fOldCode, 1))
				goto bad_end;

			continue;
		}

		// error out if we're trying to access an out-of-bounds index
		if (fNextCode >= ENTRY_COUNT)
			goto bad_end;

		if (fTable[fNewCode] != NULL) {
			// exists in table

			if (!OutputColor(fTable[fNewCode], fEntrySize[fNewCode]))
				goto bad_end;

			//memcpy(newEntry, fOldCode, fOldCodeLength);
			for (unsigned int x = 0; x < fOldCodeLength; x++)
				newEntry[x] = fOldCode[x];

			//memcpy(newEntry + fOldCodeLength, fTable[fNewCode], 1);
			newEntry[fOldCodeLength] = fTable[fNewCode][0];
		} else {
			// does not exist in table

			//memcpy(newEntry, fOldCode, fOldCodeLength);
			for (unsigned int x = 0; x < fOldCodeLength; x++)
				newEntry[x] = fOldCode[x];

			//memcpy(newEntry + fOldCodeLength, fOldCode, 1);
			newEntry[fOldCodeLength] = fOldCode[0];

			if (!OutputColor(newEntry, fOldCodeLength + 1))
				goto bad_end;
		}
		fTable[fNextCode] = MemblockAllocate(fOldCodeLength + 1);
		if (fTable[fNextCode] == NULL)
			goto bad_end;

		//memcpy(fTable[fNextCode], newEntry, fOldCodeLength + 1);
		for (unsigned int x = 0; x < fOldCodeLength + 1; x++)
			fTable[fNextCode][x] = newEntry[x];

		fEntrySize[fNextCode] = fOldCodeLength + 1;

		//memcpy(fOldCode, fTable[fNewCode], fEntrySize[fNewCode]);
		for (int x = 0; x < fEntrySize[fNewCode]; x++)
			fOldCode[x] = fTable[fNewCode][x];

		fOldCodeLength = fEntrySize[fNewCode];
		fNextCode++;

		if (fNextCode > fMaxCode && fBits < LZ_MAX_BITS) {
			fBits++;
			fMaxCode = (1 << fBits) - 1;
		}
	}

	MemblockDeleteAll();
	if (fNewCode == -1)
		return false;

	if (debug)
		syslog(LOG_INFO, "GIFLoad::ReadGIFImageData() - Done\n");

	return true;

bad_end:
	if (debug)
		syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Reached a bad end\n");

	MemblockDeleteAll();
	return false;
}
Пример #2
0
void Mesh::CreateSVG(const std::string& filename) const {
  std::ofstream ostr(filename.c_str());
  ostr << "<body bgcolor=dddddd  onLoad=\"render()\" >\n";

  // a few checkboxes at the top of the page to control the visualization
  ostr << "<form name=\"orderForm\">\n";
  ostr << "   <input type=\"checkbox\" name=\"illegal\"   checked  "
       << " onClick=\"render()\">draw illegal (red) & next legal collapse (blue) edges<br>\n";
  ostr << "   <input type=\"checkbox\" name=\"wireframe\"          "
       << " onClick=\"render()\">draw all edges<br>\n";
  ostr << "   <input type=\"checkbox\" name=\"black\"              "
       << " onClick=\"render()\">toggle white vs. black<br>\n";
  ostr << "</form>\n";

  // javascript to actually change the visualization based on the checkboxes
  ostr << "<script language=\"JavaScript\">\n";
  ostr << "  function render() {\n";
  ostr << "    var mysvg = document.getElementById(\"mesh\");\n";
  ostr << "    if (document.orderForm.wireframe.checked == false) {\n";
  ostr << "      var polys = mysvg.children;\n";
  ostr << "      for (var i = 0; i < polys.length; i++) {\n";
  ostr << "        if (polys[i].tagName.toUpperCase() == \"POLYGON\") {\n";
  ostr << "          polys[i].style[\"strokeWidth\"] = \"0\"\n";
  ostr << "        }\n";
  ostr << "      }\n";
  ostr << "    } else if (document.orderForm.black.checked == false) {\n";
  ostr << "      var polys = mysvg.children;\n";
  ostr << "      for (var i = 0; i < polys.length; i++) {\n";
  ostr << "        if (polys[i].tagName.toUpperCase() == \"POLYGON\") {\n";
  ostr << "          polys[i].style.stroke = \"#FFFFFF\"\n";
  ostr << "          polys[i].style[\"strokeWidth\"] = \"1\"\n";
  ostr << "        }\n";
  ostr << "      }\n";
  ostr << "      mysvg.style.background = \"white\"\n";
  ostr << "    } else  {\n";
  ostr << "      var polys = mysvg.children;\n";
  ostr << "      for (var i = 0; i < polys.length; i++) {\n";
  ostr << "        if (polys[i].tagName.toUpperCase() == \"POLYGON\") {\n";
  ostr << "          polys[i].style.stroke = \"#000000\"\n";
  ostr << "          polys[i].style[\"strokeWidth\"] = \"2\"\n";
  ostr << "        }\n";
  ostr << "      }\n";
  ostr << "      mysvg.style.background = \"white\"\n";
  ostr << "    }\n";
  ostr << "    if (document.orderForm.illegal.checked == false) {\n";
  ostr << "      var polys = mysvg.children;\n";
  ostr << "      for (var i = 0; i < polys.length; i++) {\n";
  ostr << "        if (polys[i].tagName.toUpperCase() == \"LINE\") {\n";
  ostr << "          polys[i].style[\"strokeWidth\"] = \"0\"\n";
  ostr << "        }\n";
  ostr << "      }\n";
  ostr << "    } else  {\n";
  ostr << "      var polys = mysvg.children;\n";
  ostr << "      for (var i = 0; i < polys.length; i++) {\n";
  ostr << "        if (polys[i].tagName.toUpperCase() == \"LINE\") {\n";
  ostr << "          polys[i].style[\"strokeWidth\"] = \"5\"\n";
  ostr << "        }\n";
  ostr << "      }\n";
  ostr << "      mysvg.style.background = \"white\"\n";
  ostr << "    }\n";
  ostr << "  }\n";
  ostr << "</script>\n";

  ostr << "<svg  id=\"mesh\" height=\"" << height + 2*BORDER
       << "\" width=\""    << width + 2*BORDER
       << "\" style=\"background:white\" shape-rendering=\"crispEdges\">\n";

  // draw the triangles with the average color of the vertices
  for (triangles_set::const_iterator itr = triangles.begin(); itr != triangles.end(); itr++) {
    Triangle *t = *itr;
    float r = (t->getVertex(0)->r() +t->getVertex(1)->r() +t->getVertex(2)->r()) / 3.0;
    float g = (t->getVertex(0)->g() +t->getVertex(1)->g() +t->getVertex(2)->g()) / 3.0;
    float b = (t->getVertex(0)->b() +t->getVertex(1)->b() +t->getVertex(2)->b()) / 3.0;
    ostr << "<polygon points=\""
         << std::setw(8) << t->getVertex(0)->x() << "," << std::setw(8) << t->getVertex(0)->y() << "  "
         << std::setw(8) << t->getVertex(1)->x() << "," << std::setw(8) << t->getVertex(1)->y() << "  "
         << std::setw(8) << t->getVertex(2)->x() << "," << std::setw(8) << t->getVertex(2)->y()
         << "\" style=\"fill:#" << OutputColor(Vertex(0,0,r,g,b)) 
         << ";stroke:#" << OutputColor(Vertex(0,0,r,g,b)) 
         << ";stroke-width:1"
         << ";stroke-linecap:round\" "
         << ";stroke-linejoin:round\" "
         << "/>" << std::endl;
  }

  // draw the illegal edges in red
  for (edges_map::const_iterator itr = edges.begin(); itr != edges.end(); itr++) {
    const Edge *e = itr->second;
    assert (itr->first.first->getID() < itr->first.second->getID());
    if (!e->isLegal()) {
      ostr << "<line "
           << "x1=\"" << std::setw(8) << e->getV1()->x() << "\" "
           << "y1=\"" << std::setw(8) << e->getV1()->y() << "\" "
           << "x2=\"" << std::setw(8) << e->getV2()->x() << "\" " 
           << "y2=\"" << std::setw(8) << e->getV2()->y() << "\" "
           << " stroke=\"red\" "
           << " stroke-width=\"0\" "
           << " stroke-linecap=\"round\" "
           << "/>" << std::endl;
    }
  }

  // draw the next (legal) edge to collapse in blue
  Edge *e = FindEdge();
  if (e != NULL) {
    ostr << "<line "
         << "x1=\"" << std::setw(8) << e->getV1()->x() << "\" " 
         << "y1=\"" << std::setw(8) << e->getV1()->y() << "\" "
         << "x2=\"" << std::setw(8) << e->getV2()->x() << "\" " 
         << "y2=\"" << std::setw(8) << e->getV2()->y() << "\" "
         << " stroke=\"blue\" "
         << " stroke-width=\"0\" "
         << " stroke-linecap=\"round\" "
         << "/>" << std::endl;
  }
  
  ostr << "</svg>\n";

  // print some simple stats at the bottom of the page
  ostr << "<p>" << *this << "</p>" << std::endl;
  ostr << "</body>\n";
}
Пример #3
0
bool
GIFLoad::ReadGIFImageData() 
{
	unsigned char newEntry[4096];
	
	unsigned char cs;
	fInput->Read(&cs, 1);
	if (cs == fPalette->size_in_bits) {
		if (!InitFrame(fPalette->size_in_bits))
			return false;
	} else if (cs > fPalette->size_in_bits) {
		if (debug)
			syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Code_size should be %d, not "
				"%d, allowing it\n", fCodeSize, cs);
		if (!InitFrame(cs))
			return false;
	} else if (cs < fPalette->size_in_bits) {
		if (debug)
			syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Code_size should be %d, not "
				"%d\n", fCodeSize, cs);
		return false;
	}
	
	if (debug)
		syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Starting LZW\n");
	
	while ((fNewCode = NextCode()) != -1 && fNewCode != fEndCode) {
		if (fNewCode == fClearCode) {
			ResetTable();
			fNewCode = NextCode();
			fOldCode[0] = fNewCode;
			fOldCodeLength = 1;
			if (!OutputColor(fOldCode, 1)) goto bad_end;
			if (fNewCode == -1 || fNewCode == fEndCode) {
				if (debug)
					syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Premature fEndCode "
						"or error\n");
				goto bad_end;
			}
			continue;
		}
		
		// Explicitly check for lack of clear code at start of file
		if (fOldCodeLength == 0) {
			fOldCode[0] = fNewCode;
			fOldCodeLength = 1;
			if (!OutputColor(fOldCode, 1))
				goto bad_end;
			continue;
		}
		
		if (fTable[fNewCode] != NULL) { // Does exist in table
			if (!OutputColor(fTable[fNewCode], fEntrySize[fNewCode]))
				goto bad_end;
			
			//memcpy(newEntry, fOldCode, fOldCodeLength);
			for (int x = 0; x < fOldCodeLength; x++) {
				newEntry[x] = fOldCode[x];
			}
			
			//memcpy(newEntry + fOldCodeLength, fTable[fNewCode], 1);
			newEntry[fOldCodeLength] = *fTable[fNewCode];
		} else { // Does not exist in table
			//memcpy(newEntry, fOldCode, fOldCodeLength);
			for (int x = 0; x < fOldCodeLength; x++) {
				newEntry[x] = fOldCode[x];
			}
			
			//memcpy(newEntry + fOldCodeLength, fOldCode, 1);
			newEntry[fOldCodeLength] = *fOldCode;
			
			if (!OutputColor(newEntry, fOldCodeLength + 1))
				goto bad_end;
		}
		fTable[fNextCode] = MemblockAllocate(fOldCodeLength + 1);

		//memcpy(fTable[fNextCode], newEntry, fOldCodeLength + 1);
		for (int x = 0; x < fOldCodeLength + 1; x++) {
			fTable[fNextCode][x] = newEntry[x];
		}
		
		fEntrySize[fNextCode] = fOldCodeLength + 1;
		
		//memcpy(fOldCode, fTable[fNewCode], fEntrySize[fNewCode]);
		for (int x = 0; x < fEntrySize[fNewCode]; x++) {
			fOldCode[x] = fTable[fNewCode][x];
		}
		
		fOldCodeLength = fEntrySize[fNewCode];
		fNextCode++;
		
		if (fNextCode > fMaxCode && fBits != 12) {
			fBits++;
			fMaxCode = (1 << fBits) - 1;
		}
	}

	MemblockDeleteAll();
	if (fNewCode == -1)
		return false;
	if (debug)
		syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Done\n");
	return true;
	
bad_end:
	if (debug)
		syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Reached a bad end\n");
	MemblockDeleteAll();
	return false;
}