Exemplo n.º 1
0
int on_chunk_data(multipart_parser* p, const char *at, size_t length)
{
    mp_arg_t *mp_arg = (mp_arg_t *)multipart_parser_get_data(p);
    mp_arg->partno++;
    if(mp_arg->check_name == -1)
    {
        mp_arg->check_name = 0;
        return 0;
    }
    if(length < 1)
        return 0;
    //multipart_parser_set_data(p, mp_arg);
    char md5sum[33];
    if(save_img(mp_arg->thr_arg, at, length, md5sum) == -1)
    {
        LOG_PRINT(LOG_DEBUG, "Image Save Failed!");
        LOG_PRINT(LOG_ERROR, "%s fail post save", mp_arg->address);
        evbuffer_add_printf(mp_arg->req->buffer_out, 
            "<h1>Failed!</h1>\n"
            "<p>File save failed!</p>\n"
            );
    }
    else
    {
        mp_arg->succno++;
        LOG_PRINT(LOG_INFO, "%s succ post pic:%s size:%d", mp_arg->address, md5sum, length);
        evbuffer_add_printf(mp_arg->req->buffer_out, 
            "<h1>MD5: %s</h1>\n"
            "Image upload successfully! You can get this image via this address:<br/><br/>\n"
            "<a href=\"/%s\">http://yourhostname:%d/%s</a>?w=width&h=height&g=isgray&x=position_x&y=position_y&r=rotate&q=quality&f=format\n",
            md5sum, md5sum, settings.port, md5sum
            );
    }
    return 0;
}
Exemplo n.º 2
0
int binary_parse(evhtp_request_t *req, const char *content_type, const char *address, const char *buff, int post_size)
{
    int err_no = 0;
    if(is_img(content_type) != 1)
    {
        LOG_PRINT(LOG_DEBUG, "fileType[%s] is Not Supported!", content_type);
        LOG_PRINT(LOG_ERROR, "%s fail post type", address);
        err_no = 1;
        goto done;
    }
    char md5sum[33];
    LOG_PRINT(LOG_DEBUG, "Begin to Save Image...");
    evthr_t *thread = get_request_thr(req);
    thr_arg_t *thr_arg = (thr_arg_t *)evthr_get_aux(thread);
    if(save_img(thr_arg, buff, post_size, md5sum) == -1)
    {
        LOG_PRINT(LOG_DEBUG, "Image Save Failed!");
        LOG_PRINT(LOG_ERROR, "%s fail post save", address);
        goto done;
    }
    err_no = -1;
    LOG_PRINT(LOG_INFO, "%s succ post pic:%s size:%d", address, md5sum, post_size);
    json_return(req, err_no, md5sum, post_size);

done:
    return err_no;
}
Exemplo n.º 3
0
Arquivo: mapper.c Projeto: rzmz/GoSky
/**
 * Antud funktsioon kasutab genereeritud mapi ja tekitab sellest pildi
 * */
void generate_image_from_map(char *fname){
	int x,y;
	int H=pixel_map.H;
	int W=pixel_map.W;



	create_img(W,H);

	for(y=0;y<H;y++){
	     for(x=0;x<W;x++){
		   point p=pixel_map.map[y][x];
		   set_pixel(x,y, get_pixel(p.x, p.y));
	     }
		}
	save_img(fname);
	return;
	}
Exemplo n.º 4
0
static int save_load(enum fmt fmt, GP_Size w, GP_Size h)
{
	GP_Context *img, *res;

	img = GP_ContextAlloc(w, h, GP_PIXEL_RGB888);
	
	if (img == NULL) {
		tst_warn("GP_ContextAlloc failed");
		return TST_UNTESTED;
	}

	int ret = save_img(fmt, img, "test");

	if (ret) {
		if (errno == ENOSYS) {
			tst_msg("Save %s: ENOSYS", strfmt(fmt));
			return TST_SKIPPED;
		}
		
		tst_msg("Failed to save %s: %s",
		           strfmt(fmt), strerror(errno));
		return TST_FAILED;
	}

	res = load(fmt, "test");

	if (res == NULL) {
		tst_msg("Failed to load %s: %s",
		           strfmt(fmt), strerror(errno));
		return TST_FAILED;
	}

	GP_ContextFree(img);
	GP_ContextFree(res);

	return TST_SUCCESS;
}
Exemplo n.º 5
0
/* ============================================================
 * = Main
 * ============================================================*/
