コード例 #1
0
        public Bitmap CreateBitmap()
        {
            UrfPage page = pages[currentPage - 1];

            Bitmap bitmap = new Bitmap(page.Header.Width, page.Header.Height, PixelFormat.Format24bppRgb);

            bitmap.SetResolution(page.Header.Resolution, page.Header.Resolution);

            int bytesPerPixel = page.Header.BitsPerPixel / 8;

            BinaryReader reader = new BinaryReader(this.fileStream);

            reader.BaseStream.Seek(page.StreamStart, SeekOrigin.Begin);

            int lineCount = 0;

            while (lineCount < page.Header.Height && reader.BaseStream.Position < reader.BaseStream.Length)
            {
                int lineRepeat = reader.ReadByte() + 1;

                int     linePixelsCount = 0;
                Color[] linePixels      = new Color[page.Header.Width];

                while (linePixelsCount < page.Header.Width)
                {
                    int   pixelRepeat = reader.ReadByte();
                    Color pixel       = Color.White;

                    if (pixelRepeat == 128)
                    {
                        // the rest of the line should be white
                        pixelRepeat = page.Header.Width - linePixelsCount;
                    }
                    else if (pixelRepeat > 128)
                    {
                        // number of non repeating pixels
                        int nonRepeatingPixels = 257 - pixelRepeat;
                        pixelRepeat = 0;

                        for (int i = 0; i < nonRepeatingPixels; i++)
                        {
                            linePixels[linePixelsCount++] = ReadPixel(reader, page.Header, bytesPerPixel);
                        }
                    }
                    else
                    {
                        // read one pixel and repeat that
                        pixel = ReadPixel(reader, page.Header, bytesPerPixel);
                        // Increasing the pixel repeat counter by one since the repeat counter is -1
                        pixelRepeat++;
                    }

                    // repeating pixels in a line
                    for (int i = 0; i < pixelRepeat; i++)
                    {
                        linePixels[linePixelsCount++] = pixel;
                    }
                }

                if (linePixelsCount > page.Header.Width)
                {
                    throw new Exception("Error Parsing document, line is bigger than expected.");
                }

                // write line to the raster output, repeating lines
                for (int y = 0; y < lineRepeat; y++)
                {
                    for (int x = 0; x < linePixels.Length; x++)
                    {
                        bitmap.SetPixel(x, lineCount, linePixels[x]);
                    }

                    lineCount++;
                }
            }

            return(bitmap);
        }
コード例 #2
0
        public UrfDocument(string filePath)
        {
            this.fileStream  = File.OpenRead(filePath);
            this.currentPage = 1;

            BinaryReader reader = new BinaryReader(this.fileStream);

            byte[] header = new byte[8];
            header = reader.ReadBytes(header.Length);

            if (!URF_HEADER.SequenceEqual(header))
            {
                throw new ArgumentException(string.Format("Cannot render {0}: it is not a supported format", filePath));
            }

            int pageCount = reader.ReadInt();

            this.pages = new List <UrfPage>(pageCount);

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                UrfPage currentPage = new UrfPage();

                currentPage.Header      = new UrfHeader(reader);
                currentPage.StreamStart = reader.BaseStream.Position;

                int bytesPerPixel = currentPage.Header.BitsPerPixel / 8;

                int lineCount = 0;
                while (lineCount < currentPage.Header.Height && reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    int lineRepeat = reader.ReadByte() + 1;

                    int linePixelsCount = 0;
                    while (linePixelsCount < currentPage.Header.Width)
                    {
                        int pixelRepeat = reader.ReadByte();

                        if (pixelRepeat == 128)
                        {
                            // rest of the line should be white
                            pixelRepeat = currentPage.Header.Width - linePixelsCount;
                        }
                        else if (pixelRepeat > 128)
                        {
                            // number of non repeating pixels
                            int nonRepeatingPixels = 257 - pixelRepeat;
                            pixelRepeat = 0;

                            for (int i = 0; i < nonRepeatingPixels; i++)
                            {
                                reader.BaseStream.Seek(bytesPerPixel, SeekOrigin.Current);
                                linePixelsCount++;
                            }
                        }
                        else
                        {
                            // read one pixel and repeat that
                            reader.BaseStream.Seek(bytesPerPixel, SeekOrigin.Current);
                            // increasing the pixel repeat counter by one since the repeat counter is -1
                            pixelRepeat++;
                        }

                        // repeating pixels in a line
                        linePixelsCount += pixelRepeat;
                    }

                    if (linePixelsCount > currentPage.Header.Width)
                    {
                        throw new Exception("Error Parsing document, line is bigger than expected");
                    }

                    lineCount += lineRepeat;
                }
                currentPage.StreamEnd = reader.BaseStream.Position;

                this.pages.Add(currentPage);
                for (int i = 1; i < currentPage.Header.Copies; i++)
                {
                    this.pages.Add(currentPage);
                }
            }
        }