I would like to call a C api function from Fortran. The C function accepts an array of bytes:
void image(const void *bitmap, int w, int h);
where three successive bytes in *bitmap represent an RGB colour tripple and are interpreted as unsigned char in C. I want to initialize the bitmap in Fortran and take care of drawing inside C. Current definition in Fortran uses
integer*1 rgbImage(6,2)
to initialize an image of 2×2 for example, but compiler won’t accept assignment
rgbImage(1,1) = 255
to get red colour. I’ve seen hints of using BYTE, UNSIGNED*1, LOGICAL*1 for unsigned single bytes, but gfortran (MacPort’s gcc 4.4 or 4.6 under Mac OS X) isn’t really happy with either of them. I could probably get away by cheating and assigning value -1 instead of 255, but that is very uncomfortable to use. The compiler flag -fno-range-check helped compile the code, but might not be available in other Fortran compilers and I consider it an ugly solution (I would still want to catch other warning). The values 'FF'X or '11111111'B are also recognized as 32-bit integers.
It is highly desirable for the code to be portable across different Fortran compilers.
My suggestion would be to use
CHARACTERvariables, and useACHARto set values (andICHARto convert back to integers as necessary). That should get you what you want and be completely portable. eg,Updated to add: if you’re going to use the Fortran 2003 iso_c_binding stuff to interface to the C routines (highly recommended!) then you might as well make that rgbImage array characters of kind
c_char, egwhere you’ve defined the interface for the routine