S exc(C c){ static S psb; //string buffer static S pcbb; //codeblock buffer ST st=top(rst);O o;I d; //current stack,temp var for various computations,another temp var if(init){memset(v,0,sizeof(v));init=0;} if(pl&&!ps&&!pcb&&!pc){C b[2]={c,0};pl=0;psh(st,newocb(b,1));} else if(v[c]&&(isalpha(c)?1:!icb)&&!pv&&!ps&&!pc&&!pcb){ //if variable && not defining variable && not parsing string/char o=v[c];if(o->t==TCB)excb(o); //if variable is code block and not in code block, run codeblock else psh(st,dup(o)); //push variable contents } //push/run variable if defined else if(pcb&&c&&!ps&&!pc){ if(c=='{')cbi++;else if(c=='}')cbi--; //create indents if new block is made if(cbi<=0){pcbb[pcb-1]=0;psh(st,newocbk(pcbb,pcb-1));pcb=0;} //finish block if indent is 0 else{pcbb=rlc(pcbb,pcb+1);pcbb[pcb-1]=c;++pcb;} //create code block } else if(pc&&!ps){if(c=='\\'&&!pe)pe=1;else{psh(st,newosc(pe?pec(c):c));pc=pe=0;}} else if(ps&&c) if(c=='\''&&!pe){exc('"');exc('"');}else{ //string restarting if(c=='"'&&!pe){psb[ps-1]=0;psh(st,newosk(psb,ps-1));ps=0;}else if(c=='\\'&&!pe)pe=1;else{psb=rlc(psb,ps+1);psb[ps-1]=pe?pec(c):c;++ps;pe=0;}} //string parsing else if(pm&&c){ //math pm=0;switch(c){ #define MO(c,f) case c:math(f,st);BK; MO('q',sqrt)MO('[',floor)MO(']',ceil)MO('s',sin)MO('S',asin)MO('c',cos)MO('C',acos)MO('t',tan)MO('T',atan) #undef MO #define MO(c,f) case c:f(st);BK; MO('d',mdst)MO('r',mrng) #undef MO #define MO(c,v) case c:psh(st,newod(v));BK; MO('p',M_PI)MO('e',exp(1.0))MO('l',299792458) #undef MO default:PE; }} //math
void Compression::RLC() { std::fstream rlc(path + std::string(".rlc"),std::ios::out); for(int x = 0;x < raw_width;x += block_size){ for(int y = 0;y < raw_height;y += block_size){ unsigned int zero_count = 0; for(int u = 0;u < block_size;++u){ for(int v = 0;v < block_size;++v){ if(dct[x + zigzag_x[u][v]][y + zigzag_y[u][v]]){ rlc << "(" << zero_count << "," << dct[x + zigzag_x[u][v]][y + zigzag_y[u][v]] << ")"; zero_count = 0; } else{ ++zero_count; } } } if(zero_count){ rlc << "EOB"; rlc << std::endl; } else{ rlc << std::endl; } } } rlc.close(); }
S tos(O o){ S r,t;L z,i;switch(o->t){ case TD:r=alc(BZ)/*hope this is big enough!*/;sprintf(r,"%f",o->d);z=strlen(r)-1;while(r[z]=='0')r[z--]=0;if(r[z]=='.')r[z]=0;BK; case TS:r=alc(o->s.z+1);memcpy(r,o->s.s,o->s.z);r[o->s.z]=0;BK; case TA:r=alc(1);r[0]='[';z=1;for(i=0;i<len(o->a);++i){L l;if(i){r=rlc(r,z+1);r[z++]=',';}t=tos(o->a->st[i]);l=strlen(t);r=rlc(r,z+l);memcpy(r+z,t,l);z+=l;DL(t);}r=rlc(r,z+2);r[z]=']';r[z+1]=0;BK; case TCB:r=alc(o->s.z+3);r[0]='{';memcpy(r+1,o->s.s,o->s.z);memcpy(r+1+o->s.z,"}",2);BK; }R r; } //tostring (copies)
void test_rlc(void) { for(;;) { r.A = rand()%256; printf("A = (%hhu) %s, Tc = %hhu\n", r.A, itoab8(r.A), F_CARRY(r.F) ); rlc(); printf("rlc A\n"); printf("A = (%hhu) %s, Tc = %hhu\n", r.A, itoab8(r.A), F_CARRY(r.F) ); printf("\n=============================\n"); } }
void Compression::AriVLD() { ac_decoder acd; ac_model acm; ac_decoder_init (&acd,(path + std::string(".ari")).c_str()); ac_model_init (&acm, NSYM1, NULL, ADAPT); std::fstream rlc(path + std::string(".rlc"),std::ios::out); while(loop_num){ char c = (char)ac_decode_symbol (&acd,&acm); if(c == 'B'){ rlc << c; rlc << std::endl; } else{ rlc << c; } --loop_num; } ac_decoder_done (&acd); ac_model_done (&acm); }
void ReadFileTGA(CByteImage& img, const char* filename) { // Open the file and read the header FILE *stream = fopen(filename, "rb"); if (stream == 0) throw CError("ReadFileTGA: could not open %s", filename); CTargaHead h; if (fread(&h, sizeof(CTargaHead), 1, stream) != 1) throw CError("ReadFileTGA(%s): file is too short", filename); // Throw away the image descriptor if (h.idLength > 0) { char* tmp = new char[h.idLength]; int nread = fread(tmp, sizeof(uchar), h.idLength, stream); delete tmp; // throw away this data if (nread != h.idLength) throw CError("ReadFileTGA(%s): file is too short", filename); } bool isRun = (h.imageType & 8) != 0; bool reverseRows = (h.descriptor & TargaScreenOrigin) != 0; int fileBytes = (h.pixelSize + 7) / 8; // Read the colormap uchar colormap[TargaCMapSize][TargaCMapBands]; int cMapSize = 0; bool grayRamp = false; if (h.colorMapType == 1) { cMapSize = (h.cMapLength[1] << 8) + h.cMapLength[0]; if (h.cMapBits != 24) throw CError("ReadFileTGA(%s): only 24-bit colormap currently supported", filename); int l = fileBytes * cMapSize; if (l > TargaCMapSize * TargaCMapBands) throw CError("ReadFileTGA(%s): colormap is too large", filename); if (fread(colormap, sizeof(uchar), l, stream) != l) throw CError("ReadFileTGA(%s): could not read the colormap", filename); // Check if it's just a standard gray ramp int i; for (i = 0; i < cMapSize; i++) { for (int j = 0; j < TargaCMapBands; j++) if (colormap[i][j] != i) break; } grayRamp = (i == cMapSize); // didn't break out too soon } bool isGray = h.imageType == TargaRawBW || h.imageType == TargaRunBW || grayRamp && (h.imageType == TargaRawColormap || h.imageType == TargaRunColormap); bool isRaw = h.imageType == TargaRawBW || h.imageType == TargaRawRGB || h.imageType == TargaRawRGB && isGray; // Determine the image shape CShape sh(h.width, h.height, (isGray) ? 1 : 4); // Allocate the image if necessary img.ReAllocate(sh, false); // Construct a run-length code reader CTargaRLC rlc(! isRaw); // Read in the rows for (int y = 0; y < sh.height; y++) { int yr = reverseRows ? sh.height-1-y : y; uchar* ptr = (uchar *) img.PixelAddress(0, yr, 0); if (fileBytes == sh.nBands && isRaw) { // Special case for raw image, same as destination int n = sh.width*sh.nBands; if (fread(ptr, sizeof(uchar), n, stream) != n) throw CError("ReadFileTGA(%s): file is too short", filename); } else { // Read one pixel at a time for (int x = 0; x < sh.width; x++, ptr += sh.nBands) { uchar* buf = rlc.getBytes(fileBytes, stream); if (fileBytes == 1 && sh.nBands == 1) { ptr[0] = buf[0]; } else if (fileBytes == 1 && sh.nBands == 4) { for (int i = 0; i < 3; i++) ptr[i] = (isGray) ? buf[0] : colormap[buf[0]][i]; ptr[3] = 255; // full alpha; } else if ((fileBytes == 3 || fileBytes == 4) && sh.nBands == 4) { int i; for (i = 0; i < fileBytes; i++) ptr[i] = buf[i]; if (i == 3) // missing alpha channel ptr[3] = 255; // full alpha; } else throw CError("ReadFileTGA(%s): unhandled pixel depth or # of bands", filename); } } } if (fclose(stream)) throw CError("ReadFileTGA(%s): error closing file", filename); }
O mods(O a,O b){ L z;S s;C d[BZ];Reprog*p;Resub rs[10];O r,os=pop(top(rst));if(os->t!=TS)TE;s=os->s.s;p=regcomp(a->s.s);if(!p)ex("bad regex");memset(rs,0,sizeof(rs)); for(r=newos("",0);s<os->s.s+os->s.z&®exec(p,s,rs,10);s=rs[0].e.ep,memset(rs,0,sizeof(rs))){if(rs[0].s.sp>s){z=rs[0].s.sp-s;r->s.s=rlc(r->s.s,r->s.z+z);memcpy(r->s.s+r->s.z,s,z);r->s.z+=z;}if(b->s.z==0)continue;regsub(b->s.s,d,BZ,rs,sizeof(rs));z=strlen(d);r->s.s=rlc(r->s.s,r->s.z+z);memcpy(r->s.s+r->s.z,d,z);r->s.z+=z;} if(s<os->s.s+os->s.z){z=os->s.s+os->s.z-s;r->s.s=rlc(r->s.s,r->s.z+z);memcpy(r->s.s+r->s.z,s,z);r->s.z+=z;}r->s.s=rlc(r->s.s,r->s.z+1);r->s.s[r->s.z]=0;dlo(os);DL(p);R r; }