You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Shaola Ren (JIRA)" <ji...@apache.org> on 2014/05/29 02:24:02 UTC

[jira] [Commented] (PDFBOX-1915) Implement shading with Coons and tensor-product patch meshes

    [ https://issues.apache.org/jira/browse/PDFBOX-1915?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14011876#comment-14011876 ] 

Shaola Ren commented on PDFBOX-1915:
------------------------------------

A strategy to decide patch coordinates and color

the code are available at https://bitbucket.org/xinshu/pdfbox.git

for web view https://bitbucket.org/xinshu/pdfbox

This is stuff edited from the emails from me and Tilman, I am sorry that I didn't post my questions on Jira 

earlier, hopefully these emails don't have any thing that is not suitable to be published publicly, if there are such 

issues, Tilman, Please, Feel free to delete or edit them. I am sorry for this unconvenience.

Hello Shaola,

I can't really decide about the strategy because it involves math and this is my weak point. What I have understood 

so far is:
- you have problem using the strategy mentioned in the project description, i.e. deciding color and position

(inside/outside) in getRaster() based on the current point. Because you can easily draw a coons patch by having an 

image to draw into, but not the opposite way, i.e. having a point and then decide.
- you can possibly do it if you are using a helper table, but that can cost memory
- another strategy is to divide the patch into tiny triangles (which will create something not-so-perfect like I 

e-mailed you)

As I said, I favor a strategy that gives results first. One could also use a misc strategy, i.e. one for the 

unfolded patches and one for the folded ones.

You can also ask math questions on http://math.stackexchange.com, as long as these are specific questions on small 

problems, and that you credit the answer in your code.

Some general stuff:
- did you read my "community bonding period" message in the dev list? I realize that we're not respecting the 

rules, i.e. this technical talk should be in the JIRA issue. The best would be that you write a summary and post it 

there, or open a comment with copy&paste with each of the mails, and also attach that drawing, and the patch 

outlines. I think this is about 3-5 mails only.
You don't have to do it immediately, but it should be done by the end of the week.
- the more general talk about pdfbox can be in the dev list.
- please post the bitbucket URL in the JIRA issue
- The advantage is that other developers can also give you good advice, not just me
- what you're working on is kindof the "top of the pyramid" for shadings. Several other open source programs 

support only shadings 1-3, same for several closed source programs. If you succeed, the Google summer of code is a 

great addition to your CV!

Tilman





Am 28.05.2014 16:35, schrieb 任少辣:
> Hello Tilman,
>
> I thought it more, may be first just focus on getting an image close to the original image, I mean the expected 

image will be less smooth than the original one.
>
> I can avoid to generate all the points information by creating a triangle list similar to shading type 5 after 

step 2, then the accomplishment is reduced to triangle meshes. The cubic Bezier curve information and bilinear 

interpolation of color are reserved as well, as they are used in step 1 and 2. For this updated method, I have more 

confidence if you can bear the error I mean the image will be less smooth as I need to approximate a curve segment 

to a line segment, with a trade off of speed, the error can be adjusted by changing the density of the grid.
>
> Sorry for the frequent changes, but we should admit that just learning the information in the pdf document is not 

sufficient, at least I think this is the truth.
>
> Best,
> Shaola
>
> Sorry, there is a calculation typo in the last email if you received, 20 x 9 x 10^4 bytes ~ 2 MB, not 20 x 9 x 

10^2 bytes ~ 2 MB
>
> Maybe it is worth to try my original method, with this method, I think at least it may give a complete result for 

the test files I already have.
>
> The details of the method is as following, it generates every point in a patch, now every point in a patch is 

created by search.
>
> A unit patch mapped to a real patch, the real patch can be described by the assistant variable u and v in the 

unit patch.
>
> 1. dividing the unit patch into a m x n grid, then using cubic bezier curve, I can map this grid to the real 

patch, during this process, the coordinate and color of cross point can be determined directly (cubic bezier curve 

and bilinear interpolation).
>
> 2. in the mapped patch, the curve which forms the mapped grid will be taken as a composition of many line 

segments, then the whole mapped patch is formed by many small quadrilaterals, splitting each quadrilateral into two 

triangle as I showed in the graph to solve the potential folding issue, now for each triangle, the color and 

position of each corner are already calculated, for the points inside a triangle, the color is calculated by 

barycentric interpolation. The color priority at each point is considered automatically.
> 

*******************************************************************************************************************

******************************************************************
> This difficult part of this method is to ensure to generate every points inside the patch, I did some code last 

week, but didn't test thoroughly.
>
> 3. for each generated point type is Point not Point2D, they will be stored in a hashmap, point is key, the value 

is the corresponding color value, the value cannot be an array, should be an object, so a class PixelColor is 

needed, I only store these point inside a patch.
>
> 4. all the above steps will be done by a separate method in Type6ShadingContext.java, the method will be only 

called once for each patch, as I will create another field in class Type6ShadingContext called private 

HashMap<Point, PixelColor> positionAndColor, this field is initialized using the previous method in 

Type6ShadingContext's constructor.
>
> 5. for multiple patches, I may need to unite their hashmap positionAndColor together to totalmap.
>
> 6. to accomplish getRaster(), for each (i + row, j + col), I will check whether it is contained in the totalmap, 

if contains, gives it the corresponding color, if not, gives it the background color if applicable.
>
> Memory consumption, each (key, value) pair, key 4 + 4 bytes, value 4 + 4 + 4 bytes, for the tested files I have 

now, the patch part is about 300 pixels x 300 pixels, so about 20 x 9 x 10^4 bytes ~ 2 MB, as using hashmap, I 

would say 6 MB will be used. For larger images, it maybe a problem, but very large images will create problems(both 

speed and memory) for the searching method as well.
>
> I think I need your opinion before I really start to code this method, as there is a risk to waste time before 

getting a decent result. My oponion is that it is worth to try if there is no better method.
>
> Best,
> Shaola

> Implement shading with Coons and tensor-product patch meshes
> ------------------------------------------------------------
>
>                 Key: PDFBOX-1915
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-1915
>             Project: PDFBox
>          Issue Type: Improvement
>          Components: Rendering
>    Affects Versions: 2.0.0
>            Reporter: Tilman Hausherr
>              Labels: graphical, gsoc2014, java, math, shading
>         Attachments: CONICAL.pdf, GWG060_Shading_x1a.pdf, HSBWHEEL.pdf, McAfee-ShadingType7.pdf, TENSOR.pdf, XYZsweep.pdf, asy-coons-but-really-tensor.pdf, asy-tensor-rainbow.pdf, asy-tensor.pdf, coons-function.pdf, coons-function.ps, coons-nofunction-CMYK.pdf, coons-nofunction-CMYK.ps, coons-nofunction-Duotone.pdf, coons-nofunction-Duotone.ps, coons-nofunction-Gray.pdf, coons-nofunction-Gray.ps, coons-nofunction-RGB.pdf, coons-nofunction-RGB.ps, coons2-function.pdf, coons2-function.ps, eci_altona-test-suite-v2_technical_H.pdf, lamp_cairo.pdf
>
>
> Of the seven shading methods described in the PDF specification, type 6 (Coons patch meshes) and type 7 (Tensor-product patch meshes) haven't been implemented. I have done type 1, 4 and 5, but I don't know the math for type 6 and 7. My math days are decades away.
> Knowledge prerequisites: 
> - java, although you don't have to be a java ace, just feel confortable
> - math: you should know what "cubic Bézier curves", "Degenerate Bézier curves", "bilinear interpolation", "tensor-product", "affine transform matrix" and "Bernstein polynomials" are, or be able to learn it
> - maven (basic)
> - svn (basic)
> - an IDE like Netbeans or Eclipse or IntelliJ (basic)
> - ideally, you are either a math student who likes to program, or a computer science student who is specializing in graphics.
> A first look at PDFBOX: try the command utility here:
> https://pdfbox.apache.org/commandline/#pdfToImage
> and use your favorite PDF, or the PDFs mentioned in PDFBOX-615, these have the shading types that are already implemented.
> Some simple source code to convert to images:
> String filename = "blah.pdf";
> PDDocument document = PDDocument.loadNonSeq(new File(filename), null);
> List<PDPage> pdPages = document.getDocumentCatalog().getAllPages();
> int page = 0;
> for (PDPage pdPage : pdPages)
> {
> ++page;
> BufferedImage bim = RenderUtil.convertToImage(pdPage, BufferedImage.TYPE_BYTE_BINARY, 300);
> ImageIO.write(bim, "png", new File(filename+page+".png"));
> }
> document.close();
> You are not starting from scratch. The implementation of type 4 and 5 shows you how to read parameters from the PDF and set the graphics. You don't have to learn the complete PDF spec, only 15 pages related to the two shading types, and 6 pages about shading in general. The PDF specification is here:
> http://www.adobe.com/devnet/pdf/pdf_reference.html
> The tricky parts are:
> - decide whether a point(x,y) is inside or outside a patch
> - decide the color of a point within the patch
> To get an idea about the code, look at the classes GouraudTriangle, GouraudShadingContext, Type4ShadingContext and Vertex here
> https://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/
> or download the whole project from the repository.
> https://pdfbox.apache.org/downloads.html#scm
> If you want to see the existing code in the debugger with a Gouraud shading, try this file:
> http://asymptote.sourceforge.net/gallery/Gouraud.pdf
> Testing:
> I have attached several example PDFs. To see which one has which shading, open them with an editor like NOTEPAD++, and search for "/ShadingType" (without the quotes). If your images are rendering like the example PDFs, then you were successful.
> Optional:
> Review and optimize the complete shading package for speed; implement cubic spline interpolation for type 0 (sampled) functions (that one is really low-low priority, see details by looking up "cubic spline interpolation" in the PDF spec, which tells that it is disregarded in printing, and I don't have a test PDF).
> Mentor: Tilman Hausherr (European timezone, languages: german, english, french)



--
This message was sent by Atlassian JIRA
(v6.2#6252)