Esempio n. 1
0
        static void testmirror(string orig, string origni, string truecolor)
        {
            string mirror = TestsHelper.addSuffixToName(orig, "_mirror");
            string recov  = TestsHelper.addSuffixToName(orig, "_recov");
            long   crc0   = 0;
            bool   interlaced;
            bool   palete;

            {
                PngReader pngr = FileHelper.CreatePngReader(orig);
                palete = pngr.ImgInfo.Indexed;
                PngHelperInternal.InitCrcForTests(pngr);
                pngr.SetUnpackedMode(true);
                interlaced = pngr.IsInterlaced();
                PngWriter pngw = FileHelper.CreatePngWriter(mirror, pngr.ImgInfo, true);
                pngw.SetFilterType(FilterType.FILTER_CYCLIC); // just to test all filters
                pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL);
                pngw.SetUseUnPackedMode(true);
                for (int row = 0; row < pngr.ImgInfo.Rows; row++)
                {
                    ImageLine line = pngr.ReadRowInt(row);
                    mirrorLine(line);
                    pngw.WriteRow(line, row);
                }
                pngr.End();
                crc0 = PngHelperInternal.GetCrctestVal(pngr);
                pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL);
                pngw.End();
            }
            // mirror again, now with BYTE (if depth<16) and loading all rows
            {
                PngReader pngr2 = FileHelper.CreatePngReader(mirror);
                pngr2.SetUnpackedMode(true);
                PngWriter pngw = FileHelper.CreatePngWriter(recov, pngr2.ImgInfo, true);
                pngw.SetFilterType(FilterType.FILTER_AGGRESSIVE);
                pngw.CopyChunksFirst(pngr2, ChunkCopyBehaviour.COPY_ALL);
                pngw.SetUseUnPackedMode(true);
                ImageLines lines = pngr2.ImgInfo.BitDepth < 16 ? pngr2.ReadRowsByte() : pngr2
                                   .ReadRowsInt();
                for (int row = 0; row < pngr2.ImgInfo.Rows; row++)
                {
                    ImageLine line = lines.GetImageLineAtMatrixRow(row);
                    mirrorLine(line);
                    pngw.WriteRow(line, row);
                }
                pngr2.End();
                pngw.End();
            }
            // now check
            if (orig[11] != 'i')
            {
                TestsHelper.testCrcEquals(recov, crc0);
            }
            //if (interlaced)
            //    additionalTestInterlaced(orig, origni);
            //if (palete && System.IO.File.Exists(truecolor))
            //    additionalTestPalette(orig, truecolor);
        }
