コード例 #1
0
ファイル: PNM.c プロジェクト: PierreGts/SDA
PNMImage* readPNM(char* filename) {
    FILE* fp;
    char buffer[16];
    int c;

    // Open PNM file for reading
    fp = fopen(filename, "rb");
    if (!fp) {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        return NULL;
    }

    // Read image format
    if (!fgets(buffer, sizeof(buffer), fp)) {
        return NULL;
    }

    if (buffer[0] != 'P' || buffer[1] != '6') {
        fprintf(stderr, "Invalid image format (must be 'P6')\n");
        return NULL;
    }

    // Check for comments
    c = getc(fp);
    while (c == '#') {
        while (getc(fp) != '\n');
        c = getc(fp);
    }

    ungetc(c, fp);

    // Read image size
    size_t width;
    size_t height;

    if (fscanf(fp, "%zu %zu", &width, &height) != 2) {
        fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
        return NULL;
    }

    // Read RGB depth
    if (fscanf(fp, "%d", &c) != 1) {
        fprintf(stderr, "Invalid RGB depth (error loading '%s')\n", filename);
        return NULL;
    }

    if (c != 255) {
        fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
        return NULL;
    }

    while (fgetc(fp) != '\n') ;

    // Allocate memory
    PNMImage* image = createPNM(width, height);
    if (!image) {
        return NULL;
    }

    // Read pixels
    if (fread(image->data, 3 * image->width,
              image->height, fp) != image->height) {
        fprintf(stderr, "Error loading image '%s'\n", filename);
        freePNM(image);
        return NULL;
    }

    fclose(fp);
    return image;
}
コード例 #2
0
ファイル: pnm_manipulation.c プロジェクト: snovo/Proti
/*----------------------------converte----------------------------*/
PNM *converte(PNM *pnm, char*tipoDes)
{

	char tipoOrig = pnm->tipo[1], tipoDest;
	
	PNM *temp = (PNM *)malloc(sizeof(PNM));
	
	if(strlen(tipoDes) == 2) tipoDest = tipoDes[1];
	else return NULL;
	
	
	switch(tipoDest)
	{
		case '1':
			switch(tipoOrig)
			{
				case '1': temp = copia(pnm);
					break;
				case '2': temp = p2_2_p1(pnm);
					break;
				case '3': temp = p3_2_p1(pnm);
					break;
				case '4': temp = p4_2_p1(pnm);		
					break;
				case '5': temp = p5_2_p1(pnm);		
					break;
				case '6': temp = p6_2_p1(pnm);			
					break;
				default: return NULL;
			}
		
			break;
		case '2':
			switch(tipoOrig)
			{
				case '1': temp = p1_2_p2(pnm);
					break;
				case '2': temp = copia(pnm);
					break;
				case '3': temp = p3_2_p2(pnm);
					break;
				case '4': temp = p4_2_p2(pnm);		
					break;
				case '5': temp = p5_2_p2(pnm);		
					break;
				case '6': temp = p6_2_p2(pnm);			
					break;
				default: return NULL;
			}
			break;
		case '3':
			switch(tipoOrig)
			{
				case '1': temp = p1_2_p3(pnm);
					break;
				case '2': temp = p2_2_p3(pnm);
					break;
				case '3': temp = copia(pnm);		
					break;
				case '4': temp = p4_2_p3(pnm);		
					break;
				case '5': temp = p5_2_p3(pnm);			
					break;
				case '6': temp = p6_2_p3(pnm);			
					break;
				default: return NULL;
			}
			break;
		case '4':
			switch(tipoOrig)
			{
				case '1': temp = p1_2_p4(pnm);		
					break;
				case '2': temp = p2_2_p4(pnm);			
					break;
				case '3': temp = p3_2_p4(pnm);		
					break;
				case '4': temp = copia(pnm);		
					break;
				case '5': temp = p5_2_p4(pnm);		
					break;
				case '6': temp = p6_2_p4(pnm);			
					break;
				default: return NULL;
			}
			break;
		case '5':
			switch(tipoOrig)
			{
				case '1': temp = p1_2_p5(pnm);		
					break;
				case '2': temp = p2_2_p5(pnm);			
					break;
				case '3': temp = p3_2_p5(pnm);
					break;
				case '4': temp = p4_2_p5(pnm);
					break;
				case '5': temp = copia(pnm);		
					break;
				case '6': temp = p6_2_p5(pnm);	
					break;
				default: return NULL;
			}
			break;
		case '6':
			switch(tipoOrig)
			{
				case '1': temp = p1_2_p6(pnm);		
					break;
				case '2': temp = p2_2_p6(pnm);			
					break;
				case '3': temp = p3_2_p6(pnm);
					break;
				case '4': temp = p4_2_p6(pnm);		
					break;
				case '5': temp = p5_2_p6(pnm);		
					break;
				case '6': temp = copia(pnm);			
					break;
				default: return NULL;
			}
			break;
		default: return NULL;
	}
	
	if(pnm != NULL)
	{
		freePNM(pnm);
		pnm = NULL;
	}
	
	return temp;
}