int main(int argc, char * const argv[]) { // initialize int c; int frame_size = C64_FRAME_SIZE; int output_port = 99999; FILE* fp = stdin; int ip[4] = {127, 0, 0, 1}; while ((c = getopt(argc, argv, "t:p:i:a:")) != -1) { if (c == 't') { if (strcmp(optarg, "c64") == 0) { frame_size = C64_FRAME_SIZE; } else if (strcmp(optarg, "pet") == 0) { frame_size = PET_FRAME_SIZE; } else if (strcmp(optarg, "pet40") == 0) { frame_size = PET_40_FRAME_SIZE; } } else if (c == 'p') { output_port = atoi(optarg); } else if (c == 'i') { fp = fopen(optarg, "rb"); } else if (c == 'a') // ip address { sscanf(optarg, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); } } Frame frame(frame_size); Tools::Timer timer; NetPort port(ip[0],ip[1],ip[2],ip[3],5555,output_port); NetPort timeListenerPort(127,0,0,1,5556,output_port); unsigned char temp[2000]; float start_pts = -1; double external_offset = 0.0; while (frame.read(fp)) { fprintf(stderr, "pts: %f\n", frame.pts()); if (start_pts < 0.0) { start_pts = frame.pts(); timer.start(); } float rel_pts = frame.pts() - start_pts; // get current time double currentTime = timer.getTime() + external_offset; fprintf(stderr, "current time: %lf timer %lf offset %lf\n", currentTime, timer.getTime(), external_offset); while (currentTime < rel_pts) { float sleepTimeUs = (rel_pts - currentTime) * 1000000.0; // check for timer update uint32_t nTimeUs = 0; int ret = timeListenerPort.recv((unsigned char*)&nTimeUs, sizeof(uint32_t)); if (ret == sizeof(uint32_t)) { uint32_t timeUs = ntohl(nTimeUs); printf("got time update: %d at time %lf\n", timeUs, timer.getTime()); double ext_time = (double)timeUs / 1000000.0; external_offset = ext_time - timer.getTime(); } if (sleepTimeUs > 1.0) { usleep(sleepTimeUs); } currentTime = timer.getTime() + external_offset; } fprintf(stderr, "sending frame at time %lf relpts %f error %f\n", currentTime, rel_pts, currentTime-rel_pts); // write data to output //fwrite(frame.data, 1, C64_FRAME_SIZE, fp_out); //memcpy(temp, frame.data(), frame.dataSize()); int ret = port.send(frame.data(), frame.dataSize()); //int ret = port.send(temp, 2000); //printf("send returned %d op %d %d %d %d %d\n", ret, ip[0], ip[1], ip[2], ip[3], output_port); } if (fp != stdin) { fclose(fp); } return 0; }
void convertImageFromGray(unsigned char* gray, int width, int height, int dim, float time, FILE* fp_out, bool output_image, bool output_pts, int searchRange) { char bmpFname[100]; Image outputImage(width, height); Tools::Timer timer; double dctTime = 0.0; double matchTime = 0.0; double convTime = 0.0; int matching; unsigned char glyphIndex; fprintf(stderr, "converting rgb frame at time %f\n", time); sprintf(bmpFname, "image_%0.4f.ppm", time); if (output_pts) { // write pts to output stream fwrite(&time, sizeof(time), 1, fp_out); fprintf(stderr, "output pts!\n"); } for (int y = 0; y < height; y += dim) { for (int x = 0; x < width; x += dim) { // copy values into input buffer for (int yy = 0; yy < dim; yy ++) { for (int xx = 0; xx < dim; xx++) { dctInput[xx][yy] = gray[(y+yy)*width + (x+xx)]; } } timer.start(); #ifdef USE_1D_DCT dct1WithInput(dctInput, dctOutput, cos1DLookup, dim); #else dctWithInput(dctInput, dctOutput, cosLookup, dim); #endif dctTime += timer.getTime(); timer.start(); matching = getMatchingGlyph(dctOutput, searchRange); matchTime += timer.getTime(); glyphIndex = (unsigned char)matching; fwrite(&glyphIndex, 1, 1, fp_out); if (output_image) { // write to png unsigned char *glyphString; int glyphPix = dim*dim; if (matching < 128) { glyphString = &glyphs[matching * glyphPix]; } else { glyphString = &glyphs[(matching - 128)*glyphPix]; } int ind; ind = 0; for (int yy = 0; yy < dim; yy ++) { for (int xx = 0; xx < dim; xx++) { Pixel* pixel = outputImage.pixelAt(x+xx, y+yy); if ((glyphString[ind] == '0' && matching < 128) || (glyphString[ind] == '1' && matching >= 128)) { pixel->rgb[0] = 0; pixel->rgb[1] = 0; pixel->rgb[2] = 0; } else { pixel->rgb[0] = 1.0; pixel->rgb[1] = 1.0; pixel->rgb[2] = 1.0; } ind++; } } } } } if (output_image) { outputImage.writePPM(bmpFname); } fprintf(stderr, "dct time %lf match time %lf conversion time %lf\n", dctTime, matchTime, convTime); }
int main(int argc, const char * argv[]) { // insert code here... //std::cout << "Hello, World!\n"; Palette c64palette(c64_colors, num_64_colors); Ditherer* ditherer = Ditherer::createC64Ditherer(); const char* dirname = argv[1]; const char* outdirname = argv[2]; const char* out64fname = argv[3]; int index = 0; char thisfname[256]; char nextfname[256]; char outfname[256]; unsigned char* c64frame = NULL; FILE* fp = fopen(out64fname, "wb"); NetPort port(192,168,1,25,99998,99999); Tools::Timer frameTimer; frameTimer.start(); for (index = 1; index < 9999; index++) { // check if file can be opened sprintf(thisfname, "%s/%04d.ppm", dirname, index); sprintf(nextfname, "%s/%04d.ppm", dirname, index+1); wait_for_file(nextfname); printf("%s\n", thisfname); // open image Image inputImage(thisfname); int imWidth = inputImage.getWidth(); int imHeight = inputImage.getHeight(); Image halfImage(inputImage, imWidth/2, imHeight); Image* dithered = ditherer->createDitheredImageFromImageWithPalette(halfImage, c64palette); C64Image* c64im = (C64Image*)dithered; /* // test - output to new ppm sprintf(outfname, "%s/%04d.ppm", outdirname, index); Image fullImage(*dithered, imWidth, imHeight); fullImage.writePPM(outfname); */ int c64FrameSize = c64im->getC64FrameSize(); if (!c64frame) { c64frame = (unsigned char*)malloc(sizeof(unsigned char) * c64FrameSize); } float time = (float)index / 8.0; c64im->getC64Frame(c64frame, time); fwrite(c64frame, 1, c64FrameSize, fp); float thisTime = frameTimer.getTime(); printf("pts %f, time %f\n", time, thisTime); while (thisTime < time) { float diff = time - thisTime; float usec = diff * 1000000.0; usleep(usec); thisTime = frameTimer.getTime(); } // send bytes to port port.send(&c64frame[4], 9216); delete dithered; } fclose(fp); return 0; }