我在将char
值保存到类型为unsigned short
.
---编辑:这是输入 .pgm ---
P2
6 10
255
255 255 255 255 10 255
255 255 255 255 20 255
255 255 255 255 30 255
255 255 255 255 40 255
255 255 255 255 50 255
255 255 255 255 60 255
110 255 255 255 70 255
255 100 90 80 255 255
255 255 255 255 255 255
255 255 255 255 255 255
所以我们假设保存一个由 0 和 1 或其他数字组成的矩阵。结构如下:
typedef struct{
unsigned short rgb[3];
}PIXEL_T;
typedef struct{
int format;
int nrows;
int ncolumns;
int max_color;
PIXEL_T **pixels;
}PBM_T;
这是将 1 或 0 字符保存到适当结构字段中的代码。
void pbmMatrixASCII(char* line, size_t len, FILE* file_stream, PBM_T *img_struct) {
int i = 0, j = 0;
char *token;
while(getline(&line, &len, file_stream) != EOF) {
token = strtok(line, " \n\t");
while (token != NULL) {
//DEBUG("%s", token);
img_struct->pixels[i][j].rgb[0] = strtol(token, NULL, 0);
token = strtok(NULL, " \n\t");
printf("%hu ", img_struct->pixels[i][j].rgb[0]);
j++;
}
printf("\n");
i++;
}
}
现在这适用于 0 和 1。问题是,当我们必须保存一个值从 1 到 255 的矩阵时。它会保存并打印垃圾。我们尝试了几种铸造方法。例如:
img_struct->pixels[i][j].rgb[0] = (unsigned short) token;
他们不工作。我们如何解决这个问题?(另外,是否有一种正确的方法来实际转换这些值,一种同时解决这两个问题的方法?)
----针对一些反馈进行编辑----
我们正在读取 pbm 和 pgm 文件。其中第二行是矩阵的大小,然后我们将其读取到img_struct->nrows
and img_struct->ncolumns
。这是代码:
//Detects the filetype and reads the matrix size (also reads max color in case of pgm)
//Reads the first line to determine the magic number
getline(&line, &len, file_stream);
sscanf(line, "P%d", &aux_format);
//Verifies if the format is valid. Exits if not
if(aux_format > 6 || aux_format < 1) {
ERROR(ERR_FORMAT,"Invalid format\n");
}
img_struct->format = aux_format; //Saves the format into the structure, after being verified
int size_is_read = 0; //Variable used to verify if the size has been read (a makeshift boolean)
while(getline(&line, &len, file_stream) != EOF){
if (hasCommentsOrEmptyLines(line))
continue;
//Reads the size of the matrix
if(size_is_read == 0) {
sscanf(line,"%d %d",&columns,&rows);
size_is_read = 1;
}
if(img_struct->format == 1 || img_struct->format == 4) { //P1 and P4 are .pbm files and therefore don't have a maximum colour field
break;
} else if(img_struct->format == 2 || img_struct->format == 5){ //Every other file type needs to have its maximum colour field read
while(getline(&line, &len, file_stream) != EOF) {
if(hasCommentsOrEmptyLines(line))
continue;
//reads the max color
sscanf(line,"%d",&(img_struct->max_color));
break;
}
}
break;
}
之后,内存分配后跟矩阵读取器函数调用(见上文):
//Save the image size in the appropriate structure fields
img_struct->nrows = rows;
img_struct->ncolumns = columns;
//Allocates the appropriate size of the matrix to save the bitmap
img_struct->pixels = MALLOC(sizeof(PIXEL_T *)*rows);
if (img_struct->pixels == NULL)
ERROR(ERR_ALLOC,"Error allocating memory for the bitmap matrix");
for(i = 0; i < rows; i++) {
img_struct->pixels[i] = MALLOC(sizeof(PIXEL_T)*columns);
}
//Iterate each position of the line array and convert it to an unsigned short
switch(img_struct->format) {
case 1:
pbmMatrixASCII(line, len, file_stream, img_struct);
break;
case 2:
printf("teste\n");
pgmMatrixASCII(line, len, file_stream, img_struct);
break;