color_image_t *color_image_cpy(color_image_t *src) { color_image_t *im=color_image_new(src->width,src->height); memcpy(im->c1,src->c1,sizeof(*(src->c1))*src->width*src->height); memcpy(im->c2,src->c2,sizeof(*(src->c1))*src->width*src->height); memcpy(im->c3,src->c3,sizeof(*(src->c1))*src->width*src->height); return im; }
/* return a new image in lab color space */ color_image_t *rgb_to_lab(const color_image_t *im){ color_image_t *res = color_image_new(im->width, im->height); const int npix = im->stride*im->height; const float T=0.008856; const float color_attenuation = 1.5f; int i; for(i=0 ; i<npix ; i++){ const float r = im->c1[i]/255.f; const float g = im->c2[i]/255.f; const float b = im->c3[i]/255.f; float X=0.412453 * r + 0.357580 * g + 0.180423 * b; float Y=0.212671 * r + 0.715160 * g + 0.072169 * b; float Z=0.019334 * r + 0.119193 * g + 0.950227 * b; X/=0.950456; Z/=1.088754; float Y3 = pow(Y,1./3); float fX = X>T ? pow(X,1./3) : 7.787 * X + 16/116.; float fY = Y>T ? Y3 : 7.787 * Y + 16/116.; float fZ = Z>T ? pow(Z,1./3) : 7.787 * Z + 16/116.; float L = Y>T ? 116 * Y3 - 16.0 : 903.3 * Y; float A = 500 * (fX - fY); float B = 200 * (fY - fZ); // correct L*a*b*: dark area or light area have less reliable colors float correct_lab = exp(-color_attenuation*pow2(pow2(L/100) - 0.6)); res->c1[i] = L; res->c2[i] = A*correct_lab; res->c3[i] = B*correct_lab; } return res; }
static color_image_t *load_ppm(const char *fname) { FILE *f=fopen(fname,"r"); if(!f) { //perror("could not open infile"); exit(1); } int px,width,height,maxval; if(fscanf(f,"P%d %d %d %d",&px,&width,&height,&maxval)!=4 || maxval!=255 || (px!=6 && px!=5)) { fprintf(stderr,"Error: input not a raw PGM/PPM with maxval 255\n"); exit(1); } fgetc(f); /* eat the newline */ color_image_t *im=color_image_new(width,height); int i; for(i=0;i<width*height;i++) { im->c1[i]=fgetc(f); if(px==6) { im->c2[i]=fgetc(f); im->c3[i]=fgetc(f); } else { im->c2[i]=im->c1[i]; im->c3[i]=im->c1[i]; } } fclose(f); return im; }
/* resize a color image with bilinear interpolation */ color_image_t *color_image_resize_bilinear(const color_image_t *src, const float scale){ const int width = src->width, height = src->height; const int newwidth = (int) (1.5f + (width-1) / scale); // 0.5f for rounding instead of flooring, and the remaining comes from scale = (dst-1)/(src-1) const int newheight = (int) (1.5f + (height-1) / scale); color_image_t *dst = color_image_new(newwidth,newheight); if(height*newwidth < width*newheight){ color_image_t *tmp = color_image_new(newwidth,height); color_image_resize_horiz(tmp,src); color_image_resize_vert(dst,tmp); color_image_delete(tmp); }else{ color_image_t *tmp = color_image_new(width,newheight); color_image_resize_vert(tmp,src); color_image_resize_horiz(dst,tmp); color_image_delete(tmp); } return dst; }
/* Compute a refinement of the optical flow (wx and wy are modified) between im1 and im2 */ void variational(image_t *wx, image_t *wy, const color_image_t *im1, const color_image_t *im2, variational_params_t *params){ // Check parameters if(!params){ params = (variational_params_t*) malloc(sizeof(variational_params_t)); if(!params){ fprintf(stderr,"error: not enough memory\n"); exit(1); } variational_params_default(params); } // initialize global variables half_alpha = 0.5f*params->alpha; half_gamma_over3 = params->gamma*0.5f/3.0f; half_delta_over3 = params->delta*0.5f/3.0f; float deriv_filter[3] = {0.0f, -8.0f/12.0f, 1.0f/12.0f}; deriv = convolution_new(2, deriv_filter, 0); float deriv_filter_flow[2] = {0.0f, -0.5f}; deriv_flow = convolution_new(1, deriv_filter_flow, 0); // presmooth images int width = im1->width, height = im1->height, filter_size; color_image_t *smooth_im1 = color_image_new(width, height), *smooth_im2 = color_image_new(width, height); float *presmooth_filter = gaussian_filter(params->sigma, &filter_size); convolution_t *presmoothing = convolution_new(filter_size, presmooth_filter, 1); color_image_convolve_hv(smooth_im1, im1, presmoothing, presmoothing); color_image_convolve_hv(smooth_im2, im2, presmoothing, presmoothing); convolution_delete(presmoothing); free(presmooth_filter); compute_one_level(wx, wy, smooth_im1, smooth_im2, params); // free memory color_image_delete(smooth_im1); color_image_delete(smooth_im2); convolution_delete(deriv); convolution_delete(deriv_flow); }
void color_gist_scaletab_wrap(uint8_t *data, int height, int width, int nblocks, int n_scale, const int *orientations_per_scale, float *desc, int desc_size) { color_image_t *im=color_image_new(width, height); int i, size = height * width; for (i = 0; i < size; ++i) { im->c1[i] = *(data++); im->c2[i] = *(data++); im->c3[i] = *(data++); } float *desc_out = color_gist_scaletab(im, nblocks, n_scale, orientations_per_scale); memcpy(desc, desc_out, desc_size * sizeof(float)); free(desc_out); color_image_delete(im); }
color_image_t* SimilarityContent::makeGistImage(const VImage* vim) { // Convert to a color_image_t QImage small = vim->getQImage()->scaled(GIST_SIZE, GIST_SIZE); const uchar* data = small.constBits(); int width = small.width(), height = small.height(); int area = width*height; color_image_t *im = color_image_new(width, height); for(int i=0, i3=0; i<area; ++i, i3+=3) { im->c1[i]=data[i3+0]; im->c2[i]=data[i3+1]; im->c3[i]=data[i3+2]; } return im; }
void color_gist_scaletab_wrap(unsigned char *data, int height, int width, int nblocks, int n_scale, const int *orientations_per_scale, float *desc, int desc_size) { float *desc_out; color_image_t *im=color_image_new(width, height); int i, size = height * width; // Not only copies to data structure but also switches BGR -> RGB for (i = 0; i < size; ++i) { im->c3[i] = *(data++); im->c2[i] = *(data++); im->c1[i] = *(data++); } desc_out = color_gist_scaletab(im, nblocks, n_scale, orientations_per_scale); if (desc_out != NULL) memcpy(desc, desc_out, desc_size * sizeof(float)); free(desc_out); color_image_delete(im); }
static color_image_t *color_image_add_padding(color_image_t *src, int padding) { int i, j; color_image_t *img = color_image_new(src->width + 2*padding, src->height + 2*padding); for(j = 0; j < src->height; j++) { for(i = 0; i < src->width; i++) { img->c1[(j+padding)*img->width+i+padding] = src->c1[j*src->width+i]; img->c2[(j+padding)*img->width+i+padding] = src->c2[j*src->width+i]; img->c3[(j+padding)*img->width+i+padding] = src->c3[j*src->width+i]; } } for(j = 0; j < padding; j++) { for(i = 0; i < src->width; i++) { img->c1[j*img->width+i+padding] = src->c1[(padding-j-1)*src->width+i]; img->c2[j*img->width+i+padding] = src->c2[(padding-j-1)*src->width+i]; img->c3[j*img->width+i+padding] = src->c3[(padding-j-1)*src->width+i]; img->c1[(j+padding+src->height)*img->width+i+padding] = src->c1[(src->height-j-1)*src->width+i]; img->c2[(j+padding+src->height)*img->width+i+padding] = src->c2[(src->height-j-1)*src->width+i]; img->c3[(j+padding+src->height)*img->width+i+padding] = src->c3[(src->height-j-1)*src->width+i]; } } for(j = 0; j < img->height; j++) { for(i = 0; i < padding; i++) { img->c1[j*img->width+i] = img->c1[j*img->width+padding+padding-i-1]; img->c2[j*img->width+i] = img->c2[j*img->width+padding+padding-i-1]; img->c3[j*img->width+i] = img->c3[j*img->width+padding+padding-i-1]; img->c1[j*img->width+i+padding+src->width] = img->c1[j*img->width+img->width-padding-i-1]; img->c2[j*img->width+i+padding+src->width] = img->c2[j*img->width+img->width-padding-i-1]; img->c3[j*img->width+i+padding+src->width] = img->c3[j*img->width+img->width-padding-i-1]; } } return img; }
color_image_t *color_image_jpeg_load(FILE *fp) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; JSAMPARRAY buffer; int row_stride; int index = 0; color_image_t *image = NULL; float *r_p, *g_p, *b_p; JSAMPROW buffer_p; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); jpeg_read_header(&cinfo, TRUE); cinfo.out_color_space = JCS_RGB; cinfo.quantize_colors = FALSE; image = color_image_new(cinfo.image_width, cinfo.image_height); if(image == NULL) { return NULL; } jpeg_start_decompress(&cinfo); row_stride = cinfo.output_width * cinfo.output_components; buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); r_p = image->c1; g_p = image->c2; b_p = image->c3; while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, buffer, 1); buffer_p = buffer[0]; index = cinfo.output_width; while(index--) { *r_p++ = (float) *buffer_p++; *g_p++ = (float) *buffer_p++; *b_p++ = (float) *buffer_p++; } } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); return image; }
color_image_t *input3darray_to_color_image(const mxArray *p){ const int *dims = mxGetDimensions(p); const int h = dims[0], w = dims[1]; assert( dims[2]==3 ); float *in = (float*) mxGetData(p); color_image_t *out = color_image_new(w, h); const int s = out->stride; for(int c=0 ; c<3 ; c++){ float *inptr = in + c*w*h; float *outptr = out->c1 + c*s*h; for( int j=0 ; j<h ; j++){ for( int i=0 ; i<w ; i++){ outptr[j*s+i] = inptr[i*h+j]; } } } return out; }
color_image_t *color_image_pnm_load(FILE *fp){ color_image_t *image = NULL; ppm_hdr_t ppm_hdr; if(!get_ppm_hdr(fp, &ppm_hdr)) { return NULL; } switch(ppm_hdr.magic) { case 1: /* PBM ASCII */ case 2: /* PGM ASCII */ case 3: /* PPM ASCII */ case 4: /* PBM RAW */ case 5: /* PGM RAW */ fprintf(stderr, "color_image_pnm_load: only PPM raw with maxval 255 supported\n"); break; case 6: /* PPM RAW */ image = color_image_new(ppm_hdr.width, ppm_hdr.height); raw_read_color(fp, image); break; } return image; }
/* create a pyramid of color images using a given scale factor, stopping when one dimension reach min_size and with applying a gaussian smoothing of standard deviation spyr (no smoothing if 0) */ color_image_pyramid_t *color_image_pyramid_create(const color_image_t *src, const float scale_factor, const int min_size, const float spyr){ const int nb_max_scale = 1000; // allocate structure color_image_pyramid_t *pyramid = color_image_pyramid_new(); pyramid->min_size = min_size; pyramid->scale_factor = scale_factor; convolution_t *conv = NULL; if(spyr>0.0f){ int fsize; float *filter_coef = gaussian_filter(spyr, &fsize); conv = convolution_new(fsize, filter_coef, 1); free(filter_coef); } color_image_pyramid_set_size(pyramid, nb_max_scale); pyramid->images[0] = color_image_cpy(src); int i; for( i=1 ; i<nb_max_scale ; i++){ const int oldwidth = pyramid->images[i-1]->width, oldheight = pyramid->images[i-1]->height; const int newwidth = (int) (1.5f + (oldwidth-1) / scale_factor); const int newheight = (int) (1.5f + (oldheight-1) / scale_factor); if( newwidth <= min_size || newheight <= min_size){ color_image_pyramid_set_size(pyramid, i); break; } if(spyr>0.0f){ color_image_t* tmp = color_image_new(oldwidth, oldheight); color_image_convolve_hv(tmp,pyramid->images[i-1], conv, conv); pyramid->images[i]= color_image_resize_bilinear(tmp, scale_factor); color_image_delete(tmp); }else{ pyramid->images[i] = color_image_resize_bilinear(pyramid->images[i-1], scale_factor); } } if(spyr>0.0f){ convolution_delete(conv); } return pyramid; }
/* compute image first and second order spatio-temporal derivatives of a color image */ void get_derivatives(const color_image_t *im1, const color_image_t *im2, const convolution_t *deriv, color_image_t *dx, color_image_t *dy, color_image_t *dt, color_image_t *dxx, color_image_t *dxy, color_image_t *dyy, color_image_t *dxt, color_image_t *dyt) { // derivatives are computed on the mean of the first image and the warped second image color_image_t *tmp_im2 = color_image_new(im2->width,im2->height); v4sf *tmp_im2p = (v4sf*) tmp_im2->c1, *dtp = (v4sf*) dt->c1, *im1p = (v4sf*) im1->c1, *im2p = (v4sf*) im2->c1; const v4sf half = {0.5f,0.5f,0.5f,0.5f}; int i=0; for(i=0 ; i<3*im1->height*im1->stride/4 ; i++){ *tmp_im2p = half * ( (*im2p) + (*im1p) ); *dtp = (*im2p)-(*im1p); dtp+=1; im1p+=1; im2p+=1; tmp_im2p+=1; } // compute all other derivatives color_image_convolve_hv(dx, tmp_im2, deriv, NULL); color_image_convolve_hv(dy, tmp_im2, NULL, deriv); color_image_convolve_hv(dxx, dx, deriv, NULL); color_image_convolve_hv(dxy, dx, NULL, deriv); color_image_convolve_hv(dyy, dy, NULL, deriv); color_image_convolve_hv(dxt, dt, deriv, NULL); color_image_convolve_hv(dyt, dt, NULL, deriv); // free memory color_image_delete(tmp_im2); }
/* allocate a new color image and copy the content from src */ color_image_t *color_image_cpy(const color_image_t *src){ color_image_t *dst = color_image_new(src->width, src->height); memcpy(dst->c1, src->c1, 3*src->stride*src->height*sizeof(float)); return dst; }
/* compute the saliency of a given image */ image_t* saliency(const color_image_t *im, float sigma_image, float sigma_matrix ){ int width = im->width, height = im->height, filter_size; // smooth image color_image_t *sim = color_image_new(width, height); float *presmooth_filter = gaussian_filter(sigma_image, &filter_size); convolution_t *presmoothing = convolution_new(filter_size, presmooth_filter, 1); color_image_convolve_hv(sim, im, presmoothing, presmoothing); convolution_delete(presmoothing); free(presmooth_filter); // compute derivatives float deriv_filter[2] = {0.0f, -0.5f}; convolution_t *deriv = convolution_new(1, deriv_filter, 0); color_image_t *imx = color_image_new(width, height), *imy = color_image_new(width, height); color_image_convolve_hv(imx, sim, deriv, NULL); color_image_convolve_hv(imy, sim, NULL, deriv); convolution_delete(deriv); // compute autocorrelation matrix image_t *imxx = image_new(width, height), *imxy = image_new(width, height), *imyy = image_new(width, height); v4sf *imx1p = (v4sf*) imx->c1, *imx2p = (v4sf*) imx->c2, *imx3p = (v4sf*) imx->c3, *imy1p = (v4sf*) imy->c1, *imy2p = (v4sf*) imy->c2, *imy3p = (v4sf*) imy->c3, *imxxp = (v4sf*) imxx->data, *imxyp = (v4sf*) imxy->data, *imyyp = (v4sf*) imyy->data; int i; for(i = 0 ; i<height*im->stride/4 ; i++){ *imxxp = (*imx1p)*(*imx1p) + (*imx2p)*(*imx2p) + (*imx3p)*(*imx3p); *imxyp = (*imx1p)*(*imy1p) + (*imx2p)*(*imy2p) + (*imx3p)*(*imy3p); *imyyp = (*imy1p)*(*imy1p) + (*imy2p)*(*imy2p) + (*imy3p)*(*imy3p); imxxp+=1; imxyp+=1; imyyp+=1; imx1p+=1; imx2p+=1; imx3p+=1; imy1p+=1; imy2p+=1; imy3p+=1; } // integrate autocorrelation matrix float *smooth_filter = gaussian_filter(sigma_matrix, &filter_size); convolution_t *smoothing = convolution_new(filter_size, smooth_filter, 1); image_t *tmp = image_new(width, height); convolve_horiz(tmp, imxx, smoothing); convolve_vert(imxx, tmp, smoothing); convolve_horiz(tmp, imxy, smoothing); convolve_vert(imxy, tmp, smoothing); convolve_horiz(tmp, imyy, smoothing); convolve_vert(imyy, tmp, smoothing); convolution_delete(smoothing); free(smooth_filter); // compute smallest eigenvalue v4sf vzeros = {0.0f,0.0f,0.0f,0.0f}; v4sf vhalf = {0.5f,0.5f,0.5f,0.5f}; v4sf *tmpp = (v4sf*) tmp->data; imxxp = (v4sf*) imxx->data; imxyp = (v4sf*) imxy->data; imyyp = (v4sf*) imyy->data; for(i = 0 ; i<height*im->stride/4 ; i++){ (*tmpp) = vhalf*( (*imxxp)+(*imyyp) ) ; (*tmpp) = __builtin_ia32_sqrtps(__builtin_ia32_maxps(vzeros, (*tmpp) - __builtin_ia32_sqrtps(__builtin_ia32_maxps(vzeros, (*tmpp)*(*tmpp) + (*imxyp)*(*imxyp) - (*imxxp)*(*imyyp) ) ))); tmpp+=1; imxyp+=1; imxxp+=1; imyyp+=1; } image_delete(imxx); image_delete(imxy); image_delete(imyy); color_image_delete(imx); color_image_delete(imy); color_image_delete(sim); return tmp; }
/* perform flow computation at one level of the pyramid */ void compute_one_level(image_t *wx, image_t *wy, color_image_t *im1, color_image_t *im2, const variational_params_t *params){ const int width = wx->width, height = wx->height, stride=wx->stride; image_t *du = image_new(width,height), *dv = image_new(width,height), // the flow increment *mask = image_new(width,height), // mask containing 0 if a point goes outside image boundary, 1 otherwise *smooth_horiz = image_new(width,height), *smooth_vert = image_new(width,height), // horiz: (i,j) contains the diffusivity coeff from (i,j) to (i+1,j) *uu = image_new(width,height), *vv = image_new(width,height), // flow plus flow increment *a11 = image_new(width,height), *a12 = image_new(width,height), *a22 = image_new(width,height), // system matrix A of Ax=b for each pixel *b1 = image_new(width,height), *b2 = image_new(width,height); // system matrix b of Ax=b for each pixel color_image_t *w_im2 = color_image_new(width,height), // warped second image *Ix = color_image_new(width,height), *Iy = color_image_new(width,height), *Iz = color_image_new(width,height), // first order derivatives *Ixx = color_image_new(width,height), *Ixy = color_image_new(width,height), *Iyy = color_image_new(width,height), *Ixz = color_image_new(width,height), *Iyz = color_image_new(width,height); // second order derivatives image_t *dpsis_weight = compute_dpsis_weight(im1, 5.0f, deriv); int i_outer_iteration; for(i_outer_iteration = 0 ; i_outer_iteration < params->niter_outer ; i_outer_iteration++){ int i_inner_iteration; // warp second image image_warp(w_im2, mask, im2, wx, wy); // compute derivatives get_derivatives(im1, w_im2, deriv, Ix, Iy, Iz, Ixx, Ixy, Iyy, Ixz, Iyz); // erase du and dv image_erase(du); image_erase(dv); // initialize uu and vv memcpy(uu->data,wx->data,wx->stride*wx->height*sizeof(float)); memcpy(vv->data,wy->data,wy->stride*wy->height*sizeof(float)); // inner fixed point iterations for(i_inner_iteration = 0 ; i_inner_iteration < params->niter_inner ; i_inner_iteration++){ // compute robust function and system compute_smoothness(smooth_horiz, smooth_vert, uu, vv, dpsis_weight, deriv_flow, half_alpha ); compute_data_and_match(a11, a12, a22, b1, b2, mask, du, dv, Ix, Iy, Iz, Ixx, Ixy, Iyy, Ixz, Iyz, half_delta_over3, half_gamma_over3); sub_laplacian(b1, wx, smooth_horiz, smooth_vert); sub_laplacian(b2, wy, smooth_horiz, smooth_vert); // solve system sor_coupled(du, dv, a11, a12, a22, b1, b2, smooth_horiz, smooth_vert, params->niter_solver, params->sor_omega); // update flow plus flow increment int i; v4sf *uup = (v4sf*) uu->data, *vvp = (v4sf*) vv->data, *wxp = (v4sf*) wx->data, *wyp = (v4sf*) wy->data, *dup = (v4sf*) du->data, *dvp = (v4sf*) dv->data; for( i=0 ; i<height*stride/4 ; i++){ (*uup) = (*wxp) + (*dup); (*vvp) = (*wyp) + (*dvp); uup+=1; vvp+=1; wxp+=1; wyp+=1;dup+=1;dvp+=1; } } // add flow increment to current flow memcpy(wx->data,uu->data,uu->stride*uu->height*sizeof(float)); memcpy(wy->data,vv->data,vv->stride*vv->height*sizeof(float)); } // free memory image_delete(du); image_delete(dv); image_delete(mask); image_delete(smooth_horiz); image_delete(smooth_vert); image_delete(uu); image_delete(vv); image_delete(a11); image_delete(a12); image_delete(a22); image_delete(b1); image_delete(b2); image_delete(dpsis_weight); color_image_delete(w_im2); color_image_delete(Ix); color_image_delete(Iy); color_image_delete(Iz); color_image_delete(Ixx); color_image_delete(Ixy); color_image_delete(Iyy); color_image_delete(Ixz); color_image_delete(Iyz); }
static PyObject* gist_extract(PyObject *self, PyObject *args) { int nblocks=4; int n_scale=3; int orientations_per_scale[50]={8,8,4}; PyArrayObject *image, *descriptor; if (!PyArg_ParseTuple(args, "O", &image)) { return NULL; } if (PyArray_TYPE(image) != NPY_UINT8) { PyErr_SetString(PyExc_TypeError, "type of image must be uint8"); return NULL; } if (PyArray_NDIM(image) != 3) { PyErr_SetString(PyExc_TypeError, "dimensions of image must be 3."); return NULL; } npy_intp *dims_image = PyArray_DIMS(image); const int w = (int) *(dims_image+1); const int h = (int) *(dims_image); // Read image to color_image_t structure color_image_t *im=color_image_new(w,h); for (int y=0, i=0 ; y<h ; ++y) { for (int x=0 ; x<w ; ++x, ++i) { im->c1[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 0); im->c2[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 1); im->c3[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 2); } } // Extract descriptor float *desc=color_gist_scaletab(im,nblocks,n_scale,orientations_per_scale); int descsize=0; /* compute descriptor size */ for(int i=0;i<n_scale;i++) descsize+=nblocks*nblocks*orientations_per_scale[i]; descsize*=3; /* color */ // Allocate output npy_intp dim_desc[1] = {descsize}; descriptor = (PyArrayObject *) PyArray_SimpleNew(1, dim_desc, NPY_FLOAT); // Set val for (int i=0 ; i<descsize ; ++i) { *(float *)PyArray_GETPTR1(descriptor, i) = desc[i]; } // Release memory color_image_delete(im); free(desc); return PyArray_Return(descriptor); }
color_image_t *color_image_png_load( FILE* fp, const char* file_name ){ // read the header png_byte header[8]; fread(header, 1, 8, fp); if (png_sig_cmp(header, 0, 8)){ fprintf(stderr, "error: %s is not a PNG.\n", file_name); fclose(fp); return 0; } png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr){ fprintf(stderr, "error: png_create_read_struct returned 0.\n"); fclose(fp); return 0; } // create png info struct png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr){ fprintf(stderr, "error: png_create_info_struct returned 0.\n"); png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); fclose(fp); return 0; } // create png info struct png_infop end_info = png_create_info_struct(png_ptr); if (!end_info){ fprintf(stderr, "error: png_create_info_struct returned 0.\n"); png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fclose(fp); return 0; } // the code in this if statement gets called if libpng encounters an error if (setjmp(png_jmpbuf(png_ptr))) { fprintf(stderr, "error from libpng\n"); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fp); return 0; } // init png reading png_init_io(png_ptr, fp); // let libpng know you already read the first 8 bytes png_set_sig_bytes(png_ptr, 8); // read all the info up to the image data png_read_info(png_ptr, info_ptr); // variables to pass to get info int bit_depth, color_type; png_uint_32 temp_width, temp_height; // get info about png png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, NULL, NULL, NULL); // Update the png info struct. png_read_update_info(png_ptr, info_ptr); // Row size in bytes. int rowbytes = png_get_rowbytes(png_ptr, info_ptr); // Allocate the image_data as a big block, to be given to opengl png_byte * image_data; image_data = (png_byte*) malloc(sizeof(png_byte)*rowbytes*temp_height); assert(image_data!=NULL); // row_pointers is for pointing to image_data for reading the png with libpng png_bytep * row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*temp_height); assert(row_pointers!=NULL); // set the individual row_pointers to point at the correct offsets of image_data unsigned int i; for (i = 0; i <temp_height; i++) row_pointers[i] = image_data + i * rowbytes; // read the png into image_data through row_pointers png_read_image(png_ptr, row_pointers); // copy into color image color_image_t* image = color_image_new(temp_width,temp_height); if( color_type==0 ) { assert((unsigned)rowbytes == temp_width || !"error: not a proper gray png image"); for(i=0; i<temp_height; i++){ int j; for(j=0; j<temp_width; j++) image->c1[i*image->stride+j] = image->c2[i*image->stride+j] = image->c3[i*image->stride+j] = image_data[i*image->width+j]; } } else if( color_type == 2 ) { assert((unsigned)rowbytes == 3*temp_width || !"error: not a proper color png image"); for(i=0; i<temp_height; i++) { int j; for(j=0; j<temp_width; j++){ image->c1[i*image->stride+j] = image_data[3*(i*image->width+j)+0]; image->c2[i*image->stride+j] = image_data[3*(i*image->width+j)+1]; image->c3[i*image->stride+j] = image_data[3*(i*image->width+j)+2]; } } } else assert(!"error: unknown PNG color type" ); // clean up png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); free(row_pointers); return image; }