/// <summary> /// This method is called when PixelMap save is requested, if it returns true /// the calling method will stop illiterating through the PixelMapFactorys and /// return success to the user. /// </summary> /// <param name="path">File path or object of the image to load.</param> /// <param name="pixelMap">PixelMap to save.</param> /// <param name="flags">Bitmask of flags defining how the pixel map should be saved.</param> /// <returns>True if the save was successfull else false.</returns> protected override bool RequestSave(object path, PixelMap pixelMap, PixelMapSaveFlags flags) { if (path.ToString().ToLower().EndsWith(".png") == false) { return(false); } // Not supported yet so throw error. throw new Exception("PNG factory is not currently supported."); }
/// <summary> /// This method is called when a PixelMap save is requested, it illiterates through all /// the registered PixelMapFactory instances to see if there is one capable to saving the /// given format. /// </summary> /// <param name="path">File path or save pixel map to.</param> /// <param name="flags">Bitmask of flags to define how the pixel map should be saved.</param> /// <returns>True if save was successfull else false.</returns> public static bool SavePixelMap(object path, PixelMap pixelMap, PixelMapSaveFlags flags) { foreach (PixelMapFactory factory in _loaderList) { if (factory.RequestSave(path, pixelMap, flags) == true) { return(true); } } return(false); }
/// <summary> /// This method is called when PixelMap save is requested, if it returns true /// the calling method will stop illiterating through the PixelMapFactorys and /// return success to the user. /// </summary> /// <param name="path">File path or object of the image to load.</param> /// <param name="pixelMap">PixelMap to save.</param> /// <param name="flags">Bitmask of flags defining how the pixel map should be saved.</param> /// <returns>True if the save was successfull else false.</returns> protected override bool RequestSave(object path, PixelMap pixelMap, PixelMapSaveFlags flags) { if (path.ToString().ToLower().EndsWith(".bmp") == false && path.ToString().ToLower().EndsWith(".dib") == false) return false; Stream stream = StreamFactory.RequestStream(path, StreamMode.Truncate); if (stream == null) return false; BinaryWriter writer = new BinaryWriter(stream); // Work out how to save. int bitCount = 24; if ((flags & PixelMapSaveFlags.BITDEPTH1) != 0) bitCount = 1; if ((flags & PixelMapSaveFlags.BITDEPTH4) != 0) bitCount = 4; if ((flags & PixelMapSaveFlags.BITDEPTH8) != 0) bitCount = 8; if ((flags & PixelMapSaveFlags.BITDEPTH24) != 0) bitCount = 24; if ((flags & PixelMapSaveFlags.BITDEPTH32) != 0) bitCount = 32; // Write in header. writer.Write(new char[2] { 'B', 'M' }); // Write in file header. writer.Write((int)0); writer.Write((short)0); writer.Write((short)0); writer.Write((int)54); // Write in image header. writer.Write((int)40); writer.Write((int)pixelMap.Width); writer.Write((int)pixelMap.Height); writer.Write((short)1); writer.Write((short)bitCount); writer.Write((int)0); writer.Write((int)0); writer.Write((int)3780); writer.Write((int)3780); writer.Write((int)0); writer.Write((int)0); // Work out the horizontal padding size. int paddingWidth = (int)((float)pixelMap.Width * (float)bitCount / 8.0f); while (paddingWidth % 4 != 0) paddingWidth++; // Grab a few usefull details from the pixel map. int maskColor = pixelMap.MaskColor, maskRed, maskGreen, maskBlue, maskAlpha; ColorMethods.SplitColor(ColorFormats.A8R8G8B8, maskColor, out maskRed, out maskGreen, out maskBlue, out maskAlpha); // Write in based of bit count switch (bitCount) { case 1: // Write out our lovely monocromatic colors, black and white obviously :). writer.Write((int)ColorMethods.CombineColor(ColorFormats.A8R8G8B8, ColorFormats.B8G8R8, unchecked((int)0xFF000000))); writer.Write((int)ColorMethods.CombineColor(ColorFormats.A8R8G8B8, ColorFormats.B8G8R8, unchecked((int)0xFFFFFFFF))); // Write byte here! for (int y = pixelMap.Height - 1; y >= 0; y--) { int bit = 128; int bitMask = 0; for (int x = 0; x < pixelMap.Width; x++) { // Mask with bit if pixel should be black. int red, green, blue, alpha; ColorMethods.SplitColor(ColorFormats.A8R8G8B8, pixelMap[x, y], out red, out green, out blue, out alpha); if (red > 128 || green > 128 || blue > 128) bitMask |= bit; // Flips horizontal direction at every 8th horizontal pixel. // Update bit mask. bit >>= 1; if (bit < 1) { writer.Write((byte)bitMask); bit = 128; bitMask = 0; } } // Write in last bit if neccessary. if (bitMask > 0) writer.Write((byte)bitMask); // Pad out scan line. for (int i = (pixelMap.Width / 8); i < paddingWidth - 1; i++) writer.Write((byte)0); } break; //case 4: // - Can't think when this would be needed? Can you? // break; //case 8: // Same as 4bit. // break; case 24: for (int y = pixelMap.Height - 1; y >= 0; y--) { for (int x = 0; x < paddingWidth / (bitCount / 8); x++) { int red = 0, green = 0, blue = 0, alpha = 0; if (x < pixelMap.Width) { // If the pixel map is masked we need to substitute the pixel color // for the correct mask color or we will just end up with an invisible pixel (>_>). if (pixelMap[x, y] != pixelMap.MaskColor) { ColorMethods.SplitColor(ColorFormats.A8R8G8B8, pixelMap[x, y], out red, out green, out blue, out alpha); } else { blue = maskBlue; green = maskGreen; red = maskRed; } writer.Write((byte)blue); writer.Write((byte)green); writer.Write((byte)red); } else { writer.Write((byte)0); } } } break; case 32: for (int y = pixelMap.Height - 1; y >= 0; y--) { for (int x = 0; x < paddingWidth / (bitCount / 8); x++) { int red = 0, green = 0, blue = 0, alpha = 0; if (x < pixelMap.Width) { // If the pixel map is masked we need to substitute the pixel color // for the correct mask color or we will just end up with an invisible pixel (>_>). if (pixelMap[x, y] != pixelMap.MaskColor) { ColorMethods.SplitColor(ColorFormats.A8R8G8B8, pixelMap[x, y], out red, out green, out blue, out alpha); } else { blue = maskBlue; green = maskGreen; red = maskRed; alpha = maskAlpha; } writer.Write((byte)blue); writer.Write((byte)green); writer.Write((byte)red); writer.Write((byte)alpha); } else { writer.Write((byte)0); } } } break; default: stream.Close(); throw new Exception(bitCount+"bit bmp saving is not supported"); } // Seek back to start and write in file size stream.Flush(); int fileSize = (int)stream.Length; stream.Seek(2, SeekOrigin.Begin); writer.Write(fileSize); // Seek back to start and write in image size. stream.Seek(34, SeekOrigin.Begin); writer.Write(fileSize - 54); // Cleanup stream and return success. stream.Close(); return true; }
/// <summary> /// This method is called when PixelMap save is requested, if it returns true /// the calling method will stop illiterating through the PixelMapFactorys and /// return success to the user. /// </summary> /// <param name="path">File path or object of the image to load.</param> /// <param name="pixelMap">PixelMap to save.</param> /// <param name="flags">Bitmask of flags defining how the pixel map should be saved.</param> /// <returns>True if the save was successfull else false.</returns> protected override bool RequestSave(object path, PixelMap pixelMap, PixelMapSaveFlags flags) { if (path.ToString().ToLower().EndsWith(".png") == false) return false; // Not supported yet so throw error. throw new Exception("PNG factory is not currently supported."); }
/// <summary> /// This method is called when PixelMap save is requested, if it returns true /// the calling method will stop illiterating through the PixelMapFactorys and /// return success to the user. /// </summary> /// <param name="path">File path or object of the image to load.</param> /// <param name="pixelMap">PixelMap to save.</param> /// <param name="flags">Bitmask of flags defining how the pixel map should be saved.</param> /// <returns>True if the save was successfull else false.</returns> protected override bool RequestSave(object path, PixelMap pixelMap, PixelMapSaveFlags flags) { if (path.ToString().ToLower().EndsWith(".bmp") == false && path.ToString().ToLower().EndsWith(".dib") == false) { return(false); } Stream stream = StreamFactory.RequestStream(path, StreamMode.Truncate); if (stream == null) { return(false); } BinaryWriter writer = new BinaryWriter(stream); // Work out how to save. int bitCount = 24; if ((flags & PixelMapSaveFlags.BITDEPTH1) != 0) { bitCount = 1; } if ((flags & PixelMapSaveFlags.BITDEPTH4) != 0) { bitCount = 4; } if ((flags & PixelMapSaveFlags.BITDEPTH8) != 0) { bitCount = 8; } if ((flags & PixelMapSaveFlags.BITDEPTH24) != 0) { bitCount = 24; } if ((flags & PixelMapSaveFlags.BITDEPTH32) != 0) { bitCount = 32; } // Write in header. writer.Write(new char[2] { 'B', 'M' }); // Write in file header. writer.Write((int)0); writer.Write((short)0); writer.Write((short)0); writer.Write((int)54); // Write in image header. writer.Write((int)40); writer.Write((int)pixelMap.Width); writer.Write((int)pixelMap.Height); writer.Write((short)1); writer.Write((short)bitCount); writer.Write((int)0); writer.Write((int)0); writer.Write((int)3780); writer.Write((int)3780); writer.Write((int)0); writer.Write((int)0); // Work out the horizontal padding size. int paddingWidth = (int)((float)pixelMap.Width * (float)bitCount / 8.0f); while (paddingWidth % 4 != 0) { paddingWidth++; } // Grab a few usefull details from the pixel map. int maskColor = pixelMap.MaskColor, maskRed, maskGreen, maskBlue, maskAlpha; ColorMethods.SplitColor(ColorFormats.A8R8G8B8, maskColor, out maskRed, out maskGreen, out maskBlue, out maskAlpha); // Write in based of bit count switch (bitCount) { case 1: // Write out our lovely monocromatic colors, black and white obviously :). writer.Write((int)ColorMethods.CombineColor(ColorFormats.A8R8G8B8, ColorFormats.B8G8R8, unchecked ((int)0xFF000000))); writer.Write((int)ColorMethods.CombineColor(ColorFormats.A8R8G8B8, ColorFormats.B8G8R8, unchecked ((int)0xFFFFFFFF))); // Write byte here! for (int y = pixelMap.Height - 1; y >= 0; y--) { int bit = 128; int bitMask = 0; for (int x = 0; x < pixelMap.Width; x++) { // Mask with bit if pixel should be black. int red, green, blue, alpha; ColorMethods.SplitColor(ColorFormats.A8R8G8B8, pixelMap[x, y], out red, out green, out blue, out alpha); if (red > 128 || green > 128 || blue > 128) { bitMask |= bit; } // Flips horizontal direction at every 8th horizontal pixel. // Update bit mask. bit >>= 1; if (bit < 1) { writer.Write((byte)bitMask); bit = 128; bitMask = 0; } } // Write in last bit if neccessary. if (bitMask > 0) { writer.Write((byte)bitMask); } // Pad out scan line. for (int i = (pixelMap.Width / 8); i < paddingWidth - 1; i++) { writer.Write((byte)0); } } break; //case 4: // - Can't think when this would be needed? Can you? // break; //case 8: // Same as 4bit. // break; case 24: for (int y = pixelMap.Height - 1; y >= 0; y--) { for (int x = 0; x < paddingWidth / (bitCount / 8); x++) { int red = 0, green = 0, blue = 0, alpha = 0; if (x < pixelMap.Width) { // If the pixel map is masked we need to substitute the pixel color // for the correct mask color or we will just end up with an invisible pixel (>_>). if (pixelMap[x, y] != pixelMap.MaskColor) { ColorMethods.SplitColor(ColorFormats.A8R8G8B8, pixelMap[x, y], out red, out green, out blue, out alpha); } else { blue = maskBlue; green = maskGreen; red = maskRed; } writer.Write((byte)blue); writer.Write((byte)green); writer.Write((byte)red); } else { writer.Write((byte)0); } } } break; case 32: for (int y = pixelMap.Height - 1; y >= 0; y--) { for (int x = 0; x < paddingWidth / (bitCount / 8); x++) { int red = 0, green = 0, blue = 0, alpha = 0; if (x < pixelMap.Width) { // If the pixel map is masked we need to substitute the pixel color // for the correct mask color or we will just end up with an invisible pixel (>_>). if (pixelMap[x, y] != pixelMap.MaskColor) { ColorMethods.SplitColor(ColorFormats.A8R8G8B8, pixelMap[x, y], out red, out green, out blue, out alpha); } else { blue = maskBlue; green = maskGreen; red = maskRed; alpha = maskAlpha; } writer.Write((byte)blue); writer.Write((byte)green); writer.Write((byte)red); writer.Write((byte)alpha); } else { writer.Write((byte)0); } } } break; default: stream.Close(); throw new Exception(bitCount + "bit bmp saving is not supported"); } // Seek back to start and write in file size stream.Flush(); int fileSize = (int)stream.Length; stream.Seek(2, SeekOrigin.Begin); writer.Write(fileSize); // Seek back to start and write in image size. stream.Seek(34, SeekOrigin.Begin); writer.Write(fileSize - 54); // Cleanup stream and return success. stream.Close(); return(true); }
public static bool SaveImage(object url, Image image, PixelMapSaveFlags flags) { return(SaveImage(url, image, flags, 0)); }
/// <summary> /// Saves the given image to an image file. /// </summary> /// <param name="url">Location to save the image to.</param> /// <param name="image">Image to save to file.</param> /// <param name="flags">Bitmask of flags describing how to save the image/</param> /// <param name="frame">Index of frame to save.</param> /// <returns></returns> public static bool SaveImage(object url, Image image, PixelMapSaveFlags flags, int frame) { return(PixelMapFactory.SavePixelMap(url, image[frame].PixelMap, flags)); }
/// <summary> /// This method is called when PixelMap save is requested, if it returns true /// the calling method will stop illiterating through the PixelMapFactorys and /// return success to the user. /// </summary> /// <param name="path">File path or object of the image to load.</param> /// <param name="pixelMap">PixelMap to save.</param> /// <param name="flags">Bitmask of flags defining how the pixel map should be saved.</param> /// <returns>True if the save was successfull else false.</returns> protected abstract bool RequestSave(object path, PixelMap pixelMap, PixelMapSaveFlags flags);
/// <summary> /// This method is called when a PixelMap save is requested, it illiterates through all /// the registered PixelMapFactory instances to see if there is one capable to saving the /// given format. /// </summary> /// <param name="path">File path or save pixel map to.</param> /// <param name="flags">Bitmask of flags to define how the pixel map should be saved.</param> /// <returns>True if save was successfull else false.</returns> public static bool SavePixelMap(object path, PixelMap pixelMap, PixelMapSaveFlags flags) { foreach (PixelMapFactory factory in _loaderList) { if (factory.RequestSave(path, pixelMap, flags) == true) return true; } return false; }
public static bool SaveImage(object url, Image image, PixelMapSaveFlags flags) { return SaveImage(url, image, flags, 0); }
/// <summary> /// Saves the given image to an image file. /// </summary> /// <param name="url">Location to save the image to.</param> /// <param name="image">Image to save to file.</param> /// <param name="flags">Bitmask of flags describing how to save the image/</param> /// <param name="frame">Index of frame to save.</param> /// <returns></returns> public static bool SaveImage(object url,Image image,PixelMapSaveFlags flags,int frame) { return PixelMapFactory.SavePixelMap(url,image[frame].PixelMap,flags); }
/// <summary> /// This method is called when PixelMap save is requested, if it returns true /// the calling method will stop illiterating through the PixelMapFactorys and /// return success to the user. /// </summary> /// <param name="path">File path or object of the image to load.</param> /// <param name="pixelMap">PixelMap to save.</param> /// <param name="flags">Bitmask of flags defining how the pixel map should be saved.</param> /// <returns>True if the save was successfull else false.</returns> protected abstract bool RequestSave(object path, PixelMap pixelMap, PixelMapSaveFlags flags);