static GlyphImage ReadGlyphImages(string filename) { using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open)) { Hjg.Pngcs.PngReader reader = new Hjg.Pngcs.PngReader(fs, filename); Hjg.Pngcs.ImageInfo imgInfo = reader.ImgInfo; Hjg.Pngcs.ImageLine iline2 = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE); int imgH = imgInfo.Rows; int imgW = imgInfo.Cols; int stride = imgInfo.BytesPerRow; int widthPx = imgInfo.Cols; int[] buffer = new int[(stride / 4) * imgH]; //read each row //and fill the glyph image int startWriteAt = (imgW * (imgH - 1)); int destIndex = startWriteAt; for (int row = 0; row < imgH; row++) { Hjg.Pngcs.ImageLine iline = reader.ReadRowByte(row); byte[] scline = iline.ScanlineB; int b_src = 0; destIndex = startWriteAt; for (int mm = 0; mm < imgW; ++mm) { byte b = scline[b_src]; byte g = scline[b_src + 1]; byte r = scline[b_src + 2]; byte a = scline[b_src + 3]; b_src += 4; buffer[destIndex] = (b << 16) | (g << 8) | (r) | (a << 24); destIndex++; } startWriteAt -= imgW; } GlyphImage img = new GlyphImage(imgW, imgH); img.SetImageBuffer(buffer, true); return(img); } }
public static void Save(MemBitmap bmp, Stream strm) { //------------- unsafe { PixelFarm.CpuBlit.Imaging.TempMemPtr tmp = MemBitmap.GetBufferPtr(bmp); int *intBuffer = (int *)tmp.Ptr; int imgW = bmp.Width; int imgH = bmp.Height; Hjg.Pngcs.ImageInfo imgInfo = new Hjg.Pngcs.ImageInfo(imgW, imgH, 8, true); //8 bits per channel with alpha Hjg.Pngcs.PngWriter writer = new Hjg.Pngcs.PngWriter(strm, imgInfo); Hjg.Pngcs.ImageLine iline = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE); int startReadAt = 0; int imgStride = imgW * 4; int srcIndex = 0; int srcIndexRowHead = (tmp.LengthInBytes / 4) - imgW; for (int row = 0; row < imgH; row++) { byte[] scanlineBuffer = iline.ScanlineB; srcIndex = srcIndexRowHead; for (int b = 0; b < imgStride;) { int srcInt = intBuffer[srcIndex]; srcIndex++; scanlineBuffer[b] = (byte)((srcInt >> 16) & 0xff); scanlineBuffer[b + 1] = (byte)((srcInt >> 8) & 0xff); scanlineBuffer[b + 2] = (byte)((srcInt) & 0xff); scanlineBuffer[b + 3] = (byte)((srcInt >> 24) & 0xff); b += 4; } srcIndexRowHead -= imgW; startReadAt += imgStride; writer.WriteRow(iline, row); } writer.End(); } }
static void SaveImgBufferToFile(GlyphImage glyphImg, string filename) { //------------- int[] intBuffer = glyphImg.GetImageBuffer(); using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create)) { int imgW = glyphImg.Width; int imgH = glyphImg.Height; Hjg.Pngcs.ImageInfo imgInfo = new Hjg.Pngcs.ImageInfo(imgW, imgH, 8, true); //8 bits per channel with alpha Hjg.Pngcs.PngWriter writer = new Hjg.Pngcs.PngWriter(fs, imgInfo); Hjg.Pngcs.ImageLine iline = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE); int startReadAt = 0; int imgStride = imgW * 4; int srcIndex = 0; int srcIndexRowHead = intBuffer.Length - imgW; for (int row = 0; row < imgH; row++) { byte[] scanlineBuffer = iline.ScanlineB; srcIndex = srcIndexRowHead; for (int b = 0; b < imgStride;) { int srcInt = intBuffer[srcIndex]; srcIndex++; scanlineBuffer[b] = (byte)((srcInt >> 16) & 0xff); scanlineBuffer[b + 1] = (byte)((srcInt >> 8) & 0xff); scanlineBuffer[b + 2] = (byte)((srcInt) & 0xff); scanlineBuffer[b + 3] = (byte)((srcInt >> 24) & 0xff); b += 4; } srcIndexRowHead -= imgW; startReadAt += imgStride; writer.WriteRow(iline, row); } writer.End(); } }
public static MemBitmap Read(Stream strm) { Hjg.Pngcs.PngReader reader = new Hjg.Pngcs.PngReader(strm); Hjg.Pngcs.ImageInfo imgInfo = reader.ImgInfo; Hjg.Pngcs.ImageLine iline2 = new Hjg.Pngcs.ImageLine(imgInfo, Hjg.Pngcs.ImageLine.ESampleType.BYTE); int imgH = imgInfo.Rows; int imgW = imgInfo.Cols; int widthPx = imgInfo.Cols; int stride = widthPx * 4; //expand to 32 bits int[] buffer = new int[(stride / 4) * imgH]; bool isInverted = false; if (isInverted) { //read each row //and fill the glyph image int startWriteAt = (imgW * (imgH - 1)); int destIndex = startWriteAt; for (int row = 0; row < imgH; row++) { Hjg.Pngcs.ImageLine iline = reader.ReadRowByte(row); byte[] scline = iline.ScanlineB; int b_src = 0; destIndex = startWriteAt; if (imgInfo.BitspPixel == 32) { for (int mm = 0; mm < imgW; ++mm) { byte b = scline[b_src]; byte g = scline[b_src + 1]; byte r = scline[b_src + 2]; byte a = scline[b_src + 3]; b_src += 4; buffer[destIndex] = (b << 16) | (g << 8) | (r) | (a << 24); destIndex++; } } else if (imgInfo.BitspPixel == 24) { for (int mm = 0; mm < imgW; ++mm) { byte b = scline[b_src]; byte g = scline[b_src + 1]; byte r = scline[b_src + 2]; b_src += 3; buffer[destIndex] = (b << 16) | (g << 8) | (r) | (255 << 24); destIndex++; } } else { throw new NotSupportedException(); } startWriteAt -= imgW; } return(MemBitmap.CreateFromCopy(imgW, imgH, buffer)); } else { //read each row //and fill the glyph image int startWriteAt = 0; int destIndex = startWriteAt; for (int row = 0; row < imgH; row++) { Hjg.Pngcs.ImageLine iline = reader.ReadRowByte(row); byte[] scline = iline.ScanlineB; int b_src = 0; destIndex = startWriteAt; if (imgInfo.BitspPixel == 32) { for (int mm = 0; mm < imgW; ++mm) { byte b = scline[b_src]; byte g = scline[b_src + 1]; byte r = scline[b_src + 2]; byte a = scline[b_src + 3]; b_src += 4; if (a > 0) { } //buffer[destIndex] = (b << 16) | (g << 8) | (r) | (a << 24); buffer[destIndex] = -1; destIndex++; } startWriteAt += imgW; } else if (imgInfo.BitspPixel == 24) { for (int mm = 0; mm < imgW; ++mm) { byte b = scline[b_src]; byte g = scline[b_src + 1]; byte r = scline[b_src + 2]; if (g == 0) { } b_src += 3; buffer[destIndex] = (b << 16) | (g << 8) | (r) | (255 << 24); destIndex++; } startWriteAt += imgW; } else { throw new NotSupportedException(); } } return(MemBitmap.CreateFromCopy(imgW, imgH, buffer)); } }