#include #include #include int main(int argc, char *argv[]) { uint32 imageWidth, imageLength ; uint32 Count, Pointer, Pointer_Save ; uint32 Width_Feet, Width_Box, Length_Box, Length_Feet, Line ; int Box_Total, Box_Length, RGB_Color ; TIFF *tif; unsigned char *buf; tstrip_t strip ; tif = TIFFOpen("test.tif", "w"); imageWidth = 200 ; imageLength = 200 ; Box_Length = 8 ; if ( tif ) { TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, imageWidth); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, imageLength); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 64); TIFFSetField(tif, TIFFTAG_XRESOLUTION, 0); TIFFSetField(tif, TIFFTAG_YRESOLUTION, 0); TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); buf = (char *) malloc(sizeof(uint32) * imageWidth * imageLength ) ; for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) { Pointer = 0 ; Pointer_Save = 0 ; Width_Feet = imageWidth / Box_Length ; Length_Feet = imageLength / Box_Length ; Box_Total = 0 ; unsigned char *buf_copy = buf ; /* 外圍迴圈以長度為結束點 */ for ( Length_Box = 1 ; Length_Box <= Length_Feet ; Length_Box ++ ) { Pointer = Pointer_Save ; /* 內圍迴圈以寬度為結束點 */ for ( Width_Box = 1 ; Width_Box <= Width_Feet ; Width_Box ++ ) { Box_Total ++ ; for ( Line = 1 ; Line <= Box_Length ; Line ++ ) { RGB_Color = 0 ; for ( Count = Pointer ; Count < ( Pointer + ( Box_Length * 3 ) ) ; Count ++ ) { RGB_Color ++ ; /* 單數方格用綠色,雙數方格用紅色 */ if ( Box_Total % 2 == 0 ) { buf[Count] = rgb_write(RGB_Color, 1); } else { buf[Count] = rgb_write(RGB_Color, 2); } if ( RGB_Color == 3 ) { RGB_Color = 0 ; } } Pointer = Count + ( ( imageWidth - Box_Length ) * 3 ) ; } Pointer = Pointer_Save + ( Width_Box * Box_Length * 3 ) ; if ( Width_Box == Width_Feet ) { Pointer_Save = Count ; } } } TIFFWriteEncodedStrip(tif, strip, buf, imageWidth * imageLength * 3 ) ; } } TIFFClose(tif); free(buf); printf("Done!\n"); return 0; } /* RGB 值寫入函式 */ int rgb_write (int RGB_Color,int Red_or_Green ) { int Output = 0 ; if ( Red_or_Green == 1 ) { if ( RGB_Color == 1 ) Output = 255 ; if ( RGB_Color == 2 ) Output = 0 ; if ( RGB_Color == 3 ) Output = 0 ; } if ( Red_or_Green == 2 ) { if ( RGB_Color == 1 ) Output = 0 ; if ( RGB_Color == 2 ) Output = 255 ; if ( RGB_Color == 3 ) Output = 0 ; } return Output ; }