VB icon

load pcx

Email
Submitted on: 1/2/2015 6:44:00 PM
By: Ferry Timmers (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: PHP 4.0, PHP 5.0
Views: 1466
 
     creates a GD image resource from a pcx file
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :load pcx
//**************************************
Please inform me if you are going to use it. You may not include it in comercial products.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: load pcx
// Description:creates a GD image resource from a pcx file
// By: Ferry Timmers (from psc cd)
//
// Inputs:PCX version 5 files
$img = imagecreatefrompcx($filename);
//
// Returns:GB image resource
//
// Side Effects:returns false on error
//**************************************

<?php
/******************************************
 * GD PCX reader *
 **
 * Author: Ferry Timmers *
 **
 * Date: 18-12-2008 20:22 *
 **
 * Desc: Creates a GD image resource from *
 *a specified pcx file.*
 *Needs PCX version 5 *
 **
 ******************************************/
//------------------------------------------------------------------------------
function imagecreatefrompcx($filename)
{
	if (!$fp = @fopen($filename, 'rb'))
		return (false);
	
	// * * * Header * * *
	fread($fp, 1); // == 0x0A
	if (($version = ord(fread($fp, 1))) != 5)
	{
		fclose($fp);
		return (false);
	}
	
	$bbp = ord(fread($fp, 2));
	list($xmin, $ymin, $xmax, $ymax) = array_values(unpack('S4', fread($fp, 8)));
	$width = $xmax - $xmin + 1;
	$height = $ymax - $ymin + 1;
	//$size = $width * $height;
	
	fseek($fp, 54, SEEK_CUR);
	list($bpl) = array_values(unpack('S', fread($fp, 2)));
	
	// * * * Pallet * * *
	fseek($fp, -769, SEEK_END);
	if (ord(fread($fp, 1)) != 12)
	{
		fclose($fp);
		return (false);
	}
	
	$img = imagecreate($width, $height);
	
	for($i = 0; $i < 256; $i++)
	{
		list($r, $g, $b) = array_values(unpack('C3', fread($fp, 3)));
		$color[$i] = imagecolorallocate($img, $r, $g, $b);
	}
	
	// * * * Data * * *
	fseek($fp, 128, SEEK_SET);
	
	for ($y = 0; $y < $height; $y++)
	{
		$x = 0;
		while ($x < $bpl)
		{
			$c = ord(fread($fp, 1));
			if (($c & 0xC0) == 0xC0)
			{
				$c &= 0x3F;
				if (($c + $x) > $bpl)
					$c = $bpl - $x;
				
				$c += $x;
				$d = $color[ord(fread($fp, 1))];
				while ($x < $c)
				{
					imagesetpixel($img, $x, $y, $d);
					$x++;
				}
			}
			else
			{
				imagesetpixel($img, $x, $y, $color[$c]);
				$x++;
			}
		}
	}
	
	fclose($fp);
	
	return ($img);
}
//------------------------------------------------------------------------------
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.