Esempio n. 1
0
void PLBmpDecoder::Open (PLDataSource * pDataSrc)
{
  m_pBMI = getInfoHeader (pDataSrc, &m_Pal[0]);
  
  PLPixelFormat pf;
  if (m_pBMI->biBitCount <= 8) {
    pf = PLPixelFormat::L8;
  } else {
    if (m_pBMI->biBitCount == 32) {
      pf = PLPixelFormat::A8R8G8B8;
    } else {
      pf = PLPixelFormat::X8R8G8B8;
    }
  }

  PLPoint DPI(0,0);
  if (m_pBMI->biXPelsPerMeter > 0)
    DPI.x = (int)((float)m_pBMI->biXPelsPerMeter / 39.37f+0.5);
  if (DPI.x <= 1)
    DPI.x = 0;
  if (m_pBMI->biYPelsPerMeter > 0)
    DPI.y = (int)((float)m_pBMI->biYPelsPerMeter / 39.37f+0.5);
  if (DPI.y <= 1)
    DPI.y = 0;

  SetBmpInfo (PLPoint (m_pBMI->biWidth, m_pBMI->biHeight), 
        DPI, pf);
}
Esempio n. 2
0
int main (int argc, char** argv)
{
	// for storing results of argument processing
	// general
	bool displayInfo = true;
	char* inFileName = "";

	// threshold filter
	bool runThreshold = false;
	float threshold = THRESH_ERROR;

	// crop
	bool runCrop = false;
	int cropWidth = CROP_DIM_ERROR;
	int cropHeight = CROP_DIM_ERROR;
	int cropStart[] = {0,0};

	// resize
	bool runResize = false;
	float scaling = SCALING_ERROR;
	char* resType = "";

	// default output file name
	char* outFileName = "out.bmp";
	
	// process arguments, checking for errors
	if (processArguments(argc, argv, &runThreshold, &runCrop, &runResize, 
		&displayInfo, &inFileName, &outFileName, &threshold, &cropWidth, 
		&cropHeight, cropStart, &scaling, &resType) != EXIT_SUCCESS)
	{
		fprintf(stderr, "Unable to process arguments.\n");
		return EXIT_FAILURE;
	}

	// open the file
	FILE *inFile = fopen(inFileName, "r");

	// check that file opened okay
	if (inFile == NULL)
	{
		fprintf(stderr, "Unable to open file: %s\n", inFileName);
		return EXIT_FAILURE;
	}

	// read in the file header and check that it went okay
	struct bmpFileHeader *fileHead = getFileHeader(inFile);

	if (fileHead == NULL)
	{
		fprintf(stderr, "Unable to set up file header.\n");
		return EXIT_FAILURE;
	}

	struct bmpInfoHeader *infoHead = getInfoHeader(inFile);

	if (infoHead == NULL)
	{
		fprintf(stderr, "Unable to set up info header.\n");
		return EXIT_FAILURE;
	}

	// check the format of the file
	if (formatCheck(fileHead, infoHead) != EXIT_SUCCESS)
	{
		fprintf(stderr, "Program cannot handle this format.\n");
		return EXIT_FAILURE;
	}


	// if we're displaying info, do so
	if (displayInfo)
	{
		// print out header information
		printInfo(fileHead, infoHead);
	}

	// move file pointer on to start of image, if necessary
	if (fileHead->offset != (sizeof(struct bmpFileHeader) + 
		sizeof(struct bmpInfoHeader)))
	{
		fseek(inFile, 
			((int)fileHead->offset - (sizeof(struct bmpFileHeader) + 
				sizeof(struct bmpInfoHeader))), SEEK_CUR);
	}

	// if we're running a threshold filter, do so
	if (runThreshold)
	{
		// apply threshold filter to image, checking for errors
		if (applyThreshold(inFile, fileHead, infoHead, outFileName, threshold) 
			== EXIT_FAILURE)
		{
			fprintf(stderr, "Unable to apply threshold.\n");
			return EXIT_FAILURE;
		}
	}

	// if we're cropping the image, do so
	if (runCrop)
	{
		// crop the image, checking for errors
		if (cropImage(inFile, fileHead, infoHead, outFileName, cropWidth,
			cropHeight, cropStart) == EXIT_FAILURE)
		{
			fprintf(stderr, "Unable to crop image.\n");
			return EXIT_FAILURE;
		}
	}

	// if we're resizing the image do so
	if (runResize)
	{
		// resize image checking for errors
		if (resizeImage(inFile, fileHead, infoHead, outFileName, scaling, resType) 
			== EXIT_FAILURE)
		{
			fprintf(stderr, "Unable to resize image.\n");
			return EXIT_FAILURE;
		}
	}

	// free up allocated memory
	free(infoHead);
	free(fileHead);

	// close input file, checking for errors
	if (fclose(inFile) != 0)
	{
		fprintf(stderr, "Unable to close input file.\n");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}