void AudioServer::sample_set_signed_data(RID p_sample, const DVector<float>& p_buffer) { SampleFormat format = sample_get_format(p_sample); ERR_EXPLAIN("IMA ADPCM is not supported."); ERR_FAIL_COND(format==SAMPLE_FORMAT_IMA_ADPCM); int len = p_buffer.size(); ERR_FAIL_COND( len == 0 ); DVector<uint8_t> data; DVector<uint8_t>::Write w; DVector<float>::Read r = p_buffer.read(); switch(format) { case SAMPLE_FORMAT_PCM8: { data.resize(len); w=data.write(); int8_t *samples8 = (int8_t*)w.ptr(); for(int i=0;i<len;i++) { float sample = Math::floor( r[i] * (1<<8) ); if (sample<-128) sample=-128; else if (sample>127) sample=127; samples8[i]=sample; } } break; case SAMPLE_FORMAT_PCM16: { data.resize(len*2); w=data.write(); int16_t *samples16 = (int16_t*)w.ptr(); for(int i=0;i<len;i++) { float sample = Math::floor( r[i] * (1<<16) ); if (sample<-32768) sample=-32768; else if (sample>32767) sample=32767; samples16[i]=sample; } } break; } w = DVector<uint8_t>::Write(); sample_set_data(p_sample,data); }
Dictionary PolygonPathFinder::_get_data() const{ Dictionary d; DVector<Vector2> p; DVector<int> ind; Array connections; p.resize(points.size()-2); connections.resize(points.size()-2); ind.resize(edges.size()*2); DVector<float> penalties; penalties.resize(points.size()-2); { DVector<Vector2>::Write wp=p.write(); DVector<float>::Write pw=penalties.write(); for(int i=0;i<points.size()-2;i++) { wp[i]=points[i].pos; pw[i]=points[i].penalty; DVector<int> c; c.resize(points[i].connections.size()); { DVector<int>::Write cw=c.write(); int idx=0; for (Set<int>::Element *E=points[i].connections.front();E;E=E->next()) { cw[idx++]=E->get(); } } connections[i]=c; } } { DVector<int>::Write iw=ind.write(); int idx=0; for (Set<Edge>::Element *E=edges.front();E;E=E->next()) { iw[idx++]=E->get().points[0]; iw[idx++]=E->get().points[1]; } } d["bounds"]=bounds; d["points"]=p; d["penalties"]=penalties; d["connections"]=connections; d["segments"]=ind; return d; }
Variant::Variant(const DVector<Face3>& p_face_array) { DVector<Vector3> vertices; int face_count=p_face_array.size(); vertices.resize(face_count*3); if (face_count) { DVector<Face3>::Read r = p_face_array.read(); DVector<Vector3>::Write w = vertices.write(); for(int i=0;i<face_count;i++) { for(int j=0;j<3;j++) w[i*3+j]=r[i].vertex[j]; } r=DVector<Face3>::Read(); w=DVector<Vector3>::Write(); } type = NIL; *this = vertices; }
DVector<int> TileMap::_get_tile_data() const { DVector<int> data; data.resize(tile_map.size()*2); DVector<int>::Write w = data.write(); int idx=0; for(const Map<PosKey,Cell>::Element *E=tile_map.front();E;E=E->next()) { uint8_t *ptr = (uint8_t*)&w[idx]; encode_uint16(E->key().x,&ptr[0]); encode_uint16(E->key().y,&ptr[2]); uint32_t val = E->get().id; if (E->get().flip_h) val|=(1<<29); if (E->get().flip_v) val|=(1<<30); encode_uint32(val,&ptr[4]); idx+=2; } w = DVector<int>::Write(); return data; }
static void _decompress_etc(Image *p_img) { ERR_FAIL_COND(p_img->get_format() != Image::FORMAT_ETC); int imgw = p_img->get_width(); int imgh = p_img->get_height(); DVector<uint8_t> src = p_img->get_data(); DVector<uint8_t> dst; DVector<uint8_t>::Read r = src.read(); int mmc = p_img->get_mipmaps(); for (int i = 0; i <= mmc; i++) { dst.resize(dst.size() + imgw * imgh * 3); const uint8_t *srcbr = &r[p_img->get_mipmap_offset(i)]; DVector<uint8_t>::Write w = dst.write(); uint8_t *wptr = &w[dst.size() - imgw * imgh * 3]; int bw = MAX(imgw / 4, 1); int bh = MAX(imgh / 4, 1); for (int y = 0; y < bh; y++) { for (int x = 0; x < bw; x++) { uint8_t block[4 * 4 * 4]; rg_etc1::unpack_etc1_block(srcbr, (unsigned int *)block); srcbr += 8; int maxx = MIN(imgw, 4); int maxy = MIN(imgh, 4); for (int yy = 0; yy < maxy; yy++) { for (int xx = 0; xx < maxx; xx++) { uint32_t src_ofs = (yy * 4 + xx) * 4; uint32_t dst_ofs = ((y * 4 + yy) * imgw + x * 4 + xx) * 3; wptr[dst_ofs + 0] = block[src_ofs + 0]; wptr[dst_ofs + 1] = block[src_ofs + 1]; wptr[dst_ofs + 2] = block[src_ofs + 2]; } } } } imgw = MAX(1, imgw / 2); imgh = MAX(1, imgh / 2); } r = DVector<uint8_t>::Read(); //print_line("Re Creating ETC into regular image: w "+itos(p_img->get_width())+" h "+itos(p_img->get_height())+" mm "+itos(p_img->get_mipmaps())); *p_img = Image(p_img->get_width(), p_img->get_height(), p_img->get_mipmaps(), Image::FORMAT_RGB, dst); if (p_img->get_mipmaps()) p_img->generate_mipmaps(-1, true); }
String _Marshalls::variant_to_base64(const Variant& p_var) { int len; Error err = encode_variant(p_var,NULL,len); ERR_FAIL_COND_V( err != OK, "" ); DVector<uint8_t> buff; buff.resize(len); DVector<uint8_t>::Write w = buff.write(); err = encode_variant(p_var,&w[0],len); ERR_FAIL_COND_V( err != OK, "" ); int b64len = len / 3 * 4 + 4 + 1; DVector<uint8_t> b64buff; b64buff.resize(b64len); DVector<uint8_t>::Write w64 = b64buff.write(); int strlen = base64_encode((char*)(&w64[0]), (char*)(&w[0]), len); //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen); w64[strlen] = 0; String ret = (char*)&w64[0]; return ret; };
Array StreamPeer::_get_partial_data(int p_bytes) { Array ret; DVector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(DVector<uint8_t>()); return ret; } DVector<uint8_t>::Write w = data.write(); int received; Error err = get_partial_data(&w[0], p_bytes, received); w = DVector<uint8_t>::Write(); if (err != OK) { data.resize(0); } else if (received != data.size()) { data.resize(received); } ret.push_back(err); ret.push_back(data); return ret; }
void Particles2DEditorPlugin::_file_selected(const String& p_file) { print_line("file: "+p_file); int epc=epoints->get_val(); Image img; Error err = ImageLoader::load_image(p_file,&img); ERR_EXPLAIN("Error loading image: "+p_file); ERR_FAIL_COND(err!=OK); img.convert(Image::FORMAT_GRAYSCALE_ALPHA); ERR_FAIL_COND(img.get_format()!=Image::FORMAT_GRAYSCALE_ALPHA); Size2i s = Size2(img.get_width(),img.get_height()); ERR_FAIL_COND(s.width==0 || s.height==0); DVector<uint8_t> data = img.get_data(); DVector<uint8_t>::Read r = data.read(); Vector<Point2i> valid_positions; valid_positions.resize(s.width*s.height); int vpc=0; for(int i=0;i<s.width*s.height;i++) { uint8_t a = r[i*2+1]; if (a>128) { valid_positions[vpc++]=Point2i(i%s.width,i/s.width); } } valid_positions.resize(vpc); ERR_EXPLAIN("No pixels with transparency > 128 in image.."); ERR_FAIL_COND(valid_positions.size()==0); DVector<Point2> epoints; epoints.resize(epc); DVector<Point2>::Write w = epoints.write(); Size2 extents = Size2(img.get_width()*0.5,img.get_height()*0.5); for(int i=0;i<epc;i++) { Point2 p = valid_positions[Math::rand()%vpc]; p-=s/2; w[i]=p/extents; } w = DVector<Point2>::Write(); undo_redo->create_action("Set Emission Mask"); undo_redo->add_do_method(particles,"set_emission_points",epoints); undo_redo->add_do_method(particles,"set_emission_half_extents",extents); undo_redo->add_undo_method(particles,"set_emission_points",particles->get_emission_points()); undo_redo->add_undo_method(particles,"set_emission_half_extents",particles->get_emission_half_extents()); undo_redo->commit_action(); }
int VideoStreamTheoraplayer::get_pending_frame_count() const { if (!clip) return 0; if (!frame.empty()) return 1; TheoraVideoFrame* f = clip->getNextFrame(); if (!f) return 0; float w=clip->getWidth(),h=clip->getHeight(); int imgsize = w * h * f->mBpp; int size = f->getStride() * f->getHeight() * f->mBpp; DVector<uint8_t> data; data.resize(imgsize); DVector<uint8_t>::Write wr = data.write(); uint8_t* ptr = wr.ptr(); copymem(ptr, f->getBuffer(), imgsize); /* for (int i=0; i<h; i++) { int dstofs = i * w * f->mBpp; int srcofs = i * f->getStride() * f->mBpp; copymem(ptr + dstofs, f->getBuffer() + dstofs, w * f->mBpp); }; */ frame = Image(); frame.create(w, h, 0, f->mBpp == 3 ? Image::FORMAT_RGB : Image::FORMAT_RGBA, data); clip->popFrame(); return 1; };
RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String& p_original_path, Error *r_error) { if (r_error) *r_error=ERR_FILE_CANT_OPEN; FileAccess *f = FileAccess::open(p_path,FileAccess::READ); ERR_FAIL_COND_V(!f,RES()); DVector<uint8_t> data; data.resize(f->get_len()); ERR_FAIL_COND_V(data.size()==0,RES()); { DVector<uint8_t>::Write w = data.write(); f->get_buffer(w.ptr(),data.size()); } Ref<DynamicFontData> dfd; dfd.instance(); dfd->set_font_data(data); if (r_error) *r_error=OK; return dfd; }
void AudioServer::sample_set_signed_data(RID p_sample, const DVector<float>& p_buffer) { int len = p_buffer.size(); ERR_FAIL_COND( len == 0 ); DVector<uint8_t> data; data.resize(len*2); DVector<uint8_t>::Write w=data.write(); int16_t *samples = (int16_t*)w.ptr(); DVector<float>::Read r = p_buffer.read(); for(int i=0;i<len;i++) { float sample = r[i]; sample = Math::floor( sample * (1<<16) ); if (sample<-32768) sample=-32768; else if (sample>32767) sample=32767; samples[i]=sample; } w = DVector<uint8_t>::Write(); sample_set_data(p_sample,data); }
Error jpeg_load_image_from_buffer(Image *p_image,const uint8_t* p_buffer, int p_buffer_len) { jpgd::jpeg_decoder_mem_stream mem_stream(p_buffer,p_buffer_len); jpgd::jpeg_decoder decoder(&mem_stream); if (decoder.get_error_code() != jpgd::JPGD_SUCCESS) { return ERR_CANT_OPEN; } const int image_width = decoder.get_width(); const int image_height = decoder.get_height(); int comps = decoder.get_num_components(); if (comps==3) comps=4; //weird if (decoder.begin_decoding() != jpgd::JPGD_SUCCESS) return ERR_FILE_CORRUPT; const int dst_bpl = image_width * comps; DVector<uint8_t> data; data.resize(dst_bpl * image_height); DVector<uint8_t>::Write dw = data.write(); jpgd::uint8 *pImage_data = (jpgd::uint8*)dw.ptr(); for (int y = 0; y < image_height; y++) { const jpgd::uint8* pScan_line; jpgd::uint scan_line_len; if (decoder.decode((const void**)&pScan_line, &scan_line_len) != jpgd::JPGD_SUCCESS) { return ERR_FILE_CORRUPT; } jpgd::uint8 *pDst = pImage_data + y * dst_bpl; memcpy(pDst, pScan_line, dst_bpl); } //all good Image::Format fmt; if (comps==1) fmt=Image::FORMAT_GRAYSCALE; else fmt=Image::FORMAT_RGBA; dw = DVector<uint8_t>::Write(); p_image->create(image_width,image_height,0,fmt,data); return OK; }
void EditorExportPlatformWindows::store_16(DVector<uint8_t>& vector, uint16_t value) { const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value); int size = vector.size(); vector.resize( size + 2 ); DVector<uint8_t>::Write w = vector.write(); w[size]=bytes[0]; w[size+1]=bytes[1]; }
void image_compress_squish(Image *p_image) { int w=p_image->get_width(); int h=p_image->get_height(); if (p_image->get_mipmaps() == 0) { ERR_FAIL_COND( !w || w % 4 != 0); ERR_FAIL_COND( !h || h % 4 != 0); } else { ERR_FAIL_COND( !w || w !=nearest_power_of_2(w) ); ERR_FAIL_COND( !h || h !=nearest_power_of_2(h) ); }; if (p_image->get_format()>=Image::FORMAT_BC1) return; //do not compress, already compressed Image::AlphaMode alpha = p_image->detect_alpha(); Image::Format target_format; int shift=0; int squish_comp=squish::kColourRangeFit; switch(alpha) { case Image::ALPHA_NONE: target_format = Image::FORMAT_BC1; shift=1; squish_comp|=squish::kDxt1; break; case Image::ALPHA_BIT: target_format = Image::FORMAT_BC2; squish_comp|=squish::kDxt3; break; case Image::ALPHA_BLEND: target_format = Image::FORMAT_BC3; squish_comp|=squish::kDxt5; break; } p_image->convert(Image::FORMAT_RGBA); //always expects rgba int mm_count = p_image->get_mipmaps(); DVector<uint8_t> data; int target_size = Image::get_image_data_size(w,h,target_format,mm_count); data.resize(target_size); DVector<uint8_t>::Read rb = p_image->get_data().read(); DVector<uint8_t>::Write wb = data.write(); int dst_ofs=0; for(int i=0;i<=mm_count;i++) { int src_ofs = p_image->get_mipmap_offset(i); squish::CompressImage( &rb[src_ofs],w,h,&wb[dst_ofs],squish_comp); dst_ofs+=(MAX(4,w)*MAX(4,h))>>shift; w>>=1; h>>=1; } rb = DVector<uint8_t>::Read(); wb = DVector<uint8_t>::Write(); p_image->create(p_image->get_width(),p_image->get_height(),p_image->get_mipmaps(),target_format,data); }
void CollisionPolygon2D::_add_to_collision_object(Object *p_obj) { if (unparenting) return; CollisionObject2D *co = p_obj->cast_to<CollisionObject2D>(); ERR_FAIL_COND(!co); if (polygon.size()==0) return; bool solids=build_mode==BUILD_SOLIDS; if (solids) { //here comes the sun, lalalala //decompose concave into multiple convex polygons and add them Vector< Vector<Vector2> > decomp = Geometry::decompose_polygon(polygon); for(int i=0;i<decomp.size();i++) { Ref<ConvexPolygonShape2D> convex = memnew( ConvexPolygonShape2D ); convex->set_points(decomp[i]); co->add_shape(convex,get_transform()); if (trigger) co->set_shape_as_trigger(co->get_shape_count()-1,true); } } else { Ref<ConcavePolygonShape2D> concave = memnew( ConcavePolygonShape2D ); DVector<Vector2> segments; segments.resize(polygon.size()*2); DVector<Vector2>::Write w=segments.write(); for(int i=0;i<polygon.size();i++) { w[(i<<1)+0]=polygon[i]; w[(i<<1)+1]=polygon[(i+1)%polygon.size()]; } w=DVector<Vector2>::Write(); concave->set_segments(segments); co->add_shape(concave,get_transform()); if (trigger) co->set_shape_as_trigger(co->get_shape_count()-1,true); } //co->add_shape(shape,get_transform()); }
void Shape::add_vertices_to_array(DVector<Vector3> &array, const Transform& p_xform) { Vector<Vector3> toadd = _gen_debug_mesh_lines(); if (toadd.size()) { int base=array.size(); array.resize(base+toadd.size()); DVector<Vector3>::Write w = array.write(); for(int i=0;i<toadd.size();i++) { w[i+base]=p_xform.xform(toadd[i]); } } }
Variant::Variant(const Vector<Vector3>& p_array) { type=NIL; DVector<Vector3> v; int len=p_array.size(); if (len>0) { v.resize(len); DVector<Vector3>::Write w = v.write(); const Vector3 *r = p_array.ptr(); for (int i=0;i<len;i++) w[i]=r[i]; } *this=v; }
Variant::operator DVector<Plane>() const { Array va= operator Array(); DVector<Plane> planes; int va_size=va.size(); if (va_size==0) return planes; planes.resize(va_size); DVector<Plane>::Write w = planes.write(); for(int i=0;i<va_size;i++) w[i]=va[i]; return planes; }
static Ref<Font> make_font2(int p_height,int p_ascent, int p_charcount, const int *p_char_rects,int p_kerning_count,const int *p_kernings,int p_w, int p_h, const unsigned char *p_img) { Ref<Font> font( memnew( Font ) ); DVector<uint8_t> img; img.resize(p_w*p_h*2); { DVector<uint8_t>::Write w = img.write(); for(int i=0;i<(p_w*p_h*2);i++) { w[i]=p_img[i]; } } Image image(p_w,p_h,0,Image::FORMAT_GRAYSCALE_ALPHA,img); Ref<ImageTexture> tex = memnew( ImageTexture ); tex->create_from_image(image); font->add_texture( tex ); for (int i=0;i<p_charcount;i++) { const int *c = &p_char_rects[i*8]; int chr=c[0]; Rect2 frect; frect.pos.x=c[1]; frect.pos.y=c[2]; frect.size.x=c[3]; frect.size.y=c[4]; Point2 align( c[6], c[5]); int advance=c[7]; font->add_char( chr, 0, frect, align,advance ); } for(int i=0;i<p_kerning_count;i++) { font->add_kerning_pair(p_kernings[i*3+0],p_kernings[i*3+1],p_kernings[i*3+2]); } font->set_height( p_height ); font->set_ascent( p_ascent ); return font; }
Variant _Marshalls::base64_to_variant(const String& p_str) { int strlen = p_str.length(); CharString cstr = p_str.ascii(); DVector<uint8_t> buf; buf.resize(strlen / 4 * 3 + 1); DVector<uint8_t>::Write w = buf.write(); int len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); Variant v; Error err = decode_variant(v, &w[0], len); ERR_FAIL_COND_V( err!=OK, Variant() ); return v; };
Variant::operator DVector<Face3>() const { DVector<Vector3> va= operator DVector<Vector3>(); DVector<Face3> faces; int va_size=va.size(); if (va_size==0) return faces; faces.resize(va_size/3); DVector<Face3>::Write w = faces.write(); DVector<Vector3>::Read r = va.read(); for(int i=0;i<va_size;i++) w[i/3].vertex[i%3]=r[i]; return faces; }
Variant ConcavePolygonShape2DSW::get_data() const { DVector<Vector2> rsegments; int len = segments.size(); rsegments.resize(len*2); DVector<Vector2>::Write w = rsegments.write(); for(int i=0;i<len;i++) { w[(i<<1)+0]=points[segments[i].points[0]]; w[(i<<1)+1]=points[segments[i].points[1]]; } w=DVector<Vector2>::Write(); return rsegments; }
void _File::store_var(const Variant& p_var) { ERR_FAIL_COND(!f); int len; Error err = encode_variant(p_var,NULL,len); ERR_FAIL_COND( err != OK ); DVector<uint8_t> buff; buff.resize(len); DVector<uint8_t>::Write w = buff.write(); err = encode_variant(p_var,&w[0],len); ERR_FAIL_COND( err != OK ); w=DVector<uint8_t>::Write(); store_32(len); store_buffer(buff); }
RID VisualServer::get_white_texture() { if (white_texture.is_valid()) return white_texture; DVector<uint8_t> wt; wt.resize(16*3); { DVector<uint8_t>::Write w =wt.write(); for(int i=0;i<16*3;i++) w[i]=255; } Image white(4,4,0,Image::FORMAT_RGB,wt); white_texture=texture_create(); texture_allocate(white_texture,4,4,Image::FORMAT_RGB); texture_set_data(white_texture,white); return white_texture; }
Error PacketPeer::get_packet_buffer(DVector<uint8_t> &r_buffer) const { const uint8_t *buffer; int buffer_size; Error err = get_packet(&buffer,buffer_size); if (err) return err; r_buffer.resize(buffer_size); if (buffer_size==0) return OK; DVector<uint8_t>::Write w = r_buffer.write(); for(int i=0;i<buffer_size;i++) w[i]=buffer[i]; return OK; }
Array StreamPeer::_get_data(int p_bytes) { Array ret; DVector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(DVector<uint8_t>()); return ret; } DVector<uint8_t>::Write w = data.write(); Error err = get_data(&w[0], p_bytes); w = DVector<uint8_t>::Write(); ret.push_back(err); ret.push_back(data); return ret; }
Ref<Mesh> Shape::get_debug_mesh() { if (debug_mesh_cache.is_valid()) return debug_mesh_cache; Vector<Vector3> lines = _gen_debug_mesh_lines(); debug_mesh_cache = Ref<Mesh>(memnew(Mesh)); if (!lines.empty()) { //make mesh DVector<Vector3> array; array.resize(lines.size()); { DVector<Vector3>::Write w=array.write(); for(int i=0;i<lines.size();i++) { w[i]=lines[i]; } } Array arr; arr.resize(Mesh::ARRAY_MAX); arr[Mesh::ARRAY_VERTEX]=array; SceneTree *st=OS::get_singleton()->get_main_loop()->cast_to<SceneTree>(); debug_mesh_cache->add_surface(Mesh::PRIMITIVE_LINES,arr); if (st) { debug_mesh_cache->surface_set_material(0,st->get_debug_collision_material()); } } return debug_mesh_cache; }
DVector<uint8_t> _File::get_buffer(int p_length) const { DVector<uint8_t> data; ERR_FAIL_COND_V(!f,data); ERR_FAIL_COND_V(p_length<0,data); if (p_length==0) return data; Error err = data.resize(p_length); ERR_FAIL_COND_V(err!=OK,data); DVector<uint8_t>::Write w = data.write(); int len = f->get_buffer(&w[0],p_length); ERR_FAIL_COND_V( len < 0 , DVector<uint8_t>()); w = DVector<uint8_t>::Write(); if (len < p_length) data.resize(p_length); return data; }
DVector<Face3> TriangleMesh::get_faces() const { if (!valid) return DVector<Face3>(); DVector<Face3> faces; int ts = triangles.size(); faces.resize(triangles.size()); DVector<Face3>::Write w=faces.write(); DVector<Triangle>::Read r = triangles.read(); DVector<Vector3>::Read rv = vertices.read(); for(int i=0;i<ts;i++) { for(int j=0;j<3;j++) { w[i].vertex[j]=rv[r[i].indices[j]]; } } w = DVector<Face3>::Write(); return faces; }
DVector<Point2> Curve2D::bake(int p_subdivs) const { int pc = points.size(); DVector<Point2> ret; if (pc<2) return ret; ret.resize((pc-1)*p_subdivs+1); DVector<Point2>::Write w = ret.write(); const Point *r = points.ptr(); for(int i=0;i<pc;i++) { int ofs = pc*p_subdivs; int limit=(i==pc-1)?p_subdivs+1:p_subdivs; for(int j=0;j<limit;j++) { Vector2 p0 = r[i].pos; Vector2 p1 = p0+r[i].out; Vector2 p3 = r[i].pos; Vector2 p2 = p3+r[i].in; real_t t = j/(real_t)p_subdivs; w[ofs+j]=_bezier_interp(t,p0,p1,p2,p3); } } w = DVector<Point2>::Write(); return ret; }