コード例 #1
0
ファイル: LbpMrf.cpp プロジェクト: 2php/ShadowDetection
void LbpMrf::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
{
  if(img_input.empty())
    return;

  loadConfig();

  if(firstTime)
  {
    saveConfig();
  }

  IplImage TempImage(img_input);
  MEImage InputImage(img_input.cols, img_input.rows, img_input.channels());
  MEImage OutputImage(img_input.cols, img_input.rows, img_input.channels());

  InputImage.SetIplImage((void*)&TempImage);

  Detector->DetectMotions(InputImage);
  Detector->GetMotionsMask(OutputImage);
  img_output = (IplImage*)OutputImage.GetIplImage();
  bitwise_not(img_output, img_bgmodel);

  if(showOutput)
  {
    cv::imshow("LBP-MRF FG", img_output);
    cv::imshow("LBP-MRF BG", img_bgmodel);
  }

  firstTime = false;
}
コード例 #2
0
ファイル: Effects.cpp プロジェクト: alanhorizon/Transcendence
void GenerateEffectImage (CUniverse &Universe, CXMLElement *pCmdLine)
	{
	CString sError;
	int i, j;

	//	Input file

	CString sInput = pCmdLine->GetAttribute(CONSTLIT("input"));
	if (sInput.IsBlank())
		{
		printf("Input file required.\n");
		return;
		}

	//	Output file (optional)

	CString sFilespec = pCmdLine->GetAttribute(CONSTLIT("output"));
	if (!sFilespec.IsBlank())
		sFilespec = pathAddExtensionIfNecessary(sFilespec, CONSTLIT(".bmp"));

	//	Load a resource file so that we can create a design load context

	CResourceDb Resources(CONSTLIT("Transcendence"));
	if (Resources.Open(DFOPEN_FLAG_READ_ONLY, &sError) != NOERROR)
		{
		printf("%s\n", (LPSTR)sError);
		return;
		}

	CExternalEntityTable *pEntities;
	if (Resources.LoadEntities(&sError, &pEntities) != NOERROR)
		{
		printf("%s\n", sError.GetASCIIZPointer());
		return;
		}

	//	Generate a list of effect render structures

	TArray<SEffectRender> Effects;

	//	Load the input file

	CFileReadBlock InputFile(sInput);
	if (InputFile.Open() != NOERROR)
		{
		printf("Unable to open input file: %s\n", sInput.GetASCIIZPointer());
		return;
		}

	//	Parse the file

	CXMLElement *pRenderFile;
	if (CXMLElement::ParseXML(&InputFile, pEntities, &pRenderFile, &sError) != NOERROR)
		{
		printf("%s\n", sError.GetASCIIZPointer());
		return;
		}

	//	Keep track of the max cell size and frame count

	int cxCellWidth = 0;
	int cyCellHeight = 0;
	int iCellsPerEffect = 1;

	//	Generate structures

	SDesignLoadCtx LoadCtx;
	LoadCtx.sResDb = Resources.GetFilespec();
	LoadCtx.pResDb = &Resources;
	LoadCtx.bNoVersionCheck = true;

	for (i = 0; i < pRenderFile->GetContentElementCount(); i++)
		{
		CXMLElement *pRender = pRenderFile->GetContentElement(i);
		CXMLElement *pEffectDesc = pRender->GetContentElementByTag(CONSTLIT("Effect"));
		if (pEffectDesc == NULL)
			{
			printf("<Effect> tag required.\n");
			return;
			}

		SEffectRender *pEffect = Effects.Insert();

		//	Parse the effect

		if (pEffect->pEffectCreator.LoadEffect(LoadCtx, CONSTLIT("none"), pEffectDesc, NULL_STR) != NOERROR)
			{
			printf("%s\n", LoadCtx.sError.GetASCIIZPointer());
			return;
			}

		//	Bind

		if (pEffect->pEffectCreator.Bind(LoadCtx) != NOERROR)
			{
			printf("%s\n", LoadCtx.sError.GetASCIIZPointer());
			return;
			}

		//	Create a painter

		pEffect->pPainter = pEffect->pEffectCreator->CreatePainter();
		if (pEffect->pPainter == NULL)
			{
			printf("Unable to create painter.\n");
			return;
			}

		//	Render specs

		int cyHeight = pRender->GetAttributeIntegerBounded(CONSTLIT("height"), 0, -1, 128);
		int cxWidth = pRender->GetAttributeIntegerBounded(CONSTLIT("width"), 0, -1, 128);

		cxCellWidth = Max(cxCellWidth, cxWidth);
		cyCellHeight = Max(cyCellHeight, cyHeight);

		//	Figure out how many animation cells

		pEffect->iLifetime = Max(1, pEffect->pEffectCreator->GetLifetime());
		iCellsPerEffect = Max(iCellsPerEffect, pEffect->iLifetime);
		}

	//	Create the resulting image

	CImageGrid Output;
	Output.Create(iCellsPerEffect * Effects.GetCount(), cxCellWidth, cyCellHeight);

	//	Paint

	for (i = 0; i < Effects.GetCount(); i++)
		{
		SEffectRender *pEffect = &Effects[i];

		SViewportPaintCtx Ctx;

		for (j = 0; j < pEffect->iLifetime; j++)
			{
			int x, y;
			Output.GetCellCenter(i * iCellsPerEffect + j, &x, &y);

			//	Create a context

			Ctx.iTick = j;

			//	Paint the effect

			pEffect->pPainter->Paint(Output.GetImage(), x, y, Ctx);

			//	Update

			pEffect->pPainter->OnUpdate();
			}
		}

	//	Output

	OutputImage(Output.GetImage(), sFilespec);
	}