int main(int argc, char **argv)
{
  puts(">>>>>>>>>>>>>> Moravec Point Feature Detector");

  // Define Kernel Size and Moravec Threshold
  int moravec_kernel_size;
  int moravec_threshold;

  // Define raw-image holder 
  img_t raw_src;
 
  /* ------------------------------------------------------------
   * Load Image and Init Kernel Size and Threshold
   * ------------------------------------------------------------*/

  char *path = "data/u0367panLeft.raw";
  int row = 887;
  int col = 805;
  int ker_size = 3;
  int thresh = 30;

  // Not work now...
/* parse_params(argc,
	       argv,
	       &path,
	       &row,
	       &col,
	       &ker_size,
	       &thresh);*/
  
  // So we do it explicitely
  if (argc == 1) {
    puts(">> Use Built-in Params.");
  } else if (argc == 6) {
    puts(">> Load Params from Console. Single Thresh");
    path = argv[1];
    row = atoi(argv[2]);
    col = atoi(argv[3]);
    ker_size = atoi(argv[4]);
    thresh = atoi(argv[5]);
  } else if (argc > 7) {
    puts(">> Load Params from Console. Multi Thresh");
    path = argv[1];
    row = atoi(argv[2]);
    col = atoi(argv[3]);
    ker_size = atoi(argv[4]);
    if (atoi(argv[5]) != 0) { // argv[5] is treated as a flag
      puts("Multi Thresh Usage.");
      printf(">>  %s <raw_img_path> <row> <col> <kernel_size> 0 <threshold1> <threshold2> ...\n", argv[0]);
      exit (-1);
    }
    thresh = atoi(argv[6]); // this case, argv[6] as the threshold
  } else { // Else? Else Print out Usage and then exit!
    help(argc, argv);
    exit (-1);
  }

  printout_params(argv[0], path, row, col, ker_size, thresh);


  puts(">>>>>>>>>>>>>> Load Image & Set Kernel Size, Threshold");
  load_img(&raw_src, path, row, col);
  set_moravec_kernel_size(&moravec_kernel_size, ker_size);
  set_moravec_threshold(&moravec_threshold, thresh);

  // check 
  puts(">>>>>>>>>>>>>> Check Params");
  printf("## Check:\n##>> r:%d, c:%d; k: %d, t: %d\n", row, col, moravec_kernel_size, moravec_threshold); 

  /* ------------------------------------------------------------
   *  Define Variables and then Processing
   * ------------------------------------------------------------*/
  // Differences
  img_t raw_diff_l_r, raw_diff_t_b, raw_diff_tl_br, raw_diff_tr_bl;
  // Square Sums of Differences
  img_t raw_sqsum_l_r, raw_sqsum_t_b, raw_sqsum_tl_br, raw_sqsum_tr_bl; 
  // Min of Squares
  img_t output_sqsum_min; 
  // output: as a mask
  img_t output_01_mask; 

  puts(">>>>>>>>>>>>>> Differences");
  // DIFF 
  diff_left_right(raw_src,
		  &raw_diff_l_r);
  diff_top_bottom(raw_src,
		  &raw_diff_t_b);
  diff_topleft_bottomright(raw_src,
			   &raw_diff_tl_br);
  diff_topright_bottomleft(raw_src,
			   &raw_diff_tr_bl);
  
  puts(">>>>>>>>>>>>>> SqSum"); 
  // SQSUM
  sqsum_left_right(raw_diff_l_r,
		   &raw_sqsum_l_r,
		   moravec_kernel_size);
  sqsum_top_bottom(raw_diff_t_b,
		   &raw_sqsum_t_b,
		   moravec_kernel_size);
  sqsum_topleft_bottomright(raw_diff_tl_br,
			    &raw_sqsum_tl_br,
			    moravec_kernel_size);
  sqsum_topright_bottomleft(raw_diff_tr_bl,
			    &raw_sqsum_tr_bl,
			    moravec_kernel_size);

  puts(">>>>>>>>>>>>>> Min of Four"); 
  // MIN
  four2one(&output_sqsum_min, 
	   raw_sqsum_l_r,
	   raw_sqsum_t_b,
	   raw_sqsum_tl_br,
	   raw_sqsum_tr_bl);
 
  puts(">>>>>>>>>>>>>> Apply Threshold"); 
  apply_threshold(output_sqsum_min,
		  &output_01_mask,
		  moravec_threshold);

  char outpath[60];
  sprintf(outpath, "thrld_%d.%s", moravec_threshold, "raw");
  printf(">> Save to file: %s", outpath);
  save_img(&output_01_mask, outpath);

  // if there are other thresholds
  if (atoi(argv[5]) == 0) {
    for (int t = 7; t < argc; ++t) {
      set_moravec_threshold(&moravec_threshold, atoi(argv[t]));
      apply_threshold(output_sqsum_min,
		      &output_01_mask,
		      moravec_threshold);
      sprintf(outpath, "thrld_%d.%s", moravec_threshold, "raw");
      printf(">> Save to file: %s", outpath);
      save_img(&output_01_mask, outpath);
    }
  }

  return 0;
}
Exemplo n.º 6
0
/**
 * @brief post_request_cb The callback function of a POST request to upload a image.
 *
 * @param req The request with image buffer.
 * @param arg It is not useful.
 */
