2013-05-04 2 views
0

RGB888 이미지 데이터를 TIFF 이미지로 변환하려고합니다. 그러나 코드는 부적절한 이미지를 생성합니다. 내가 텍스트 file.Here에서 RGB 데이터를 읽고 있어요 출력 이미지RGB888 데이터에서 생성 된 부적절한 Tiff 이미지

Attached the improper image 검은 색 지역이 안가 그 코드가 제로로 낮은 RGB 값을 만드는 것 같다에게 있습니다. 알파 채널을 사용하지 않고 티파니 이미지를 만듭니다. 문제를 이해하도록 도와주세요.

TIFF *out= TIFFOpen("new.tif", "w"); 

int sampleperpixel = 3; 
uint32 width=320; 
uint32 height=240; 
unsigned char image[width*height*sampleperpixel]; 
int pixval; 
int count=0; 


FILE *ptr=fopen("data.txt","r"); 
if (ptr!=NULL) 
    { 
     while(count<width*height) 
     {fscanf(ptr,"%d",&pixval);    
     *(image+count)=(unsigned char)pixval; 
     count++;} 
    } 
printf("%d\n",count); 

TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); // set the width of the image 
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height); // set the height of the image 
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel); // set number of channels per pixel 
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); // set the size of the channels 
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); // set the origin of the image. 
// Some other essential fields to set that you do not have to understand for now. 
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); 

tsize_t linebytes = sampleperpixel * width; 
unsigned char *buf = NULL; 
//if (TIFFScanlineSize(out)<linebytes) 
// buf =(unsigned char *)_TIFFmalloc(linebytes); 
//else 
    buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out)); 




TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, width*sampleperpixel)); 

uint32 row ; 
//Now writing image to the file one strip at a time 
for (row = 0; row < height; row++) 
{ 
    //memcpy(buf, &image[(height-row-1)*linebytes], linebytes); // check the index here, and figure out why not using h*linebytes 
    memcpy(buf, &image[(row)*linebytes], linebytes);   
if (TIFFWriteScanline(out, buf, row, 0) < 0) 
    break; 
} 


TIFFClose(out); 

if (buf) 
_TIFFfree(buf); 
+0

한 번에 한 단계 씩 진행해야합니다. 먼저 텍스트 파일에서 데이터를 읽는 대신 harcoded 값으로 모든 스캔 라인을 채우려 고 시도하면 어떻게됩니까? (예 : 완전히 빨간색 티파니 생성) – Jem

+0

TIFFTAG_ROWSPERSTRIP이 잘못되었습니다. HEIGHT 또는 HEIGHT의 분수 분할로 설정해야합니다. 나머지는 괜찮아 보인다. 실제 파일을 게시하면 어디에서 잘못되었는지 자세히 알 수 있습니다. – BitBank

+0

@BitBank 전체 코드이므로 파일에 대해 자세히 설명해주십시오. 이 외에도 데이터 파일 만 있습니다. 감사합니다 – vid09

답변

0

this tutorial에서 예를 발견했습니다. 당신이 원저자가 아니기 때문에, 항상 다른 사람들을 돕고 표절을 피하기위한 링크를 게시해야합니다.

예제는 추가 한 파트를 제외하고 훌륭하게 작동합니다. (당신이 fread이 무엇을 읽을 후) 바이트 배열에 파일을 읽으려면, 대신를 사용

fread(image, 1, sizeof image, ptr); 

를 원래의 코드에서, 당신은 sampleperpixel

while(count<width*height*sampleperpixel) ... 

그래서 이미지의 1/3을 읽을 생략 .

관련 문제