3 * Handler for the Gimp's native file format (XCF)
6 * https://en.wikipedia.org/wiki/XCF_(file_format)
7 * Specification in Gnome repository:
8 * http://svn.gnome.org/viewvc/gimp/trunk/devel-docs/xcf.txt?view=markup
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
30 * Handler for the Gimp's native file format; getimagesize() doesn't
35 class XCFHandler
extends BitmapHandler
{
40 function mustRender( $file ) {
49 * @param array $params
52 function getThumbType( $ext, $mime, $params = null ) {
53 return array( 'png', 'image/png' );
57 * Get width and height from the XCF header.
60 * @param string $filename
63 function getImageSize( $image, $filename ) {
64 $header = self
::getXCFMetaData( $filename );
69 # Forge a return array containing metadata information just like getimagesize()
70 # See PHP documentation at: http://www.php.net/getimagesize
72 $metadata[0] = $header['width'];
73 $metadata[1] = $header['height'];
74 $metadata[2] = null; # IMAGETYPE constant, none exist for XCF.
75 $metadata[3] = sprintf(
76 'height="%s" width="%s"', $header['height'], $header['width']
78 $metadata['mime'] = 'image/x-xcf';
79 $metadata['channels'] = null;
80 $metadata['bits'] = 8; # Always 8-bits per color
82 assert( '7 == count($metadata); ' .
83 '# return array must contains 7 elements just like getimagesize() return' );
89 * Metadata for a given XCF file
91 * Will return false if file magic signature is not recognized
95 * @param string $filename Full path to a XCF file
96 * @return bool|array Metadata Array just like PHP getimagesize()
98 static function getXCFMetaData( $filename ) {
99 # Decode master structure
100 $f = fopen( $filename, 'rb' );
104 # The image structure always starts at offset 0 in the XCF file.
105 # So we just read it :-)
106 $binaryHeader = fread( $f, 26 );
110 * Master image structure:
112 * byte[9] "gimp xcf " File type magic
113 * byte[4] version XCF version
117 * byte 0 Zero-terminator for version tag
118 * uint32 width With of canvas
119 * uint32 height Height of canvas
120 * uint32 base_type Color mode of the image; one of
124 * (enum GimpImageBaseType in libgimpbase/gimpbaseenums.h)
128 "A9magic" . # A: space padded
129 "/a5version" . # a: zero padded
131 "/Nheight" . # N: unsigned long 32bit big endian
135 } catch ( Exception
$mwe ) {
140 if ( $header['magic'] !== 'gimp xcf' ) {
141 wfDebug( __METHOD__
. " '$filename' has invalid magic signature.\n" );
145 # TODO: we might want to check for sane values of width and height
147 wfDebug( __METHOD__
.
148 ": canvas size of '$filename' is {$header['width']} x {$header['height']} px\n" );
154 * Store the channel type
156 * Greyscale files need different command line options.
158 * @param File $file The image object, or false if there isn't one.
159 * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
160 * @param string $filename The filename
163 public function getMetadata( $file, $filename ) {
164 $header = self
::getXCFMetadata( $filename );
167 // Try to be consistent with the names used by PNG files.
168 // Unclear from base media type if it has an alpha layer,
169 // so just assume that it does since it "potentially" could.
170 switch ( $header['base_type'] ) {
172 $metadata['colorType'] = 'truecolour-alpha';
175 $metadata['colorType'] = 'greyscale-alpha';
178 $metadata['colorType'] = 'index-coloured';
181 $metadata['colorType'] = 'unknown';
185 // Marker to prevent repeated attempted extraction
186 $metadata['error'] = true;
188 return serialize( $metadata );
192 * Should we refresh the metadata
194 * @param File $file The file object for the file in question
195 * @param string $metadata Serialized metadata
196 * @return bool One of the self::METADATA_(BAD|GOOD|COMPATIBLE) constants
198 public function isMetadataValid( $file, $metadata ) {
200 // Old metadata when we just put an empty string in there
201 return self
::METADATA_BAD
;
203 return self
::METADATA_GOOD
;
208 * Must use "im" for XCF
210 * @param string $dstPath
211 * @param bool $checkDstPath
214 protected function getScalerType( $dstPath, $checkDstPath = true ) {
219 * Can we render this file?
221 * Image magick doesn't support indexed xcf files as of current
222 * writing (as of 6.8.9-3)
226 public function canRender( $file ) {
227 MediaWiki\
suppressWarnings();
228 $xcfMeta = unserialize( $file->getMetadata() );
229 MediaWiki\restoreWarnings
();
230 if ( isset( $xcfMeta['colorType'] ) && $xcfMeta['colorType'] === 'index-coloured' ) {
233 return parent
::canRender( $file );