static int lp_type (lua_State *L) { if (testpattern(L, 1)) lua_pushliteral(L, "pattern"); else lua_pushnil(L); return 1; }
/* ** traverse grammar at index 'arg', pushing all its keys and patterns ** into the stack. Create a new table (before all pairs key-pattern) to ** collect all keys and their associated positions in the final tree ** (the "position table"). ** Return the number of rules and (in 'totalsize') the total size ** for the new tree. */ static int collectrules (lua_State *L, int arg, int *totalsize) { int n = 1; /* to count number of rules */ int postab = lua_gettop(L) + 1; /* index of position table */ int size; /* accumulator for total size */ lua_newtable(L); /* create position table */ getfirstrule(L, arg, postab); size = 2 + getsize(L, postab + 2); /* TGrammar + TRule + rule */ lua_pushnil(L); /* prepare to traverse grammar table */ while (lua_next(L, arg) != 0) { if (lua_tonumber(L, -2) == 1 || lua_equal(L, -2, postab + 1)) { /* initial rule? */ lua_pop(L, 1); /* remove value (keep key for lua_next) */ continue; } if (!testpattern(L, -1)) /* value is not a pattern? */ luaL_error(L, "rule '%s' is not a pattern", val2str(L, -2)); luaL_checkstack(L, LUA_MINSTACK, "grammar has too many rules"); lua_pushvalue(L, -2); /* push key (to insert into position table) */ lua_pushinteger(L, size); lua_settable(L, postab); size += 1 + getsize(L, -1); /* update size */ lua_pushvalue(L, -2); /* push key (for next lua_next) */ n++; } *totalsize = size + 1; /* TTrue to finish list of rules */ return n; }
const unsigned char *V4L2Camera::getFrame() { const unsigned char *outbuf; const unsigned char *inbuf; unsigned char *tmpbuf = NULL; if (!isOK()) { failed: failed_ = true; return testpattern(); } if (use_mmap_) { struct v4l2_buffer buffer = {}; buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buffer.memory = V4L2_MEMORY_MMAP; if (mmap_prev_frame_ != ~0) { buffer.index = mmap_prev_frame_; if (ioctl(fd_, VIDIOC_QBUF, &buffer) == -1) { perror("QBUF failed"); goto failed; } } if (ioctl(fd_, VIDIOC_DQBUF, &buffer) == -1) { perror("DQBUF failed"); goto failed; } if (0) printf("got index %d frame %u\n", buffer.index, buffer.sequence); inbuf = frameptrs_[buffer.index]; mmap_prev_frame_ = buffer.index; } else { tmpbuf = new unsigned char[frame_size_]; read(fd_, tmpbuf, frame_size_); inbuf = tmpbuf; } if (needsconv(pixfmt_)) { if (retbuf_ == NULL) retbuf_ = new unsigned char[frame_size_]; outbuf = retbuf_; convert(pixfmt_, frame_size_, inbuf, retbuf_); } else { outbuf = inbuf; } if (tmpbuf) delete[] tmpbuf; return outbuf; }
const unsigned char *V4LCamera::getFrame() { unsigned char *ret; if (!isOK()) return testpattern(); if (use_mmap_) { if (0) printf("VIDIOCSYNC frame %u\n", mmap_nextframe_); if (ioctl(fd_, VIDIOCSYNC, &mmap_nextframe_) == -1) { failed_ = true; perror("VIDIOSYNC failed"); } ret = frameptrs_[mmap_nextframe_]; mmap_nextframe_ = (mmap_nextframe_ + 1) % mmap_frames_; struct video_mmap vidmmap; vidmmap.frame = mmap_nextframe_; vidmmap.height = imageHeight(); vidmmap.width = imageWidth(); vidmmap.format = VIDEO_PALETTE_YUV420P; if (0) printf("vidmmap.frame=%u height=%d width=%d format=%u\n", vidmmap.frame, vidmmap.height, vidmmap.width, vidmmap.format); if (ioctl(fd_, VIDIOCMCAPTURE, &vidmmap) == -1) { perror("VIDIOCMCAPTURE failed - reverting to read"); use_mmap_ = false; buf_ = new unsigned char[imageSize()]; } } else { int r = read(fd_, buf_, imageSize()); if (r != imageSize()) failed_ = true; ret = buf_; } if (recfd_ != -1) writeFrame(ret); return ret; }
/* ** push on the stack the index and the pattern for the ** initial rule of grammar at index 'arg' in the stack; ** also add that index into position table. */ static void getfirstrule (lua_State *L, int arg, int postab) { lua_rawgeti(L, arg, 1); /* access first element */ if (lua_isstring(L, -1)) { /* is it the name of initial rule? */ lua_pushvalue(L, -1); /* duplicate it to use as key */ lua_gettable(L, arg); /* get associated rule */ } else { lua_pushinteger(L, 1); /* key for initial rule */ lua_insert(L, -2); /* put it before rule */ } if (!testpattern(L, -1)) { /* initial rule not a pattern? */ if (lua_isnil(L, -1)) luaL_error(L, "grammar has no initial rule"); else luaL_error(L, "initial rule '%s' is not a pattern", lua_tostring(L, -2)); } lua_pushvalue(L, -2); /* push key */ lua_pushinteger(L, 1); /* push rule position (after TGrammar) */ lua_settable(L, postab); /* insert pair at position table */ }
// demo shows a timed demonstration void demo(int w, int h, int sec) { refcard(w, h); sleep(sec); rshapes(w, h, 50); sleep(sec); testpattern(w, h, "OpenVG on RasPi"); sleep(sec); imagetable(w, h); sleep(sec); rotext(w, h, 30, "Raspi"); sleep(sec); tb(w, h); sleep(sec); fontrange(w, h); sleep(sec); sunearth(w, h); sleep(sec); raspi(w, h, "The Raspberry Pi"); sleep(sec); gradient(w,h); sleep(sec); advert(w, h); }
// main initializes the system and shows the picture. // Exit and clean up when you hit [RETURN]. int main(int argc, char **argv) { int w, h, n; char *usage = "%s [command]\n\tdemo sec\n\tastro\n\ttest ...\n\trand n\n\trotate n ...\n\timage\n\ttext\n\tfontsize\n\traspi\n\tadvert\n\tgradient\n"; char *progname = argv[0]; saveterm(); init(&w, &h); rawterm(); switch (argc) { case 2: if (strncmp(argv[1], "image", 5) == 0) { imagetable(w, h); } else if (strncmp(argv[1], "text", 4) == 0) { tb(w, h); } else if (strncmp(argv[1], "astro", 5) == 0) { sunearth(w, h); } else if (strncmp(argv[1], "fontsize", 8) == 0) { fontrange(w, h); } else if (strncmp(argv[1], "advert", 6) == 0) { advert(w,h); } else if (strncmp(argv[1], "raspi", 5) == 0) { raspi(w, h, "The Raspberry Pi"); } else if (strncmp(argv[1], "gradient", 8) == 0) { gradient(w,h); } else { restoreterm(); fprintf(stderr, usage, progname); return 1; } break; case 3: n = atoi(argv[2]); if (strncmp(argv[1], "demo", 4) == 0) { if (n < 1 || n > 30) { n = 5; } demo(w, h, n); } else if (strncmp(argv[1], "rand", 4) == 0) { if (n < 1 || n > 1000) { n = 100; } rshapes(w, h, n); } else if (strncmp(argv[1], "test", 4) == 0) { testpattern(w, h, argv[2]); } else { restoreterm(); fprintf(stderr, usage, progname); return 1; } break; case 4: if (strncmp(argv[1], "rotate", 6) == 0) { rotext(w, h, atoi(argv[2]), argv[3]); } else { restoreterm(); fprintf(stderr, usage, progname); return 1; } break; default: refcard(w, h); } waituntil(0x1b); restoreterm(); finish(); return 0; }