GLvoid CApiDumpDraw::DumpPng(GLchar *fileName) { GLint width = 0; GLint height = 0; GLint x = 0; GLint y = 0; GLubyte *data = NULL; GLubyte *pData = NULL; pic_data pic_d; GLubyte *rgba[4]; GLubyte *r,*g,*b,*a; GLint viewport[4]; g_opengl->glGetIntegerv(GL_VIEWPORT, viewport); x = viewport[0]; y = viewport[1]; width = viewport[2]; height = viewport[3]; data = (GLubyte *)malloc(4*width*height); pData = data; g_opengl->glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); pic_d.width = width; pic_d.height = height; pic_d.bit_depth = 8; pic_d.flag = HAVE_ALPHA; r = rgba[0] = (GLubyte*)malloc(width*height); g = rgba[1] = (GLubyte*)malloc(width*height); b = rgba[2] = (GLubyte*)malloc(width*height); a = rgba[3] = (GLubyte*)malloc(width*height); r = rgba[0]; g = rgba[1]; b = rgba[2]; a = rgba[3]; for(GLint i = 0; i < width*height; i++) { *r = pData[0]; *g = pData[1]; *b = pData[2]; *a = pData[3]; r++; g++; b++; a++; pData += 4; } pic_d.rgba = rgba; write_png_file(fileName, &pic_d); free(data); free(rgba[0]); free(rgba[1]); free(rgba[2]); free(rgba[3]); m_pAnalyzer->DumpStringToApiDump(fileName); m_pAnalyzer->DumpStringToApiDump("\n"); }
void generate_thumbnails() { FILE *tfp; char file[MAX_PATH_STR_LEN]; int i, n; MediaScanImage *thumb; Buffer *dbuf; MediaScan *s = ms_create(); ms_set_result_callback(s, my_result_callback); ms_set_error_callback(s, my_error_callback); ms_add_thumbnail_spec(s, THUMB_PNG, 32,32, TRUE, 0, 90); for(i = 0; expected_results[i].filename; i++) { ms_scan_file(s, expected_results[i].filename, TYPE_IMAGE); if (!strcmp("JPEG", result._thumbs[0]->codec)) sprintf(file, "%s.jpg", expected_results[i].thumb_filename); else sprintf(file, "%s.png",expected_results[i].thumb_filename); write_png_file(file); } ms_destroy(s); }
static void run( const char *input, const char *output ) { Huint i; Huint j; image *img; clock_t start; clock_t end; double ela; double bytes; double bandw; img = read_png_file( input ); start = clock(); send_image( img ); recv_image( img ); end = clock(); write_png_file( output, img ); ela = (double)(end - start); ela /= 2405516000.0; bytes = LOOPS * DATA * 8.0; bandw = bytes / ela; printf( "Processed %.2f bytes of data in %.2f seconds\n", bytes, ela ); printf( "Bandwidth: %.2f bytes/sec\n", bandw ); }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file_in> <file_out>"); // Read file + setup logic png_info_t* png_info = malloc(sizeof(png_info_t)); FILE *fp = read_png_file_stats(argv[1], png_info); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * png_info->height); for (int y=0; y < png_info->height; y++) row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_info->png_ptr,png_info->info_ptr)); row_pointers_post_bh = (png_bytep*) malloc(sizeof(png_bytep) * png_info->height); for (int y=0; y < png_info->height; y++) row_pointers_post_bh[y] = (png_byte*) malloc(png_get_rowbytes(png_info->png_ptr,png_info->info_ptr)); row_pointers_post_bv = (png_bytep*) malloc(sizeof(png_bytep) * png_info->height); for (int y=0; y < png_info->height; y++) row_pointers_post_bv[y] = (png_byte*) malloc(png_get_rowbytes(png_info->png_ptr,png_info->info_ptr)); read_png_file(&row_pointers, png_info, fp); // Distributed preparation logic // Distribute chunks of png int global_height = png_info->height; int global_width = png_info->width; int local_min_height; int local_max_height; int local_min_width; int local_max_width; if (world_rank < world_size) { local_min_height = global_height / world_size * (world_rank); local_max_height = (global_height / world_size * (world_rank + 1)); } else { // last rank - takes extra local_min_height = global_height / world_size * (world_rank); local_max_height = global_height; } local_min_width = 0; local_max_width = global_width; distributed_info_t *distributed_info = malloc(sizeof(distributed_info_t)); distributed_info->local_min_height = local_min_height; distributed_info->local_max_height = local_max_height; distributed_info->local_min_width = local_min_width; distributed_info->local_max_width = local_max_width; // Define Need and Have regions (N and H) // Blur logic blur(png_info, distributed_info); // region goes from local_min_height to local_max_height - 1 // Write file + cleanup logic write_png_file(argv[2], &row_pointers_post_bv, png_info); free(distributed_info); for (int y = 0; y < png_info->height; y++) { free(row_pointers[y]); free(row_pointers_post_bh[y]); free(row_pointers_post_bv[y]); } free(row_pointers); free(row_pointers_post_bh); free(row_pointers_post_bv); free(png_info); return 0; }
int main(int argc, char **argv) { if (argc != 4) abort_("Usage: program_name <file_in> <file_out> <shift>"); read_png_file(argv[1]); process_file(argv[3]); write_png_file(argv[2]); }
static void save_icon(char * filename, int resolution) { unsigned char * buffer; if (file_exists(filename) != 0) return; buffer = (unsigned char *)malloc(resolution*resolution*3); memset((void*)buffer, '\255', resolution*resolution*3); write_png_file(filename, resolution, resolution, buffer); free(buffer); }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file_in> <file_out>"); // three of the above functions called here .... read_png_file(argv[1]); process_file(); write_png_file(argv[2]); return 0; }
void framecapture_writepngframes(void) { int frame; frame_struct_t* frame_ptr = NULL; for (frame = 0; frame < nr_frames; frame++) { frame_ptr = framelinkedlist_search_in_list(frame, NULL); if (NULL == frame_ptr) { printf("\n Search [id = %d] failed, no such element found\n", frame); } else { char fname[64]; sprintf(fname, "f%03d-frame%d.png", frame_ptr->id, frame_ptr->name); printf("writing frame, %s\n", fname); #if 0 int x, y; png_bytep *row_pointers; row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * 720); for (y = 0; y < 720; y++) { row_pointers[y] = (png_byte *)malloc(1280 * 4); } for (y = 0; y < 720; y++) { png_byte *row = row_pointers[y]; for (x = 0; x < 1280; x++) { png_byte *ptr = &(row[x * 4]); uint8_t *buf = frame_ptr->buf; ptr[0] = buf[(y * 1280 + x) * 4 + 2]; /* R */ ptr[1] = buf[(y * 1280 + x) * 4 + 1]; /* G */ ptr[2] = buf[(y * 1280 + x) * 4 + 0]; /* B */ ptr[3] = buf[(y * 1280 + x) * 4 + 3]; /* A */ } } #endif #if 0 write_png_file(fname, 1280, 720, row_pointers); #endif /* * write_png_file frees row_pointers */ FILE* pFile = fopen(fname, "wb"); fwrite(frame_ptr->buf, 1, frame_ptr->size, pFile); chmod(fname, 0666); fclose(pFile); } } }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file_in> <file_out>"); read_png_file(argv[1]); process_file(argv[2]); write_png_file("output.png"); /* int y; for (y=0; y<height; y++) free(row_pointers[y]); free(row_pointers); */ return 0; }
int main(int argc, char *argv[]) { char *c="obraz"; char nazwa_obr[10]; int i; int sizex=0; int sizey=0; int siatka[100][100]; int csiatka[100][100]; int ilosc_gen; FILE* in=fopen(argv[1], "r");/*plik z wspolrzednymi aktywnymi*/ FILE* in2=fopen(argv[2], "r");/*plik z wymiarami siatki*/ fscanf(in2,"%d %d", &sizex,&sizey); siatka[100][100]=siatka[sizex][sizey]; csiatka[100][100]=csiatka[sizex][sizey]; printf("Proszę podać ilość generacji:\n"); scanf("%d", &ilosc_gen); wyzeruj(sizex, sizey, siatka); wyzeruj(sizex, sizey, csiatka); wprowadz_gen(sizey, csiatka, in); for(i=0; i< ilosc_gen ; i++){ sprintf(nazwa_obr,"%s%d.png",c,i); automat(sizex, sizey, siatka, csiatka); process_file(sizex, sizey, siatka); write_png_file(nazwa_obr); } return 0; }
int main(int argc, char **argv) { int c; int num_handle = 4; int img = 1; bool received_all_fragments = false; bool * received_fragments = calloc(N, sizeof(bool)); bool * tempBoolCheck = calloc(N, sizeof(bool)); while ((c = getopt (argc, argv, "t:i:")) != -1) { switch (c) { case 't': num_handle = strtoul(optarg, NULL, 10); if (num_handle == 0) { printf("%s: option requires an argument > 0 -- 't'\n", argv[0]); return -1; } break; case 'i': img = strtoul(optarg, NULL, 10); if (img == 0) { printf("%s: option requires an argument > 0 -- 'i'\n", argv[0]); return -1; } break; default: return -1; } } png_structp png_ptr[num_handle]; png_infop info_ptr[num_handle]; png_byte * output_buffer = calloc(WIDTH*HEIGHT*4, sizeof(png_byte)); //Lock for pthread // pthread_mutex_init(&lock, NULL); pthread_mutex_init(&lock_hd, NULL); // pthread_mutex_init(&lock_read_cb, NULL); char * url = malloc(sizeof(char)*strlen(BASE_URL)+4*5); char * url_2 = malloc(sizeof(char)*strlen(BASE_URL_2)+4*5); char * url_3 = malloc(sizeof(char)*strlen(BASE_URL_3)+4*5); png_bytep input_buffer[num_handle]; struct bufdata bd[num_handle]; struct headerdata hd[num_handle]; for(int i = 0; i< num_handle; i++) { input_buffer[i] = malloc(sizeof(png_byte)*BUF_SIZE); bd[i].buf = input_buffer[i]; hd[i].received_fragments = received_fragments; } // request appropriate URL sprintf(url, BASE_URL, img); sprintf(url_2, BASE_URL_2, img); sprintf(url_3, BASE_URL_3, img); printf("requesting URL %s\n", url); CURL *handles[num_handle]; CURLM *multi_handle; CURLcode res = 0; int running_handles; CURLMsg *msg; /* for picking up messages with the transfer status */ int msgs_left; /* how many messages are left */ for (int i = 0; i < num_handle; i++) { handles[i] = curl_easy_init(); if(!handles[i]) { abort_("[main] could not initialize curl"); } curl_easy_setopt(handles[i], CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(handles[i], CURLOPT_WRITEDATA, &(bd[i])); curl_easy_setopt(handles[i], CURLOPT_HEADERDATA, &hd[i]); curl_easy_setopt(handles[i], CURLOPT_HEADERFUNCTION, header_cb); if(i%3 == 0) { curl_easy_setopt(handles[i], CURLOPT_URL, url); } else if(i%3 == 1) { curl_easy_setopt(handles[i], CURLOPT_URL, url_2); } else { curl_easy_setopt(handles[i], CURLOPT_URL, url_3); } //keep pointer to array index //curl_easy_setopt(handles[i], CURLOPT_PRIVATE, url); // reset input buffer bd[i].len = bd[i].pos = 0; bd[i].max_size = BUF_SIZE; } /* init a multi stack */ multi_handle = curl_multi_init(); /* add the individual transfers */ for (int i = 0; i < num_handle; i++) { curl_multi_add_handle(multi_handle, handles[i]); } // do curl request; check for errors // res = curl_easy_perform(curl); // if(res != CURLE_OK) // abort_("[main] curl_easy_perform() failed: %s\n", // curl_easy_strerror(res)); //curl_multi_perform(multi_handle, &running_handles); do { int numfds=0; res = curl_multi_wait(multi_handle, NULL, 0, MAX_WAIT_MSECS, &numfds); if(res != CURLM_OK) { fprintf(stderr, "error: curl_multi_wait() returned %d\n", res); return EXIT_FAILURE; } curl_multi_perform(multi_handle, &running_handles); #ifdef _DEBUG_1_ //printf("\n## numfds: %d, num_handle: %d\n", numfds, num_handle); fflush(stdout); #endif /* If the amount of running_handles is changed from the previous call (or is less than the amount of easy handles you've added to the multi handle), you know that there is one or more transfers less "running". You can then call curl_multi_info_read to get information about each individual completed transfer, and that returned info includes CURLcode and more. If an added handle fails very quickly, it may never be counted as a running_handle.*/ if(running_handles < num_handle) { #ifdef _DEBUG_1_ printf("\nIn IF:, running_handles: %d\n", running_handles); fflush(stdout); #endif while((msg = curl_multi_info_read(multi_handle, &msgs_left))) { #ifdef _DEBUG_1_ printf("\nmsgs_left:%d\n", msgs_left); fflush(stdout); #endif if (msg->msg == CURLMSG_DONE) { CURL* replaceHandle = msg->easy_handle; curl_multi_remove_handle(multi_handle, replaceHandle); curl_multi_add_handle(multi_handle, replaceHandle); int iBdIndex = -1; for (int i = 0; i < num_handle; i++) { if (handles[i] == replaceHandle) { iBdIndex = i; #ifdef _DEBUG_1_ printf("\nfound handle, i:%d", i); fflush(stdout); #endif break; } } res = msg->data.result; if(res != CURLE_OK) { #ifdef _DEBUG_1_ printf("\nContinued in the while loop, res is %d", res); fflush(stdout); #endif continue; //abort_("[main] curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } //check for dup // if (received_fragments[hd[iBdIndex].n]) // { // #ifdef _DEBUG_1_ // printf("\nContinued in the while loop, dup for %d", hd[iBdIndex].n); // fflush(stdout); // #endif // continue; // } //pthread_mutex_lock(&lock); png_ptr[iBdIndex] = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr[iBdIndex]) { abort_("[main] png_create_read_struct failed"); } // Write to the shared resource . // read PNG (as downloaded from network) and copy it to output buffer #ifdef _DEBUG_1_ printf("\nindex: %d, bd->pos: %d, bd->len: %d, hd.n: %d\n",iBdIndex, bd[iBdIndex].pos, bd[iBdIndex].len,hd[iBdIndex].n); fflush(stdout); #endif png_bytep* row_pointers = read_png_file(png_ptr[iBdIndex], &info_ptr[iBdIndex], &bd[iBdIndex]); paint_destination(png_ptr[iBdIndex], row_pointers, hd[iBdIndex].n*BUF_WIDTH, 0, output_buffer); // reset input buffer bd[iBdIndex].len = bd[iBdIndex].pos = 0; bd[iBdIndex].max_size = BUF_SIZE; //bd[1].len = bd[1].pos = 0; bd[1].max_size = BUF_SIZE; // free allocated memory for (int y=0; y<BUF_HEIGHT; y++) { free(row_pointers[y]); } free(row_pointers); png_destroy_read_struct(&png_ptr[iBdIndex], &info_ptr[iBdIndex], NULL); // check for unreceived fragments received_all_fragments = true; for (int i = 0; i < N; i++) { if (!received_fragments[i]) { received_all_fragments = false; } } //tempBoolCheck tempBoolCheck[hd[iBdIndex].n]=true; for(int i = 0; i < N; i++) { if(!tempBoolCheck[i]) { received_all_fragments=false; } } //pthread_mutex_unlock(&lock); } else { //Error, replace the handle #ifdef _DEBUG_1_ printf("\nError, replace the handle\n"); fflush(stdout); #endif CURL* replaceHandle = msg->easy_handle; curl_multi_remove_handle(multi_handle, replaceHandle); curl_multi_add_handle(multi_handle, replaceHandle); for (int i = 0; i < num_handle; i++) { if (handles[i] == replaceHandle) { bd[i].len = bd[i].pos = 0; bd[i].max_size = BUF_SIZE; break; } } } //break; } } } while(!received_all_fragments); free(url); free(url_2); free(url_3); //free(input_buffer); //curl_easy_cleanup(curl); curl_multi_cleanup(multi_handle); /* Free the CURL handles */ for (int i=0; i<num_handle; i++) { free(input_buffer[i]); curl_easy_cleanup(handles[i]); } // now, write the array back to disk using write_png_file png_bytep * output_row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * HEIGHT); for (int i = 0; i < HEIGHT; i++) output_row_pointers[i] = &output_buffer[i*WIDTH*4]; write_png_file("output.png", output_row_pointers); free(output_row_pointers); free(output_buffer); free(received_fragments); //destory lock // pthread_mutex_destroy(&lock); pthread_mutex_destroy(&lock_hd); // pthread_mutex_destroy(&lock_read_cb); return 0; }
int main(int argc, char **argv) { clock_t begin, end, p_begin, p_end; double time_spent, p_time_spent; begin = clock(); /* here, do your time-consuming job */ int c; int num_threads = 4; int img = 1; bool * received_fragments = calloc(N, sizeof(bool)); png_byte * output_buffer = calloc(WIDTH*HEIGHT*4, sizeof(png_byte)); while ((c = getopt (argc, argv, "t:")) != -1) { switch (c) { case 't': num_threads = strtoul(optarg, NULL, 10); if (num_threads == 0) { printf("%s: option requires an argument > 0 -- 't'\n", argv[0]); return -1; } break; case 'i': img = strtoul(optarg, NULL, 10); if (img == 0) { printf("%s: option requires an argument > 0 -- 'i'\n", argv[0]); return -1; } break; default: return -1; } } CURLcode global_init = curl_global_init(CURL_GLOBAL_NOTHING); if (global_init) abort_("[main] could not initialize curl"); struct shared_res shared_resource0 = {received_fragments, output_buffer, img, 0}; struct shared_res shared_resource1 = {received_fragments, output_buffer, img, 1}; struct shared_res shared_resource2 = {received_fragments, output_buffer, img, 2}; //struct shared_res * pshared_resource = &shared_resource; pthread_t thread [num_threads]; p_begin = clock(); for (int i = 0; i < num_threads; ++i) { if (i%3 == 0){ pthread_create (& thread [i] , NULL , thread_callback, &shared_resource0); } else if (i%3 == 1){ pthread_create (& thread [i] , NULL , thread_callback, &shared_resource1); } else{ pthread_create (& thread [i] , NULL , thread_callback, &shared_resource2); } } for (int i = 0; i < num_threads; ++i) { pthread_join ( thread [i] , NULL ); } p_end = clock(); pthread_mutex_destroy(&mutex1); pthread_mutex_destroy(&mutex2); // now, write the array back to disk using write_png_file png_bytep * output_row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * HEIGHT); for (int i = 0; i < HEIGHT; i++) output_row_pointers[i] = &output_buffer[i*WIDTH*4]; write_png_file("output.png", output_row_pointers); free(output_row_pointers); free(output_buffer); free(received_fragments); curl_global_cleanup(); end = clock(); p_time_spent = (double)(p_end - p_begin) / CLOCKS_PER_SEC; time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("parallel time = %g\n", p_time_spent); printf("total time = %g\n", time_spent); return 0; }
/* an individual produces an artwork */ void paintatron::produce_art(int index, unsigned char * img, int img_width, int img_height, QImage * source_images, int no_of_source_images, QString * source_texts, int no_of_source_texts, char * filename) { int x,y,x2,y2,n=0,itt,c, source_x, source_y, source_index=0,R,G,B,R2=0,G2=0,B2=0,actuator_offset=0,sensor_offset; int source_char_index; float scale_x, scale_y, angle, radius, radius_scale=img_width; int cx = img_width/2; int cy = img_height/2; const char * texts[128]; //const int sensor_inputs = sensor_image_inputs; gprcm_population * pop = &sys.island[0]; gprcm_function * f = &pop->individual[index]; gprcm_clear_state(f, pop->rows, pop->columns, pop->sensors, pop->actuators); for (itt = 0; itt < no_of_source_texts; itt++) { texts[itt] = source_texts[itt].toStdString().c_str(); } /* for every image pixel */ for (y = 0; y < img_height; y++) { for (x = 0; x < img_width; x++, n+=3) { if (no_of_source_images > 0) { source_index = 0; for (itt = 0; itt < image_itterations; itt++) { actuator_offset = itt*actuator_image_inputs; sensor_offset = itt*sensor_image_inputs; /* source image to use */ source_index++; if (source_index > no_of_source_images) source_index -= no_of_source_images; cx += (int)((fmod(fabs(gprcm_get_actuator(f, initial_actuator+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*img_width/16); if (cx > img_width) cx -= img_width; if (cx < 0) cx += img_width; cy += (int)((fmod(fabs(gprcm_get_actuator(f, initial_actuator+1+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*img_height/16); if (cy > img_height) cy -= img_height; if (cy < 0) cy += img_height; if ((source_images[source_index].width() > 0) && (source_images[source_index].height() > 0)) { angle = fmod(fabs(gprcm_get_actuator(f, initial_actuator+2+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*2*3.1415927f; radius = fmod(fabs(gprcm_get_actuator(f, initial_actuator+3+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*radius_scale; scale_x = ((fmod(fabs(gprcm_get_actuator(f, initial_actuator+4+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*8); scale_y = ((fmod(fabs(gprcm_get_actuator(f, initial_actuator+5+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*8); x2 = cx + (int)(radius*sin(angle)); y2 = cy + (int)(radius*cos(angle)); source_x = abs(x2*(int)(source_images[source_index].width())*scale_x/img_width) % source_images[source_index].width(); source_y = abs(y2*(int)(source_images[source_index].height())*scale_y/img_height) % source_images[source_index].height(); /* get the colour at this location */ QColor col = QColor::fromRgb (source_images[source_index].pixel(source_x,source_y)); col.getRgb(&R,&G,&B); /* set sensors to the colour values */ gprcm_set_sensor_complex(f, 5+sensor_offset, R/255.0f, G/255.0f, pop->sensors, pop->actuators, pop->rows, pop->columns); gprcm_set_sensor_complex(f, 6+sensor_offset, G/255.0f, B/255.0f, pop->sensors, pop->actuators, pop->rows, pop->columns); gprcm_set_sensor_complex(f, 7+sensor_offset, B/255.0f, R/255.0f, pop->sensors, pop->actuators, pop->rows, pop->columns); col = QColor::fromRgb (source_images[source_index].pixel((source_x+5) % source_images[source_index].width(),source_y)); col.getRgb(&R2,&G2,&B2); gprcm_set_sensor(f, 8+sensor_offset, (abs(R-R2) + abs(G-G2) + abs(B-B2))/255.0f); col = QColor::fromRgb (source_images[source_index].pixel(source_x,(source_y+5) % source_images[source_index].height())); col.getRgb(&R2,&G2,&B2); gprcm_set_sensor(f, 9+sensor_offset, (abs(R-R2) + abs(G-G2) + abs(B-B2))/255.0f); } } } if (no_of_source_texts > 0) { sensor_offset = image_itterations*sensor_image_inputs; actuator_offset = image_itterations*actuator_image_inputs; source_index = (int)(fmod(fabs(gprcm_get_actuator(f, initial_actuator+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*no_of_source_texts); source_char_index = (int)(fmod(fabs(gprcm_get_actuator(f, initial_actuator+1+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*(source_texts[source_index].length()-1)); gprcm_set_sensor(f, 10+sensor_offset, texts[source_index][source_char_index]/128.0f); } gprcm_set_sensor(f, 0, (x*2/(float)img_width)-1.0f); gprcm_set_sensor(f, 1, (y*2/(float)img_height)-1.0f); gprcm_set_sensor(f, 2, n/(float)(img_width*img_height*3)); gprcm_set_sensor(f, 3, (float)sqrt(((x-cx)*(x-cx)) + ((y-cy)*(y-cy)))/img_width); for (itt = 0; itt < run_steps; itt++) { /* run the program */ gprcm_run(f, pop, dropout_rate, 0, 0); } for (c = 0; c < 3; c++) { img[n + c] = (unsigned char)(fmod(fabs(gprcm_get_actuator(f, c, pop->rows, pop->columns, pop->sensors)),1.0f)*255); } radius_scale = img_width - (fmod(fabs(gprcm_get_actuator(f, 3, pop->rows, pop->columns, pop->sensors)),1.0f)*img_width); gprcm_set_sensor(f, 4,radius_scale/img_width); } } write_png_file(filename, img_width, img_height, img); }
virtual void encodeImage(CameraImage& ci) { write_png_file(ci.raw.buffer, &ci.encoded); }
void image_save(const image * img, const char * filename){ write_png_file(filename, img->width, img->height, img->buffer, img->type, 8); }
bool PngImageExt::Write(const char * filename) { write_png_file(filename, width, height, data); return true; }
int main(int argc, char **argv) { int c; int num_threads = 4; int img = 1; bool * received_fragments = calloc(N, sizeof(bool)); while ((c = getopt (argc, argv, "t:i:")) != -1) { switch (c) { case 't': num_threads = strtoul(optarg, NULL, 10); if (num_threads == 0) { printf("%s: option requires an argument > 0 -- 't'\n", argv[0]); return -1; } break; case 'i': img = strtoul(optarg, NULL, 10); if (img == 0) { printf("%s: option requires an argument > 0 -- 'i'\n", argv[0]); return -1; } break; default: return -1; } } //Lock for pthread pthread_mutex_init(&lock, NULL); CURLcode result = curl_global_init(CURL_GLOBAL_NOTHING); // 1 if(result != CURLE_OK) // 2 { #ifdef _DEBUG_1_ printf("\nCURL Global Init Failed\n"); fflush(stdout); #endif } png_byte * output_buffer = calloc(WIDTH*HEIGHT*4, sizeof(png_byte)); char * url = malloc(sizeof(char)*strlen(BASE_URL)+4*5); char * url_2 = malloc(sizeof(char)*strlen(BASE_URL_2)+4*5); char * url_3 = malloc(sizeof(char)*strlen(BASE_URL_3)+4*5); // request appropriate URL sprintf(url, BASE_URL, img); sprintf(url_2, BASE_URL_2, img); sprintf(url_3, BASE_URL_3, img); printf("requesting URL %s\n", url); struct threaddata tempthreaddata = {0}; tempthreaddata.url = url; tempthreaddata.output_buffer = output_buffer; tempthreaddata.received_fragments = received_fragments; struct threaddata tempthreaddata2 = {0}; tempthreaddata2.url = url_2; tempthreaddata2.output_buffer = output_buffer; tempthreaddata2.received_fragments = received_fragments; struct threaddata tempthreaddata3 = {0}; tempthreaddata3.url = url_3; tempthreaddata3.output_buffer = output_buffer; tempthreaddata3.received_fragments = received_fragments; //Thread management /* the next three lines (and the use of attr) weren't in lecture; * they aren't mandatory, but show how you can use pthread_attr_t * to make sure that the thread is joinable (and not detacthed */ pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_t tid[num_threads]; for(int i = 0; i < num_threads; i++) { //Call refactored function (loop) //requestFrag((void *) &tempthreaddata); #ifdef _DEBUG_1_ printf("\n###Before create###\n"); printf("%s\n", tempthreaddata.url); fflush(stdout); #endif if(i%3 == 0) { pthread_create(&tid[i], NULL, requestFrag, &tempthreaddata); } else if (i%3 == 1) { pthread_create(&tid[i], NULL, requestFrag, &tempthreaddata2); } else { pthread_create(&tid[i], NULL, requestFrag, &tempthreaddata3); } } for(int i = 0; i < num_threads; i++){ pthread_join(tid[i], NULL); } #ifdef _DEBUG_1_ printf("\n###2###\n"); fflush(stdout); #endif free(url); free(url_2); free(url_3); //Cleanup global init curl_global_cleanup(); // now, write the array back to disk using write_png_file png_bytep * output_row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * HEIGHT); for (int i = 0; i < HEIGHT; i++) output_row_pointers[i] = &output_buffer[i*WIDTH*4]; write_png_file("output.png", output_row_pointers); free(output_row_pointers); free(output_buffer); free(received_fragments); //destory lock pthread_mutex_destroy(&lock); return 0; }
void encode(char *msg, char *img, char *out_filename) { unsigned char *data, *enc_data, *final_data; char *filename = 0; int len, plen, final_len; FILE *fp = fopen(msg, "rb"); // Not saving the last null byte (in legal C strings) in either case // because all the chars are going into the image if (!fp) { len = strlen(msg); data = malloc(len); memcpy(data, msg, len); } else { filename = basename(msg); int fplen = strlen(filename)+1; // 1 for the colon char fprefix[fplen]; sprintf(fprefix, "%s:", filename); // Read the file to data fseek(fp, 0, SEEK_END); int file_len = ftell(fp); len = fplen + file_len; rewind(fp); data = malloc(len); // all chars are 1 byte int read_size = fread(data, 1, file_len, fp); if (file_len != read_size) Error("Error reading file"); // Prepend filename with colons (filenames can't have colons) // Filename gets encrypted too memmove(data+fplen, data, file_len); memcpy(data, fprefix, fplen); } fclose(fp); // Encrypt the data enc_data = aes_encrypt(&en, data, &len); // len gets updated here ^ to the new len of the encrypted data free(data); // Don't need this anymore plen = digits(len)+1; char prefix[plen]; sprintf(prefix, filename ? "%d>" : "%d<", len); // The different symbols are to differentiate between file data and plaintext final_len = plen + len; int padding = 3 - (final_len % 3); // Data needs to be divisible by 3 before being put // into the image int padded_len = final_len + padding; final_data = malloc(padded_len); memcpy(final_data, enc_data, len); free(enc_data); // All we need now is the final data // Prepend the encrypted data with the prefix of how long // it is so we know where to stop when decoding memmove(final_data+plen, final_data, len); memcpy(final_data, prefix, plen); // Last, add the padding of null bytes to make it divisible by 3 // Note: this will get removed in the end because // we keep the size not including the padding. We need to have it divisibile by 3 because // if we have 2 bits left of data, we need to store it in 1 whole pixel, which holds // 6 possible bits of information for (int i = final_len; i < padded_len; i++) final_data[i] = '\0'; // Checking if possible read_png_file(img); int space = (width * height) * (3.0f / 4.0f); if (padded_len > space) { // Make sure there is enough space in the image for the data puts("Not enough space in image."); char *rspace; rspace = byteconvert(padded_len); printf("Data size: %s\n", rspace); free(rspace); rspace = byteconvert(space); printf("Space available: %s\n", rspace); free(rspace); exit(1); } // Loop through all the data, 3 bytes at a time, // putting every 3 bytes into groups of 4 pixels // (8 bits for 3 bytes is 24 total bits, 6 bits for each of // 4 pixels (matching of total 24)) int pixel_loc = 0; int x, y; png_byte* pixel; for (int i = 0; i < padded_len; i += 3, pixel_loc += 4) { // next group of pixels every loop, so add 4 each time // Loop for each color component and for each byte of data // (3 of each) // Each byte has 8 bits, 2 of each go into each of the 4 pixels for (int j = 0; j < 3; j++, pixel_loc -= 4) { // reset to continue looping through the same pixels every time by doing -= 4 // Loop for each of 4 pixels to put the data into for (int p = 0; p < 4; p++, pixel_loc++) { x = pixel_loc % width, y = pixel_loc / width; pixel = &(row_pointers[y][x*3]); // j for color component // Replace last 2 bits with zeros pixel[j] &= ~3; int sh = p * 2; // Add 2 bits of data at a time pixel[j] |= ((int)final_data[i + j] & (3 << sh)) >> sh; // & 3 gets only the last 2 bits of the data (1 byte) because // 3 is 00000011 in binary, // 3 << 2 is 00001100, 3 << 4 is 00110000, etc } } } // Save the image with the data inside it // (use output filename if specified, otherwsie just "new-image.png") if (out_filename) write_png_file(out_filename); else write_png_file("new-image.png"); free(final_data); // Done with this }
int main(int argc, char **argv) { int c; int num_threads = 4; int img = 1; bool received_all_fragments = false; bool * received_fragments = calloc(N, sizeof(bool)); while ((c = getopt (argc, argv, "t:i:")) != -1) { switch (c) { case 't': num_threads = strtoul(optarg, NULL, 10); if (num_threads == 0) { printf("%s: option requires an argument > 0 -- 't'\n", argv[0]); return -1; } break; case 'i': img = strtoul(optarg, NULL, 10); if (img == 0) { printf("%s: option requires an argument > 0 -- 'i'\n", argv[0]); return -1; } break; default: return -1; } } CURL *curl; CURLcode res; png_structp png_ptr; png_infop info_ptr; png_byte * output_buffer = calloc(WIDTH*HEIGHT*4, sizeof(png_byte)); curl = curl_easy_init(); if (!curl) abort_("[main] could not initialize curl"); char * url = malloc(sizeof(char)*strlen(BASE_URL)+4*5); png_bytep input_buffer = malloc(sizeof(png_byte)*BUF_SIZE); struct bufdata bd; bd.buf = input_buffer; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &bd); struct headerdata hd; hd.received_fragments = received_fragments; curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hd); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_cb); // request appropriate URL sprintf(url, BASE_URL, img); printf("requesting URL %s\n", url); curl_easy_setopt(curl, CURLOPT_URL, url); do { png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[main] png_create_read_struct failed"); // reset input buffer bd.len = bd.pos = 0; bd.max_size = BUF_SIZE; // do curl request; check for errors res = curl_easy_perform(curl); if(res != CURLE_OK) abort_("[main] curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); // read PNG (as downloaded from network) and copy it to output buffer png_bytep* row_pointers = read_png_file(png_ptr, &info_ptr, &bd); paint_destination(png_ptr, row_pointers, hd.n*BUF_WIDTH, 0, output_buffer); // free allocated memory for (int y=0; y<BUF_HEIGHT; y++) free(row_pointers[y]); free(row_pointers); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); // check for unreceived fragments received_all_fragments = true; for (int i = 0; i < N; i++) if (!received_fragments[i]) received_all_fragments = false; } while (!received_all_fragments); free(url); free(input_buffer); curl_easy_cleanup(curl); // now, write the array back to disk using write_png_file png_bytep * output_row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * HEIGHT); for (int i = 0; i < HEIGHT; i++) output_row_pointers[i] = &output_buffer[i*WIDTH*4]; write_png_file("output.png", output_row_pointers); free(output_row_pointers); free(output_buffer); free(received_fragments); return 0; }