Created
March 15, 2020 19:58
-
-
Save parthsarthiprasad/7b208377be447d8e44463ce656218715 to your computer and use it in GitHub Desktop.
header file for reading png
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***** | |
rw_png_v2.chpl - | |
Program that reads a PNG file from disk, prints the RGB values found | |
at one pixel, changes it to 1, 2, 3, and writes the change to disk. | |
This version accesses the image data directly from Chapel. | |
Call: | |
rw_png_v2 | |
--inname=<file> file to read from | |
--outname=<file> file to write to | |
--x=<#> x coordinate of pixel to print | |
--y=<#> y coordinate of pixel to print | |
*****/ | |
/* Command line arguments. */ | |
config const inname : string; /* name of file to read */ | |
config const outname : string; /* file to create with modded pixel */ | |
config const x : c_int; /* pixel to change */ | |
config const y : c_int; /* pixel to change */ | |
/* The C image data structure. */ | |
extern class rgbimage { | |
var ncol : c_int; /* width (columns) of image */ | |
var nrow : c_int; /* height (rows) of image */ | |
var npix : c_int; /* number pixels = w * h */ | |
var r : c_ptr(c_uchar); /* red plane */ | |
var g : c_ptr(c_uchar); /* green plane */ | |
var b : c_ptr(c_uchar); /* blue plane */ | |
} | |
/* Our variables */ | |
var rgb : rgbimage; /* the image we read */ | |
var xy : int(32); /* 1D index of x, y coord */ | |
/* External img_png linkage. */ | |
extern proc PNG_read(fname : c_string, ref img : rgbimage) : c_int; | |
extern proc PNG_write(fname : c_string, img : rgbimage) : c_int; | |
extern proc free_rgbimage(ref img : rgbimage) : void; | |
/* The rest of the interface we don't use now. */ | |
/* | |
extern proc PNG_isa(fname : c_string) : c_int; | |
extern proc alloc_rgbimage(ref img : rgbimage, | |
ncol : c_int, nrow : c_int) : c_int; | |
extern proc read_rgb(img : rgbimage, x, y : c_int, | |
ref r, ref g, ref b : c_uchar) : c_int; | |
extern proc write_rgb(img : rgbimage, x, y : c_int, r, g, b : c_uchar) : c_int; | |
*/ | |
/**** Top Level ****/ | |
/* Here we go. First read the image, get the pixel requested, change it, and | |
write it back out. Finally we need to free the allocation made in | |
PNG_read. */ | |
PNG_read(inname.c_str(), rgb); | |
/* Now we can access the fields directly. */ | |
xy = (y * rgb.ncol) + x; | |
writef("\nRead %4i x %4i PNG image\n", rgb.ncol, rgb.nrow); | |
writef("At %4i,%4i R %3u G %3u B %3u\n\n", x,y, | |
rgb.r(xy), rgb.g(xy), rgb.b(xy)); | |
rgb.r(xy) = 1; | |
rgb.g(xy) = 2; | |
rgb.b(xy) = 3; | |
PNG_write(outname.c_str(), rgb); | |
free_rgbimage(rgb); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***** | |
img_png_v2.h - | |
Public declarations for the PNG file support. Also includes support | |
for our in-memory image data structure. | |
This version has been modified for interop with Chapel (uses now | |
_rgbimage, with rgbimage being a pointer to the structure). | |
Public Interface: | |
PNG_isa - test if file is in PNG format | |
PNG_read - read an image from disk | |
PNG_write - write an image to disk | |
alloc_rgbimage - allocate our internal image storage | |
free_rgbimage - release image memory | |
read_rgb - get a pixel in the image | |
write_rgb - set a pixel | |
Required Libraries: | |
libpng | |
*****/ | |
#ifndef _IMGPNG | |
#define _IMGPNG 1 | |
/*** Data Types ***/ | |
typedef unsigned char uchar; | |
/*** Data Structures ***/ | |
/* a color image with RGB planes stored separately */ | |
typedef struct __rgbimage { | |
int ncol; /* width (number columns) of image */ | |
int nrow; /* height (number rows) of image */ | |
int npix; /* number pixels = w * h */ | |
uchar *r; /* red plane */ | |
uchar *g; /* green plane */ | |
uchar *b; /* blue plane */ | |
} _rgbimage, *rgbimage; | |
/*** External Functions ***/ | |
/* test if a file is in PNG format, returning true if so, 0 if not | |
fname - name of file to read | |
*/ | |
extern int PNG_isa(const char *); | |
/* read a PNG image, converting it to an rgbimage | |
fname - name of file to read (if NULL, use stdin) | |
img - pointer to image to create and read (frees old if non-NULL) | |
returns < 0 on error | |
modifies img | |
*/ | |
extern int PNG_read(const char *, _rgbimage **); | |
/* write an rgbimage to disk in PNG format | |
fname - name of file to write to (if NULL, use stdout) | |
img - image to write | |
returns < 0 on error | |
*/ | |
extern int PNG_write(const char *, _rgbimage *); | |
/* allocate an image in our format, initializing contents to 0 | |
img - image to create (frees old if non-NULL) | |
ncol, nrow - size of image | |
returns < 0 on error | |
modifies img | |
*/ | |
extern int alloc_rgbimage(_rgbimage **, int, int); | |
/* release memory for an image | |
img - image to free | |
modifies img (set to NULL when done) | |
*/ | |
extern void free_rgbimage(_rgbimage **); | |
/* get pixel's RGB values | |
img - image | |
x, y - pixel coordinates | |
r, g, b - color planes (all modified) | |
*/ | |
extern int read_rgb(_rgbimage *, int, int, uchar *, uchar *, uchar *); | |
/* change a pixel's RGB values | |
img - image | |
x, y - pixel coordinates | |
r, g, b - color values | |
*/ | |
extern int write_rgb(_rgbimage *, int, int, uchar, uchar, uchar); | |
#endif /* _IMGPNG */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment