public static bool load( ClassBitmap bitmap, String filename ) { try { using (Texture2D texture = Texture2D.FromStream( Plasmacore.device, TitleContainer.OpenStream(filename))) { init( bitmap, texture ); return true; } } catch (Exception) { return false; } }
public static void init( ClassBitmap bitmap, ClassString filename ) { String name = filename.data; if (name.Equals("internal:font_system_17")) { init( bitmap, embedded_font_system_17 ); return; } int slash_pos = name.LastIndexOf('/'); int other_slash_pos = name.LastIndexOf('\\'); if (slash_pos == -1 || (other_slash_pos != -1 && other_slash_pos < slash_pos)) { slash_pos = other_slash_pos; } if (slash_pos != -1) { name = name.Substring( slash_pos + 1 ); } bool success = true; if (name.EndsWith(".png") || name.EndsWith(".jpg") || name.EndsWith(".jpeg")) { success = load( bitmap, "images/"+name ); } else { if ( !load( bitmap, "images/" + name + ".png" ) ) { if ( !load( bitmap, "images/" + name + ".jpg" ) ) { success = load( bitmap, "images/" + name + ".jpeg" ); } } } if ( !success ) { throw new ClassFileNotFoundError(filename); } }
public static void init( ClassBitmap bitmap, byte[] bytes ) { try { using (Texture2D texture = Texture2D.FromStream( Plasmacore.device, new MemoryStream(bytes))) { init( bitmap, texture ); } } catch (Exception) { throw new ClassInvalidOperandError( Bard.str("bytes") ); } }
public static void init( ClassBitmap bitmap, Texture2D texture ) { int w = texture.Bounds.Width; int h = texture.Bounds.Height; Color[] colors = new Color[w*h]; texture.GetData<Color>( colors ); bitmap.init(w,h); int[] data = bitmap.property_data; int count = colors.Length; for (int i=0; i<count; ++i) { Color color = colors[i]; data[i] = (color.A<<24) | (color.R<<16) | (color.G<<8) | color.B; } }
public static void init( ClassBitmap bitmap, ClassArrayList_of_Byte raw_data ) { init( bitmap, raw_data.property_data ); }
public static void copy_pixels_to( ClassBitmap src, int src_x, int src_y, int w, int h, ClassBitmap dest, int dest_x, int dest_y, bool blend_alpha) { if (src == null || dest == null) { throw new ClassNullReferenceError(); } int src_width = src.property_width; int dest_width = dest.property_width; int[] src_data = src.property_data; int[] dest_data = dest.property_data; int dest_skip_width = dest_width - w; int src_skip_width = src_width - w; int src_i = src_y * src_width + src_x - 1; int dest_i = dest_y * dest_width + dest_x - 1; if (blend_alpha) { for (int j=h; j>0; --j) { for (int i=w; i>0; --i) { int bottom = dest_data[++dest_i]; int top = src_data[++src_i]; int tr = (top >> 16) & 255; int tg = (top >> 8) & 255; int tb = (top & 255); int r = (bottom >> 16) & 255; int g = (bottom >> 8) & 255; int b = (bottom & 255); int inv_alpha = 255 - ((top >> 24) & 255); // we assume that tr, tg, and tb are premultiplied tr += ((r * inv_alpha) / 255); tg += ((g * inv_alpha) / 255); tb += ((b * inv_alpha) / 255); dest_data[dest_i] = ((255<<24) | (tr<<16) | (tg<<8) | tb); } dest_i += dest_skip_width; src_i += src_skip_width; } } else { for (int j=h; j>0; --j) { for (int i=w; i>0; --i) { dest_data[++dest_i] = src_data[++src_i]; } dest_i += dest_skip_width; src_i += src_skip_width; } } }
public static void set( ClassTexture texture, ClassBitmap bitmap, CompoundVector2 pos ) { int[] data = bitmap.property_data; int count = data.Length; int w = bitmap.property_width; int h = bitmap.property_height; Color[] colors = new Color[w*h]; int src_pos = -1; int dest_pos = -1; for (int j=h; j>0; --j) { for (int i=w; i>0; --i) { // premultiply color components by alpha component int c = data[++src_pos]; int a = (c >> 24) & 255; int r = (c >> 16) & 255; int g = (c >> 8) & 255; int b = c & 255; colors[++dest_pos] = new Color( (r*a)/255, (g*a)/255, (b*a)/255, a ); } } Texture2D tex = ((NativeTextureData) texture.property_native_data).texture; tex.SetData<Color>( 0, new Rectangle((int)pos.property_x,(int)pos.property_y,w,h), colors, 0, w*h ); }
public static void init( ClassTexture texture, ClassBitmap bitmap, int pixel_format ) { int[] data = bitmap.property_data; int count = data.Length; int w = bitmap.property_width; int h = bitmap.property_height; int wp2 = 1; while (wp2 < w) wp2 <<= 1; int hp2 = 1; while (hp2 < h) hp2 <<= 1; int dest_skip = wp2 - w; Color[] colors = new Color[wp2*hp2]; int src_pos = -1; int dest_pos = -1; for (int j=h; j>0; --j) { for (int i=w; i>0; --i) { // premultiply color components by alpha component int c = data[++src_pos]; int a = (c >> 24) & 255; int r = (c >> 16) & 255; int g = (c >> 8) & 255; int b = c & 255; colors[++dest_pos] = new Color( (r*a)/255, (g*a)/255, (b*a)/255, a ); } dest_pos += dest_skip; } Texture2D tex = new Texture2D( Plasmacore.device, wp2, hp2, false, SurfaceFormat.Color ); tex.SetData<Color>( colors ); texture.property_native_data = new NativeTextureData(tex); texture.property_image_size.property_x = w; texture.property_image_size.property_y = h; texture.property_texture_size.property_x = wp2; texture.property_texture_size.property_y = hp2; }