CZP Image Creation by Simple C-Program

Custom CZP (Circular Zone Plate) Image (with Dan-chan Logo) Digital Spatial Journey

This article describes how to create CZP (Circular Zone Plate) image file by simple C-Programing on Ubuntu under WSL2 (Windows Sub-Systems for Linux Version 2).

スポンサーリンク (Sponsored Links)

What is WSL2?

WSL2 (Windows Sub-System for Linux, version 2) is one of Linux OS environment under Windows OS.

What is Ubuntu?

Ubuntu is one of Linux based OS (Operation System).

Ubuntu makes easy to build C/C++ Programing Environment with simple commands (descibed in this article).

What is CZP?

CZP stands for Circular Zone Plate.

CZP is utilized as one of test charts for frequency response evaluation of image processing visually.

How to create CZP?

How to generate image file?

PPM file (file extension “.ppm”) is one of image file formats which you can check file data by both text editors (e.g. “Wordpad”) and image browsers/editors (e.g. “GIMP”).

PPM image file can be generated by major computer programing launguages like C/C++ as same manner as text file creation.

If you need CZP image with different image formats (else than “ppm”), “GIMP” can convert “ppm” to other majar image format (e.g. “JPG”, “BMP”).

“PPM” stands for Portable PixMap.

Structure of PPM File Format

The beggining of PPM file contents is so called “Magic Number” (file descriptor), “P3” which indicates the type of file and ASCII encoding and 3 color elements (Red, Green, Blue).

Following 2 integer numbers express number of columns and rows.

Rests of integer numbers (0 .. 255) are contents of image data with the order of Red, Green and Blue as a set of one pixel data.

CZP Image File Creation by C Programing

Set up of C/C++ Program Environment

If you neet to set up C/C++ programing environemnt under Ubunutu, following commands (to be input after Ubuntu “$” prompt) will help you to install GNU C compiler and necessary libraries.

sudo apt install build-essntial

You can verify GCC version with following commands, after completing the installation.

gcc --versionll

“build-essential” contains C/C++ compilers and other necessary basic functions (e.g. libraries).

CZP Image Generation Program by C-Language

Following is one of C-Program (file name: “czp.c”) to generate CZP image file with PPM format.

You can put following contents (as the file name “czp.c”) under home directory of Ubuntu, utilizing test editor (e.g. “Nano”, “Vi”).

To generate the file named “czp.c” by “Nano” Editor, following commands to be used.

nano czp.c

Copy and paste following program contents.

/*
	File name: czp.c
	Last Edit Date: 25 May 2022
	Author: Dan-chan
        Copyright (c) 2022 Dan-chan, All Rights Reserved.  

	Description:
	Create Circular Zone Plate image (with ppm ascii format).
*/

#include <stdio.h>
#include <math.h>

void main()
{
	FILE	*fd;

	int	i, j, k;	// Variables
	int	p1 = 8;		// Color dipth [bit/channel] (R, G, B)
	int	p2 = 256;	// Bit resolution /channel (R, G, B)
	int 	width = 1280; 	// Image width (x-direction) 
	int 	height = 720;	// image height (y-direction)
	double	pi = 3.142857;	// constant
	
	fd = fopen("CZP_1280x720_p2560_ascii.ppm", "w");
	
	double p = 2560.0;      // CZP parameter to define x-size of circles 
	double q = 2560.0;      // CZP parameter to define y-size of circles
	
// PPM file format header

	fprintf(fd, "P3\n");
	fprintf(fd, "%d %d\n", width, height);
	fprintf(fd, "%d\n", p2);


// Image Data Contents Calculation

	for (i = 0; i < height; i+
	{
		for (j = 0; j < width; j++)
		{
			k = (int) (ceil((double) p2/2.0-1 + p2/2.0*sin((double)pi/2.0*((j - width/2.0)*(j - width/2.0)/p + (i-height/2.0)*(i-height/2.0)/q)+ pi/2.0)));
			fprintf(fd, "%d %d %d\n", k, k, k);
		}
	}
	fclose(fd);
}

After saving file “czp.c”, verify availability of the file with following commands (list files).

ll

“czp.c” compiled by GCC on Ubuntu

After creating “czp.c” in home dirrectory under Ubuntu, you can make executable file with following commands.

gcc czp.c -lm

Notes) “-lm” option is need to cover #include libraries (e.g. <stdio.h>, <math.h>).

To generate CZP image file with ppm format, following commands to be used.

./a.out

You will find CZP image file named “CZP_1280x720_p2560_ascii.ppm” through file lists commands which described in previous chapter.

Review Output Results

When you open “CZP_1280x720_p2560_ascii.ppm” by text editor, you will find following texts (ASCII format) at the first part of the file.

P3
1280 720
256
71 71 71
6 6 6
12 12 12
.......

In case that your PC has Image Editor “GIMP” installed, you can see CZP image visually as below.

CZP (Circular Zone Plate) Image (with Dan-chan Logo)
CZP (Circular Zone Plate) Image (with Dan-chan Logo)

Summary & Further Considerations

This article instructed how to create CZP (Circular Zone Plate) image file (with “ppm” file format) by simple C-Programing on Ubuntu.

“ppm” file format has benefit. It’s easy to check image data by both text editors and image browsers/editors, and to create files by program languages (like C/C++) with same manner as text file creation.

Image editor, “GIMP” can open “ppm” files and convert those to other majar image file formats (e.g. “JPG”, “BMP”).

We can apply this method (“ppm” image file creation by program languages) for other test chart images (e.g. checker board).

Created files can be shared by copying target files between Ubuntu/WSL2 and Windows.

Please enjoy geometric and beautiful CZP images !!