Esempio n. 2
0
        private void _convert(string inputPdfPath)
        {
            _outputToInfo(inputPdfPath);

            // make a sub path in the same folder as this

            // put file(s) in there

            // single file has NAME.png

            // multiples are NAME_000.png

            // overwrite just uses folder always

            // otherwise, append output folder if it exists and is NOT empty

            try
            {
                outputFolder = _createOutputFolder(inputPdfPath, _overwrite);
                if (outputFolder == null)
                {
                    _outputToInfo("Error");
                    return;
                }

                string historyFile = Path.Combine(outputFolder, "history.txt");

                _outputToInfo("Output to: " + outputFolder);
                _outputToInfo(" ");
                using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
                {
                    rasterizer.Open(inputPdfPath, _lastInstalledVersion, false);

                    if (_multiFiles)
                    {
                        string outputPath = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inputPdfPath));
                        int    nFiles     = 0;
                        for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
                        {
                            if (cancel)
                            {
                                break;
                            }
                            if (_isPageNumberRequired(pageNumber))
                            {
                                string pageFilePath = outputPath + "_" + pageNumber.ToString("000") + _imageExt;
                                Image  img          = rasterizer.GetPage(_dpi, _dpi, pageNumber);
                                img.Save(pageFilePath, _imageFmt);
                                _outputToInfo(img);
                                _outputToInfo("  " + Path.GetFileName(pageFilePath));
                                _outputToInfo("  Dimensions:" + img.Width + " x " + img.Height);
                                _outputToInfo();

                                _history(historyFile, inputPdfPath, pageFilePath);
                                nFiles++;
                            }
                        }
                        _outputToInfo("  complete, " + nFiles + " files in:");
                        _outputToInfo("     " + outputFolder);
                        _outputToInfo();
                    }
                    else
                    {
                        // work in PNG here
                        string        outputPath = Path.GetTempPath();
                        string        prefix = Guid.NewGuid().ToString().Substring(0, 7);
                        List <string> pathList = new List <string>();
                        int           w = 0, h = 0;
                        for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
                        {
                            if (cancel)
                            {
                                break;
                            }
                            if (_isPageNumberRequired(pageNumber))
                            {
                                string pageFilePath = Path.Combine(outputPath, prefix + "_" + pageNumber.ToString() + ".png");
                                Image  img          = rasterizer.GetPage(_dpi, _dpi, pageNumber);
                                img.Save(pageFilePath, ImageFormat.Png);
                                _outputToInfo(img);
                                pathList.Add(pageFilePath);
                                _outputToInfo("  building page: " + pageNumber + " of " + rasterizer.PageCount);
                                _outputToInfo();
                                h += img.Height;
                                w  = Math.Max(w, img.Width);
                            }
                        }

                        // combine!!
                        if (cancel == false && pathList.Count > 0)
                        {
                            string    bigImagePath = Path.Combine(outputPath, prefix + "_BIG" + ".png");
                            PngReader pngr         = FileHelper.CreatePngReader(pathList[0]); // or you can use the constructor
                            ImageInfo ii           = new ImageInfo(w, h, pngr.ImgInfo.BitDepth, pngr.ImgInfo.Alpha, false, false);
                            PngWriter pngw         = FileHelper.CreatePngWriter(bigImagePath, ii, true);
                            int       chunkBehav   = ChunkCopyBehaviour.COPY_ALL_SAFE; // tell to copy all 'safe' chunks
                            pngw.CopyChunksFirst(pngr, chunkBehav);                    // copy some metadata from reader
                            pngr.End();

                            int destRow = 0;
                            int ic      = 1;
                            foreach (string path in pathList)
                            {
                                pngr = FileHelper.CreatePngReader(path);
                                ImageLines iLines = pngr.ReadRowsInt(0, pngr.ImgInfo.Rows, 1);

                                int nrr = iLines.Nrows;
                                for (int i = 0; i < nrr; i++)
                                {
                                    pngw.WriteRow(iLines.GetImageLineAtMatrixRow(i), destRow++);
                                }
                                _outputToInfo(" combining page:" + ic + " of " + pathList.Count);
                                ic++;
                                pngr.End();
                                File.Delete(path); // remove them as we go
                            }

                            pngw.CopyChunksLast(pngr, chunkBehav); // metadata after the image pixels? can happen
                            pngw.End();                            // dont forget this
                            _autoCrop(bigImagePath);


                            // convert to required type if not PNG
                            string outputSingleFile = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inputPdfPath) + _imageExt);
                            Bitmap bm;
                            if (_imageExt != ".png")
                            {
                                bm = new Bitmap(bigImagePath);
                                bm.Save(outputSingleFile, _imageFmt);
                            }
                            else
                            {
                                bm = new Bitmap(bigImagePath);
                                _moveFile(bigImagePath, outputSingleFile);
                            }

                            _outputToInfo(bm);
                            _outputToInfo("  Dimensions:" + bm.Width + " x " + bm.Height);
                            _outputToInfo("  Complete: ");
                            _outputToInfo("   " + outputSingleFile);
                            _history(historyFile, inputPdfPath, outputSingleFile);
                        }
                        else
                        {
                            _outputToInfo("  NO PAGES set! Try again.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _outputToInfo("Error");
                _outputToInfo(ex.ToString());
                throw;
            }
        }