コード例 #3
0
void GenerateImageChart (CUniverse &Universe, CXMLElement *pCmdLine)
	{
	int i;

	enum OrderTypes
		{
		orderSmallest = 1,
		orderLargest = 2,
		orderName = 3,
		orderLevel = 4,
		orderSovereign = 5,
		orderManufacturer = 6,
		};

	//	Item criteria

	bool bHasItemCriteria;
	CString sCriteria;
	CItemCriteria ItemCriteria;
	if (bHasItemCriteria = pCmdLine->FindAttribute(CONSTLIT("itemCriteria"), &sCriteria))
		CItem::ParseCriteria(sCriteria, &ItemCriteria);
	else
		CItem::InitCriteriaAll(&ItemCriteria);

	//	Get the criteria from the command line.

	CDesignTypeCriteria Criteria;
	if (pCmdLine->FindAttribute(CONSTLIT("criteria"), &sCriteria))
		{
		if (CDesignTypeCriteria::ParseCriteria(sCriteria, &Criteria) != NOERROR)
			{
			printf("ERROR: Unable to parse criteria.\n");
			return;
			}
		}
	else if (bHasItemCriteria)
		{
		if (CDesignTypeCriteria::ParseCriteria(CONSTLIT("i"), &Criteria) != NOERROR)
			{
			printf("ERROR: Unable to parse criteria.\n");
			return;
			}
		}
	else
		{
		printf("ERROR: Expected criteria.\n");
		return;
		}

	bool bAll = pCmdLine->GetAttributeBool(CONSTLIT("all"));

	//	Options

	bool bTextBoxesOnly = pCmdLine->GetAttributeBool(CONSTLIT("textBoxesOnly"));
	bool bFieldUNID = pCmdLine->GetAttributeBool(CONSTLIT("unid"));

	//	Figure out what order we want

	CString sOrder = pCmdLine->GetAttribute(CONSTLIT("sort"));
	int iOrder;
	if (strEquals(sOrder, CONSTLIT("smallest")))
		iOrder = orderSmallest;
	else if (strEquals(sOrder, CONSTLIT("largest")))
		iOrder = orderLargest;
	else if (strEquals(sOrder, CONSTLIT("level")))
		iOrder = orderLevel;
	else if (strEquals(sOrder, CONSTLIT("sovereign")))
		iOrder = orderSovereign;
	else if (strEquals(sOrder, CONSTLIT("manufacturer")))
		iOrder = orderManufacturer;
	else
		iOrder = orderName;

	bool b3DGrid = pCmdLine->GetAttributeBool(CONSTLIT("3DGrid"));
	bool bDockingPorts = pCmdLine->GetAttributeBool(CONSTLIT("portPos"));
	bool bDevicePos = pCmdLine->GetAttributeBool(CONSTLIT("devicePos"));

	//	Image size

	int cxDesiredWidth;
	if (pCmdLine->FindAttributeInteger(CONSTLIT("width"), &cxDesiredWidth))
		cxDesiredWidth = Max(512, cxDesiredWidth);
	else
		cxDesiredWidth = 1280;

	//	Spacing

	int cxSpacing = pCmdLine->GetAttributeInteger(CONSTLIT("xSpacing"));
	int cxExtraMargin = pCmdLine->GetAttributeInteger(CONSTLIT("xMargin"));
	int cxImageMargin = 2 * pCmdLine->GetAttributeInteger(CONSTLIT("xImageMargin"));

	//	Font for text

	CString sTypeface;
	int iSize;
	bool bBold;
	bool bItalic;

	if (!CG16bitFont::ParseFontDesc(pCmdLine->GetAttribute(CONSTLIT("font")),
			&sTypeface,
			&iSize,
			&bBold,
			&bItalic))
		{
		sTypeface = CONSTLIT("Arial");
		iSize = 10;
		bBold = false;
		bItalic = false;
		}

	CG16bitFont NameFont;
	NameFont.Create(sTypeface, -PointsToPixels(iSize), bBold, bItalic);
	CG32bitPixel rgbNameColor = CG32bitPixel(255, 255, 255);

	//	Rotation

	int iRotation = pCmdLine->GetAttributeInteger(CONSTLIT("rotation"));

	//	Output file

	CString sFilespec = pCmdLine->GetAttribute(CONSTLIT("output"));
	if (!sFilespec.IsBlank())
		sFilespec = pathAddExtensionIfNecessary(sFilespec, CONSTLIT(".bmp"));

	//	Generate a sorted table of types

	TSortMap<CString, SEntryDesc> Table;
	for (i = 0; i < Universe.GetDesignTypeCount(); i++)
		{
		CDesignType *pType = Universe.GetDesignType(i);
		SEntryDesc NewEntry;

		//	Make sure we match the criteria

		if (!pType->MatchesCriteria(Criteria))
			continue;

		//	Figure stuff stuff out based on the specific design type

		switch (pType->GetType())
			{
			case designItemType:
				{
				CItemType *pItemType = CItemType::AsType(pType);
				CItem Item(pItemType, 1);

				//	Skip if not in item criteria

				if (!Item.MatchesCriteria(ItemCriteria))
					continue;

				//	Skip virtual classes

				if (pItemType->IsVirtual())
					continue;

				//	Initialize the entry

				NewEntry.pType = pType;
				NewEntry.sName = pItemType->GetNounPhrase(0);
				NewEntry.pImage = &pItemType->GetImage();
				NewEntry.iSize = RectWidth(NewEntry.pImage->GetImageRect());
				break;
				}

			case designShipClass:
				{
				CShipClass *pClass = CShipClass::AsType(pType);

				//	Skip non-generic classess

				if (!bAll && !pClass->HasLiteralAttribute(CONSTLIT("genericClass")))
					continue;

				//	Initialize the entry

				NewEntry.pType = pType;
				NewEntry.sName = pClass->GetNounPhrase(0);
				NewEntry.iSize = RectWidth(pClass->GetImage().GetImageRect());
				NewEntry.pImage = &pClass->GetImage();
				NewEntry.iRotation = pClass->Angle2Direction(iRotation);
				NewEntry.sSovereignName = (pClass->GetDefaultSovereign() ? pClass->GetDefaultSovereign()->GetTypeNounPhrase() : NULL_STR);
				break;
				}

			case designStationType:
				{
				CStationType *pStationType = CStationType::AsType(pType);

				//	Skip generic classes

				if (!bAll && !pStationType->HasLiteralAttribute(CONSTLIT("generic")))
					continue;

				NewEntry.pType = pType;
				NewEntry.sName = pStationType->GetNounPhrase(0);
				NewEntry.iSize = pStationType->GetSize();
				NewEntry.sSovereignName = (pStationType->GetSovereign() ? pStationType->GetSovereign()->GetTypeNounPhrase() : NULL_STR);

				InitStationTypeImage(NewEntry, pStationType);

				break;
				}

			default:
				//	Don't know how to handle this type
				continue;
				break;
			}

		//	Adjust name

		if (bFieldUNID)
			NewEntry.sName = strPatternSubst(CONSTLIT("%s (%x)"), NewEntry.sName, NewEntry.pType->GetUNID());

		//	Compute the sort key

		char szBuffer[1024];
		switch (iOrder)
			{
			case orderLargest:
				wsprintf(szBuffer, "%09d%s%x",
						1000000 - NewEntry.iSize,
						NewEntry.sName.GetASCIIZPointer(),
						pType->GetUNID());
				break;

			case orderLevel:
				wsprintf(szBuffer, "%09d%s%x",
						pType->GetLevel(),
						NewEntry.sName.GetASCIIZPointer(),
						pType->GetUNID());
				break;

			case orderSmallest:
				wsprintf(szBuffer, "%09d%s%x",
						NewEntry.iSize,
						NewEntry.sName.GetASCIIZPointer(),
						pType->GetUNID());
				break;

			case orderSovereign:
				wsprintf(szBuffer, "%s|%s|%x", NewEntry.sSovereignName.GetASCIIZPointer(), NewEntry.sName.GetASCIIZPointer(), pType->GetUNID());
				NewEntry.sCategorize = NewEntry.sSovereignName;
				break;

			case orderManufacturer:
				{
				CString sManufacturer = NewEntry.pType->GetPropertyString(CONSTLIT("manufacturer"));
				wsprintf(szBuffer, "%s|%s|%x", sManufacturer.GetASCIIZPointer(), NewEntry.sName.GetASCIIZPointer(), pType->GetUNID());
				NewEntry.sCategorize = sManufacturer;
				break;
				}

			default:
				wsprintf(szBuffer, "%s%x", NewEntry.sName.GetASCIIZPointer(), pType->GetUNID());
				break;
			}

		//	Add to list

		Table.Insert(CString(szBuffer), NewEntry);
		}

	//	Allocate an arranger that tracks where to paint each world.

	CImageArranger Arranger;

	//	Settings for the overall arrangement

	CImageArranger::SArrangeDesc Desc;
	Desc.cxDesiredWidth = Max(512, cxDesiredWidth - (2 * (cxSpacing + cxExtraMargin)));
	Desc.cxSpacing = cxSpacing;
	Desc.cxExtraMargin = cxExtraMargin;
	Desc.pHeader = &NameFont;

	//	Generate a table of cells for the arranger

	TArray<CCompositeImageSelector> Selectors;
	Selectors.InsertEmpty(Table.GetCount());

	CString sLastCategory;
	TArray<CImageArranger::SCellDesc> Cells;
	for (i = 0; i < Table.GetCount(); i++)
		{
		SEntryDesc &Entry = Table[i];

		CImageArranger::SCellDesc *pNewCell = Cells.Insert();
		pNewCell->cxWidth = (Entry.pImage ? RectWidth(Entry.pImage->GetImageRect()) : 0) + cxImageMargin;
		pNewCell->cyHeight = (Entry.pImage ? RectHeight(Entry.pImage->GetImageRect()) : 0) + cxImageMargin;
		pNewCell->sText = Entry.sName;

		if (!strEquals(sLastCategory, Entry.sCategorize))
			{
			sLastCategory = Entry.sCategorize;
			pNewCell->bStartNewRow = true;
			}
		}

	//	Arrange

	Arranger.ArrangeByRow(Desc, Cells);

	//	Create a large image

	CG32bitImage Output;
	int cxWidth = Max(cxDesiredWidth, Arranger.GetWidth());
	int cyHeight = Arranger.GetHeight();
	Output.Create(cxWidth, cyHeight);
	printf("Creating %dx%d image.\n", cxWidth, cyHeight);

	//	Paint the images

	for (i = 0; i < Table.GetCount(); i++)
		{
		SEntryDesc &Entry = Table[i];

		int x = Arranger.GetX(i);
		int y = Arranger.GetY(i);

		//	Paint

		if (x != -1)
			{
			int xCenter = x + (Arranger.GetWidth(i) / 2);
			int yCenter = y + (Arranger.GetHeight(i) / 2);

			int xOffset;
			int yOffset;
			Entry.pImage->GetImageOffset(0, Entry.iRotation, &xOffset, &yOffset);
			int cxImage = RectWidth(Entry.pImage->GetImageRect());
			int cyImage = RectHeight(Entry.pImage->GetImageRect());

			//	Paint image

			if (!bTextBoxesOnly && Entry.pImage)
				{
				Entry.pImage->PaintImageUL(Output,
						x + (Arranger.GetWidth(i) - cxImage) / 2,
						y + (Arranger.GetHeight(i) - cyImage) / 2,
						0,
						Entry.iRotation);
				}

			//	Paint type specific stuff

			switch (Entry.pType->GetType())
				{
				case designStationType:
					{
					CStationType *pStationType = CStationType::AsType(Entry.pType);
                    int xStationCenter = xCenter - xOffset;
                    int yStationCenter = yCenter - yOffset;

					if (bDockingPorts)
						pStationType->PaintDockPortPositions(Output, xStationCenter, yStationCenter);

					if (bDevicePos)
						pStationType->PaintDevicePositions(Output, xStationCenter, yStationCenter);

                    //  If we have docking or device positions, mark the center of the station

                    if (bDockingPorts || bDevicePos)
                        {
                        const int LINE_HALF_LENGTH = 24;
                        const CG32bitPixel RGB_CENTER_CROSS(255, 255, 0);
                        Output.DrawLine(xStationCenter - LINE_HALF_LENGTH, yStationCenter, xStationCenter + LINE_HALF_LENGTH, yStationCenter, 1, RGB_CENTER_CROSS);
                        Output.DrawLine(xStationCenter, yStationCenter - LINE_HALF_LENGTH, xStationCenter, yStationCenter + LINE_HALF_LENGTH, 1, RGB_CENTER_CROSS);
                        }
					break;
					}
				}

			//	Paint the 3D grid, if necessary

			if (b3DGrid)
				{
				int iScale = Entry.pImage->GetImageViewportSize();
				Metric rMaxRadius = g_KlicksPerPixel * cxImage * 0.5;
				const Metric rGridSize = LIGHT_SECOND;

				Metric rRadius;
				for (rRadius = rGridSize; rRadius <= rMaxRadius; rRadius += rGridSize)
					{
					int iRadius = (int)((rRadius / g_KlicksPerPixel) + 0.5);
					const int iGridAngle = 8;
					int iPrevAngle = 0;
					int iAngle;
					for (iAngle = iGridAngle; iAngle <= 360; iAngle += iGridAngle)
						{
						int xFrom, yFrom;
						C3DConversion::CalcCoord(iScale, iPrevAngle, iRadius, 0, &xFrom, &yFrom);

						int xTo, yTo;
						C3DConversion::CalcCoord(iScale, iAngle, iRadius, 0, &xTo, &yTo);

						Output.DrawLine(xFrom + xCenter, yFrom + yCenter, xTo + xCenter, yTo + yCenter, 1, CG32bitPixel(255, 255, 0));

						iPrevAngle = iAngle;
						}
					}
				}

			//	Paint name

			int xText = Arranger.GetTextX(i);
			int yText = Arranger.GetTextY(i);
			if (xText != -1)
				{
				if (bTextBoxesOnly)
					Output.Fill(xText, yText, Arranger.GetTextWidth(i), Arranger.GetTextHeight(i), 0xffff);

				if (!bTextBoxesOnly)
					{
					Output.FillColumn(xCenter,
							y + Arranger.GetHeight(i),
							yText - (y + Arranger.GetHeight(i)),
							rgbNameColor);

					NameFont.DrawText(Output,
							xText,
							yText,
							rgbNameColor,
							Entry.sName);
					}
				}
			}
		}

	//	Write to file or clipboard

	OutputImage(Output, sFilespec);
	}
