image filter(image im, double *K, int Ks, double divisor, double offset) { image oi; unsigned int ix, iy, l; int kx, ky; double cp[3]; oi = alloc_img(im->width, im->height); if ( oi != NULL ) { for(ix=0; ix < im->width; ix++) { for(iy=0; iy < im->height; iy++) { cp[0] = cp[1] = cp[2] = 0.0; for(kx=-Ks; kx <= Ks; kx++) { for(ky=-Ks; ky <= Ks; ky++) { for(l=0; l<3; l++) cp[l] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * ((double)GET_PIXEL_CHECK(im, ix+kx, iy+ky, l)) + offset; } } for(l=0; l<3; l++) cp[l] = (cp[l]>255.0) ? 255.0 : ((cp[l]<0.0) ? 0.0 : cp[l]) ; put_pixel_unsafe(oi, ix, iy, (color_component)cp[0], (color_component)cp[1], (color_component)cp[2]); } } return oi; } return NULL; }
image get_ppm(FILE *pf) { char buf[PPMREADBUFLEN], *t; image img; unsigned int w, h, d; int r; if (pf == NULL) return NULL; t = fgets(buf, PPMREADBUFLEN, pf); /* the code fails if the white space following "P6" is not '\n' */ if ( (t == NULL) || ( strncmp(buf, "P6\n", 3) != 0 ) ) return NULL; do { /* Px formats can have # comments after first line */ t = fgets(buf, PPMREADBUFLEN, pf); if ( t == NULL ) return NULL; } while ( strncmp(buf, "#", 1) == 0 ); r = sscanf(buf, "%u %u", &w, &h); if ( r < 2 ) return NULL; r = fscanf(pf, "%u", &d); if ( (r < 1) || ( d != 255 ) ) return NULL; fseek(pf, 1, SEEK_CUR); /* skip one byte, should be whitespace */ img = alloc_img(w, h); if ( img != NULL ) { size_t rd = fread(img->buf, sizeof(pixel), w*h, pf); if ( rd < w*h ) { free_img(img); return NULL; } return img; } }
image read_ppm(char* fis_in) { image img; unsigned int w, h; char a; int b; FILE *f = fopen(fis_in, "r"); if (f == NULL) { perror("Error opening input file"); return NULL; } fscanf(f, "%c%d\n", &a, &b); fscanf(f, "%u %u\n", &w, &h); fscanf(f, "%d\n", &b); fpos_t curr_pos; fgetpos(f, &curr_pos); fseek (f , SEEK_CUR, SEEK_END); long f_size = ftell (f); fsetpos(f, &curr_pos); char* buf = malloc(f_size * sizeof(char)); img = alloc_img(w, h); if (img != NULL) { size_t rd = fread(buf, 1, f_size, f); if (rd < w * h) { free_img(img); fclose(f); return NULL; } char *tok = strtok(buf, "\n"); unsigned int i = 0, j = 0; while (tok != NULL) { if (j == 3) { j = 0; i++; } if (j % 3 == 0) img->buf[i].r = atoi(tok); else if (j % 3 == 1) img->buf[i].g = atoi(tok); else img->buf[i].b = atoi(tok); tok = strtok(NULL, "\n"); j++; } free(buf); fclose(f); return img; } fclose(f); return img; }
void equalize(struct img *in, struct img *out, int *xlat_r, int *xlat_g, int *xlat_b) { out->xsize = in->xsize; out->ysize = in->ysize; alloc_img(out); for(int pix = 0; pix < in->xsize * in->ysize; pix++) { out->r[pix] = xlat_r[in->r[pix]]; out->g[pix] = xlat_g[in->g[pix]]; out->b[pix] = xlat_b[in->b[pix]]; } }
// Load a 24-bit RGB PPM file and return the loaded image. // The function takes care of allocating the memory for the image. // If filename is "-", use stdin img_t *load_ppm(char *filename) { bool use_stdin = strcmp(filename, "-") == 0; FILE *f = use_stdin ? stdin : fopen(filename, "r"); if (f == NULL) return NULL; char line[128]; // The PPM format states a line is at most 70 character long unsigned int width, height; unsigned int maxval; // Reads the PPM header, making sure it's valid int matches; // Fist line should be P3 matches = fscanf(f, "%2s", line); // read 2 characters if (matches != 1 || strcmp(line, "P3") != 0) goto error; // Second line should be image width and height matches = fscanf(f, "%u %u", &width, &height); if (matches != 2) goto error; // Third line should be the maximum value per component matches = fscanf(f, "%u", &maxval); if (matches != 1) goto error; // Allocate memory for image structure and image data img_t *img = alloc_img(width, height); // Next, should be the image data in RGB order for (unsigned int i = 0; i < width * height; i++) { unsigned r, g, b; matches = fscanf(f, "%u %u %u", &r, &g, &b); if (matches != 3) goto error; if (r > maxval || g > maxval || b > maxval) goto error; pixel_t p = {r, g, b}; img->data[i] = p; } if (!use_stdin) { fclose(f); } return img; error: if (!use_stdin) { fclose(f); } return NULL; }
void writeppm(char *filename, const int width, const int height, unsigned char *img) { int x, y, px; image image; FILE *fp; fopen_s(&fp, filename, "wb"); image = alloc_img(width, height); for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { px = (y * width + x) * 3; put_pixel_unsafe(image, x, y, img[px + 0], img[px + 1], img[px + 2]); } } output_ppm(fp, image); fclose(fp); }
/*! *********************************************************************** * \brief * Allocate the Decoder Structure * \par Output: * Decoder Parameters *********************************************************************** */ static int alloc_decoder( DecoderParams **p_Dec) { if ((*p_Dec = (DecoderParams *) calloc(1, sizeof(DecoderParams)))==NULL) { fprintf(stderr, "alloc_decoder: p_Dec\n"); return -1; } alloc_img(&((*p_Dec)->p_Vid)); alloc_params(&((*p_Dec)->p_Inp)); (*p_Dec)->p_Vid->p_Inp = (*p_Dec)->p_Inp; (*p_Dec)->p_trace = NULL; (*p_Dec)->bufferSize = 0; (*p_Dec)->bitcounter = 0; return 0; }
image read_ppm (FILE * pf) { char buf[PPMREADBUFLEN], *t; image img; unsigned int w, h, d; int r; if (pf == NULL) return NULL; t = fgets (buf, PPMREADBUFLEN, pf); if ((t == NULL) || (strncmp (buf, "P5\n", 3) != 0)) return NULL; do { /* Px formats can have # comments after first line */ t = fgets (buf, PPMREADBUFLEN, pf); if (t == NULL) return NULL; } while (strncmp (buf, "#", 1) == 0); r = sscanf (buf, "%u %u", &w, &h); if (r < 2) return NULL; // The program fails if the first byte of the image is equal to 32. because // the fscanf eats the space and the image is read with some bit less r = fscanf (pf, "%u\n", &d); if ((r < 1) || (d != 255)) return NULL; img = alloc_img (w, h); if (img != NULL) { size_t rd = fread (img->buf, sizeof (pixel_t), w * h, pf); if (rd < w * h) { free_img (img); return NULL; } return img; } }
image tocolor(grayimage img) { unsigned int x, y; image timg; luminance l; unsigned int ofs; timg = alloc_img(img->width, img->height); for(x=0; x < img->width; x++) { for(y=0; y < img->height; y++) { ofs = (y * img->width) + x; l = img->buf[ofs][0]; timg->buf[ofs][0] = l; timg->buf[ofs][1] = l; timg->buf[ofs][2] = l; } } return timg; }
int main(int argc, char **argv) { init_spus(); srand((unsigned)time(NULL)); char *fis_in, *fis_out; int zoom, rows, cols, i, j, overlap_spu, overlap_ppu, patch_w, patch_h, nr_patches; if (argc < 8) { fprintf(stderr, "Error: Missing some parameters.\n"); fprintf(stderr, "Run: ./program fis_in fis_out zoom nr_bucati_dim1 nr_bucati_dim2 banda_de_suprapunere_dim1 banda_de_suprapunere_dim2\n"); return -1; } fis_in = argv[1]; fis_out = argv[2]; zoom = atoi(argv[3]); rows = atoi(argv[4]); cols = atoi(argv[5]); overlap_spu = atoi(argv[6]); overlap_ppu = atoi(argv[7]); image img_src = read_ppm(fis_in); if (img_src == NULL) { fprintf(stderr, "Error reading image file.\n"); return -1; } patch_w = (zoom * img_src->width) / cols; patch_h = (zoom * img_src->height) / rows; nr_patches = rows * cols; printf("PPU: NR PATCHES NECESARY = %d\n", nr_patches); int **spu_patch_id_vector = alloc_patch_id_vector(rows); if (spu_patch_id_vector == NULL) return -1; printf("PPU: ZOOM=%d ROWS=%d COLS=%d img->width=%d img->height=%d patch_w=%d patch_h=%d\n", zoom, rows, cols, img_src->width, img_src->height, patch_w, patch_h); int* rand_seed = make_seed_vector(); if (rand_seed == NULL) return -1; int ***min_borders = malloc_align(SPU_THREADS * sizeof(int**), 4); if (min_borders == NULL) { perror("PPU: malloc_align failed in main"); return -1; } for (i = 0; i < SPU_THREADS; i++) { min_borders[i] = alloc_aligned_matrix((rows-1), overlap_spu); if (min_borders[i] == NULL) return -1; } pixel_t **patches_to_send = make_patches(img_src, patch_w, patch_h, nr_patches); send_patch_info(&patch_w, &patch_h, &rows, &nr_patches, spu_patch_id_vector, patches_to_send, rand_seed, &overlap_spu, min_borders); stop_spus(); int out_img_width = zoom * img_src->width; int out_img_height = zoom * img_src->height; image img_dst = alloc_img(out_img_width, out_img_height); for (i = 0; i < SPU_THREADS; i++) { printf("PPU: spu[%d]: ID= ", i); for (j = 0; j < rows; j++) printf("%d ", spu_patch_id_vector[i][j]); printf("\n"); } make_final_image(img_dst, patch_w, patch_h, spu_patch_id_vector, rows, patches_to_send); write_ppm(fis_out, img_dst); free_img(img_src); free_img(img_dst); free_seed_vector(rand_seed); free_patch_id_vector(spu_patch_id_vector); for (i = 0; i < SPU_THREADS; i++) free_aligned_matrix(min_borders[i], rows-1); return 0; }