OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /* Base class for image metadata parsers that only need to look at a short | |
6 fragment at the start of the file */ | |
7 function SimpleImageParser(parent, type, urlFilter, headerSize) { | |
8 ImageParser.call(this, parent, type, urlFilter); | |
9 this.headerSize = headerSize; | |
10 } | |
11 | |
12 SimpleImageParser.prototype = {__proto__: ImageParser.prototype}; | |
13 | |
14 SimpleImageParser.prototype.parse = function( | |
15 file, metadata, callback, errorCallback) { | |
16 var self = this; | |
17 util.readFileBytes(file, 0, this.headerSize, | |
18 function (file, br) { | |
19 try { | |
20 self.parseHeader(metadata, br); | |
21 callback(metadata); | |
22 } catch(e) { | |
23 errorCallback(e.toString()); | |
24 } | |
25 }, | |
26 errorCallback); | |
27 }; | |
28 | |
29 | |
30 function PngParser(parent) { | |
31 SimpleImageParser.call(this, parent, 'png', /\.png$/i, 24); | |
32 } | |
33 | |
34 PngParser.prototype = {__proto__: SimpleImageParser.prototype}; | |
35 | |
36 PngParser.prototype.parseHeader = function(metadata, br) { | |
37 br.setByteOrder(ByteReader.BIG_ENDIAN); | |
38 | |
39 var signature = br.readString(8); | |
40 if (signature != '\x89PNG\x0D\x0A\x1A\x0A') | |
41 throw new Error('Invalid PNG signature: ' + signature); | |
42 | |
43 br.seek(12); | |
44 var ihdr = br.readString(4); | |
45 if (ihdr != 'IHDR') | |
46 throw new Error('Missing IHDR chunk'); | |
47 | |
48 metadata.width = br.readScalar(4); | |
49 metadata.height = br.readScalar(4); | |
50 }; | |
51 | |
52 MetadataDispatcher.registerParserClass(PngParser); | |
53 | |
54 | |
55 function BmpParser(parent) { | |
56 SimpleImageParser.call(this, parent, 'bmp', /\.bmp$/i, 28); | |
57 } | |
58 | |
59 BmpParser.prototype = {__proto__: SimpleImageParser.prototype}; | |
60 | |
61 BmpParser.prototype.parseHeader = function(metadata, br) { | |
62 br.setByteOrder(ByteReader.LITTLE_ENDIAN); | |
63 | |
64 var signature = br.readString(2); | |
65 if (signature != 'BM') | |
66 throw new Error('Invalid BMP signature: ' + signature); | |
67 | |
68 br.seek(18); | |
69 metadata.width = br.readScalar(4); | |
70 metadata.height = br.readScalar(4); | |
71 }; | |
72 | |
73 MetadataDispatcher.registerParserClass(BmpParser); | |
74 | |
75 | |
76 function GifParser(parent) { | |
77 SimpleImageParser.call(this, parent, 'gif', /\.Gif$/i, 10); | |
78 } | |
79 | |
80 GifParser.prototype = {__proto__: SimpleImageParser.prototype}; | |
81 | |
82 GifParser.prototype.parseHeader = function(metadata, br) { | |
83 br.setByteOrder(ByteReader.LITTLE_ENDIAN); | |
84 | |
85 var signature = br.readString(6); | |
86 if (!signature.match(/GIF8(7|9)a/)) | |
87 throw new Error('Invalid GIF signature: ' + signature); | |
88 | |
89 metadata.width = br.readScalar(2); | |
90 metadata.height = br.readScalar(2); | |
91 }; | |
92 | |
93 MetadataDispatcher.registerParserClass(GifParser); | |
OLD | NEW |