Bug 42961

Summary: [PATCH] WMF renderer paints nothing for some WMF files
Product: Batik - Now in Jira Reporter: Trejkaz (pen name) <trejkaz>
Component: UtilitiesAssignee: Batik Developer's Mailing list <batik-dev>
Status: RESOLVED FIXED    
Severity: major Keywords: PatchAvailable
Priority: P2    
Version: 1.8   
Target Milestone: ---   
Hardware: Other   
OS: other   
Bug Depends on:    
Bug Blocks: 43194    
Attachments: not rendered.wmf
Patch fixes the issue for this file
Another WMF that still has a problem

Description Trejkaz (pen name) 2007-07-23 16:57:35 UTC
I'm attaching a file which doesn't render at all in the WMF renderer.  Other
tools (e.g. Paint) seem to work fine.
Comment 1 Trejkaz (pen name) 2007-07-23 16:59:17 UTC
Created attachment 20536 [details]
not rendered.wmf

Attaching sample file.

Code used to test:

    File file = new File("not rendered.wmf");
    WMFRecordStore store = new WMFRecordStore();
    store.read(new DataInputStream(new FileInputStream(file)));
    WMFPainter painter = new WMFPainter(store, 1.0f);
    BufferedImage image = new BufferedImage(store.getWidthPixels(),
					    store.getHeightPixels(),
					    BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    painter.paint(graphics);
    graphics.dispose();
    ImageIO.write(image, "png", new File("not rendered.png"));

Result is a PNG which is entirely transparent.
Comment 2 Trejkaz (pen name) 2007-07-23 18:01:56 UTC
Created attachment 20537 [details]
Patch fixes the issue for this file

Wow, I didn't think it would be this simple.

WMFRecordStore line 82 was exiting the loop when recSize <= 0.	In the WMF file
I attached here, there was a record of size exactly 0.	Changing the condidion
to recSize < 0 fixes the issue.
Comment 3 Trejkaz (pen name) 2007-07-23 18:49:03 UTC
Created attachment 20538 [details]
Another WMF that still has a problem

Patch solves about 95% of incidents but I found another eight that still fail. 
Here's one.
Comment 4 Trejkaz (pen name) 2007-07-23 19:00:03 UTC
It seems that the new file's problem is that it contains two unimplemented
functions, META_SETSTRETCHBLTMODE and META_STRETCHDIB.

Implementing these is out of my league... feel free to split this into a
different bug if it makes more sense to do that.  I suspect the remaining files
are similar because when I look at the files, they do all contain similar
characteristics (they all contain a small image scaled to be bigger.)
Comment 5 Trejkaz (pen name) 2007-07-23 20:48:13 UTC
LOL...

I only just realised this (because I do the diffs relative to our local branch)
that the previous fix is dependent on my fix to Bug 42479 which caused the
problem. :-)
Comment 6 Hervé Girod 2007-07-27 14:01:25 UTC
(In reply to comment #4)
> It seems that the new file's problem is that it contains two unimplemented
> functions, META_SETSTRETCHBLTMODE and META_STRETCHDIB.
> 
> Implementing these is out of my league... feel free to split this into a
> different bug if it makes more sense to do that.  I suspect the remaining files
> are similar because when I look at the files, they do all contain similar
> characteristics (they all contain a small image scaled to be bigger.)
> 

Does your last fix fix the problem with the new file ? I understand that no.
Comment 7 Trejkaz (pen name) 2007-07-27 23:01:31 UTC
No, my fix only solved the issue for the first file (which was in turn due to 
a fix for another bug, funnily enough.)  The second file has unimplemented 
commands in it.
Comment 8 Hervé Girod 2007-07-28 01:58:30 UTC
(In reply to comment #7)
> No, my fix only solved the issue for the first file (which was in turn due to 
> a fix for another bug, funnily enough.)  The second file has unimplemented 
> commands in it.

The second example was incorrectly defined as text, so it seems impossible to
retrieve it as a WMF file. I tried to change its type as octet/stream, but it
does not work. Can you resubmit it as stream ? ;-)
Comment 9 Trejkaz (pen name) 2007-07-29 16:06:03 UTC
Seems to be okay now, if I click on it, it tries to open in Paint.
Comment 10 Hervé Girod 2007-07-30 12:29:55 UTC
(In reply to comment #9)
> Seems to be okay now, if I click on it, it tries to open in Paint.
> 

I can't open it with Paint on my PC (contrary to the first example). It says
that it is not a valid file...
Comment 11 Trejkaz (pen name) 2007-07-30 23:49:34 UTC
Works fine here.  I don't even get a warning, it just opens quietly.  This is on
Vista, so it's possible some versions of Windows treat the file differently.
Comment 12 Hervé Girod 2007-08-02 07:22:32 UTC
(In reply to comment #11)
> Works fine here.  I don't even get a warning, it just opens quietly.  This is on
> Vista, so it's possible some versions of Windows treat the file differently.
> 

You're right, I was trying to open it on my old Windows Me, but it works fine
with my other XP box. However, I think this is a little more difficult here:
- It will be easy to cope with the two META_STRETCHDIB and
META_SETSTRETCHBLTMODE functions, because I have already done something like
that (stretching bitmaps) for another existing function before.
- But there is another problem to cope with: the file does not have an Aldus
Header, so it will also be necessary to decode the MAPMODE, which is not done at
the moment in the code (in WMFHeaderProperties and WMFRecordStore). Also the
AbstractWMFReader will need to be slightly modified to take care of this new
case (for the moment, it assumes that the Aldus header was present).

I think I will be able to propose a patch for the end of the month, if someone
has not done it before...
Comment 13 Hervé Girod 2007-08-02 07:24:15 UTC
(In reply to comment #12) 
> I think I will be able to propose a patch for the end of the month, if someone
> has not done it before...
> 

End of the month because I'm going to Japan for holidays tomorrow, returning
home the 17th of August ;-) 
Comment 14 Trejkaz (pen name) 2007-08-03 02:10:56 UTC
You're right, there is no APM header... we handle these by yanking the 
viewport size out and creating a fake APM header, which works for a lot of 
pictures but sometimes produces the wrong scaling. Implementing support for 
doing it properly will certainly add time, although those of us who currently 
work around it in a shoddy way will certainly thank you. :-)

And I envy you.  I had my two weeks in Japan almost a whole year ago, and I'm 
still pining for Akihabara.
Comment 15 Hervé Girod 2007-08-23 02:32:16 UTC
(In reply to comment #14)
> You're right, there is no APM header... we handle these by yanking the 
> viewport size out and creating a fake APM header, which works for a lot of 
> pictures but sometimes produces the wrong scaling. Implementing support for 
> doing it properly will certainly add time, although those of us who currently 
> work around it in a shoddy way will certainly thank you. :-)
> 
> And I envy you.  I had my two weeks in Japan almost a whole year ago, and I'm 
> still pining for Akihabara.
> 
OK, returned from japan (that was GREAT !!), and had time since my return a few
days ago to fix this. Since it is in my local copy, I need to sync before
sending the patch (I think it will be done before end of the week-end). I had to
fix 3 things to be able to see the image:
- implement META_STRETCHDIB
- implement META_MAPMODE (the file is in ANISOTROPIC mode)
- allow to read metafiles without APM

Comment 16 Hervé Girod 2007-08-23 02:40:34 UTC
I just created a new bug #43194, because i think this is some "big" patch, and
it is a slightly different issue from this one. I reused your second example for
this.
Comment 17 Hervé Girod 2007-08-26 05:47:58 UTC
(In reply to comment #16)
> I just created a new bug #43194, because i think this is some "big" patch, and
> it is a slightly different issue from this one. I reused your second example for
> this.

OK, patch uploaded for #43194, this should close this bug and the other.
Comment 18 Cameron McCormack 2007-09-18 19:12:52 UTC
Patch from bug #43194 applied.