Fortran: How to save a value of 255 in one byte?

I would like to name the C api function from Fortran. Function C takes an array of bytes:

void image(const void *bitmap, int w, int h);

where three consecutive bytes in *bitmaprepresent triplex RGB colors and are interpreted as unsigned charin C. I want to initialize the bitmap in Fortran and take care of drawing inside C. The current definition in Fortran uses

integer*1 rgbImage(6,2)

to initialize a 2x2 image, for example, but the compiler does not accept assignment

rgbImage(1,1) = 255

to get a red color. I've seen hints of the use of BYTE, UNSIGNED*1, LOGICAL*1for unsigned single byte, but gfortran (MacPort gcc 4.4 or 4.6 in Mac OS X) is not very happy with any of them. I may be able to trick and assign a value -1instead 255, but it is very inconvenient to use. The compiler flag -fno-range-checkhelped compile the code, but may not be available in other Fortran compilers, and I find this an ugly solution (I still want to catch another warning). Values 'FF'Xor are '11111111'Balso recognized as 32-bit integers.

It is highly desirable that the code be portable for different Fortran compilers.

+5
source share
2 answers

CHARACTER ACHAR ( ICHAR ). , , . ,

character, dimension(6,2) :: rgbImage

rgbImage(1,1) = achar(255)

: iso_c_binding Fortran 2003, C ( !), , rgbImage c_char,

character(kind=c_char), dimension(6,2) :: rgbImage
integer(kind=c_int) :: w, h

...
rgbImage(1,1) = achar(255)
...

call image(rgbImage, w, h)

 interface
    subroutine image(img, w, h) bind(C)
       use, intrinsic :: iso_c_binding
       implicit none
       integer(kind=c_int), intent(in), value :: w, h
       character(kind=c_char) :: img(:,:)
    end subroutine image
 end interface
+12

, , 1 c int Integer (1), , iVal (:), Int, Integer (2) iVal2 (:), :

Where(iVal(:) < 0)
    iVal2(:) = -iVal(:)+127
ElseWhere
    iVal2(:) = iVal(:)
End Where

... / / () ().

( .. Fortran), , Fortran, (, BTest(), iBSet() ..).

0

All Articles