コード例 #4
0
void TestPolygons (CUniverse &Universe, CXMLElement *pCmdLine)
{
    int i;

    const int FRAME_WIDTH = 256;
    const int FRAME_HEIGHT = 256;
    const int OUTPUT_WIDTH = FRAME_WIDTH * 2;
    const int OUTPUT_HEIGHT = FRAME_HEIGHT;

    //	Options

    int iCount = pCmdLine->GetAttributeInteger(CONSTLIT("count"));
    if (iCount == 0)
        iCount = 1000;

    int iBltCount = Max(1, iCount / 10);

    //	Create the output image

    CG32bitImage Output;
    Output.Create(OUTPUT_WIDTH, OUTPUT_HEIGHT, CG32bitImage::alpha8);

    //	Create a regular polygon

    const int iSides = 17;
    const Metric rRadius = 100.0;
    const Metric rAngleStep = (TAU / iSides);

    TArray<CVector> Shape1;
    Shape1.InsertEmpty(iSides);
    for (i = 0; i < iSides; i++)
        Shape1[i] = CVector::FromPolar(i * rAngleStep, rRadius);

    //	Create a point array, which we'll use for the binary region

    SPoint Shape1Points[iSides];
    for (i = 0; i < iSides; i++)
    {
        Shape1Points[i].x = (int)Shape1[i].GetX();
        Shape1Points[i].y = (int)Shape1[i].GetY();
    }

    //	Create a path

    CGPath Shape1Path;
    Shape1Path.Init(Shape1);

    //	We do timing tests first

    TNumberSeries<Metric> Timing;

    //	Time rasterization of a binary region

    const int CALLS_PER_SAMPLE = 1000;
    const int BLTS_PER_SAMPLE = 100;
    DWORD dwStart = ::GetTickCount();
    for (i = 0; i < iCount; i++)
    {
        CG16bitBinaryRegion Region;
        Region.CreateFromPolygon(iSides, Shape1Points);

        if (((i + 1) % CALLS_PER_SAMPLE) == 0)
            Timing.Insert((Metric)::sysGetTicksElapsed(dwStart, &dwStart));
    }

    printf("CG16bitBinaryRegion::CreateFromPolygon: %s ms per %d\n", (LPSTR)strFromDouble(Timing.GetMean()), CALLS_PER_SAMPLE);

    //	Time rasterization of path

    Timing.DeleteAll();
    dwStart = ::GetTickCount();
    for (i = 0; i < iCount; i++)
    {

        CGRegion Region;
        Shape1Path.Rasterize(&Region, 1);

        if (((i + 1) % CALLS_PER_SAMPLE) == 0)
            Timing.Insert((Metric)::sysGetTicksElapsed(dwStart, &dwStart));
    }

    printf("CGPath::Rasterize: %s ms per %d\n", (LPSTR)strFromDouble(Timing.GetMean()), CALLS_PER_SAMPLE);

    //	Create the regions

    CG16bitBinaryRegion Shape1BinaryRegion;
    Shape1BinaryRegion.CreateFromPolygon(iSides, Shape1Points);

    CGRegion Shape1Region;
    Shape1Path.Rasterize(&Shape1Region, 4);

    //	Time to blt

    Timing.DeleteAll();
    dwStart = ::GetTickCount();
    for (i = 0; i < iBltCount; i++)
    {
        CGDraw::Region(Output, (FRAME_WIDTH / 2), (FRAME_HEIGHT / 2), Shape1BinaryRegion, CG32bitPixel(255, 255, 255));

        if (((i + 1) % BLTS_PER_SAMPLE) == 0)
            Timing.Insert((Metric)::sysGetTicksElapsed(dwStart, &dwStart));
    }

    printf("CGDraw::Region (CG16bitBinaryRegion): %s ms per %d\n", (LPSTR)strFromDouble(Timing.GetMean()), BLTS_PER_SAMPLE);

    Timing.DeleteAll();
    dwStart = ::GetTickCount();
    for (i = 0; i < iBltCount; i++)
    {
        CGDraw::Region(Output, (FRAME_WIDTH / 2), (FRAME_HEIGHT / 2), Shape1Region, CG32bitPixel(255, 255, 255));

        if (((i + 1) % BLTS_PER_SAMPLE) == 0)
            Timing.Insert((Metric)::sysGetTicksElapsed(dwStart, &dwStart));
    }

    printf("CGDraw::Region (CGRegion): %s ms per %d\n", (LPSTR)strFromDouble(Timing.GetMean()), BLTS_PER_SAMPLE);

    //	Clear

    Output.Fill(CG32bitPixel(0, 0, 0));

    //	Blt result

    int x = 0;
    int y = 0;
    CGDraw::Region(Output, x + (FRAME_WIDTH / 2), y + (FRAME_HEIGHT / 2), Shape1BinaryRegion, CG32bitPixel(255, 255, 255));

    x = FRAME_WIDTH;
    CGDraw::Region(Output, x + (FRAME_WIDTH / 2), y + (FRAME_HEIGHT / 2), Shape1Region, CG32bitPixel(255, 255, 255));

    //	Copy to clipboard

    OutputImage(Output, NULL_STR);
}
コード例 #5
0
void GenerateShipImageChart (CUniverse &Universe, CXMLElement *pCmdLine)
	{
	int i;

	enum OrderTypes
		{
		orderSmallest = 1,
		orderLargest = 2,
		orderName = 3,
		};

	//	Options

	bool bTextBoxesOnly = pCmdLine->GetAttributeBool(CONSTLIT("textBoxesOnly"));

	//	Figure out what order we want

	CString sOrder = pCmdLine->GetAttribute(CONSTLIT("sort"));
	int iOrder;
	if (strEquals(sOrder, CONSTLIT("smallest")))
		iOrder = orderSmallest;
	else if (strEquals(sOrder, CONSTLIT("largest")))
		iOrder = orderLargest;
	else
		iOrder = orderName;

	//	Image size

	int cxDesiredWidth;
	if (pCmdLine->FindAttributeInteger(CONSTLIT("width"), &cxDesiredWidth))
		cxDesiredWidth = Max(512, cxDesiredWidth);
	else
		cxDesiredWidth = 1280;

	//	Spacing

	int cxSpacing = pCmdLine->GetAttributeInteger(CONSTLIT("xSpacing"));
	int cxExtraMargin = pCmdLine->GetAttributeInteger(CONSTLIT("xMargin"));

	//	Rotation

	int iRotation = pCmdLine->GetAttributeInteger(CONSTLIT("rotation"));

	//	Font for text

	CString sTypeface;
	int iSize;
	bool bBold;
	bool bItalic;

	if (!CG16bitFont::ParseFontDesc(pCmdLine->GetAttribute(CONSTLIT("font")),
			&sTypeface,
			&iSize,
			&bBold,
			&bItalic))
		{
		sTypeface = CONSTLIT("Arial");
		iSize = 10;
		bBold = false;
		bItalic = false;
		}

	CG16bitFont NameFont;
	NameFont.Create(sTypeface, -PointsToPixels(iSize), bBold, bItalic);
	WORD wNameColor = CG16bitImage::RGBValue(255, 255, 255);

	//	Output file

	CString sFilespec = pCmdLine->GetAttribute(CONSTLIT("output"));
	if (!sFilespec.IsBlank())
		sFilespec = pathAddExtensionIfNecessary(sFilespec, CONSTLIT(".bmp"));

	//	Generate a table of ships

	CSymbolTable Table(FALSE, TRUE);
	for (i = 0; i < Universe.GetShipClassCount(); i++)
		{
		CShipClass *pClass = Universe.GetShipClass(i);

		//	Skip player ship classes

		if (pClass->GetPlayerSettings())
			continue;

		//	Skip non-generic classes

		if (!pClass->HasAttribute(CONSTLIT("genericClass")))
			continue;

		//	Compute the sort key

		char szBuffer[1024];
		switch (iOrder)
			{
			case orderLargest:
				wsprintf(szBuffer, "%04d%s%x",
						2048 - RectWidth(pClass->GetImage().GetImageRect()),
						pClass->GetName().GetASCIIZPointer(),
						pClass);
				break;

			case orderSmallest:
				wsprintf(szBuffer, "%04d%s%x",
						RectWidth(pClass->GetImage().GetImageRect()),
						pClass->GetName().GetASCIIZPointer(),
						pClass);
				break;

			default:
				wsprintf(szBuffer, "%s%x", pClass->GetName().GetASCIIZPointer(), pClass);
				break;
			}

		//	Add to list

		Table.AddEntry(CString(szBuffer), (CObject *)pClass);
		}

	//	Allocate a map that tracks where to paint each ship

	CPaintMap Map(Table.GetCount());

	//	Arrange the ships

	SArrangeDesc Desc;
	Desc.cxDesiredWidth = Max(512, cxDesiredWidth - (2 * (cxSpacing + cxExtraMargin)));
	Desc.cxSpacing = cxSpacing;
	Desc.cxExtraMargin = cxExtraMargin;
	Desc.pHeader = &NameFont;

	ArrangeByRow(Table, Desc, Map);
	//ArrangeByCell(Table, cxDesiredWidth, Map);

	//	Create a large image

	CG16bitImage Output;
	int cxWidth = Max(cxDesiredWidth, Map.GetWidth());
	int cyHeight = Map.GetHeight();
	Output.CreateBlank(cxWidth, cyHeight, false);
	printf("Creating %dx%d image.\n", cxWidth, cyHeight);

	//	Paint the images

	for (i = 0; i < Table.GetCount(); i++)
		{
		CShipClass *pClass = (CShipClass *)Table.GetValue(i);

		int x = Map.GetX(i);
		int y = Map.GetY(i);
		if (x != -1)
			{
			if (!bTextBoxesOnly)
				pClass->GetImage().PaintImageUL(Output,
						x,
						y,
						0,
						Angle2Direction(iRotation));

			//	Paint name

			int xText = Map.GetTextX(i);
			int yText = Map.GetTextY(i);
			if (xText != -1)
				{
				if (bTextBoxesOnly)
					Output.Fill(xText, yText, Map.GetTextWidth(i), Map.GetTextHeight(i), 0xffff);

				if (!bTextBoxesOnly)
					{
					Output.FillColumn(x + (Map.GetWidth(i) / 2),
							y + Map.GetHeight(i),
							yText - (y + Map.GetHeight(i)),
							wNameColor);

					NameFont.DrawText(Output,
							xText,
							yText,
							wNameColor,
							255,
							pClass->GetNounPhrase(0));
					}
				}
			}
		}

	//	Write to file or clipboard

	OutputImage(Output, sFilespec);
	}