Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds

The human knowledge belongs to the world
¡The infomation should be free!

Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940
Google Ads

PCX width and height - Practice Exercises Java

Lesson 8:

File management

Exercise 8.33:

PCX width and height


Create a program that checks if a file seems a PCX image and, if so, show your width and height, from the following specification:

<---- What is the PCX format? <

The PCX format is used to store images. Is the company that designed format ZSoft to create your image manipulation program known as Paintbrush.
br /> Internal details of a PCX file A PCX file consists of the following parts: a header file, a bitmap header, a color table and bytes that define the image.
br /> Specifically, the data forming the file header and the bitmap header are the following:
br /> Head Position Bytes Meaning
0 1 ID: must be 10
January 1 PCX File Version
0 = Version 2.5
2 = Version 2.8 with Palette
3 = Version 2.8 default palette
4 = Paintbrush for Windows
5 = Version 3.0 or higher, with the end of file palette.
February 1 Must be 1 to indicate RLE
January 3 bits per pixel
1 - Monochrome
4-16 colors
8 to 256 colors
24-16700000 color (true color, "truecolor")
August 4 Image Coordinates
Xmin, Ymin, Xmax, Ymax (4 data 2 bytes each)
February 12 horizontal resolution, in dots per inch
February 14 Vertical resolution in dots per inch
16 48 Colormap with the definition of the pallet in case of a 16 or fewer colors.
Organized in fields of 16 bytes * 3
64 1 Reserved
65 1 Number of planes (4 to 16 colors, 3-bits for RGB -24)
66 2 bytes per line image (the image width, measured in bytes)
68 2 Information palette
1 = Color
2 = Grayscale
70 2 Screen width (only used by Paintbrush IV and above)
72 2 Screen height (only used by Paintbrush IV and above)
74 54 bytes of padding, to complete 128 bytes of header. All bytes should be 0.

With these details we can find some information about the file. For example, to know the width from a program created for us, we should open the file, move to position 4 (counting from 0), two data read four bytes (a "int16" in the nomenclature used by many programming languages), which would Xmin, Ymin, Xmax, Ymax, and calculating: Width = Xmax - Xmin + 1, H = Ymax - Ymin + 1.

If the version number is 5, the color palette will be in the last 769 bytes of the file: First you get a value 12, then the R, G, B of the 256 colors in the palette (these values ​​are 0 255, should be divided by 4 if displayed on a VGA screen, because the RGB components of a VGA card only go from 0 to 63).

Images in 24-bit color (8 bits, 3 planes) bear palette for each point are stored components R, G, B.

Source Code:

public class Main
	public static void main(String[] args)
		BinaryReader myFile;

		myFile = new BinaryReader(File.Open("1.pcx", FileMode.Open));

		byte mark = myFile.ReadByte();
		if (mark != 10)
			System.out.println("Not a PCX file!");
			myFile.BaseStream.Seek(4, SeekOrigin.Begin);
			short xMin = myFile.ReadInt16();
			short yMin = myFile.ReadInt16();
			short xMax = myFile.ReadInt16();
			short yMax = myFile.ReadInt16();
			System.out.println("Width: " + (xMax - xMin + 1));
			System.out.println("Height: " + (yMax - yMin + 1));

Exercisey 8.33

Share Now:

Google Ads

Privacy Policy:

Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.


This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.