void post_request_cb(evhtp_request_t *req, void *arg)
{
    int post_size = 0;
    char *boundary = NULL, *boundary_end = NULL;
    int boundary_len = 0;
    char *fileName = NULL;
    char *boundaryPattern = NULL;
    char *buff = NULL;

    int req_method = evhtp_request_get_method(req);
    if(req_method >= 16)
        req_method = 16;
    LOG_PRINT(LOG_INFO, "Method: %d", req_method);
    if(strcmp(method_strmap[req_method], "POST") != 0)
    {
        LOG_PRINT(LOG_INFO, "Request Method Not Support.");
        goto err;
    }


    const char *content_len = evhtp_header_find(req->headers_in, "Content-Length");
    post_size = atoi(content_len);
    const char *content_type = evhtp_header_find(req->headers_in, "Content-Type");
    if(strstr(content_type, "multipart/form-data") == 0)
    {
        LOG_PRINT(LOG_ERROR, "POST form error!");
        goto err;
    }
    else if(strstr(content_type, "boundary") == 0)
    {
        LOG_PRINT(LOG_ERROR, "boundary NOT found!");
        goto err;
    }

    boundary = strchr(content_type, '=');
    boundary++;
    boundary_len = strlen(boundary);

    if (boundary[0] == '"') 
    {
        boundary++;
        boundary_end = strchr(boundary, '"');
        if (!boundary_end) 
        {
            LOG_PRINT(LOG_ERROR, "Invalid boundary in multipart/form-data POST data");
            goto err;
        }
    } 
    else 
    {
        /* search for the end of the boundary */
        boundary_end = strpbrk(boundary, ",;");
    }
    if (boundary_end) 
    {
        boundary_end[0] = '\0';
        boundary_len = boundary_end-boundary;
    }

    LOG_PRINT(LOG_INFO, "boundary Find. boundary = %s", boundary);
            
    /* 依靠evbuffer自己实现php处理函数 */
	evbuf_t *buf;
    buf = req->buffer_in;
    buff = (char *)malloc(post_size);
    int rmblen, evblen;
    int img_size = 0;

    if(evbuffer_get_length(buf) <= 0)
    {
        LOG_PRINT(LOG_ERROR, "Empty Request!");
        goto err;
    }

    while((evblen = evbuffer_get_length(buf)) > 0)
    {
        LOG_PRINT(LOG_INFO, "evblen = %d", evblen);
        rmblen = evbuffer_remove(buf, buff, evblen);
        LOG_PRINT(LOG_INFO, "rmblen = %d", rmblen);
        if(rmblen < 0)
        {
            LOG_PRINT(LOG_ERROR, "evbuffer_remove failed!");
            goto err;
        }
    }

    int start = -1, end = -1;
    const char *fileNamePattern = "filename=";
    const char *typePattern = "Content-Type";
    const char *quotePattern = "\"";
    const char *blankPattern = "\r\n";
    boundaryPattern = (char *)malloc(boundary_len + 4);
    sprintf(boundaryPattern, "\r\n--%s", boundary);
    LOG_PRINT(LOG_INFO, "boundaryPattern = %s, strlen = %d", boundaryPattern, (int)strlen(boundaryPattern));
    if((start = kmp(buff, post_size, fileNamePattern, strlen(fileNamePattern))) == -1)
    {
        LOG_PRINT(LOG_ERROR, "Content-Disposition Not Found!");
        goto err;
    }
    start += 9;
    if(buff[start] == '\"')
    {
        start++;
        if((end = kmp(buff+start, post_size-start, quotePattern, strlen(quotePattern))) == -1)
        {
            LOG_PRINT(LOG_ERROR, "quote \" Not Found!");
            goto err;
        }
    }
    else
    {
        if((end = kmp(buff+start, post_size-start, blankPattern, strlen(blankPattern))) == -1)
        {
            LOG_PRINT(LOG_ERROR, "quote \" Not Found!");
            goto err;
        }
    }
    fileName = (char *)malloc(end + 1);
    memcpy(fileName, buff+start, end);
    fileName[end] = '\0';
    LOG_PRINT(LOG_INFO, "fileName = %s", fileName);

    char fileType[32];
    if(get_type(fileName, fileType) == -1)
    {
        LOG_PRINT(LOG_ERROR, "Get Type of File[%s] Failed!", fileName);
        goto err;
    }
    if(is_img(fileType) != 1)
    {
        LOG_PRINT(LOG_ERROR, "fileType[%s] is Not Supported!", fileType);
        goto err;
    }

    end += start;

    if((start = kmp(buff+end, post_size-end, typePattern, strlen(typePattern))) == -1)
    {
        LOG_PRINT(LOG_ERROR, "Content-Type Not Found!");
        goto err;
    }
    start += end;
    LOG_PRINT(LOG_INFO, "start = %d", start);
    if((end =  kmp(buff+start, post_size-start, blankPattern, strlen(blankPattern))) == -1)
    {
        LOG_PRINT(LOG_ERROR, "Image Not complete!");
        goto err;
    }
    end += start;
    LOG_PRINT(LOG_INFO, "end = %d", end);
    start = end + 4;
    LOG_PRINT(LOG_INFO, "start = %d", start);
    if((end = kmp(buff+start, post_size-start, boundaryPattern, strlen(boundaryPattern))) == -1)
    {
        LOG_PRINT(LOG_ERROR, "Image Not complete!");
        goto err;
    }
    end += start;
    LOG_PRINT(LOG_INFO, "end = %d", end);
    img_size = end - start;


    LOG_PRINT(LOG_INFO, "post_size = %d", post_size);
    LOG_PRINT(LOG_INFO, "img_size = %d", img_size);
    if(img_size <= 0)
    {
        LOG_PRINT(LOG_ERROR, "Image Size is Zero!");
        goto err;
    }

    char md5sum[33];

    LOG_PRINT(LOG_INFO, "Begin to Save Image...");
    if(save_img(buff+start, img_size, md5sum) == -1)
    {
        LOG_PRINT(LOG_ERROR, "Image Save Failed!");
        goto err;
    }

    //libevhtp has bug with uri->authority->hostname, so zimg don't show hostname temporarily.
    //const char *host_name = req->uri->authority->hostname;
    //const int *host_port = req->uri->authority->port;
    //LOG_PRINT(LOG_INFO, "hostname: %s", req->uri->authority->hostname);
    //LOG_PRINT(LOG_INFO, "hostport: %d", host_port);
    evbuffer_add_printf(req->buffer_out, 
        "<html>\n<head>\n"
        "<title>Upload Successfully</title>\n"
        "</head>\n"
        "<body>\n"
        "<h1>MD5: %s</h1>\n"
        "Image upload successfully! You can get this image via this address:<br/><br/>\n"
        "http://yourhostname:%d/%s?w=width&h=height&g=isgray\n"
        "</body>\n</html>\n",
        md5sum, settings.port, md5sum
        );
    //evhtp_headers_add_header(req->headers_out, evhtp_header_new("Server", "zimg/1.0.0 (Unix) (OpenSUSE/Linux)", 0, 0));
    evhtp_headers_add_header(req->headers_out, evhtp_header_new("Server", server_name, 0, 0));
    evhtp_headers_add_header(req->headers_out, evhtp_header_new("Content-Type", "text/html", 0, 0));
    evhtp_send_reply(req, EVHTP_RES_OK);
    LOG_PRINT(LOG_INFO, "============post_request_cb() DONE!===============");
    goto done;

err:
    evbuffer_add_printf(req->buffer_out, "<html><body><h1>Upload Failed!</h1></body><html>"); 
    //evhtp_headers_add_header(req->headers_out, evhtp_header_new("Server", "zimg/1.0.0 (Unix) (OpenSUSE/Linux)", 0, 0));
    evhtp_headers_add_header(req->headers_out, evhtp_header_new("Server", server_name, 0, 0));
    evhtp_headers_add_header(req->headers_out, evhtp_header_new("Content-Type", "text/html", 0, 0));
    evhtp_send_reply(req, EVHTP_RES_200);
    LOG_PRINT(LOG_INFO, "============post_request_cb() ERROR!===============");

done:
    if(fileName)
        free(fileName);
    if(boundaryPattern)
        free(boundaryPattern);
    if(buff)
        free(buff);
}
bool IpolACEUpdater::run( MP mp ) {
    MP lst_img = mp[ "_children[ 0 ]._children" ];  // La liste des images
    const int nb_img = lst_img.size();              // Le nombre d'images
    for(int i = 0; i < nb_img; ++i) {
        MP img = lst_img[i];                        // La i-eme image
        QString name = img["_name"];
        
        if(img.type() == "ImgItem"){
            /// Load ImgItem into a QImage
            QString name = img[ "_name" ];
            QImage tmp_input;
            load_img(img,tmp_input);
            /// Save it into a processable file
            QString tmp_input_name = name + ".tmp.png";
            tmp_input.save(tmp_input_name,"PNG");
            
            QString tmp_output_name = "ACE_" + name + ".tmp.png";
            
            std::stringstream commandes;
            commandes << "../IpolACEPlugin/ServerPlugin/src_ace/ace";
            
            commandes << " -a " << (double) mp["alpha.val"];
            
            int num_weight = mp["weight.num"];
            switch(num_weight) {
                case 0:
                    commandes << " -w 1/r";
                    break;
                case 1:
                    commandes << " -w 1";
                    break;
                case 2:
                    commandes << " -w G:" << (double) mp["weight.lst"][num_weight]["_sigma.val"];
                    break;
                default:
                    add_message( mp, ET_Error, "Invalid weight type" );
                    return false;
            }
            
            int num_method = mp["method.num"];
            MP method = mp["method.lst"][num_method];
            switch(num_method) {
                case 0:
                    commandes << " -m interp:" << (int) method["_level.val"];
                    break;
                case 1:
                {
                    int num_poly = method["_degree.num"];
                    commandes << " -m poly:"   << ((QString) method["_degree.lst"][num_poly]).toStdString();
                    break;
                }
                default:
                    add_message( mp, ET_Error, "Invalid method type" );
                    return false;
            }
            
            commandes << " \"" << tmp_input_name.toStdString()  << "\"";
            commandes << " \"" << tmp_output_name.toStdString() << "\"";
            std::string cmd = commandes.str();
            int output = std::system(cmd.c_str());
            qDebug() << cmd.c_str();
            qDebug() << "Returned " << output;
            
            if(not output) {
                /// Retrieve image data into a ImgItem
                QImage tmp_output(tmp_output_name);
                MP new_img = MP::new_obj("ImgItem");
                new_treeitem(new_img , "ACE_" + name);
                new_img["img"] = MP::new_obj("Img");
                save_img(new_img["img"], tmp_output);
                
                /// Add result image to output set
                mp["_output[0]._children"] << new_img;
            }
            else {
                add_message( mp, ET_Error, "Error while running the application" );
            }
            
            /// clean temporary files
            QFile::remove(tmp_input_name);
            QFile::remove(tmp_output_name);
        }
        else {
            add_message( mp, ET_Error, "Unable to find image file in tree" );
        }
    }

    add_message( mp, ET_Info, "Ipol LSD -> OK" );
    qDebug() << "Ipol LSD just finish";
}
bool IpolLSDUpdater::run( MP mp ) {
    MP lst_img = mp[ "_children[ 0 ]._children" ];  // La liste des images
    const int nb_img = lst_img.size();              // Le nombre d'images
    for(int i = 0; i < nb_img; ++i) {
        MP img = lst_img[i];                        // La i-eme image
        QString name = img["_name"];
        
        if(img.type() == "ImgItem"){
            /// Load ImgItem into a QImage
            QString name = img[ "_name" ];
            QImage tmp_input;
            load_img(img,tmp_input);
            /// Save it into a processable file
            QString tmp_input_name = name + ".pgm";
            tmp_input.save(tmp_input_name,"PGM");
            
            QString tmp_output_name = "LSD_" + name + ".pgm";
            
            std::stringstream commandes;
            commandes << "../IpolLSDPlugin/ServerPlugin/src_lsd/lsd";
            commandes << " -s " << (double) mp["scale.val"];
            commandes << " -c " << (double) mp["sigma_coef.val"];
            commandes << " -q " << (double) mp["quant.val"];
            commandes << " -a " << (double) mp["ang_th.val"];
            commandes << " -e " << (double) mp["log_eps.val"];
            commandes << " -d " << (double) mp["density_th.val"];
            commandes << " -b " << (int)    mp["n_bins.val"];
            commandes << " -W " << (double) mp["width.val"];
            commandes << " -R \"" << tmp_output_name.toStdString() << "\"";
            commandes << "    \"" << tmp_input_name.toStdString()  << "\"";
            commandes << " results.txt";    /// Unused results file
            //commandes << " > log.txt";      /// for error management
            std::string cmd = commandes.str();
            int output = std::system(cmd.c_str());
            //qDebug() << cmd.c_str();
            //qDebug() << "Returned " << output;
            
            if(not output) {
                /// Retrieve image data into a ImgItem
                QImage tmp_output(tmp_output_name);
                MP new_img = MP::new_obj("ImgItem");
                new_treeitem(new_img , "LSD_" + name);
                new_img["img"] = MP::new_obj("Img");
                save_img(new_img["img"], tmp_output);
                
                /// Add result image to output set
                mp["_output[0]._children"] << new_img;
            }
            else {
                add_message( mp, ET_Error, "Error while running the application" );
            }
            
            /// clean temporary files
            QFile::remove(tmp_input_name);
            QFile::remove(tmp_output_name);
            QFile::remove("results.txt");
        }
        else {
            add_message( mp, ET_Error, "Unable to find image file in tree" );
        }
    }

    add_message( mp, ET_Info, "Ipol LSD -> OK" );
    qDebug() << "Ipol LSD just finish";
}