From a4721d374fa743a2763627d807c75b5bd5eaf8cc Mon Sep 17 00:00:00 2001 From: ben Date: Mon, 23 Dec 2019 21:59:32 +0100 Subject: [PATCH 01/28] Fixes djpeg load test * Test fails with `libjpeg-turbo` and `libjpeg-progs` on Ubuntu 16.04 * Epsilon reported is 4.18... --- Tests/test_file_jpeg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 59eb7c641..23d931cd6 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -494,7 +494,7 @@ class TestFileJpeg(PillowTestCase): def test_load_djpeg(self): with Image.open(TEST_FILE) as img: img.load_djpeg() - assert_image_similar(img, Image.open(TEST_FILE), 0) + assert_image_similar(img, Image.open(TEST_FILE), 5) @unittest.skipUnless(cjpeg_available(), "cjpeg not available") def test_save_cjpeg(self): From 83125b5ae48f9782b960f1dc2856887d92aa3c5d Mon Sep 17 00:00:00 2001 From: Rodrigo Benenson Date: Tue, 28 Apr 2020 18:58:26 +0200 Subject: [PATCH 02/28] Checks over exif instead of reloeaded_exif test_imagefile.py checks exif instead of reloaded_exif; making the tests pass when they should not. --- Tests/test_imagefile.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 6964d3e00..805625a24 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -261,9 +261,9 @@ class TestPyDecoder: with Image.open(out) as reloaded: reloaded_exif = reloaded.getexif() assert reloaded_exif[258] == 8 - assert 40960 not in exif + assert 40960 not in reloaded_exif assert reloaded_exif[40963] == 455 - assert exif[11] == "Pillow test" + assert reloaded_exif[11] == "Pillow test" with Image.open("Tests/images/no-dpi-in-exif.jpg") as im: # Big endian exif = im.getexif() @@ -281,9 +281,9 @@ class TestPyDecoder: with Image.open(out) as reloaded: reloaded_exif = reloaded.getexif() assert reloaded_exif[258] == 8 - assert 40960 not in exif + assert 34665 not in reloaded_exif assert reloaded_exif[40963] == 455 - assert exif[305] == "Pillow test" + assert reloaded_exif[305] == "Pillow test" @skip_unless_feature("webp") @skip_unless_feature("webp_anim") @@ -302,7 +302,7 @@ class TestPyDecoder: reloaded_exif = reloaded.getexif() assert reloaded_exif[258] == 8 assert reloaded_exif[40963] == 455 - assert exif[305] == "Pillow test" + assert reloaded_exif[305] == "Pillow test" im.save(out, exif=exif) check_exif() From 456b36308a958de45235d689ad01cf792c8589d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=BC=C3=9Flein?= Date: Wed, 29 Apr 2020 11:49:33 +0200 Subject: [PATCH 03/28] Mention Ubuntu versions younger than 16.04 I suppose people will just try it either way but it's good to know that it still works with the same libs all the way through to 20.04 --- docs/installation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/installation.rst b/docs/installation.rst index ca7e25f9a..1da7fe772 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -328,7 +328,7 @@ In Fedora, the command is:: .. Note:: ``redhat-rpm-config`` is required on Fedora 23, but not earlier versions. -Prerequisites are installed on **Ubuntu 16.04 LTS** with:: +Prerequisites for **Ubuntu 16.04 LTS or later** are installed with:: sudo apt-get install libtiff5-dev libjpeg8-dev libopenjp2-7-dev zlib1g-dev \ libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk \ From cc39dbab0e219c4784fd8d03901cc8e6fda745e2 Mon Sep 17 00:00:00 2001 From: David Walker Date: Thu, 30 Apr 2020 23:25:45 -0700 Subject: [PATCH 04/28] Fix ImageChops documentation. Many methods were incorrectly documented as requriring mode "1". The remaining ones require *both* images to be mode "1". Documentation only, [ci skip] --- src/PIL/ImageChops.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/PIL/ImageChops.py b/src/PIL/ImageChops.py index 2d13b529f..904df484e 100644 --- a/src/PIL/ImageChops.py +++ b/src/PIL/ImageChops.py @@ -54,7 +54,7 @@ def invert(image): def lighter(image1, image2): """ Compares the two images, pixel by pixel, and returns a new image containing - the lighter values. At least one of the images must have mode "1". + the lighter values. .. code-block:: python @@ -71,7 +71,7 @@ def lighter(image1, image2): def darker(image1, image2): """ Compares the two images, pixel by pixel, and returns a new image containing - the darker values. At least one of the images must have mode "1". + the darker values. .. code-block:: python @@ -88,7 +88,7 @@ def darker(image1, image2): def difference(image1, image2): """ Returns the absolute value of the pixel-by-pixel difference between the two - images. At least one of the images must have mode "1". + images. .. code-block:: python @@ -107,8 +107,7 @@ def multiply(image1, image2): Superimposes two images on top of each other. If you multiply an image with a solid black image, the result is black. If - you multiply with a solid white image, the image is unaffected. At least - one of the images must have mode "1". + you multiply with a solid white image, the image is unaffected. .. code-block:: python @@ -124,8 +123,7 @@ def multiply(image1, image2): def screen(image1, image2): """ - Superimposes two inverted images on top of each other. At least one of the - images must have mode "1". + Superimposes two inverted images on top of each other. .. code-block:: python @@ -179,7 +177,6 @@ def add(image1, image2, scale=1.0, offset=0): """ Adds two images, dividing the result by scale and adding the offset. If omitted, scale defaults to 1.0, and offset to 0.0. - At least one of the images must have mode "1". .. code-block:: python @@ -196,8 +193,7 @@ def add(image1, image2, scale=1.0, offset=0): def subtract(image1, image2, scale=1.0, offset=0): """ Subtracts two images, dividing the result by scale and adding the offset. - If omitted, scale defaults to 1.0, and offset to 0.0. At least one of the - images must have mode "1". + If omitted, scale defaults to 1.0, and offset to 0.0. .. code-block:: python @@ -212,8 +208,7 @@ def subtract(image1, image2, scale=1.0, offset=0): def add_modulo(image1, image2): - """Add two images, without clipping the result. At least one of the images - must have mode "1". + """Add two images, without clipping the result. .. code-block:: python @@ -228,8 +223,7 @@ def add_modulo(image1, image2): def subtract_modulo(image1, image2): - """Subtract two images, without clipping the result. At least one of the - images must have mode "1". + """Subtract two images, without clipping the result. .. code-block:: python @@ -244,8 +238,10 @@ def subtract_modulo(image1, image2): def logical_and(image1, image2): - """Logical AND between two images. At least one of the images must have - mode "1". + """Logical AND between two images. + + Both of the images must have mode "1". For an AND in RGB mode, use a + multiply() by a black-and-white mask. .. code-block:: python @@ -260,8 +256,9 @@ def logical_and(image1, image2): def logical_or(image1, image2): - """Logical OR between two images. At least one of the images must have - mode "1". + """Logical OR between two images. + + Both of the images must have mode "1". .. code-block:: python @@ -276,8 +273,9 @@ def logical_or(image1, image2): def logical_xor(image1, image2): - """Logical XOR between two images. At least one of the images must have - mode "1". + """Logical XOR between two images. + + Both of the images must have mode "1". .. code-block:: python From d5c38146916703b21d922f02469e19b3621c467b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 1 May 2020 19:41:51 +1000 Subject: [PATCH 05/28] Changed default offset for Exif --- src/PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 2b5e3b6f0..0c8b42a09 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3281,7 +3281,7 @@ class Exif(MutableMapping): self._data.update(ifd) self._ifds[0x8769] = ifd - def tobytes(self, offset=0): + def tobytes(self, offset=8): from . import TiffImagePlugin if self.endian == "<": From 94c7af7596e984a40a9bb7ea373ac336e55a9d36 Mon Sep 17 00:00:00 2001 From: Hugo Date: Fri, 1 May 2020 21:23:39 +0300 Subject: [PATCH 06/28] Replace spaces with tabs and add to pre-commit linting --- .pre-commit-config.yaml | 6 ++ CHANGES.rst | 58 +++++++++---------- depends/install_extra_test_images.sh | 12 ++-- .../writing-your-own-file-decoder.rst | 4 +- docs/reference/Image.rst | 4 +- docs/reference/ImageCms.rst | 10 ++-- 6 files changed, 50 insertions(+), 44 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e30453c3e..c3939a003 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,3 +30,9 @@ repos: hooks: - id: check-merge-conflict - id: check-yaml + + - repo: https://github.com/Lucas-C/pre-commit-hooks + rev: v1.1.7 + hooks: + - id: remove-tabs + exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.opt$) diff --git a/CHANGES.rst b/CHANGES.rst index 2b3141ff3..6ac9af439 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5310,23 +5310,23 @@ Pre-fork + Added keyword options to the "save" method. The following options are currently supported: - format option description + Format Option Description -------------------------------------------------------- - JPEG optimize minimize output file at the - expense of compression speed. + JPEG optimize Minimize output file at the + expense of compression speed. - JPEG progressive enable progressive output. the - option value is ignored. + JPEG progressive Enable progressive output. + The option value is ignored. - JPEG quality set compression quality (1-100). - the default value is 75. + JPEG quality Set compression quality (1-100). + The default value is 75. - JPEG smooth smooth dithered images. value - is strength (1-100). default is - off (0). + JPEG smooth Smooth dithered images. + Value is strength (1-100). + Default is off (0). - PNG optimize minimize output file at the - expense of compression speed. + PNG optimize Minimize output file at the + expense of compression speed. Expect more options in future releases. Also note that file writers silently ignore unknown options. @@ -5347,31 +5347,31 @@ Pre-fork + Various improvements to the sample scripts: "pilconvert" Carries out some extra tricks in order to make - the resulting file as small as possible. + the resulting file as small as possible. - "explode" (NEW) Split an image sequence into individual frames. + "explode" (NEW) Split an image sequence into individual frames. - "gifmaker" (NEW) Convert a sequence file into a GIF animation. - Note that the GIF encoder create "uncompressed" GIF - files, so animations created by this script are - rather large (typically 2-5 times the compressed - sizes). + "gifmaker" (NEW) Convert a sequence file into a GIF animation. + Note that the GIF encoder create "uncompressed" GIF + files, so animations created by this script are + rather large (typically 2-5 times the compressed + sizes). - "image2py" (NEW) Convert a single image to a python module. See - comments in this script for details. + "image2py" (NEW) Convert a single image to a python module. See + comments in this script for details. - "player" If multiple images are given on the command line, - they are interpreted as frames in a sequence. The - script assumes that they all have the same size. - Also note that this script now can play FLI/FLC - and GIF animations. + "player" If multiple images are given on the command line, + they are interpreted as frames in a sequence. The + script assumes that they all have the same size. + Also note that this script now can play FLI/FLC + and GIF animations. This player can also execute embedded Python animation applets (ARG format only). - "viewer" Transparent images ("P" with transparency property, - and "RGBA") are superimposed on the standard Tk back- - ground. + "viewer" Transparent images ("P" with transparency property, + and "RGBA") are superimposed on the standard Tk back- + ground. + Fixed colour argument to "new". For multilayer images, pass a tuple: (Red, Green, Blue), (Red, Green, Blue, Alpha), or (Cyan, diff --git a/depends/install_extra_test_images.sh b/depends/install_extra_test_images.sh index 36af34b54..02da12d61 100755 --- a/depends/install_extra_test_images.sh +++ b/depends/install_extra_test_images.sh @@ -4,12 +4,12 @@ # Use SVN to just fetch a single Git subdirectory svn_export() { - if [ ! -z $1 ]; then - echo "" - echo "Retrying svn export..." - echo "" - fi + if [ ! -z $1 ]; then + echo "" + echo "Retrying svn export..." + echo "" + fi - svn export --force https://github.com/python-pillow/pillow-depends/trunk/test_images ../Tests/images + svn export --force https://github.com/python-pillow/pillow-depends/trunk/test_images ../Tests/images } svn_export || svn_export retry || svn_export retry || svn_export retry diff --git a/docs/handbook/writing-your-own-file-decoder.rst b/docs/handbook/writing-your-own-file-decoder.rst index 58e2bccc5..24e46ff00 100644 --- a/docs/handbook/writing-your-own-file-decoder.rst +++ b/docs/handbook/writing-your-own-file-decoder.rst @@ -179,7 +179,7 @@ complete list, see the table in the :py:mod:`Unpack.c` module. The following table describes some commonly used **raw modes**: +-----------+-----------------------------------------------------------------+ -| mode | description | +| mode | description | +===========+=================================================================+ | ``1`` | 1-bit bilevel, stored with the leftmost pixel in the most | | | significant bit. 0 means black, 1 means white. | @@ -223,7 +223,7 @@ You can use the ``raw`` decoder to read images where data is packed in any standard machine data type, using one of the following raw modes: ============ ======================================= -mode description +mode description ============ ======================================= ``F`` 32-bit native floating point. ``F;8`` 8-bit unsigned integer. diff --git a/docs/reference/Image.rst b/docs/reference/Image.rst index c824ff176..216fa1196 100644 --- a/docs/reference/Image.rst +++ b/docs/reference/Image.rst @@ -58,8 +58,8 @@ Functions ``warnings.simplefilter('ignore', Image.DecompressionBombWarning)``. See also `the logging documentation`_ to have warnings output to the logging facility instead of stderr. - .. _decompression bombs: https://en.wikipedia.org/wiki/Zip_bomb - .. _the logging documentation: https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module + .. _decompression bombs: https://en.wikipedia.org/wiki/Zip_bomb + .. _the logging documentation: https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module Image processing ^^^^^^^^^^^^^^^^ diff --git a/docs/reference/ImageCms.rst b/docs/reference/ImageCms.rst index 922e1685a..67c581765 100644 --- a/docs/reference/ImageCms.rst +++ b/docs/reference/ImageCms.rst @@ -413,10 +413,10 @@ can be easily displayed in a chromaticity diagram, for example). with :py:attr:`.intent_supported`. :param intent: One of ``ImageCms.INTENT_ABSOLUTE_COLORIMETRIC``, - ``ImageCms.INTENT_PERCEPTUAL``, - ``ImageCms.INTENT_RELATIVE_COLORIMETRIC`` - and ``ImageCms.INTENT_SATURATION``. + ``ImageCms.INTENT_PERCEPTUAL``, + ``ImageCms.INTENT_RELATIVE_COLORIMETRIC`` + and ``ImageCms.INTENT_SATURATION``. :param direction: One of ``ImageCms.DIRECTION_INPUT``, - ``ImageCms.DIRECTION_OUTPUT`` - and ``ImageCms.DIRECTION_PROOF`` + ``ImageCms.DIRECTION_OUTPUT`` + and ``ImageCms.DIRECTION_PROOF`` :return: Boolean if the intent and direction is supported. From d23df7227c419a010c4a82599856f83abd2633b5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 2 May 2020 10:24:26 +1000 Subject: [PATCH 07/28] Updated CHANGES.rst [ci skip] --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6ac9af439..03f9cf28f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,9 @@ Changelog (Pillow) 7.2.0 (unreleased) ------------------ +- Fixes default offset for Exif #4594 + [rodrigob, radarhere] + - Fixed bug when unpickling TIFF images #4565 [radarhere] From f15e4a8e0614c05b63b9e1b84103ed75101695ba Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 8 May 2020 19:24:38 +0300 Subject: [PATCH 08/28] truncate icclist instead of changing to None --- Tests/images/icc-after-SOF.jpg | Bin 0 -> 212 bytes Tests/test_file_jpeg.py | 4 ++++ src/PIL/JpegImagePlugin.py | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Tests/images/icc-after-SOF.jpg diff --git a/Tests/images/icc-after-SOF.jpg b/Tests/images/icc-after-SOF.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a284a2298dc67db0094155b7680c24c96eadf7a9 GIT binary patch literal 212 zcmex=vcDq>wPbh^&;noUD|LjDm`ux`L99vW$#|xu%YRp^1r! zyt<{Wg^`V(v5C?D0}R|83|tIcjEsT||Bo<;f-Po11{}CuaD+ib0O&+i@c$MA2gvpI L4D%1v|Gx Date: Sat, 9 May 2020 20:17:14 +1000 Subject: [PATCH 09/28] Updated Freetype to 2.10.2 --- .github/workflows/test-windows.yml | 2 +- winbuild/config.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml index adbe97968..d45dc9467 100644 --- a/.github/workflows/test-windows.yml +++ b/.github/workflows/test-windows.yml @@ -170,7 +170,7 @@ jobs: set INCLUDE=C:\Program Files (x86)\Microsoft SDKs\Windows\V7.1A\Include set INCLIB=%GITHUB_WORKSPACE%\winbuild\depends\msvcr10-x32 set BUILD=%GITHUB_WORKSPACE%\winbuild\build - cd /D %BUILD%\freetype-2.10.1 + cd /D %BUILD%\freetype-2.10.2 call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" ${{ matrix.platform-vcvars }} echo on rmdir /S /Q objs diff --git a/winbuild/config.py b/winbuild/config.py index 93413d1e5..6d512945c 100644 --- a/winbuild/config.py +++ b/winbuild/config.py @@ -40,9 +40,9 @@ libs = { "dir": "tiff-4.1.0", }, "freetype": { - "url": "https://download.savannah.gnu.org/releases/freetype/freetype-2.10.1.tar.gz", # noqa: E501 - "filename": "freetype-2.10.1.tar.gz", - "dir": "freetype-2.10.1", + "url": "https://download.savannah.gnu.org/releases/freetype/freetype-2.10.2.tar.gz", # noqa: E501 + "filename": "freetype-2.10.2.tar.gz", + "dir": "freetype-2.10.2", }, "lcms-2.7": { "url": SF_MIRROR + "/project/lcms/lcms/2.7/lcms2-2.7.zip", From f1c66a0ba5cdf94c324dde0a14912023f988f7e8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 10 May 2020 10:50:20 +1000 Subject: [PATCH 10/28] Updated CHANGES.rst [ci skip] --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 03f9cf28f..f12a321c9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,9 @@ Changelog (Pillow) 7.2.0 (unreleased) ------------------ +- JPEG: Truncate icclist instead of setting to None #4613 + [homm] + - Fixes default offset for Exif #4594 [rodrigob, radarhere] From c1d9931adccd0962eeeffb41bcdedf76e501d854 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 10 May 2020 19:56:36 +1000 Subject: [PATCH 11/28] Added braces --- src/Tk/tkImaging.c | 11 +- src/_imaging.c | 567 +++++++++++++++++++++----------- src/_imagingcms.c | 106 +++--- src/_imagingft.c | 117 ++++--- src/_imagingmath.c | 20 +- src/_imagingmorph.c | 6 +- src/_imagingtk.c | 7 +- src/_webp.c | 21 +- src/decode.c | 168 ++++++---- src/display.c | 134 +++++--- src/encode.c | 156 +++++---- src/libImaging/Access.c | 11 +- src/libImaging/AlphaComposite.c | 9 +- src/libImaging/Bands.c | 45 ++- src/libImaging/BcnDecode.c | 20 +- src/libImaging/BitDecode.c | 34 +- src/libImaging/Blend.c | 24 +- src/libImaging/BoxBlur.c | 15 +- src/libImaging/Chops.c | 19 +- src/libImaging/Convert.c | 181 ++++++---- src/libImaging/Copy.c | 14 +- src/libImaging/Crop.c | 9 +- src/libImaging/Dib.c | 43 ++- src/libImaging/Draw.c | 193 ++++++----- src/libImaging/Effects.c | 20 +- src/libImaging/EpsEncode.c | 9 +- src/libImaging/Except.c | 3 +- src/libImaging/File.c | 9 +- src/libImaging/Fill.c | 18 +- src/libImaging/Filter.c | 46 ++- src/libImaging/FliDecode.c | 30 +- src/libImaging/Geometry.c | 207 +++++++----- src/libImaging/GetBBox.c | 69 ++-- src/libImaging/GifDecode.c | 12 +- src/libImaging/GifEncode.c | 38 ++- src/libImaging/HexDecode.c | 3 +- src/libImaging/Histo.c | 58 ++-- src/libImaging/Jpeg2KDecode.c | 109 +++--- src/libImaging/Jpeg2KEncode.c | 51 ++- src/libImaging/JpegDecode.c | 44 ++- src/libImaging/JpegEncode.c | 30 +- src/libImaging/Matrix.c | 12 +- src/libImaging/ModeFilter.c | 23 +- src/libImaging/Negative.c | 12 +- src/libImaging/Offset.c | 20 +- src/libImaging/Pack.c | 58 ++-- src/libImaging/PackDecode.c | 9 +- src/libImaging/Palette.c | 45 ++- src/libImaging/Paste.c | 51 ++- src/libImaging/PcdDecode.c | 9 +- src/libImaging/PcxDecode.c | 6 +- src/libImaging/Point.c | 41 ++- src/libImaging/Quant.c | 193 ++++++++--- src/libImaging/QuantHash.c | 20 +- src/libImaging/QuantHeap.c | 16 +- src/libImaging/QuantOctree.c | 59 +++- src/libImaging/QuantPngQuant.c | 12 +- src/libImaging/RankFilter.c | 45 ++- src/libImaging/RawDecode.c | 9 +- src/libImaging/RawEncode.c | 6 +- src/libImaging/Reduce.c | 6 +- src/libImaging/Resample.c | 63 ++-- src/libImaging/SgiRleDecode.c | 18 +- src/libImaging/Storage.c | 30 +- src/libImaging/SunRleDecode.c | 9 +- src/libImaging/TgaRleDecode.c | 19 +- src/libImaging/TgaRleEncode.c | 33 +- src/libImaging/Unpack.c | 12 +- src/libImaging/UnsharpMask.c | 9 +- src/libImaging/XbmDecode.c | 12 +- src/libImaging/ZipDecode.c | 28 +- src/libImaging/ZipEncode.c | 17 +- src/map.c | 81 +++-- src/outline.c | 27 +- src/path.c | 123 ++++--- 75 files changed, 2505 insertions(+), 1314 deletions(-) diff --git a/src/Tk/tkImaging.c b/src/Tk/tkImaging.c index 59801f58e..161a835fb 100644 --- a/src/Tk/tkImaging.c +++ b/src/Tk/tkImaging.c @@ -68,8 +68,9 @@ ImagingFind(const char* name) #else id = atol(name); #endif - if (!id) + if (!id) { return NULL; + } return (Imaging) id; } @@ -119,10 +120,11 @@ PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp, block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; - if (strcmp(im->mode, "RGBA") == 0) + if (strcmp(im->mode, "RGBA") == 0) { block.offset[3] = 3; /* alpha (or reserved, under 8.2) */ - else + } else { block.offset[3] = 0; /* no alpha */ + } } else { TCL_APPEND_RESULT(interp, "Bad mode", (char*) NULL); return TCL_ERROR; @@ -136,10 +138,11 @@ PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp, if (TK_LT_85) { /* Tk 8.4 */ TK_PHOTO_PUT_BLOCK_84(photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET); - if (strcmp(im->mode, "RGBA") == 0) + if (strcmp(im->mode, "RGBA") == 0) { /* Tk workaround: we need apply ToggleComplexAlphaIfNeeded */ /* (fixed in Tk 8.5a3) */ TK_PHOTO_SET_SIZE_84(photo, block.width, block.height); + } } else { /* Tk >=8.5 */ TK_PHOTO_PUT_BLOCK_85(interp, photo, &block, 0, 0, block.width, diff --git a/src/_imaging.c b/src/_imaging.c index 9a9a51f71..f0ba22040 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -170,8 +170,9 @@ PyImagingNew(Imaging imOut) { ImagingObject* imagep; - if (!imOut) + if (!imOut) { return NULL; + } imagep = PyObject_New(ImagingObject, &Imaging_Type); if (imagep == NULL) { @@ -197,8 +198,9 @@ _dealloc(ImagingObject* imagep) printf("imaging %p deleted\n", imagep); #endif - if (imagep->access) + if (imagep->access) { ImagingAccessDelete(imagep->image, imagep->access); + } ImagingDelete(imagep->image); PyObject_Del(imagep); } @@ -322,8 +324,9 @@ getbands(const char* mode) /* FIXME: add primitive to libImaging to avoid extra allocation */ im = ImagingNew(mode, 0, 0); - if (!im) + if (!im) { return -1; + } bands = im->bands; @@ -373,8 +376,9 @@ getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type) /* malloc check ok, type & ff is just a sizeof(something) calloc checks for overflow */ list = calloc(n, type & 0xff); - if ( ! list) + if ( ! list) { return PyErr_NoMemory(); + } seq = PySequence_Fast(arg, must_be_sequence); if ( ! seq) { @@ -413,8 +417,9 @@ getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type) return NULL; } - if (length) + if (length) { *length = n; + } return list; } @@ -546,12 +551,14 @@ getink(PyObject* color, Imaging im, char* ink) r = (UINT8) r; } else { if (im->bands == 2) { - if (!PyArg_ParseTuple(color, "L|i", &r, &a)) + if (!PyArg_ParseTuple(color, "L|i", &r, &a)) { return NULL; + } g = b = r; } else { - if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) + if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) { return NULL; + } } } ink[0] = (char) CLIP8(r); @@ -562,23 +569,26 @@ getink(PyObject* color, Imaging im, char* ink) return ink; case IMAGING_TYPE_INT32: /* signed integer */ - if (rIsInt != 1) + if (rIsInt != 1) { return NULL; + } itmp = r; memcpy(ink, &itmp, sizeof(itmp)); return ink; case IMAGING_TYPE_FLOAT32: /* floating point */ f = PyFloat_AsDouble(color); - if (f == -1.0 && PyErr_Occurred()) + if (f == -1.0 && PyErr_Occurred()) { return NULL; + } ftmp = f; memcpy(ink, &ftmp, sizeof(ftmp)); return ink; case IMAGING_TYPE_SPECIAL: if (strncmp(im->mode, "I;16", 4) == 0) { - if (rIsInt != 1) + if (rIsInt != 1) { return NULL; + } ink[0] = (UINT8) r; ink[1] = (UINT8) (r >> 8); ink[2] = ink[3] = 0; @@ -606,12 +616,14 @@ _fill(PyObject* self, PyObject* args) xsize = ysize = 256; color = NULL; - if (!PyArg_ParseTuple(args, "s|(ii)O", &mode, &xsize, &ysize, &color)) + if (!PyArg_ParseTuple(args, "s|(ii)O", &mode, &xsize, &ysize, &color)) { return NULL; + } im = ImagingNewDirty(mode, xsize, ysize); - if (!im) + if (!im) { return NULL; + } buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0; if (color) { @@ -633,8 +645,9 @@ _new(PyObject* self, PyObject* args) char* mode; int xsize, ysize; - if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) + if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) { return NULL; + } return PyImagingNew(ImagingNew(mode, xsize, ysize)); } @@ -645,8 +658,9 @@ _new_block(PyObject* self, PyObject* args) char* mode; int xsize, ysize; - if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) + if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) { return NULL; + } return PyImagingNew(ImagingNewBlock(mode, xsize, ysize)); } @@ -656,8 +670,9 @@ _linear_gradient(PyObject* self, PyObject* args) { char* mode; - if (!PyArg_ParseTuple(args, "s", &mode)) + if (!PyArg_ParseTuple(args, "s", &mode)) { return NULL; + } return PyImagingNew(ImagingFillLinearGradient(mode)); } @@ -667,8 +682,9 @@ _radial_gradient(PyObject* self, PyObject* args) { char* mode; - if (!PyArg_ParseTuple(args, "s", &mode)) + if (!PyArg_ParseTuple(args, "s", &mode)) { return NULL; + } return PyImagingNew(ImagingFillRadialGradient(mode)); } @@ -681,8 +697,9 @@ _alpha_composite(ImagingObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "O!O!", &Imaging_Type, &imagep1, - &Imaging_Type, &imagep2)) + &Imaging_Type, &imagep2)) { return NULL; + } return PyImagingNew(ImagingAlphaComposite(imagep1->image, imagep2->image)); } @@ -698,8 +715,9 @@ _blend(ImagingObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "O!O!|d", &Imaging_Type, &imagep1, &Imaging_Type, &imagep2, - &alpha)) + &alpha)) { return NULL; + } return PyImagingNew(ImagingBlend(imagep1->image, imagep2->image, (float) alpha)); @@ -762,8 +780,9 @@ _prepare_lut_table(PyObject* table, Py_ssize_t table_size) /* malloc check ok, max is 2 * 4 * 65**3 = 2197000 */ prepared = (INT16*) malloc(sizeof(INT16) * table_size); if ( ! prepared) { - if (free_table_data) + if (free_table_data) { free(table_data); + } return (INT16*) PyErr_NoMemory(); } @@ -880,8 +899,9 @@ _convert(ImagingObject* self, PyObject* args) int dither = 0; ImagingObject *paletteimage = NULL; - if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage)) + if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage)) { return NULL; + } if (paletteimage != NULL) { if (!PyImaging_Check(paletteimage)) { PyObject_Print((PyObject *)paletteimage, stderr, 0); @@ -904,11 +924,13 @@ _convert2(ImagingObject* self, PyObject* args) ImagingObject* imagep2; if (!PyArg_ParseTuple(args, "O!O!", &Imaging_Type, &imagep1, - &Imaging_Type, &imagep2)) + &Imaging_Type, &imagep2)) { return NULL; + } - if (!ImagingConvert2(imagep1->image, imagep2->image)) + if (!ImagingConvert2(imagep1->image, imagep2->image)) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -950,8 +972,9 @@ _convert_transparent(ImagingObject* self, PyObject* args) static PyObject* _copy(ImagingObject* self, PyObject* args) { - if (!PyArg_ParseTuple(args, "")) + if (!PyArg_ParseTuple(args, "")) { return NULL; + } return PyImagingNew(ImagingCopy(self->image)); } @@ -960,8 +983,9 @@ static PyObject* _crop(ImagingObject* self, PyObject* args) { int x0, y0, x1, y1; - if (!PyArg_ParseTuple(args, "(iiii)", &x0, &y0, &x1, &y1)) + if (!PyArg_ParseTuple(args, "(iiii)", &x0, &y0, &x1, &y1)) { return NULL; + } return PyImagingNew(ImagingCrop(self->image, x0, y0, x1, y1)); } @@ -971,8 +995,9 @@ _expand_image(ImagingObject* self, PyObject* args) { int x, y; int mode = 0; - if (!PyArg_ParseTuple(args, "ii|i", &x, &y, &mode)) + if (!PyArg_ParseTuple(args, "ii|i", &x, &y, &mode)) { return NULL; + } return PyImagingNew(ImagingExpand(self->image, x, y, mode)); } @@ -988,13 +1013,15 @@ _filter(ImagingObject* self, PyObject* args) float divisor, offset; PyObject* kernel = NULL; if (!PyArg_ParseTuple(args, "(ii)ffO", &xsize, &ysize, - &divisor, &offset, &kernel)) + &divisor, &offset, &kernel)) { return NULL; + } /* get user-defined kernel */ kerneldata = getlist(kernel, &kernelsize, NULL, TYPE_FLOAT32); - if (!kerneldata) + if (!kerneldata) { return NULL; + } if (kernelsize != (Py_ssize_t) xsize * (Py_ssize_t) ysize) { free(kerneldata); return ImagingError_ValueError("bad kernel size"); @@ -1022,13 +1049,15 @@ _gaussian_blur(ImagingObject* self, PyObject* args) float radius = 0; int passes = 3; - if (!PyArg_ParseTuple(args, "f|i", &radius, &passes)) + if (!PyArg_ParseTuple(args, "f|i", &radius, &passes)) { return NULL; + } imIn = self->image; imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } if (!ImagingGaussianBlur(imOut, imIn, radius, passes)) { ImagingDelete(imOut); @@ -1049,8 +1078,9 @@ _getpalette(ImagingObject* self, PyObject* args) char* mode = "RGB"; char* rawmode = "RGB"; - if (!PyArg_ParseTuple(args, "|ss", &mode, &rawmode)) + if (!PyArg_ParseTuple(args, "|ss", &mode, &rawmode)) { return NULL; + } if (!self->image->palette) { PyErr_SetString(PyExc_ValueError, no_palette); @@ -1064,8 +1094,9 @@ _getpalette(ImagingObject* self, PyObject* args) } palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8); - if (!palette) + if (!palette) { return NULL; + } pack((UINT8*) PyBytes_AsString(palette), self->image->palette->palette, palettesize); @@ -1089,24 +1120,27 @@ _getxy(PyObject* xy, int* x, int *y) { PyObject* value; - if (!PyTuple_Check(xy) || PyTuple_GET_SIZE(xy) != 2) + if (!PyTuple_Check(xy) || PyTuple_GET_SIZE(xy) != 2) { goto badarg; + } value = PyTuple_GET_ITEM(xy, 0); - if (PyLong_Check(value)) + if (PyLong_Check(value)) { *x = PyLong_AS_LONG(value); - else if (PyFloat_Check(value)) + } else if (PyFloat_Check(value)) { *x = (int) PyFloat_AS_DOUBLE(value); - else + } else { goto badval; + } value = PyTuple_GET_ITEM(xy, 1); - if (PyLong_Check(value)) + if (PyLong_Check(value)) { *y = PyLong_AS_LONG(value); - else if (PyFloat_Check(value)) + } else if (PyFloat_Check(value)) { *y = (int) PyFloat_AS_DOUBLE(value); - else + } else { goto badval; + } return 0; @@ -1141,8 +1175,9 @@ _getpixel(ImagingObject* self, PyObject* args) xy = PyTuple_GET_ITEM(args, 0); - if (_getxy(xy, &x, &y)) + if (_getxy(xy, &x, &y)) { return NULL; + } if (self->access == NULL) { Py_INCREF(Py_None); @@ -1168,20 +1203,23 @@ parse_histogram_extremap(ImagingObject* self, PyObject* extremap, if (extremap) { switch (self->image->type) { case IMAGING_TYPE_UINT8: - if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1)) + if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1)) { return NULL; + } ep->u[0] = CLIP8(i0); ep->u[1] = CLIP8(i1); break; case IMAGING_TYPE_INT32: - if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1)) + if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1)) { return NULL; + } ep->i[0] = i0; ep->i[1] = i1; break; case IMAGING_TYPE_FLOAT32: - if (!PyArg_ParseTuple(extremap, "dd", &f0, &f1)) + if (!PyArg_ParseTuple(extremap, "dd", &f0, &f1)) { return NULL; + } ep->f[0] = (FLOAT32) f0; ep->f[1] = (FLOAT32) f1; break; @@ -1205,15 +1243,17 @@ _histogram(ImagingObject* self, PyObject* args) PyObject* extremap = NULL; ImagingObject* maskp = NULL; - if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp)) + if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp)) { return NULL; + } /* Using a var to avoid allocations. */ ep = parse_histogram_extremap(self, extremap, &extrema); h = ImagingGetHistogram(self->image, (maskp) ? maskp->image : NULL, ep); - if (!h) + if (!h) { return NULL; + } /* Build an integer list containing the histogram */ list = PyList_New(h->bands * 256); @@ -1246,15 +1286,17 @@ _entropy(ImagingObject* self, PyObject* args) PyObject* extremap = NULL; ImagingObject* maskp = NULL; - if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp)) + if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp)) { return NULL; + } /* Using a local var to avoid allocations. */ ep = parse_histogram_extremap(self, extremap, &extrema); h = ImagingGetHistogram(self->image, (maskp) ? maskp->image : NULL, ep); - if (!h) + if (!h) { return NULL; + } /* Calculate the histogram entropy */ /* First, sum the histogram data */ @@ -1286,8 +1328,9 @@ static PyObject* _modefilter(ImagingObject* self, PyObject* args) { int size; - if (!PyArg_ParseTuple(args, "i", &size)) + if (!PyArg_ParseTuple(args, "i", &size)) { return NULL; + } return PyImagingNew(ImagingModeFilter(self->image, size)); } @@ -1297,8 +1340,9 @@ static PyObject* _offset(ImagingObject* self, PyObject* args) { int xoffset, yoffset; - if (!PyArg_ParseTuple(args, "ii", &xoffset, &yoffset)) + if (!PyArg_ParseTuple(args, "ii", &xoffset, &yoffset)) { return NULL; + } return PyImagingNew(ImagingOffset(self->image, xoffset, yoffset)); } @@ -1315,19 +1359,21 @@ _paste(ImagingObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "O(iiii)|O!", &source, &x0, &y0, &x1, &y1, - &Imaging_Type, &maskp)) - return NULL; + &Imaging_Type, &maskp)) { + return NULL; + } - if (PyImaging_Check(source)) + if (PyImaging_Check(source)) { status = ImagingPaste( self->image, PyImaging_AsImaging(source), (maskp) ? maskp->image : NULL, x0, y0, x1, y1 ); - else { - if (!getink(source, self->image, ink)) + } else { + if (!getink(source, self->image, ink)) { return NULL; + } status = ImagingFill2( self->image, ink, (maskp) ? maskp->image : NULL, @@ -1335,8 +1381,9 @@ _paste(ImagingObject* self, PyObject* args) ); } - if (status < 0) + if (status < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -1353,8 +1400,9 @@ _point(ImagingObject* self, PyObject* args) PyObject* list; char* mode; - if (!PyArg_ParseTuple(args, "Oz", &list, &mode)) - return NULL; + if (!PyArg_ParseTuple(args, "Oz", &list, &mode)) { + return NULL; + } if (mode && !strcmp(mode, "F")) { FLOAT32* data; @@ -1362,8 +1410,9 @@ _point(ImagingObject* self, PyObject* args) /* map from 8-bit data to floating point */ n = 256; data = getlist(list, &n, wrong_number, TYPE_FLOAT32); - if (!data) + if (!data) { return NULL; + } im = ImagingPoint(self->image, mode, (void*) data); free(data); @@ -1374,8 +1423,9 @@ _point(ImagingObject* self, PyObject* args) /* FIXME: support arbitrary number of entries (requires API change) */ n = 65536; data = getlist(list, &n, wrong_number, TYPE_UINT8); - if (!data) + if (!data) { return NULL; + } im = ImagingPoint(self->image, mode, (void*) data); free(data); @@ -1385,20 +1435,23 @@ _point(ImagingObject* self, PyObject* args) if (mode) { bands = getbands(mode); - if (bands < 0) + if (bands < 0) { return NULL; - } else + } + } else { bands = self->image->bands; + } /* map to integer data */ n = 256 * bands; data = getlist(list, &n, wrong_number, TYPE_INT32); - if (!data) + if (!data) { return NULL; + } - if (mode && !strcmp(mode, "I")) + if (mode && !strcmp(mode, "I")) { im = ImagingPoint(self->image, mode, (void*) data); - else if (mode && bands > 1) { + } else if (mode && bands > 1) { for (i = 0; i < 256; i++) { lut[i*4] = CLIP8(data[i]); lut[i*4+1] = CLIP8(data[i+256]); @@ -1409,8 +1462,9 @@ _point(ImagingObject* self, PyObject* args) im = ImagingPoint(self->image, mode, (void*) lut); } else { /* map individual bands */ - for (i = 0; i < n; i++) + for (i = 0; i < n; i++) { lut[i] = CLIP8(data[i]); + } im = ImagingPoint(self->image, mode, (void*) lut); } free(data); @@ -1424,8 +1478,9 @@ _point_transform(ImagingObject* self, PyObject* args) { double scale = 1.0; double offset = 0.0; - if (!PyArg_ParseTuple(args, "|dd", &scale, &offset)) - return NULL; + if (!PyArg_ParseTuple(args, "|dd", &scale, &offset)) { + return NULL; + } return PyImagingNew(ImagingPointTransform(self->image, scale, offset)); } @@ -1443,8 +1498,9 @@ _putdata(ImagingObject* self, PyObject* args) double scale = 1.0; double offset = 0.0; - if (!PyArg_ParseTuple(args, "O|dd", &data, &scale, &offset)) + if (!PyArg_ParseTuple(args, "O|dd", &data, &scale, &offset)) { return NULL; + } if (!PySequence_Check(data)) { PyErr_SetString(PyExc_TypeError, must_be_sequence); @@ -1463,7 +1519,7 @@ _putdata(ImagingObject* self, PyObject* args) if (PyBytes_Check(data)) { unsigned char* p; p = (unsigned char*) PyBytes_AS_STRING(data); - if (scale == 1.0 && offset == 0.0) + if (scale == 1.0 && offset == 0.0) { /* Plain string data */ for (i = y = 0; i < n; i += image->xsize, y++) { x = n - i; @@ -1471,13 +1527,14 @@ _putdata(ImagingObject* self, PyObject* args) x = image->xsize; memcpy(image->image8[y], p+i, x); } - else + } else { /* Scaled and clipped string data */ for (i = x = y = 0; i < n; i++) { image->image8[y][x] = CLIP8((int) (p[i] * scale + offset)); if (++x >= (int) image->xsize) x = 0, y++; } + } } else { seq = PySequence_Fast(data, must_be_sequence); if (!seq) { @@ -1576,8 +1633,9 @@ _quantize(ImagingObject* self, PyObject* args) int colours = 256; int method = 0; int kmeans = 0; - if (!PyArg_ParseTuple(args, "|iii", &colours, &method, &kmeans)) + if (!PyArg_ParseTuple(args, "|iii", &colours, &method, &kmeans)) { return NULL; + } if (!self->image->xsize || !self->image->ysize) { /* no content; return an empty image */ @@ -1599,8 +1657,9 @@ _putpalette(ImagingObject* self, PyObject* args) char* rawmode; UINT8* palette; Py_ssize_t palettesize; - if (!PyArg_ParseTuple(args, "sy#", &rawmode, &palette, &palettesize)) + if (!PyArg_ParseTuple(args, "sy#", &rawmode, &palette, &palettesize)) { return NULL; + } if (strcmp(self->image->mode, "L") && strcmp(self->image->mode, "LA") && strcmp(self->image->mode, "P") && strcmp(self->image->mode, "PA")) { @@ -1636,8 +1695,9 @@ _putpalettealpha(ImagingObject* self, PyObject* args) { int index; int alpha = 0; - if (!PyArg_ParseTuple(args, "i|i", &index, &alpha)) + if (!PyArg_ParseTuple(args, "i|i", &index, &alpha)) { return NULL; + } if (!self->image->palette) { PyErr_SetString(PyExc_ValueError, no_palette); @@ -1662,8 +1722,9 @@ _putpalettealphas(ImagingObject* self, PyObject* args) int i; UINT8 *values; Py_ssize_t length; - if (!PyArg_ParseTuple(args, "y#", &values, &length)) + if (!PyArg_ParseTuple(args, "y#", &values, &length)) { return NULL; + } if (!self->image->palette) { PyErr_SetString(PyExc_ValueError, no_palette); @@ -1692,8 +1753,9 @@ _putpixel(ImagingObject* self, PyObject* args) int x, y; PyObject* color; - if (!PyArg_ParseTuple(args, "(ii)O", &x, &y, &color)) + if (!PyArg_ParseTuple(args, "(ii)O", &x, &y, &color)) { return NULL; + } im = self->image; @@ -1709,11 +1771,13 @@ _putpixel(ImagingObject* self, PyObject* args) return NULL; } - if (!getink(color, im, ink)) + if (!getink(color, im, ink)) { return NULL; + } - if (self->access) + if (self->access) { self->access->put_pixel(im, x, y, ink); + } Py_INCREF(Py_None); return Py_None; @@ -1724,8 +1788,9 @@ static PyObject* _rankfilter(ImagingObject* self, PyObject* args) { int size, rank; - if (!PyArg_ParseTuple(args, "ii", &size, &rank)) + if (!PyArg_ParseTuple(args, "ii", &size, &rank)) { return NULL; + } return PyImagingNew(ImagingRankFilter(self->image, size, rank)); } @@ -1746,8 +1811,9 @@ _resize(ImagingObject* self, PyObject* args) box[3] = imIn->ysize; if (!PyArg_ParseTuple(args, "(ii)|i(ffff)", &xsize, &ysize, &filter, - &box[0], &box[1], &box[2], &box[3])) + &box[0], &box[1], &box[2], &box[3])) { return NULL; + } if (xsize < 1 || ysize < 1) { return ImagingError_ValueError("height and width must be > 0"); @@ -1807,8 +1873,9 @@ _reduce(ImagingObject* self, PyObject* args) box[3] = imIn->ysize; if (!PyArg_ParseTuple(args, "(ii)|(iiii)", &xscale, &yscale, - &box[0], &box[1], &box[2], &box[3])) + &box[0], &box[1], &box[2], &box[3])) { return NULL; + } if (xscale < 1 || yscale < 1) { return ImagingError_ValueError("scale must be > 0"); @@ -1851,8 +1918,9 @@ im_setmode(ImagingObject* self, PyObject* args) char* mode; Py_ssize_t modelen; - if (!PyArg_ParseTuple(args, "s#:setmode", &mode, &modelen)) + if (!PyArg_ParseTuple(args, "s#:setmode", &mode, &modelen)) { return NULL; + } im = self->image; @@ -1872,8 +1940,9 @@ im_setmode(ImagingObject* self, PyObject* args) return NULL; } - if (self->access) + if (self->access) { ImagingAccessDelete(im, self->access); + } self->access = ImagingAccessNew(im); Py_INCREF(Py_None); @@ -1900,8 +1969,9 @@ _transform2(ImagingObject* self, PyObject* args) &x0, &y0, &x1, &y1, &Imaging_Type, &imagep, &method, &data, - &filter, &fill)) - return NULL; + &filter, &fill)) { + return NULL; + } switch (method) { case IMAGING_TRANSFORM_AFFINE: @@ -1918,8 +1988,9 @@ _transform2(ImagingObject* self, PyObject* args) } a = getlist(data, &n, wrong_number, TYPE_DOUBLE); - if (!a) + if (!a) { return NULL; + } imOut = ImagingTransform( self->image, imagep->image, method, @@ -1927,8 +1998,9 @@ _transform2(ImagingObject* self, PyObject* args) free(a); - if (!imOut) + if (!imOut) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -1941,8 +2013,9 @@ _transpose(ImagingObject* self, PyObject* args) Imaging imOut; int op; - if (!PyArg_ParseTuple(args, "i", &op)) - return NULL; + if (!PyArg_ParseTuple(args, "i", &op)) { + return NULL; + } imIn = self->image; @@ -1963,7 +2036,7 @@ _transpose(ImagingObject* self, PyObject* args) return NULL; } - if (imOut) + if (imOut) { switch (op) { case 0: (void) ImagingFlipLeftRight(imOut, imIn); @@ -1987,6 +2060,7 @@ _transpose(ImagingObject* self, PyObject* args) (void) ImagingTransverse(imOut, imIn); break; } + } return PyImagingNew(imOut); } @@ -2000,16 +2074,19 @@ _unsharp_mask(ImagingObject* self, PyObject* args) float radius; int percent, threshold; - if (!PyArg_ParseTuple(args, "fii", &radius, &percent, &threshold)) + if (!PyArg_ParseTuple(args, "fii", &radius, &percent, &threshold)) { return NULL; + } imIn = self->image; imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } - if (!ImagingUnsharpMask(imOut, imIn, radius, percent, threshold)) + if (!ImagingUnsharpMask(imOut, imIn, radius, percent, threshold)) { return NULL; + } return PyImagingNew(imOut); } @@ -2023,13 +2100,15 @@ _box_blur(ImagingObject* self, PyObject* args) float radius; int n = 1; - if (!PyArg_ParseTuple(args, "f|i", &radius, &n)) + if (!PyArg_ParseTuple(args, "f|i", &radius, &n)) { return NULL; + } imIn = self->image; imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } if (!ImagingBoxBlur(imOut, imIn, radius, n)) { ImagingDelete(imOut); @@ -2067,12 +2146,14 @@ _getcolors(ImagingObject* self, PyObject* args) PyObject* out; int maxcolors = 256; - if (!PyArg_ParseTuple(args, "i:getcolors", &maxcolors)) + if (!PyArg_ParseTuple(args, "i:getcolors", &maxcolors)) { return NULL; + } items = ImagingGetColors(self->image, maxcolors, &colors); - if (!items) + if (!items) { return NULL; + } if (colors > maxcolors) { out = Py_None; @@ -2105,10 +2186,11 @@ _getextrema(ImagingObject* self, PyObject* args) int status; status = ImagingGetExtrema(self->image, &extrema); - if (status < 0) + if (status < 0) { return NULL; + } - if (status) + if (status) { switch (self->image->type) { case IMAGING_TYPE_UINT8: return Py_BuildValue("BB", extrema.u[0], extrema.u[1]); @@ -2121,6 +2203,7 @@ _getextrema(ImagingObject* self, PyObject* args) return Py_BuildValue("HH", extrema.s[0], extrema.s[1]); } } + } Py_INCREF(Py_None); return Py_None; @@ -2162,8 +2245,9 @@ _getband(ImagingObject* self, PyObject* args) { int band; - if (!PyArg_ParseTuple(args, "i", &band)) + if (!PyArg_ParseTuple(args, "i", &band)) { return NULL; + } return PyImagingNew(ImagingGetBand(self->image, band)); } @@ -2174,11 +2258,13 @@ _fillband(ImagingObject* self, PyObject* args) int band; int color; - if (!PyArg_ParseTuple(args, "ii", &band, &color)) + if (!PyArg_ParseTuple(args, "ii", &band, &color)) { return NULL; + } - if (!ImagingFillBand(self->image, band, color)) + if (!ImagingFillBand(self->image, band, color)) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2191,11 +2277,13 @@ _putband(ImagingObject* self, PyObject* args) int band; if (!PyArg_ParseTuple(args, "O!i", &Imaging_Type, &imagep, - &band)) + &band)) { return NULL; + } - if (!ImagingPutBand(self->image, imagep->image, band)) + if (!ImagingPutBand(self->image, imagep->image, band)) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2213,13 +2301,22 @@ _merge(PyObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "sO!|O!O!O!", &mode, &Imaging_Type, &band0, &Imaging_Type, &band1, - &Imaging_Type, &band2, &Imaging_Type, &band3)) + &Imaging_Type, &band2, &Imaging_Type, &band3)) { return NULL; + } - if (band0) bands[0] = band0->image; - if (band1) bands[1] = band1->image; - if (band2) bands[2] = band2->image; - if (band3) bands[3] = band3->image; + if (band0) { + bands[0] = band0->image; + } + if (band1) { + bands[1] = band1->image; + } + if (band2) { + bands[2] = band2->image; + } + if (band3) { + bands[3] = band3->image; + } return PyImagingNew(ImagingMerge(mode, bands)); } @@ -2233,14 +2330,16 @@ _split(ImagingObject* self, PyObject* args) PyObject* imaging_object; Imaging bands[4] = {NULL, NULL, NULL, NULL}; - if ( ! ImagingSplit(self->image, bands)) + if ( ! ImagingSplit(self->image, bands)) { return NULL; + } list = PyTuple_New(self->image->bands); for (i = 0; i < self->image->bands; i++) { imaging_object = PyImagingNew(bands[i]); - if ( ! imaging_object) + if ( ! imaging_object) { fails += 1; + } PyTuple_SET_ITEM(list, i, imaging_object); } if (fails) { @@ -2265,8 +2364,9 @@ _chop_lighter(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopLighter(self->image, imagep->image)); } @@ -2276,8 +2376,9 @@ _chop_darker(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopDarker(self->image, imagep->image)); } @@ -2287,8 +2388,9 @@ _chop_difference(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopDifference(self->image, imagep->image)); } @@ -2298,8 +2400,9 @@ _chop_multiply(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopMultiply(self->image, imagep->image)); } @@ -2309,8 +2412,9 @@ _chop_screen(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopScreen(self->image, imagep->image)); } @@ -2326,8 +2430,9 @@ _chop_add(ImagingObject* self, PyObject* args) offset = 0; if (!PyArg_ParseTuple(args, "O!|fi", &Imaging_Type, &imagep, - &scale, &offset)) + &scale, &offset)) { return NULL; + } return PyImagingNew(ImagingChopAdd(self->image, imagep->image, scale, offset)); @@ -2344,8 +2449,9 @@ _chop_subtract(ImagingObject* self, PyObject* args) offset = 0; if (!PyArg_ParseTuple(args, "O!|fi", &Imaging_Type, &imagep, - &scale, &offset)) + &scale, &offset)) { return NULL; + } return PyImagingNew(ImagingChopSubtract(self->image, imagep->image, scale, offset)); @@ -2356,8 +2462,9 @@ _chop_and(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopAnd(self->image, imagep->image)); } @@ -2367,8 +2474,9 @@ _chop_or(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopOr(self->image, imagep->image)); } @@ -2378,8 +2486,9 @@ _chop_xor(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopXor(self->image, imagep->image)); } @@ -2389,8 +2498,9 @@ _chop_add_modulo(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopAddModulo(self->image, imagep->image)); } @@ -2400,8 +2510,9 @@ _chop_subtract_modulo(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopSubtractModulo(self->image, imagep->image)); } @@ -2411,8 +2522,9 @@ _chop_soft_light(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopSoftLight(self->image, imagep->image)); } @@ -2422,8 +2534,9 @@ _chop_hard_light(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingChopHardLight(self->image, imagep->image)); } @@ -2433,8 +2546,9 @@ _chop_overlay(ImagingObject* self, PyObject* args) { ImagingObject* imagep; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; + } return PyImagingNew(ImagingOverlay(self->image, imagep->image)); } @@ -2457,8 +2571,9 @@ _font_new(PyObject* self_, PyObject* args) Py_ssize_t glyphdata_length; if (!PyArg_ParseTuple(args, "O!y#", &Imaging_Type, &imagep, - &glyphdata, &glyphdata_length)) + &glyphdata, &glyphdata_length)) { return NULL; + } if (glyphdata_length != 256 * 20) { PyErr_SetString(PyExc_ValueError, wrong_length); @@ -2466,8 +2581,9 @@ _font_new(PyObject* self_, PyObject* args) } self = PyObject_New(ImagingFontObject, &ImagingFont_Type); - if (self == NULL) + if (self == NULL) { return NULL; + } /* glyph bitmap */ self->bitmap = imagep->image; @@ -2486,10 +2602,12 @@ _font_new(PyObject* self_, PyObject* args) self->glyphs[i].sy0 = S16(B16(glyphdata, 14)); self->glyphs[i].sx1 = S16(B16(glyphdata, 16)); self->glyphs[i].sy1 = S16(B16(glyphdata, 18)); - if (self->glyphs[i].dy0 < y0) + if (self->glyphs[i].dy0 < y0) { y0 = self->glyphs[i].dy0; - if (self->glyphs[i].dy1 > y1) + } + if (self->glyphs[i].dy1 > y1) { y1 = self->glyphs[i].dy1; + } glyphdata += 20; } @@ -2515,8 +2633,9 @@ textwidth(ImagingFontObject* self, const unsigned char* text) { int xsize; - for (xsize = 0; *text; text++) + for (xsize = 0; *text; text++) { xsize += self->glyphs[*text].dx; + } return xsize; } @@ -2595,15 +2714,17 @@ _font_getmask(ImagingFontObject* self, PyObject* args) self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1 ); - if (!bitmap) + if (!bitmap) { goto failed; + } status = ImagingPaste( im, bitmap, NULL, glyph->dx0+x, glyph->dy0+b, glyph->dx1+x, glyph->dy1+b ); ImagingDelete(bitmap); - if (status < 0) + if (status < 0) { goto failed; + } x = x + glyph->dx; b = b + glyph->dy; } @@ -2623,8 +2744,9 @@ _font_getsize(ImagingFontObject* self, PyObject* args) PyObject* encoded_string; PyObject* val; - if (!PyArg_ParseTuple(args, "O:getsize", &encoded_string)) + if (!PyArg_ParseTuple(args, "O:getsize", &encoded_string)) { return NULL; + } _font_text_asBytes(encoded_string, &text); if (!text) { @@ -2651,12 +2773,14 @@ _draw_new(PyObject* self_, PyObject* args) ImagingObject* imagep; int blend = 0; - if (!PyArg_ParseTuple(args, "O!|i", &Imaging_Type, &imagep, &blend)) + if (!PyArg_ParseTuple(args, "O!|i", &Imaging_Type, &imagep, &blend)) { return NULL; + } self = PyObject_New(ImagingDrawObject, &ImagingDraw_Type); - if (self == NULL) + if (self == NULL) { return NULL; + } /* keep a reference to the image object */ Py_INCREF(imagep); @@ -2683,11 +2807,13 @@ _draw_ink(ImagingDrawObject* self, PyObject* args) { INT32 ink = 0; PyObject* color; - if (!PyArg_ParseTuple(args, "O", &color)) + if (!PyArg_ParseTuple(args, "O", &color)) { return NULL; + } - if (!getink(color, self->image->image, (char*) &ink)) + if (!getink(color, self->image->image, (char*) &ink)) { return NULL; + } return PyLong_FromLong((int) ink); } @@ -2703,12 +2829,14 @@ _draw_arc(ImagingDrawObject* self, PyObject* args) int width = 0; float start, end; int op = 0; - if (!PyArg_ParseTuple(args, "Offi|ii", &data, &start, &end, &ink, &width)) + if (!PyArg_ParseTuple(args, "Offi|ii", &data, &start, &end, &ink, &width)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n != 2) { PyErr_SetString(PyExc_TypeError, must_be_two_coordinates); free(xy); @@ -2723,8 +2851,9 @@ _draw_arc(ImagingDrawObject* self, PyObject* args) free(xy); - if (n < 0) + if (n < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2739,12 +2868,14 @@ _draw_bitmap(ImagingDrawObject* self, PyObject* args) PyObject *data; ImagingObject* bitmap; int ink; - if (!PyArg_ParseTuple(args, "OO!i", &data, &Imaging_Type, &bitmap, &ink)) + if (!PyArg_ParseTuple(args, "OO!i", &data, &Imaging_Type, &bitmap, &ink)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n != 1) { PyErr_SetString(PyExc_TypeError, "coordinate list must contain exactly 1 coordinate" @@ -2760,8 +2891,9 @@ _draw_bitmap(ImagingDrawObject* self, PyObject* args) free(xy); - if (n < 0) + if (n < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2778,12 +2910,14 @@ _draw_chord(ImagingDrawObject* self, PyObject* args) int width = 0; float start, end; if (!PyArg_ParseTuple(args, "Offii|i", - &data, &start, &end, &ink, &fill, &width)) + &data, &start, &end, &ink, &fill, &width)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n != 2) { PyErr_SetString(PyExc_TypeError, must_be_two_coordinates); free(xy); @@ -2798,8 +2932,9 @@ _draw_chord(ImagingDrawObject* self, PyObject* args) free(xy); - if (n < 0) + if (n < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2815,12 +2950,14 @@ _draw_ellipse(ImagingDrawObject* self, PyObject* args) int ink; int fill = 0; int width = 0; - if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) + if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n != 2) { PyErr_SetString(PyExc_TypeError, must_be_two_coordinates); free(xy); @@ -2835,8 +2972,9 @@ _draw_ellipse(ImagingDrawObject* self, PyObject* args) free(xy); - if (n < 0) + if (n < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2851,12 +2989,14 @@ _draw_lines(ImagingDrawObject* self, PyObject* args) PyObject *data; int ink; int width = 0; - if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &width)) + if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &width)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (width <= 1) { double *p = NULL; @@ -2870,12 +3010,13 @@ _draw_lines(ImagingDrawObject* self, PyObject* args) return NULL; } } - if (p) /* draw last point */ + if (p) {/* draw last point */ ImagingDrawPoint( self->image->image, (int) p[2], (int) p[3], &ink, self->blend ); + } } else { for (i = 0; i < n-1; i++) { double *p = &xy[i+i]; @@ -2903,12 +3044,14 @@ _draw_points(ImagingDrawObject* self, PyObject* args) PyObject *data; int ink; - if (!PyArg_ParseTuple(args, "Oi", &data, &ink)) + if (!PyArg_ParseTuple(args, "Oi", &data, &ink)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } for (i = 0; i < n; i++) { double *p = &xy[i+i]; @@ -2938,8 +3081,9 @@ _draw_outline(ImagingDrawObject* self, PyObject* args) PyObject* outline_; int ink; int fill = 0; - if (!PyArg_ParseTuple(args, "Oi|i", &outline_, &ink, &fill)) + if (!PyArg_ParseTuple(args, "Oi|i", &outline_, &ink, &fill)) { return NULL; + } outline = PyOutline_AsOutline(outline_); if (!outline) { @@ -2948,8 +3092,9 @@ _draw_outline(ImagingDrawObject* self, PyObject* args) } if (ImagingDrawOutline(self->image->image, outline, - &ink, fill, self->blend) < 0) - return NULL; + &ink, fill, self->blend) < 0) { + return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -2967,12 +3112,14 @@ _draw_pieslice(ImagingDrawObject* self, PyObject* args) int ink, fill; int width = 0; float start, end; - if (!PyArg_ParseTuple(args, "Offii|i", &data, &start, &end, &ink, &fill, &width)) + if (!PyArg_ParseTuple(args, "Offii|i", &data, &start, &end, &ink, &fill, &width)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n != 2) { PyErr_SetString(PyExc_TypeError, must_be_two_coordinates); free(xy); @@ -2987,8 +3134,9 @@ _draw_pieslice(ImagingDrawObject* self, PyObject* args) free(xy); - if (n < 0) + if (n < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -3004,12 +3152,14 @@ _draw_polygon(ImagingDrawObject* self, PyObject* args) PyObject* data; int ink; int fill = 0; - if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &fill)) + if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &fill)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n < 2) { PyErr_SetString(PyExc_TypeError, "coordinate list must contain at least 2 coordinates" @@ -3050,12 +3200,14 @@ _draw_rectangle(ImagingDrawObject* self, PyObject* args) int ink; int fill = 0; int width = 0; - if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) + if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) { return NULL; + } n = PyPath_Flatten(data, &xy); - if (n < 0) + if (n < 0) { return NULL; + } if (n != 2) { PyErr_SetString(PyExc_TypeError, must_be_two_coordinates); free(xy); @@ -3070,8 +3222,9 @@ _draw_rectangle(ImagingDrawObject* self, PyObject* args) free(xy); - if (n < 0) + if (n < 0) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -3106,12 +3259,14 @@ pixel_access_new(ImagingObject* imagep, PyObject* args) PixelAccessObject *self; int readonly = 0; - if (!PyArg_ParseTuple(args, "|i", &readonly)) + if (!PyArg_ParseTuple(args, "|i", &readonly)) { return NULL; + } self = PyObject_New(PixelAccessObject, &PixelAccess_Type); - if (self == NULL) + if (self == NULL) { return NULL; + } /* keep a reference to the image object */ Py_INCREF(imagep); @@ -3133,8 +3288,9 @@ static PyObject * pixel_access_getitem(PixelAccessObject *self, PyObject *xy) { int x, y; - if (_getxy(xy, &x, &y)) + if (_getxy(xy, &x, &y)) { return NULL; + } return getpixel(self->image->image, self->image->access, x, y); } @@ -3151,8 +3307,9 @@ pixel_access_setitem(PixelAccessObject *self, PyObject *xy, PyObject *color) return -1; } - if (_getxy(xy, &x, &y)) + if (_getxy(xy, &x, &y)) { return -1; + } if (x < 0) { x = im->xsize + x; @@ -3166,11 +3323,13 @@ pixel_access_setitem(PixelAccessObject *self, PyObject *xy, PyObject *color) return -1; } - if (!color) /* FIXME: raise exception? */ + if (!color) {/* FIXME: raise exception? */ return 0; + } - if (!getink(color, im, ink)) + if (!getink(color, im, ink)) { return -1; + } self->image->access->put_pixel(im, x, y, ink); @@ -3196,8 +3355,9 @@ _effect_mandelbrot(ImagingObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "|(ii)(dddd)i", &xsize, &ysize, &extent[0], &extent[1], &extent[2], &extent[3], - &quality)) - return NULL; + &quality)) { + return NULL; + } return PyImagingNew(ImagingEffectMandelbrot(xsize, ysize, extent, quality)); } @@ -3207,8 +3367,9 @@ _effect_noise(ImagingObject* self, PyObject* args) { int xsize, ysize; float sigma = 128; - if (!PyArg_ParseTuple(args, "(ii)|f", &xsize, &ysize, &sigma)) + if (!PyArg_ParseTuple(args, "(ii)|f", &xsize, &ysize, &sigma)) { return NULL; + } return PyImagingNew(ImagingEffectNoise(xsize, ysize, sigma)); } @@ -3218,8 +3379,9 @@ _effect_spread(ImagingObject* self, PyObject* args) { int dist; - if (!PyArg_ParseTuple(args, "i", &dist)) + if (!PyArg_ParseTuple(args, "i", &dist)) { return NULL; + } return PyImagingNew(ImagingEffectSpread(self->image, dist)); } @@ -3237,8 +3399,9 @@ _getcodecstatus(PyObject* self, PyObject* args) int status; char* msg; - if (!PyArg_ParseTuple(args, "i", &status)) + if (!PyArg_ParseTuple(args, "i", &status)) { return NULL; + } switch (status) { case IMAGING_CODEC_OVERRUN: @@ -3268,11 +3431,13 @@ _save_ppm(ImagingObject* self, PyObject* args) { char* filename; - if (!PyArg_ParseTuple(args, "s", &filename)) + if (!PyArg_ParseTuple(args, "s", &filename)) { return NULL; + } - if (!ImagingSavePPM(self->image, filename)) + if (!ImagingSavePPM(self->image, filename)) { return NULL; + } Py_INCREF(Py_None); return Py_None; @@ -3457,8 +3622,9 @@ image_item(ImagingObject *self, Py_ssize_t i) if (im->xsize > 0) { x = i % im->xsize; y = i / im->xsize; - } else + } else { x = y = 0; /* leave it to getpixel to raise an exception */ + } return getpixel(im, self->access, x, y); } @@ -3614,12 +3780,14 @@ _get_stats(PyObject* self, PyObject* args) PyObject* d; ImagingMemoryArena arena = &ImagingDefaultArena; - if (!PyArg_ParseTuple(args, ":get_stats")) + if (!PyArg_ParseTuple(args, ":get_stats")) { return NULL; + } d = PyDict_New(); - if ( ! d) + if ( ! d) { return NULL; + } PyDict_SetItemString(d, "new_count", PyLong_FromLong(arena->stats_new_count)); PyDict_SetItemString(d, "allocated_blocks", @@ -3640,8 +3808,9 @@ _reset_stats(PyObject* self, PyObject* args) { ImagingMemoryArena arena = &ImagingDefaultArena; - if (!PyArg_ParseTuple(args, ":reset_stats")) + if (!PyArg_ParseTuple(args, ":reset_stats")) { return NULL; + } arena->stats_new_count = 0; arena->stats_allocated_blocks = 0; @@ -3656,8 +3825,9 @@ _reset_stats(PyObject* self, PyObject* args) static PyObject* _get_alignment(PyObject* self, PyObject* args) { - if (!PyArg_ParseTuple(args, ":get_alignment")) + if (!PyArg_ParseTuple(args, ":get_alignment")) { return NULL; + } return PyLong_FromLong(ImagingDefaultArena.alignment); } @@ -3665,8 +3835,9 @@ _get_alignment(PyObject* self, PyObject* args) static PyObject* _get_block_size(PyObject* self, PyObject* args) { - if (!PyArg_ParseTuple(args, ":get_block_size")) + if (!PyArg_ParseTuple(args, ":get_block_size")) { return NULL; + } return PyLong_FromLong(ImagingDefaultArena.block_size); } @@ -3674,8 +3845,9 @@ _get_block_size(PyObject* self, PyObject* args) static PyObject* _get_blocks_max(PyObject* self, PyObject* args) { - if (!PyArg_ParseTuple(args, ":get_blocks_max")) + if (!PyArg_ParseTuple(args, ":get_blocks_max")) { return NULL; + } return PyLong_FromLong(ImagingDefaultArena.blocks_max); } @@ -3684,8 +3856,9 @@ static PyObject* _set_alignment(PyObject* self, PyObject* args) { int alignment; - if (!PyArg_ParseTuple(args, "i:set_alignment", &alignment)) + if (!PyArg_ParseTuple(args, "i:set_alignment", &alignment)) { return NULL; + } if (alignment < 1 || alignment > 128) { PyErr_SetString(PyExc_ValueError, "alignment should be from 1 to 128"); @@ -3707,8 +3880,9 @@ static PyObject* _set_block_size(PyObject* self, PyObject* args) { int block_size; - if (!PyArg_ParseTuple(args, "i:set_block_size", &block_size)) + if (!PyArg_ParseTuple(args, "i:set_block_size", &block_size)) { return NULL; + } if (block_size <= 0) { PyErr_SetString(PyExc_ValueError, @@ -3732,8 +3906,9 @@ static PyObject* _set_blocks_max(PyObject* self, PyObject* args) { int blocks_max; - if (!PyArg_ParseTuple(args, "i:set_blocks_max", &blocks_max)) + if (!PyArg_ParseTuple(args, "i:set_blocks_max", &blocks_max)) { return NULL; + } if (blocks_max < 0) { PyErr_SetString(PyExc_ValueError, @@ -3761,8 +3936,9 @@ _clear_cache(PyObject* self, PyObject* args) { int i = 0; - if (!PyArg_ParseTuple(args, "|i:clear_cache", &i)) + if (!PyArg_ParseTuple(args, "|i:clear_cache", &i)) { return NULL; + } ImagingMemoryClearCache(&ImagingDefaultArena, i); @@ -3951,18 +4127,22 @@ setup_module(PyObject* m) { const char* version = (char*)PILLOW_VERSION; /* Ready object types */ - if (PyType_Ready(&Imaging_Type) < 0) + if (PyType_Ready(&Imaging_Type) < 0) { return -1; + } #ifdef WITH_IMAGEDRAW - if (PyType_Ready(&ImagingFont_Type) < 0) + if (PyType_Ready(&ImagingFont_Type) < 0) { return -1; + } - if (PyType_Ready(&ImagingDraw_Type) < 0) + if (PyType_Ready(&ImagingDraw_Type) < 0) { return -1; + } #endif - if (PyType_Ready(&PixelAccess_Type) < 0) + if (PyType_Ready(&PixelAccess_Type) < 0) { return -1; + } ImagingAccessInit(); @@ -4046,8 +4226,9 @@ PyInit__imaging(void) { m = PyModule_Create(&module_def); - if (setup_module(m) < 0) + if (setup_module(m) < 0) { return NULL; + } return m; } diff --git a/src/_imagingcms.c b/src/_imagingcms.c index 0f287013b..60b6b7228 100644 --- a/src/_imagingcms.c +++ b/src/_imagingcms.c @@ -88,8 +88,9 @@ cms_profile_new(cmsHPROFILE profile) CmsProfileObject* self; self = PyObject_New(CmsProfileObject, &CmsProfile_Type); - if (!self) + if (!self) { return NULL; + } self->profile = profile; @@ -102,8 +103,9 @@ cms_profile_open(PyObject* self, PyObject* args) cmsHPROFILE hProfile; char* sProfile; - if (!PyArg_ParseTuple(args, "s:profile_open", &sProfile)) + if (!PyArg_ParseTuple(args, "s:profile_open", &sProfile)) { return NULL; + } hProfile = cmsOpenProfileFromFile(sProfile, "r"); if (!hProfile) { @@ -121,8 +123,9 @@ cms_profile_fromstring(PyObject* self, PyObject* args) char* pProfile; Py_ssize_t nProfile; - if (!PyArg_ParseTuple(args, "y#:profile_frombytes", &pProfile, &nProfile)) + if (!PyArg_ParseTuple(args, "y#:profile_frombytes", &pProfile, &nProfile)) { return NULL; + } hProfile = cmsOpenProfileFromMem(pProfile, nProfile); if (!hProfile) { @@ -198,8 +201,9 @@ cms_transform_new(cmsHTRANSFORM transform, char* mode_in, char* mode_out) CmsTransformObject* self; self = PyObject_New(CmsTransformObject, &CmsTransform_Type); - if (!self) + if (!self) { return NULL; + } self->transform = transform; @@ -292,17 +296,19 @@ pyCMSgetAuxChannelChannel (cmsUInt32Number format, int auxChannelNdx) if (T_SWAPFIRST(format) && T_DOSWAP(format)) { // reverse order, before anything but last extra is shifted last - if (auxChannelNdx == numExtras - 1) + if (auxChannelNdx == numExtras - 1) { return numColors + numExtras - 1; - else + } else { return numExtras - 2 - auxChannelNdx; + } } else if (T_SWAPFIRST(format)) { // in order, after color channels, but last extra is shifted to first - if (auxChannelNdx == numExtras - 1) + if (auxChannelNdx == numExtras - 1) { return 0; - else + } else { return numColors + 1 + auxChannelNdx; + } } else if (T_DOSWAP(format)) { // reverse order, before anything @@ -330,23 +336,26 @@ pyCMScopyAux (cmsHTRANSFORM hTransform, Imaging imDst, const Imaging imSrc) int e; // trivially copied - if (imDst == imSrc) + if (imDst == imSrc) { return; + } dstLCMSFormat = cmsGetTransformOutputFormat(hTransform); srcLCMSFormat = cmsGetTransformInputFormat(hTransform); // currently, all Pillow formats are chunky formats, but check it anyway - if (T_PLANAR(dstLCMSFormat) || T_PLANAR(srcLCMSFormat)) + if (T_PLANAR(dstLCMSFormat) || T_PLANAR(srcLCMSFormat)) { return; + } // copy only if channel format is identical, except OPTIMIZED is ignored as it // does not affect the aux channel if (T_FLOAT(dstLCMSFormat) != T_FLOAT(srcLCMSFormat) || T_FLAVOR(dstLCMSFormat) != T_FLAVOR(srcLCMSFormat) || T_ENDIAN16(dstLCMSFormat) != T_ENDIAN16(srcLCMSFormat) - || T_BYTES(dstLCMSFormat) != T_BYTES(srcLCMSFormat)) + || T_BYTES(dstLCMSFormat) != T_BYTES(srcLCMSFormat)) { return; + } numSrcExtras = T_EXTRA(srcLCMSFormat); numDstExtras = T_EXTRA(dstLCMSFormat); @@ -367,8 +376,9 @@ pyCMScopyAux (cmsHTRANSFORM hTransform, Imaging imDst, const Imaging imSrc) char* pDstExtras = imDst->image[y] + dstChannel * channelSize; const char* pSrcExtras = imSrc->image[y] + srcChannel * channelSize; - for (x = 0; x < xSize; x++) + for (x = 0; x < xSize; x++) { memcpy(pDstExtras + x * dstChunkSize, pSrcExtras + x * srcChunkSize, channelSize); + } } } } @@ -378,14 +388,16 @@ pyCMSdoTransform(Imaging im, Imaging imOut, cmsHTRANSFORM hTransform) { int i; - if (im->xsize > imOut->xsize || im->ysize > imOut->ysize) + if (im->xsize > imOut->xsize || im->ysize > imOut->ysize) { return -1; + } Py_BEGIN_ALLOW_THREADS // transform color channels only - for (i = 0; i < im->ysize; i++) + for (i = 0; i < im->ysize; i++) { cmsDoTransform(hTransform, im->image[i], imOut->image[i], im->xsize); + } // lcms by default does nothing to the auxiliary channels leaving those // unchanged. To do "the right thing" here, i.e. maintain identical results @@ -417,8 +429,9 @@ _buildTransform(cmsHPROFILE hInputProfile, cmsHPROFILE hOutputProfile, char *sIn Py_END_ALLOW_THREADS - if (!hTransform) + if (!hTransform) { PyErr_SetString(PyExc_ValueError, "cannot build transform"); + } return hTransform; /* if NULL, an exception is set */ } @@ -442,8 +455,9 @@ _buildProofTransform(cmsHPROFILE hInputProfile, cmsHPROFILE hOutputProfile, cmsH Py_END_ALLOW_THREADS - if (!hTransform) + if (!hTransform) { PyErr_SetString(PyExc_ValueError, "cannot build proof transform"); + } return hTransform; /* if NULL, an exception is set */ } @@ -462,13 +476,15 @@ buildTransform(PyObject *self, PyObject *args) { cmsHTRANSFORM transform = NULL; - if (!PyArg_ParseTuple(args, "O!O!ss|ii:buildTransform", &CmsProfile_Type, &pInputProfile, &CmsProfile_Type, &pOutputProfile, &sInMode, &sOutMode, &iRenderingIntent, &cmsFLAGS)) + if (!PyArg_ParseTuple(args, "O!O!ss|ii:buildTransform", &CmsProfile_Type, &pInputProfile, &CmsProfile_Type, &pOutputProfile, &sInMode, &sOutMode, &iRenderingIntent, &cmsFLAGS)) { return NULL; + } transform = _buildTransform(pInputProfile->profile, pOutputProfile->profile, sInMode, sOutMode, iRenderingIntent, cmsFLAGS); - if (!transform) + if (!transform) { return NULL; + } return cms_transform_new(transform, sInMode, sOutMode); } @@ -487,13 +503,15 @@ buildProofTransform(PyObject *self, PyObject *args) cmsHTRANSFORM transform = NULL; - if (!PyArg_ParseTuple(args, "O!O!O!ss|iii:buildProofTransform", &CmsProfile_Type, &pInputProfile, &CmsProfile_Type, &pOutputProfile, &CmsProfile_Type, &pProofProfile, &sInMode, &sOutMode, &iRenderingIntent, &iProofIntent, &cmsFLAGS)) + if (!PyArg_ParseTuple(args, "O!O!O!ss|iii:buildProofTransform", &CmsProfile_Type, &pInputProfile, &CmsProfile_Type, &pOutputProfile, &CmsProfile_Type, &pProofProfile, &sInMode, &sOutMode, &iRenderingIntent, &iProofIntent, &cmsFLAGS)) { return NULL; + } transform = _buildProofTransform(pInputProfile->profile, pOutputProfile->profile, pProofProfile->profile, sInMode, sOutMode, iRenderingIntent, iProofIntent, cmsFLAGS); - if (!transform) + if (!transform) { return NULL; + } return cms_transform_new(transform, sInMode, sOutMode); @@ -509,8 +527,9 @@ cms_transform_apply(CmsTransformObject *self, PyObject *args) int result; - if (!PyArg_ParseTuple(args, "nn:apply", &idIn, &idOut)) + if (!PyArg_ParseTuple(args, "nn:apply", &idIn, &idOut)) { return NULL; + } im = (Imaging) idIn; imOut = (Imaging) idOut; @@ -532,8 +551,9 @@ createProfile(PyObject *self, PyObject *args) cmsCIExyY whitePoint; cmsBool result; - if (!PyArg_ParseTuple(args, "s|d:createProfile", &sColorSpace, &dColorTemp)) + if (!PyArg_ParseTuple(args, "s|d:createProfile", &sColorSpace, &dColorTemp)) { return NULL; + } if (strcmp(sColorSpace, "LAB") == 0) { if (dColorTemp > 0.0) { @@ -575,8 +595,9 @@ cms_profile_is_intent_supported(CmsProfileObject *self, PyObject *args) int intent; int direction; - if (!PyArg_ParseTuple(args, "ii:is_intent_supported", &intent, &direction)) + if (!PyArg_ParseTuple(args, "ii:is_intent_supported", &intent, &direction)) { return NULL; + } result = cmsIsIntentSupported(self->profile, intent, direction); @@ -602,8 +623,9 @@ cms_get_display_profile_win32(PyObject* self, PyObject* args) HANDLE handle = 0; int is_dc = 0; - if (!PyArg_ParseTuple(args, "|" F_HANDLE "i:get_display_profile", &handle, &is_dc)) + if (!PyArg_ParseTuple(args, "|" F_HANDLE "i:get_display_profile", &handle, &is_dc)) { return NULL; + } filename_size = sizeof(filename); @@ -615,8 +637,9 @@ cms_get_display_profile_win32(PyObject* self, PyObject* args) ReleaseDC((HWND) handle, dc); } - if (ok) + if (ok) { return PyUnicode_FromStringAndSize(filename, filename_size-1); + } Py_INCREF(Py_None); return Py_None; @@ -745,10 +768,11 @@ _profile_read_ciexyz(CmsProfileObject* self, cmsTagSignature info, int multi) Py_INCREF(Py_None); return Py_None; } - if (multi) + if (multi) { return _xyz3_py(XYZ); - else + } else { return _xyz_py(XYZ); + } } static PyObject* @@ -826,8 +850,9 @@ static cmsBool _calculate_rgb_primaries(CmsProfileObject* self, cmsCIEXYZTRIPLE* // double array of RGB values with max on each identity hXYZ = cmsCreateXYZProfile(); - if (hXYZ == NULL) + if (hXYZ == NULL) { return 0; + } // transform from our profile to XYZ using doubles for highest precision hTransform = cmsCreateTransform(self->profile, TYPE_RGB_DBL, @@ -835,8 +860,9 @@ static cmsBool _calculate_rgb_primaries(CmsProfileObject* self, cmsCIEXYZTRIPLE* INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOCACHE | cmsFLAGS_NOOPTIMIZE); cmsCloseProfile(hXYZ); - if (hTransform == NULL) + if (hTransform == NULL) { return 0; + } cmsDoTransform(hTransform, (void*) input, result, 3); cmsDeleteTransform(hTransform); @@ -881,8 +907,9 @@ _is_intent_supported(CmsProfileObject* self, int clut) /* Only valid for ICC Intents (otherwise we read invalid memory in lcms cmsio1.c). */ if (!(intent == INTENT_PERCEPTUAL || intent == INTENT_RELATIVE_COLORIMETRIC - || intent == INTENT_SATURATION || intent == INTENT_ABSOLUTE_COLORIMETRIC)) + || intent == INTENT_SATURATION || intent == INTENT_ABSOLUTE_COLORIMETRIC)) { continue; + } id = PyLong_FromLong((long) intent); entry = Py_BuildValue("(OOO)", @@ -1276,8 +1303,9 @@ cms_profile_getattr_red_primary(CmsProfileObject* self, void* closure) cmsBool result = 0; cmsCIEXYZTRIPLE primaries; - if (cmsIsMatrixShaper(self->profile)) + if (cmsIsMatrixShaper(self->profile)) { result = _calculate_rgb_primaries(self, &primaries); + } if (! result) { Py_INCREF(Py_None); return Py_None; @@ -1292,8 +1320,9 @@ cms_profile_getattr_green_primary(CmsProfileObject* self, void* closure) cmsBool result = 0; cmsCIEXYZTRIPLE primaries; - if (cmsIsMatrixShaper(self->profile)) + if (cmsIsMatrixShaper(self->profile)) { result = _calculate_rgb_primaries(self, &primaries); + } if (! result) { Py_INCREF(Py_None); return Py_None; @@ -1308,8 +1337,9 @@ cms_profile_getattr_blue_primary(CmsProfileObject* self, void* closure) cmsBool result = 0; cmsCIEXYZTRIPLE primaries; - if (cmsIsMatrixShaper(self->profile)) + if (cmsIsMatrixShaper(self->profile)) { result = _calculate_rgb_primaries(self, &primaries); + } if (! result) { Py_INCREF(Py_None); return Py_None; @@ -1387,12 +1417,13 @@ cms_profile_getattr_icc_measurement_condition (CmsProfileObject* self, void* clo return Py_None; } - if (mc->Geometry == 1) + if (mc->Geometry == 1) { geo = "45/0, 0/45"; - else if (mc->Geometry == 2) + } else if (mc->Geometry == 2) { geo = "0d, d/0"; - else + } else { geo = "unknown"; + } return Py_BuildValue("{s:i,s:(ddd),s:s,s:d,s:s}", "observer", mc->Observer, @@ -1611,8 +1642,9 @@ PyInit__imagingcms(void) { m = PyModule_Create(&module_def); - if (setup_module(m) < 0) + if (setup_module(m) < 0) { return NULL; + } PyDateTime_IMPORT; diff --git a/src/_imagingft.c b/src/_imagingft.c index f1b299d91..795ab4d20 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -137,11 +137,12 @@ geterror(int code) { int i; - for (i = 0; ft_errors[i].message; i++) + for (i = 0; ft_errors[i].message; i++) { if (ft_errors[i].code == code) { PyErr_SetString(PyExc_OSError, ft_errors[i].message); return NULL; } + } PyErr_SetString(PyExc_OSError, "unknown freetype error"); return NULL; @@ -274,8 +275,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) self = PyObject_New(FontObject, &Font_Type); if (!self) { - if (filename) + if (filename) { PyMem_Free(filename); + } return NULL; } @@ -299,8 +301,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) } } - if (!error) + if (!error) { error = FT_Set_Pixel_Sizes(self->face, 0, size); + } if (!error && encoding && strlen((char*) encoding) == 4) { FT_Encoding encoding_tag = FT_MAKE_TAG( @@ -308,8 +311,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) ); error = FT_Select_Charmap(self->face, encoding_tag); } - if (filename) + if (filename) { PyMem_Free(filename); + } if (error) { if (self->font_bytes) { @@ -327,8 +331,9 @@ static int font_getchar(PyObject* string, int index, FT_ULong* char_out) { if (PyUnicode_Check(string)) { - if (index >= PyUnicode_GET_LENGTH(string)) + if (index >= PyUnicode_GET_LENGTH(string)) { return 0; + } *char_out = PyUnicode_READ_CHAR(string, index); return 1; } @@ -443,8 +448,9 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject * if (PyUnicode_Check(item)) { bytes = PyUnicode_AsUTF8String(item); - if (bytes == NULL) + if (bytes == NULL) { goto failed; + } feature = PyBytes_AS_STRING(bytes); size = PyBytes_GET_SIZE(bytes); } @@ -608,8 +614,9 @@ font_getsize(FontObject* self, PyObject* args) /* calculate size and bearing for a given string */ PyObject* string; - if (!PyArg_ParseTuple(args, "O|zOz:getsize", &string, &dir, &features, &lang)) + if (!PyArg_ParseTuple(args, "O|zOz:getsize", &string, &dir, &features, &lang)) { return NULL; + } count = text_layout(string, self, dir, features, lang, &glyph_info, 0); if (PyErr_Occurred()) { @@ -631,8 +638,9 @@ font_getsize(FontObject* self, PyObject* args) * Yifu Yu, 2014-10-15 */ error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT|FT_LOAD_NO_BITMAP); - if (error) + if (error) { return geterror(error); + } if (i == 0) { if (horizontal_dir) { @@ -657,21 +665,26 @@ font_getsize(FontObject* self, PyObject* args) offset = glyph_info[i].x_advance - face->glyph->metrics.width - face->glyph->metrics.horiBearingX; - if (offset < 0) + if (offset < 0) { x_advanced -= offset; - if (x_advanced > x_max) + } + if (x_advanced > x_max) { x_max = x_advanced; + } bbox.yMax += glyph_info[i].y_offset; bbox.yMin += glyph_info[i].y_offset; - if (bbox.yMax > y_max) + if (bbox.yMax > y_max) { y_max = bbox.yMax; - if (bbox.yMin < y_min) + } + if (bbox.yMin < y_min) { y_min = bbox.yMin; + } // find max distance of baseline from top - if (face->glyph->metrics.horiBearingY > yoffset) + if (face->glyph->metrics.horiBearingY > yoffset) { yoffset = face->glyph->metrics.horiBearingY; + } } else { y_max -= glyph_info[i].y_advance; @@ -685,10 +698,12 @@ font_getsize(FontObject* self, PyObject* args) y_max -= offset; } - if (bbox.xMax > x_max) + if (bbox.xMax > x_max) { x_max = bbox.xMax; - if (i == 0 || bbox.xMin < x_min) + } + if (i == 0 || bbox.xMin < x_min) { x_min = bbox.xMin; + } } FT_Done_Glyph(glyph); @@ -702,20 +717,22 @@ font_getsize(FontObject* self, PyObject* args) if (face) { if (horizontal_dir) { // left bearing - if (xoffset < 0) + if (xoffset < 0) { x_max -= xoffset; - else + } else { xoffset = 0; + } /* difference between the font ascender and the distance of * the baseline from the top */ yoffset = PIXEL(self->face->size->metrics.ascender - yoffset); } else { // top bearing - if (yoffset < 0) + if (yoffset < 0) { y_max -= yoffset; - else + } else { yoffset = 0; + } } } @@ -800,8 +817,9 @@ font_render(FontObject* self, PyObject* args) temp = bitmap.rows - glyph_slot->bitmap_top; temp -= PIXEL(glyph_info[i].y_offset); - if (temp > ascender) + if (temp > ascender) { ascender = temp; + } } if (stroker == NULL) { @@ -855,10 +873,12 @@ font_render(FontObject* self, PyObject* args) x0 = 0; x1 = bitmap.width; - if (xx < 0) + if (xx < 0) { x0 = -xx; - if (xx + x1 > im->xsize) + } + if (xx + x1 > im->xsize) { x1 = im->xsize - xx; + } source = (unsigned char*) bitmap.buffer; for (bitmap_y = 0; bitmap_y < bitmap.rows; bitmap_y++) { @@ -876,8 +896,9 @@ font_render(FontObject* self, PyObject* args) // use monochrome mask (on palette images, etc) int j, k, m = 128; for (j = k = 0; j < x1; j++) { - if (j >= x0 && (source[k] & m)) + if (j >= x0 && (source[k] & m)) { target[j] = 255; + } if (!(m >>= 1)) { m = 128; k++; @@ -887,8 +908,9 @@ font_render(FontObject* self, PyObject* args) // use antialiased rendering int k; for (k = x0; k < x1; k++) { - if (target[k] < source[k]) + if (target[k] < source[k]) { target[k] = source[k]; + } } } } @@ -919,8 +941,9 @@ font_render(FontObject* self, PyObject* args) PyObject *list_names, *list_name; error = FT_Get_MM_Var(self->face, &master); - if (error) + if (error) { return geterror(error); + } num_namedstyles = master->num_namedstyles; list_names = PyList_New(num_namedstyles); @@ -928,12 +951,14 @@ font_render(FontObject* self, PyObject* args) name_count = FT_Get_Sfnt_Name_Count(self->face); for (i = 0; i < name_count; i++) { error = FT_Get_Sfnt_Name(self->face, i, &name); - if (error) + if (error) { return geterror(error); + } for (j = 0; j < num_namedstyles; j++) { - if (PyList_GetItem(list_names, j) != NULL) + if (PyList_GetItem(list_names, j) != NULL) { continue; + } if (master->namedstyle[j].strid == name.name_id) { list_name = Py_BuildValue("y#", name.string, name.string_len); @@ -958,8 +983,9 @@ font_render(FontObject* self, PyObject* args) FT_SfntName name; PyObject *list_axes, *list_axis, *axis_name; error = FT_Get_MM_Var(self->face, &master); - if (error) + if (error) { return geterror(error); + } num_axis = master->num_axis; name_count = FT_Get_Sfnt_Name_Count(self->face); @@ -978,8 +1004,9 @@ font_render(FontObject* self, PyObject* args) for (j = 0; j < name_count; j++) { error = FT_Get_Sfnt_Name(self->face, j, &name); - if (error) + if (error) { return geterror(error); + } if (name.name_id == axis.strid) { axis_name = Py_BuildValue("y#", name.string, name.string_len); @@ -1002,12 +1029,14 @@ font_render(FontObject* self, PyObject* args) int error; int instance_index; - if (!PyArg_ParseTuple(args, "i", &instance_index)) + if (!PyArg_ParseTuple(args, "i", &instance_index)) { return NULL; + } error = FT_Set_Named_Instance(self->face, instance_index); - if (error) + if (error) { return geterror(error); + } Py_INCREF(Py_None); return Py_None; @@ -1022,8 +1051,9 @@ font_render(FontObject* self, PyObject* args) Py_ssize_t i, num_coords; FT_Fixed *coords; FT_Fixed coord; - if (!PyArg_ParseTuple(args, "O", &axes)) + if (!PyArg_ParseTuple(args, "O", &axes)) { return NULL; + } if (!PyList_Check(axes)) { PyErr_SetString(PyExc_TypeError, "argument must be a list"); @@ -1037,13 +1067,13 @@ font_render(FontObject* self, PyObject* args) } for (i = 0; i < num_coords; i++) { item = PyList_GET_ITEM(axes, i); - if (PyFloat_Check(item)) + if (PyFloat_Check(item)) { coord = PyFloat_AS_DOUBLE(item); - else if (PyLong_Check(item)) + } else if (PyLong_Check(item)) { coord = (float) PyLong_AS_LONG(item); - else if (PyNumber_Check(item)) + } else if (PyNumber_Check(item)) { coord = PyFloat_AsDouble(item); - else { + } else { free(coords); PyErr_SetString(PyExc_TypeError, "list must contain numbers"); return NULL; @@ -1053,8 +1083,9 @@ font_render(FontObject* self, PyObject* args) error = FT_Set_Var_Design_Coordinates(self->face, num_coords, coords); free(coords); - if (error) + if (error) { return geterror(error); + } Py_INCREF(Py_None); return Py_None; @@ -1090,16 +1121,18 @@ static PyMethodDef font_methods[] = { static PyObject* font_getattr_family(FontObject* self, void* closure) { - if (self->face->family_name) + if (self->face->family_name) { return PyUnicode_FromString(self->face->family_name); + } Py_RETURN_NONE; } static PyObject* font_getattr_style(FontObject* self, void* closure) { - if (self->face->style_name) + if (self->face->style_name) { return PyUnicode_FromString(self->face->style_name); + } Py_RETURN_NONE; } @@ -1200,8 +1233,9 @@ setup_module(PyObject* m) { /* Ready object type */ PyType_Ready(&Font_Type); - if (FT_Init_FreeType(&library)) + if (FT_Init_FreeType(&library)) { return 0; /* leave it uninitialized */ + } FT_Library_Version(library, &major, &minor, &patch); @@ -1230,8 +1264,9 @@ PyInit__imagingft(void) { m = PyModule_Create(&module_def); - if (setup_module(m) < 0) + if (setup_module(m) < 0) { return NULL; + } return m; } diff --git a/src/_imagingmath.c b/src/_imagingmath.c index bc66a581a..959859a1d 100644 --- a/src/_imagingmath.c +++ b/src/_imagingmath.c @@ -88,12 +88,14 @@ void name(Imaging out, Imaging im1, Imaging im2)\ static int powi(int x, int y) { double v = pow(x, y) + 0.5; - if (errno == EDOM) + if (errno == EDOM) { return 0; - if (v < MIN_INT32) + } + if (v < MIN_INT32) { v = MIN_INT32; - else if (v > MAX_INT32) + } else if (v > MAX_INT32) { v = MAX_INT32; + } return (int) v; } @@ -167,8 +169,9 @@ _unop(PyObject* self, PyObject* args) void (*unop)(Imaging, Imaging); Py_ssize_t op, i0, i1; - if (!PyArg_ParseTuple(args, "nnn", &op, &i0, &i1)) + if (!PyArg_ParseTuple(args, "nnn", &op, &i0, &i1)) { return NULL; + } out = (Imaging) i0; im1 = (Imaging) i1; @@ -190,8 +193,9 @@ _binop(PyObject* self, PyObject* args) void (*binop)(Imaging, Imaging, Imaging); Py_ssize_t op, i0, i1, i2; - if (!PyArg_ParseTuple(args, "nnnn", &op, &i0, &i1, &i2)) + if (!PyArg_ParseTuple(args, "nnnn", &op, &i0, &i1, &i2)) { return NULL; + } out = (Imaging) i0; im1 = (Imaging) i1; @@ -215,8 +219,9 @@ static void install(PyObject *d, char* name, void* value) { PyObject *v = PyLong_FromSsize_t((Py_ssize_t) value); - if (!v || PyDict_SetItemString(d, name, v)) + if (!v || PyDict_SetItemString(d, name, v)) { PyErr_Clear(); + } Py_XDECREF(v); } @@ -286,8 +291,9 @@ PyInit__imagingmath(void) { m = PyModule_Create(&module_def); - if (setup_module(m) < 0) + if (setup_module(m) < 0) { return NULL; + } return m; } diff --git a/src/_imagingmorph.c b/src/_imagingmorph.c index 050ae9f02..4f7844604 100644 --- a/src/_imagingmorph.c +++ b/src/_imagingmorph.c @@ -85,8 +85,9 @@ apply(PyObject *self, PyObject* args) /* zero boundary conditions. TBD support other modes */ outrow[0] = outrow[width-1] = 0; if (row_idx==0 || row_idx == height-1) { - for(col_idx=0; col_idxstate, 0, sizeof(decoder->state)); @@ -81,8 +83,9 @@ PyImaging_DecoderNew(int contextsize) (void) PyErr_NoMemory(); return NULL; } - } else + } else { context = 0; + } /* Initialize decoder context */ decoder->state.context = context; @@ -104,8 +107,9 @@ PyImaging_DecoderNew(int contextsize) static void _dealloc(ImagingDecoderObject* decoder) { - if (decoder->cleanup) + if (decoder->cleanup) { decoder->cleanup(&decoder->state); + } free(decoder->state.buffer); free(decoder->state.context); Py_XDECREF(decoder->lock); @@ -121,8 +125,9 @@ _decode(ImagingDecoderObject* decoder, PyObject* args) int status; ImagingSectionCookie cookie; - if (!PyArg_ParseTuple(args, "y#", &buffer, &bufsize)) + if (!PyArg_ParseTuple(args, "y#", &buffer, &bufsize)) { return NULL; + } if (!decoder->pulls_fd) { ImagingSectionEnter(&cookie); @@ -164,11 +169,13 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args) x0 = y0 = x1 = y1 = 0; /* FIXME: should publish the ImagingType descriptor */ - if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1)) + if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1)) { return NULL; + } im = PyImaging_AsImaging(op); - if (!im) + if (!im) { return NULL; + } decoder->im = im; @@ -203,8 +210,9 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args) } /* malloc check ok, overflow checked above */ state->buffer = (UINT8*) malloc(state->bytes); - if (!state->buffer) + if (!state->buffer) { return PyErr_NoMemory(); + } } /* Keep a reference to the image object, to make sure it doesn't @@ -223,8 +231,9 @@ _setfd(ImagingDecoderObject* decoder, PyObject* args) PyObject* fd; ImagingCodecState state; - if (!PyArg_ParseTuple(args, "O", &fd)) + if (!PyArg_ParseTuple(args, "O", &fd)) { return NULL; + } state = &decoder->state; @@ -330,8 +339,9 @@ PyImaging_BitDecoderNew(PyObject* self, PyObject* args) int sign = 0; int ystep = 1; if (!PyArg_ParseTuple(args, "s|iiiii", &mode, &bits, &pad, &fill, - &sign, &ystep)) + &sign, &ystep)) { return NULL; + } if (strcmp(mode, "F") != 0) { PyErr_SetString(PyExc_ValueError, "bad image mode"); @@ -339,8 +349,9 @@ PyImaging_BitDecoderNew(PyObject* self, PyObject* args) } decoder = PyImaging_DecoderNew(sizeof(BITSTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } decoder->decode = ImagingBitDecode; @@ -368,8 +379,9 @@ PyImaging_BcnDecoderNew(PyObject* self, PyObject* args) char* actual; int n = 0; int ystep = 1; - if (!PyArg_ParseTuple(args, "s|ii", &mode, &n, &ystep)) + if (!PyArg_ParseTuple(args, "s|ii", &mode, &n, &ystep)) { return NULL; + } switch (n) { case 1: /* BC1: 565 color, 1-bit alpha */ @@ -394,8 +406,9 @@ PyImaging_BcnDecoderNew(PyObject* self, PyObject* args) } decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } decoder->decode = ImagingBcnDecode; decoder->state.state = n; @@ -415,8 +428,9 @@ PyImaging_FliDecoderNew(PyObject* self, PyObject* args) ImagingDecoderObject* decoder; decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } decoder->decode = ImagingFliDecode; @@ -436,8 +450,9 @@ PyImaging_GifDecoderNew(PyObject* self, PyObject* args) char* mode; int bits = 8; int interlace = 0; - if (!PyArg_ParseTuple(args, "s|ii", &mode, &bits, &interlace)) + if (!PyArg_ParseTuple(args, "s|ii", &mode, &bits, &interlace)) { return NULL; + } if (strcmp(mode, "L") != 0 && strcmp(mode, "P") != 0) { PyErr_SetString(PyExc_ValueError, "bad image mode"); @@ -445,8 +460,9 @@ PyImaging_GifDecoderNew(PyObject* self, PyObject* args) } decoder = PyImaging_DecoderNew(sizeof(GIFDECODERSTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } decoder->decode = ImagingGifDecode; @@ -468,15 +484,18 @@ PyImaging_HexDecoderNew(PyObject* self, PyObject* args) char* mode; char* rawmode; - if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) + if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) { return NULL; + } decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingHexDecode; @@ -504,17 +523,20 @@ PyImaging_LibTiffDecoderNew(PyObject* self, PyObject* args) int fp; uint32 ifdoffset; - if (! PyArg_ParseTuple(args, "sssiI", &mode, &rawmode, &compname, &fp, &ifdoffset)) + if (! PyArg_ParseTuple(args, "sssiI", &mode, &rawmode, &compname, &fp, &ifdoffset)) { return NULL; + } TRACE(("new tiff decoder %s\n", compname)); decoder = PyImaging_DecoderNew(sizeof(TIFFSTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } if (! ImagingLibTiffInit(&decoder->state, fp, ifdoffset)) { Py_DECREF(decoder); @@ -541,15 +563,18 @@ PyImaging_PackbitsDecoderNew(PyObject* self, PyObject* args) char* mode; char* rawmode; - if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) + if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) { return NULL; + } decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingPackbitsDecode; @@ -567,12 +592,14 @@ PyImaging_PcdDecoderNew(PyObject* self, PyObject* args) ImagingDecoderObject* decoder; decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } /* Unpack from PhotoYCC to RGB */ - if (get_unpacker(decoder, "RGB", "YCC;P") < 0) + if (get_unpacker(decoder, "RGB", "YCC;P") < 0) { return NULL; + } decoder->decode = ImagingPcdDecode; @@ -592,15 +619,18 @@ PyImaging_PcxDecoderNew(PyObject* self, PyObject* args) char* mode; char* rawmode; int stride; - if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride)) + if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride)) { return NULL; + } decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->state.bytes = stride; @@ -623,15 +653,18 @@ PyImaging_RawDecoderNew(PyObject* self, PyObject* args) char* rawmode; int stride = 0; int ystep = 1; - if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep)) + if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep)) { return NULL; + } decoder = PyImaging_DecoderNew(sizeof(RAWSTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingRawDecode; @@ -656,15 +689,18 @@ PyImaging_SgiRleDecoderNew(PyObject* self, PyObject* args) char* rawmode; int ystep = 1; int bpc = 1; - if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &bpc)) + if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &bpc)) { return NULL; + } decoder = PyImaging_DecoderNew(sizeof(SGISTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->pulls_fd = 1; decoder->decode = ImagingSgiRleDecode; @@ -687,15 +723,18 @@ PyImaging_SunRleDecoderNew(PyObject* self, PyObject* args) char* mode; char* rawmode; - if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) + if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) { return NULL; + } decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingSunRleDecode; @@ -716,15 +755,18 @@ PyImaging_TgaRleDecoderNew(PyObject* self, PyObject* args) char* rawmode; int ystep = 1; int depth = 8; - if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth)) + if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth)) { return NULL; + } decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingTgaRleDecode; @@ -745,11 +787,13 @@ PyImaging_XbmDecoderNew(PyObject* self, PyObject* args) ImagingDecoderObject* decoder; decoder = PyImaging_DecoderNew(0); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, "1", "1;R") < 0) + if (get_unpacker(decoder, "1", "1;R") < 0) { return NULL; + } decoder->decode = ImagingXbmDecode; @@ -773,15 +817,18 @@ PyImaging_ZipDecoderNew(PyObject* self, PyObject* args) char* mode; char* rawmode; int interlaced = 0; - if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced)) + if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced)) { return NULL; + } decoder = PyImaging_DecoderNew(sizeof(ZIPSTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingZipDecode; decoder->cleanup = ImagingZipDecodeCleanup; @@ -826,15 +873,18 @@ PyImaging_JpegDecoderNew(PyObject* self, PyObject* args) int draft = 0; if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, - &scale, &draft)) + &scale, &draft)) { return NULL; + } - if (!jpegmode) + if (!jpegmode) { jpegmode = ""; + } decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } // libjpeg-turbo supports different output formats. // We are choosing Pillow's native format (3 color bytes + 1 padding) @@ -843,8 +893,9 @@ PyImaging_JpegDecoderNew(PyObject* self, PyObject* args) rawmode = "RGBX"; } - if (get_unpacker(decoder, mode, rawmode) < 0) + if (get_unpacker(decoder, mode, rawmode) < 0) { return NULL; + } decoder->decode = ImagingJpegDecode; decoder->cleanup = ImagingJpegDecodeCleanup; @@ -882,21 +933,24 @@ PyImaging_Jpeg2KDecoderNew(PyObject* self, PyObject* args) PY_LONG_LONG length = -1; if (!PyArg_ParseTuple(args, "ss|iiiL", &mode, &format, - &reduce, &layers, &fd, &length)) + &reduce, &layers, &fd, &length)) { return NULL; + } - if (strcmp(format, "j2k") == 0) + if (strcmp(format, "j2k") == 0) { codec_format = OPJ_CODEC_J2K; - else if (strcmp(format, "jpt") == 0) + } else if (strcmp(format, "jpt") == 0) { codec_format = OPJ_CODEC_JPT; - else if (strcmp(format, "jp2") == 0) + } else if (strcmp(format, "jp2") == 0) { codec_format = OPJ_CODEC_JP2; - else + } else { return NULL; + } decoder = PyImaging_DecoderNew(sizeof(JPEG2KDECODESTATE)); - if (decoder == NULL) + if (decoder == NULL) { return NULL; + } decoder->pulls_fd = 1; decoder->decode = ImagingJpeg2KDecode; diff --git a/src/display.c b/src/display.c index 9a337d1c0..ce2cf7e98 100644 --- a/src/display.c +++ b/src/display.c @@ -52,12 +52,14 @@ _new(const char* mode, int xsize, int ysize) { ImagingDisplayObject *display; - if (PyType_Ready(&ImagingDisplayType) < 0) + if (PyType_Ready(&ImagingDisplayType) < 0) { return NULL; + } display = PyObject_New(ImagingDisplayObject, &ImagingDisplayType); - if (display == NULL) + if (display == NULL) { return NULL; + } display->dib = ImagingNewDIB(mode, xsize, ysize); if (!display->dib) { @@ -71,8 +73,9 @@ _new(const char* mode, int xsize, int ysize) static void _delete(ImagingDisplayObject* display) { - if (display->dib) + if (display->dib) { ImagingDeleteDIB(display->dib); + } PyObject_Del(display); } @@ -80,8 +83,9 @@ static PyObject* _expose(ImagingDisplayObject* display, PyObject* args) { HDC hdc; - if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) + if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) { return NULL; + } ImagingExposeDIB(display->dib, hdc); @@ -97,8 +101,9 @@ _draw(ImagingDisplayObject* display, PyObject* args) int src[4]; if (!PyArg_ParseTuple(args, F_HANDLE "(iiii)(iiii)", &hdc, dst+0, dst+1, dst+2, dst+3, - src+0, src+1, src+2, src+3)) + src+0, src+1, src+2, src+3)) { return NULL; + } ImagingDrawDIB(display->dib, hdc, dst, src); @@ -116,16 +121,20 @@ _paste(ImagingDisplayObject* display, PyObject* args) PyObject* op; int xy[4]; xy[0] = xy[1] = xy[2] = xy[3] = 0; - if (!PyArg_ParseTuple(args, "O|(iiii)", &op, xy+0, xy+1, xy+2, xy+3)) + if (!PyArg_ParseTuple(args, "O|(iiii)", &op, xy+0, xy+1, xy+2, xy+3)) { return NULL; + } im = PyImaging_AsImaging(op); - if (!im) + if (!im) { return NULL; + } - if (xy[2] <= xy[0]) + if (xy[2] <= xy[0]) { xy[2] = xy[0] + im->xsize; - if (xy[3] <= xy[1]) + } + if (xy[3] <= xy[1]) { xy[3] = xy[1] + im->ysize; + } ImagingPasteDIB(display->dib, im, xy); @@ -139,8 +148,9 @@ _query_palette(ImagingDisplayObject* display, PyObject* args) HDC hdc; int status; - if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) + if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) { return NULL; + } status = ImagingQueryPaletteDIB(display->dib, hdc); @@ -153,8 +163,9 @@ _getdc(ImagingDisplayObject* display, PyObject* args) HWND window; HDC dc; - if (!PyArg_ParseTuple(args, F_HANDLE, &window)) + if (!PyArg_ParseTuple(args, F_HANDLE, &window)) { return NULL; + } dc = GetDC(window); if (!dc) { @@ -171,8 +182,9 @@ _releasedc(ImagingDisplayObject* display, PyObject* args) HWND window; HDC dc; - if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, &window, &dc)) + if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, &window, &dc)) { return NULL; + } ReleaseDC(window, dc); @@ -186,8 +198,9 @@ _frombytes(ImagingDisplayObject* display, PyObject* args) char* ptr; int bytes; - if (!PyArg_ParseTuple(args, "y#:frombytes", &ptr, &bytes)) + if (!PyArg_ParseTuple(args, "y#:frombytes", &ptr, &bytes)) { return NULL; + } if (display->dib->ysize * display->dib->linesize != bytes) { PyErr_SetString(PyExc_ValueError, "wrong size"); @@ -203,8 +216,9 @@ _frombytes(ImagingDisplayObject* display, PyObject* args) static PyObject* _tobytes(ImagingDisplayObject* display, PyObject* args) { - if (!PyArg_ParseTuple(args, ":tobytes")) + if (!PyArg_ParseTuple(args, ":tobytes")) { return NULL; + } return PyBytes_FromStringAndSize( display->dib->bits, display->dib->ysize * display->dib->linesize @@ -284,12 +298,14 @@ PyImaging_DisplayWin32(PyObject* self, PyObject* args) char *mode; int xsize, ysize; - if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) + if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) { return NULL; + } display = _new(mode, xsize, ysize); - if (display == NULL) + if (display == NULL) { return NULL; + } return (PyObject*) display; } @@ -324,8 +340,9 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args) HMODULE user32; Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function; - if (!PyArg_ParseTuple(args, "|ii", &includeLayeredWindows, &all_screens)) + if (!PyArg_ParseTuple(args, "|ii", &includeLayeredWindows, &all_screens)) { return NULL; + } /* step 1: create a memory DC large enough to hold the entire screen */ @@ -361,25 +378,30 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args) FreeLibrary(user32); bitmap = CreateCompatibleBitmap(screen, width, height); - if (!bitmap) + if (!bitmap) { goto error; + } - if (!SelectObject(screen_copy, bitmap)) + if (!SelectObject(screen_copy, bitmap)) { goto error; + } /* step 2: copy bits into memory DC bitmap */ rop = SRCCOPY; - if (includeLayeredWindows) + if (includeLayeredWindows) { rop |= CAPTUREBLT; - if (!BitBlt(screen_copy, 0, 0, width, height, screen, x, y, rop)) + } + if (!BitBlt(screen_copy, 0, 0, width, height, screen, x, y, rop)) { goto error; + } /* step 3: extract bits from bitmap */ buffer = PyBytes_FromStringAndSize(NULL, height * ((width*3 + 3) & -4)); - if (!buffer) + if (!buffer) { return NULL; + } core.bcSize = sizeof(core); core.bcWidth = width; @@ -387,8 +409,9 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject* args) core.bcPlanes = 1; core.bcBitCount = 24; if (!GetDIBits(screen_copy, bitmap, 0, height, PyBytes_AS_STRING(buffer), - (BITMAPINFO*) &core, DIB_RGB_COLORS)) + (BITMAPINFO*) &core, DIB_RGB_COLORS)) { goto error; + } DeleteObject(bitmap); DeleteDC(screen_copy); @@ -418,12 +441,15 @@ static BOOL CALLBACK list_windows_callback(HWND hwnd, LPARAM lParam) title_size = GetWindowTextLength(hwnd); if (title_size > 0) { title = PyUnicode_FromStringAndSize(NULL, title_size); - if (title) + if (title) { GetWindowTextW(hwnd, PyUnicode_AS_UNICODE(title), title_size+1); - } else + } + } else { title = PyUnicode_FromString(""); - if (!title) + } + if (!title) { return 0; + } /* get bounding boxes */ GetClientRect(hwnd, &inner); @@ -434,15 +460,17 @@ static BOOL CALLBACK list_windows_callback(HWND hwnd, LPARAM lParam) inner.left, inner.top, inner.right, inner.bottom, outer.left, outer.top, outer.right, outer.bottom ); - if (!item) + if (!item) { return 0; + } status = PyList_Append(window_list, item); Py_DECREF(item); - if (status < 0) + if (status < 0) { return 0; + } return 1; } @@ -453,8 +481,9 @@ PyImaging_ListWindowsWin32(PyObject* self, PyObject* args) PyObject* window_list; window_list = PyList_New(0); - if (!window_list) + if (!window_list) { return NULL; + } EnumWindows(list_windows_callback, (LPARAM) window_list); @@ -556,8 +585,9 @@ windowCallback(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) GetWindowLongPtr(wnd, sizeof(PyObject*)); current_threadstate = PyThreadState_Swap(NULL); PyEval_RestoreThread(threadstate); - } else + } else { return DefWindowProc(wnd, message, wParam, lParam); + } } /* process message */ @@ -575,28 +605,31 @@ windowCallback(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom ); - if (result) + if (result) { Py_DECREF(result); - else + } else { callback_error("window damage callback"); + } result = PyObject_CallFunction( callback, "s" F_HANDLE "iiii", "clear", dc, 0, 0, rect.right-rect.left, rect.bottom-rect.top ); - if (result) + if (result) { Py_DECREF(result); - else + } else { callback_error("window clear callback"); + } result = PyObject_CallFunction( callback, "s" F_HANDLE "iiii", "repair", dc, 0, 0, rect.right-rect.left, rect.bottom-rect.top ); - if (result) + if (result) { Py_DECREF(result); - else + } else { callback_error("window repair callback"); + } ReleaseDC(wnd, dc); EndPaint(wnd, &ps); @@ -610,17 +643,19 @@ windowCallback(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) if (result) { InvalidateRect(wnd, NULL, 1); Py_DECREF(result); - } else + } else { callback_error("window resize callback"); + } break; case WM_DESTROY: /* destroy window */ result = PyObject_CallFunction(callback, "s", "destroy"); - if (result) + if (result) { Py_DECREF(result); - else + } else { callback_error("window destroy callback"); + } Py_DECREF(callback); break; @@ -646,13 +681,16 @@ PyImaging_CreateWindowWin32(PyObject* self, PyObject* args) char* title; PyObject* callback; int width = 0, height = 0; - if (!PyArg_ParseTuple(args, "sO|ii", &title, &callback, &width, &height)) + if (!PyArg_ParseTuple(args, "sO|ii", &title, &callback, &width, &height)) { return NULL; + } - if (width <= 0) + if (width <= 0) { width = CW_USEDEFAULT; - if (height <= 0) + } + if (height <= 0) { height = CW_USEDEFAULT; + } /* register toplevel window class */ windowClass.style = CS_CLASSDC; @@ -731,8 +769,9 @@ PyImaging_DrawWmf(PyObject* self, PyObject* args) int width, height; int x0, y0, x1, y1; if (!PyArg_ParseTuple(args, "y#(ii)(iiii):_load", &data, &datasize, - &width, &height, &x0, &x1, &y0, &y1)) + &width, &height, &x0, &x1, &y0, &y1)) { return NULL; + } /* step 1: copy metafile contents into METAFILE object */ @@ -806,8 +845,9 @@ PyImaging_DrawWmf(PyObject* self, PyObject* args) error: DeleteEnhMetaFile(meta); - if (bitmap) + if (bitmap) { DeleteObject(bitmap); + } DeleteDC(dc); @@ -838,8 +878,9 @@ PyImaging_GrabScreenX11(PyObject* self, PyObject* args) xcb_generic_error_t* error; PyObject* buffer = NULL; - if (!PyArg_ParseTuple(args, "|z", &display_name)) + if (!PyArg_ParseTuple(args, "|z", &display_name)) { return NULL; + } /* connect to X and get screen data */ @@ -893,8 +934,9 @@ PyImaging_GrabScreenX11(PyObject* self, PyObject* args) free(reply); xcb_disconnect(connection); - if (!buffer) + if (!buffer) { return NULL; + } return Py_BuildValue("(ii)N", width, height, buffer); } diff --git a/src/encode.c b/src/encode.c index e56498913..b285292f3 100644 --- a/src/encode.c +++ b/src/encode.c @@ -55,12 +55,14 @@ PyImaging_EncoderNew(int contextsize) ImagingEncoderObject *encoder; void *context; - if(PyType_Ready(&ImagingEncoderType) < 0) + if(PyType_Ready(&ImagingEncoderType) < 0) { return NULL; + } encoder = PyObject_New(ImagingEncoderObject, &ImagingEncoderType); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } /* Clear the encoder state */ memset(&encoder->state, 0, sizeof(encoder->state)); @@ -73,8 +75,9 @@ PyImaging_EncoderNew(int contextsize) (void) PyErr_NoMemory(); return NULL; } - } else + } else { context = 0; + } /* Initialize encoder context */ encoder->state.context = context; @@ -93,8 +96,9 @@ PyImaging_EncoderNew(int contextsize) static void _dealloc(ImagingEncoderObject* encoder) { - if (encoder->cleanup) + if (encoder->cleanup) { encoder->cleanup(&encoder->state); + } free(encoder->state.buffer); free(encoder->state.context); Py_XDECREF(encoder->lock); @@ -125,19 +129,22 @@ _encode(ImagingEncoderObject* encoder, PyObject* args) Py_ssize_t bufsize = 16384; - if (!PyArg_ParseTuple(args, "|n", &bufsize)) + if (!PyArg_ParseTuple(args, "|n", &bufsize)) { return NULL; + } buf = PyBytes_FromStringAndSize(NULL, bufsize); - if (!buf) + if (!buf) { return NULL; + } status = encoder->encode(encoder->im, &encoder->state, (UINT8*) PyBytes_AsString(buf), bufsize); /* adjust string length to avoid slicing in encoder */ - if (_PyBytes_Resize(&buf, (status > 0) ? status : 0) < 0) + if (_PyBytes_Resize(&buf, (status > 0) ? status : 0) < 0) { return NULL; + } result = Py_BuildValue("iiO", status, encoder->state.errcode, buf); @@ -179,14 +186,16 @@ _encode_to_file(ImagingEncoderObject* encoder, PyObject* args) Py_ssize_t fh; Py_ssize_t bufsize = 16384; - if (!PyArg_ParseTuple(args, "n|n", &fh, &bufsize)) + if (!PyArg_ParseTuple(args, "n|n", &fh, &bufsize)) { return NULL; + } /* Allocate an encoder buffer */ /* malloc check ok, either constant int, or checked by PyArg_ParseTuple */ buf = (UINT8*) malloc(bufsize); - if (!buf) + if (!buf) { return PyErr_NoMemory(); + } ImagingSectionEnter(&cookie); @@ -197,12 +206,13 @@ _encode_to_file(ImagingEncoderObject* encoder, PyObject* args) status = encoder->encode(encoder->im, &encoder->state, buf, bufsize); - if (status > 0) + if (status > 0) { if (write(fh, buf, status) < 0) { ImagingSectionLeave(&cookie); free(buf); return PyErr_SetFromErrno(PyExc_OSError); } + } } while (encoder->state.errcode == 0); @@ -228,11 +238,13 @@ _setimage(ImagingEncoderObject* encoder, PyObject* args) x0 = y0 = x1 = y1 = 0; /* FIXME: should publish the ImagingType descriptor */ - if (!PyArg_ParseTuple(args, "O|(nnnn)", &op, &x0, &y0, &x1, &y1)) + if (!PyArg_ParseTuple(args, "O|(nnnn)", &op, &x0, &y0, &x1, &y1)) { return NULL; + } im = PyImaging_AsImaging(op); - if (!im) + if (!im) { return NULL; + } encoder->im = im; @@ -264,8 +276,9 @@ _setimage(ImagingEncoderObject* encoder, PyObject* args) state->bytes = (state->bits * state->xsize+7)/8; /* malloc check ok, overflow checked above */ state->buffer = (UINT8*) malloc(state->bytes); - if (!state->buffer) + if (!state->buffer) { return PyErr_NoMemory(); + } } /* Keep a reference to the image object, to make sure it doesn't @@ -284,8 +297,9 @@ _setfd(ImagingEncoderObject* encoder, PyObject* args) PyObject* fd; ImagingCodecState state; - if (!PyArg_ParseTuple(args, "O", &fd)) + if (!PyArg_ParseTuple(args, "O", &fd)) { return NULL; + } state = &encoder->state; @@ -386,8 +400,9 @@ PyImaging_EpsEncoderNew(PyObject* self, PyObject* args) ImagingEncoderObject* encoder; encoder = PyImaging_EncoderNew(0); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } encoder->encode = ImagingEpsEncode; @@ -408,15 +423,18 @@ PyImaging_GifEncoderNew(PyObject* self, PyObject* args) char *rawmode; Py_ssize_t bits = 8; Py_ssize_t interlace = 0; - if (!PyArg_ParseTuple(args, "ss|nn", &mode, &rawmode, &bits, &interlace)) + if (!PyArg_ParseTuple(args, "ss|nn", &mode, &rawmode, &bits, &interlace)) { return NULL; + } encoder = PyImaging_EncoderNew(sizeof(GIFENCODERSTATE)); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } - if (get_packer(encoder, mode, rawmode) < 0) + if (get_packer(encoder, mode, rawmode) < 0) { return NULL; + } encoder->encode = ImagingGifEncode; @@ -473,15 +491,18 @@ PyImaging_RawEncoderNew(PyObject* self, PyObject* args) Py_ssize_t stride = 0; Py_ssize_t ystep = 1; - if (!PyArg_ParseTuple(args, "ss|nn", &mode, &rawmode, &stride, &ystep)) + if (!PyArg_ParseTuple(args, "ss|nn", &mode, &rawmode, &stride, &ystep)) { return NULL; + } encoder = PyImaging_EncoderNew(0); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } - if (get_packer(encoder, mode, rawmode) < 0) + if (get_packer(encoder, mode, rawmode) < 0) { return NULL; + } encoder->encode = ImagingRawEncode; @@ -505,15 +526,18 @@ PyImaging_TgaRleEncoderNew(PyObject* self, PyObject* args) char *rawmode; Py_ssize_t ystep = 1; - if (!PyArg_ParseTuple(args, "ss|n", &mode, &rawmode, &ystep)) + if (!PyArg_ParseTuple(args, "ss|n", &mode, &rawmode, &ystep)) { return NULL; + } encoder = PyImaging_EncoderNew(0); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } - if (get_packer(encoder, mode, rawmode) < 0) + if (get_packer(encoder, mode, rawmode) < 0) { return NULL; + } encoder->encode = ImagingTgaRleEncode; @@ -534,11 +558,13 @@ PyImaging_XbmEncoderNew(PyObject* self, PyObject* args) ImagingEncoderObject* encoder; encoder = PyImaging_EncoderNew(0); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } - if (get_packer(encoder, "1", "1;R") < 0) + if (get_packer(encoder, "1", "1;R") < 0) { return NULL; + } encoder->encode = ImagingXbmEncode; @@ -569,19 +595,22 @@ PyImaging_ZipEncoderNew(PyObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "ss|nnny#", &mode, &rawmode, &optimize, &compress_level, &compress_type, - &dictionary, &dictionary_size)) + &dictionary, &dictionary_size)) { return NULL; + } /* Copy to avoid referencing Python's memory */ if (dictionary && dictionary_size > 0) { /* malloc check ok, size comes from PyArg_ParseTuple */ char* p = malloc(dictionary_size); - if (!p) + if (!p) { return PyErr_NoMemory(); + } memcpy(p, dictionary, dictionary_size); dictionary = p; - } else + } else { dictionary = NULL; + } encoder = PyImaging_EncoderNew(sizeof(ZIPSTATE)); if (encoder == NULL) { @@ -597,9 +626,10 @@ PyImaging_ZipEncoderNew(PyObject* self, PyObject* args) encoder->encode = ImagingZipEncode; encoder->cleanup = ImagingZipEncodeCleanup; - if (rawmode[0] == 'P') + if (rawmode[0] == 'P') { /* disable filtering */ ((ZIPSTATE*)encoder->state.context)->mode = ZIP_PNG_PALETTE; + } ((ZIPSTATE*)encoder->state.context)->optimize = optimize; ((ZIPSTATE*)encoder->state.context)->compress_level = compress_level; @@ -675,11 +705,13 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args) TRACE(("new tiff encoder %s fp: %d, filename: %s \n", compname, fp, filename)); encoder = PyImaging_EncoderNew(sizeof(TIFFSTATE)); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } - if (get_packer(encoder, mode, rawmode) < 0) + if (get_packer(encoder, mode, rawmode) < 0) { return NULL; + } if (! ImagingLibTiffEncodeInit(&encoder->state, filename, fp)) { Py_DECREF(encoder); @@ -1027,12 +1059,14 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) &mode, &rawmode, &quality, &progressive, &smooth, &optimize, &streamtype, &xdpi, &ydpi, &subsampling, &qtables, &extra, &extra_size, - &rawExif, &rawExifLen)) + &rawExif, &rawExifLen)) { return NULL; + } encoder = PyImaging_EncoderNew(sizeof(JPEGENCODERSTATE)); - if (encoder == NULL) + if (encoder == NULL) { return NULL; + } // libjpeg-turbo supports different output formats. // We are choosing Pillow's native format (3 color bytes + 1 padding) @@ -1041,8 +1075,9 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) rawmode = "RGBX"; } - if (get_packer(encoder, mode, rawmode) < 0) + if (get_packer(encoder, mode, rawmode) < 0) { return NULL; + } // Freed in JpegEncode, Case 5 qarrays = get_qtables_arrays(qtables, &qtablesLen); @@ -1054,8 +1089,9 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) return PyErr_NoMemory(); memcpy(p, extra, extra_size); extra = p; - } else + } else { extra = NULL; + } if (rawExif && rawExifLen > 0) { /* malloc check ok, length is from python parsearg */ @@ -1066,8 +1102,9 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) } memcpy(pp, rawExif, rawExifLen); rawExif = pp; - } else + } else { rawExif = NULL; + } encoder->encode = ImagingJpegEncode; @@ -1111,10 +1148,12 @@ j2k_decode_coord_tuple(PyObject *tuple, int *x, int *y) *x = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 0)); *y = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 1)); - if (*x < 0) + if (*x < 0) { *x = 0; - if (*y < 0) + } + if (*y < 0) { *y = 0; + } } } @@ -1144,45 +1183,50 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) &quality_mode, &quality_layers, &num_resolutions, &cblk_size, &precinct_size, &irreversible, &progression, &cinema_mode, - &fd)) + &fd)) { return NULL; + } - if (strcmp (format, "j2k") == 0) + if (strcmp (format, "j2k") == 0) { codec_format = OPJ_CODEC_J2K; - else if (strcmp (format, "jpt") == 0) + } else if (strcmp (format, "jpt") == 0) { codec_format = OPJ_CODEC_JPT; - else if (strcmp (format, "jp2") == 0) + } else if (strcmp (format, "jp2") == 0) { codec_format = OPJ_CODEC_JP2; - else + } else { return NULL; + } - if (strcmp(progression, "LRCP") == 0) + if (strcmp(progression, "LRCP") == 0) { prog_order = OPJ_LRCP; - else if (strcmp(progression, "RLCP") == 0) + } else if (strcmp(progression, "RLCP") == 0) { prog_order = OPJ_RLCP; - else if (strcmp(progression, "RPCL") == 0) + } else if (strcmp(progression, "RPCL") == 0) { prog_order = OPJ_RPCL; - else if (strcmp(progression, "PCRL") == 0) + } else if (strcmp(progression, "PCRL") == 0) { prog_order = OPJ_PCRL; - else if (strcmp(progression, "CPRL") == 0) + } else if (strcmp(progression, "CPRL") == 0) { prog_order = OPJ_CPRL; - else + } else { return NULL; + } - if (strcmp(cinema_mode, "no") == 0) + if (strcmp(cinema_mode, "no") == 0) { cine_mode = OPJ_OFF; - else if (strcmp(cinema_mode, "cinema2k-24") == 0) + } else if (strcmp(cinema_mode, "cinema2k-24") == 0) { cine_mode = OPJ_CINEMA2K_24; - else if (strcmp(cinema_mode, "cinema2k-48") == 0) + } else if (strcmp(cinema_mode, "cinema2k-48") == 0) { cine_mode = OPJ_CINEMA2K_48; - else if (strcmp(cinema_mode, "cinema4k-24") == 0) + } else if (strcmp(cinema_mode, "cinema4k-24") == 0) { cine_mode = OPJ_CINEMA4K_24; - else + } else { return NULL; + } encoder = PyImaging_EncoderNew(sizeof(JPEG2KENCODESTATE)); - if (!encoder) + if (!encoder) { return NULL; + } encoder->encode = ImagingJpeg2KEncode; encoder->cleanup = ImagingJpeg2KEncodeCleanup; diff --git a/src/libImaging/Access.c b/src/libImaging/Access.c index 15ffa11fc..755e2639a 100644 --- a/src/libImaging/Access.c +++ b/src/libImaging/Access.c @@ -22,8 +22,9 @@ static inline UINT32 hash(const char* mode) { UINT32 i = ACCESS_TABLE_HASH; - while (*mode) + while (*mode) { i = ((i<<5) + i) ^ (UINT8) *mode++; + } return i % ACCESS_TABLE_SIZE; } @@ -149,10 +150,11 @@ get_pixel_32B(Imaging im, int x, int y, void* color) static void put_pixel(Imaging im, int x, int y, const void* color) { - if (im->image8) + if (im->image8) { im->image8[y][x] = *((UINT8*) color); - else + } else { memcpy(&im->image32[y][x], color, sizeof(INT32)); + } } static void @@ -237,8 +239,9 @@ ImagingAccess ImagingAccessNew(Imaging im) { ImagingAccess access = &access_table[hash(im->mode)]; - if (im->mode[0] != access->mode[0] || strcmp(im->mode, access->mode) != 0) + if (im->mode[0] != access->mode[0] || strcmp(im->mode, access->mode) != 0) { return NULL; + } return access; } diff --git a/src/libImaging/AlphaComposite.c b/src/libImaging/AlphaComposite.c index a074334aa..20b1df9e5 100644 --- a/src/libImaging/AlphaComposite.c +++ b/src/libImaging/AlphaComposite.c @@ -33,19 +33,22 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) if (!imDst || !imSrc || strcmp(imDst->mode, "RGBA") || imDst->type != IMAGING_TYPE_UINT8 || - imDst->bands != 4) + imDst->bands != 4) { return ImagingError_ModeError(); + } if (strcmp(imDst->mode, imSrc->mode) || imDst->type != imSrc->type || imDst->bands != imSrc->bands || imDst->xsize != imSrc->xsize || - imDst->ysize != imSrc->ysize) + imDst->ysize != imSrc->ysize) { return ImagingError_Mismatch(); + } imOut = ImagingNewDirty(imDst->mode, imDst->xsize, imDst->ysize); - if (!imOut) + if (!imOut) { return NULL; + } for (y = 0; y < imDst->ysize; y++) { rgba8* dst = (rgba8*) imDst->image[y]; diff --git a/src/libImaging/Bands.c b/src/libImaging/Bands.c index 7fff04486..39ce5c49c 100644 --- a/src/libImaging/Bands.c +++ b/src/libImaging/Bands.c @@ -26,23 +26,28 @@ ImagingGetBand(Imaging imIn, int band) int x, y; /* Check arguments */ - if (!imIn || imIn->type != IMAGING_TYPE_UINT8) + if (!imIn || imIn->type != IMAGING_TYPE_UINT8) { return (Imaging) ImagingError_ModeError(); + } - if (band < 0 || band >= imIn->bands) + if (band < 0 || band >= imIn->bands) { return (Imaging) ImagingError_ValueError("band index out of range"); + } /* Shortcuts */ - if (imIn->bands == 1) + if (imIn->bands == 1) { return ImagingCopy(imIn); + } /* Special case for LXXA etc */ - if (imIn->bands == 2 && band == 1) + if (imIn->bands == 2 && band == 1) { band = 3; + } imOut = ImagingNewDirty("L", imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } /* Extract band from image */ for (y = 0; y < imIn->ysize; y++) { @@ -173,24 +178,29 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) int x, y; /* Check arguments */ - if (!imIn || imIn->bands != 1 || !imOut) + if (!imIn || imIn->bands != 1 || !imOut) { return (Imaging) ImagingError_ModeError(); + } - if (band < 0 || band >= imOut->bands) + if (band < 0 || band >= imOut->bands) { return (Imaging) ImagingError_ValueError("band index out of range"); + } if (imIn->type != imOut->type || imIn->xsize != imOut->xsize || - imIn->ysize != imOut->ysize) + imIn->ysize != imOut->ysize) { return (Imaging) ImagingError_Mismatch(); + } /* Shortcuts */ - if (imOut->bands == 1) + if (imOut->bands == 1) { return ImagingCopy2(imOut, imIn); + } /* Special case for LXXA etc */ - if (imOut->bands == 2 && band == 1) + if (imOut->bands == 2 && band == 1) { band = 3; + } /* Insert band into image */ for (y = 0; y < imIn->ysize; y++) { @@ -211,15 +221,18 @@ ImagingFillBand(Imaging imOut, int band, int color) int x, y; /* Check arguments */ - if (!imOut || imOut->type != IMAGING_TYPE_UINT8) + if (!imOut || imOut->type != IMAGING_TYPE_UINT8) { return (Imaging) ImagingError_ModeError(); + } - if (band < 0 || band >= imOut->bands) + if (band < 0 || band >= imOut->bands) { return (Imaging) ImagingError_ValueError("band index out of range"); + } /* Special case for LXXA etc */ - if (imOut->bands == 2 && band == 1) + if (imOut->bands == 2 && band == 1) { band = 3; + } color = CLIP8(color); @@ -263,16 +276,18 @@ ImagingMerge(const char* mode, Imaging bands[4]) bandsCount = i; imOut = ImagingNewDirty(mode, firstBand->xsize, firstBand->ysize); - if ( ! imOut) + if ( ! imOut) { return NULL; + } if (imOut->bands != bandsCount) { ImagingDelete(imOut); return (Imaging) ImagingError_ValueError("wrong number of bands"); } - if (imOut->bands == 1) + if (imOut->bands == 1) { return ImagingCopy2(imOut, firstBand); + } if (imOut->bands == 2) { for (y = 0; y < imOut->ysize; y++) { diff --git a/src/libImaging/BcnDecode.c b/src/libImaging/BcnDecode.c index 3e62aa6c9..f908a03ad 100644 --- a/src/libImaging/BcnDecode.c +++ b/src/libImaging/BcnDecode.c @@ -610,15 +610,21 @@ static int bc6_unquantize(UINT16 v, int prec, int sign) { int x; if (!sign) { x = v; - if (prec >= 15) return x; - if (x == 0) return 0; + if (prec >= 15) { + return x; + } + if (x == 0) { + return 0; + } if (x == ((1 << prec) - 1)) { return 0xffff; } return ((x << 15) + 0x4000) >> (prec - 1); } else { x = (INT16)v; - if (prec >= 16) return x; + if (prec >= 16) { + return x; + } if (x < 0) { s = 1; x = -x; @@ -820,7 +826,9 @@ static int decode_bcn(Imaging im, ImagingCodecState state, const UINT8* src, int put_block(im, state, (const char *)col, sizeof(col[0]), C); \ ptr += SZ; \ bytes -= SZ; \ - if (state->y >= ymax) return -1; \ + if (state->y >= ymax) {\ + return -1; \ + }\ } \ break @@ -836,7 +844,9 @@ static int decode_bcn(Imaging im, ImagingCodecState state, const UINT8* src, int put_block(im, state, (const char *)col, sizeof(col[0]), C); ptr += 16; bytes -= 16; - if (state->y >= ymax) return -1; \ + if (state->y >= ymax) {\ + return -1; \ + }\ } break; DECODE_LOOP(7, 16, rgba); diff --git a/src/libImaging/BitDecode.c b/src/libImaging/BitDecode.c index 97d263c0b..92edd746f 100644 --- a/src/libImaging/BitDecode.c +++ b/src/libImaging/BitDecode.c @@ -43,15 +43,17 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt bitstate->mask = (1<bits)-1; - if (bitstate->sign) + if (bitstate->sign) { bitstate->signmask = (1<<(bitstate->bits-1)); + } /* check image orientation */ if (state->ystep < 0) { state->y = state->ysize-1; state->ystep = -1; - } else + } else { state->ystep = 1; + } state->state = 1; @@ -67,12 +69,13 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt bytes--; /* get a byte from the input stream and insert in the bit buffer */ - if (bitstate->fill&1) + if (bitstate->fill&1) { /* fill MSB first */ bitstate->bitbuffer |= (unsigned long) byte << bitstate->bitcount; - else + } else { /* fill LSB first */ bitstate->bitbuffer = (bitstate->bitbuffer << 8) | byte; + } bitstate->bitcount += 8; @@ -85,35 +88,39 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt if (bitstate->fill&2) { /* store LSB first */ data = bitstate->bitbuffer & bitstate->mask; - if (bitstate->bitcount > 32) + if (bitstate->bitcount > 32) { /* bitbuffer overflow; restore it from last input byte */ bitstate->bitbuffer = byte >> (8 - (bitstate->bitcount - bitstate->bits)); - else + } else { bitstate->bitbuffer >>= bitstate->bits; - } else + } + } else { /* store MSB first */ data = (bitstate->bitbuffer >> (bitstate->bitcount - bitstate->bits)) & bitstate->mask; + } bitstate->bitcount -= bitstate->bits; if (bitstate->lutsize > 0) { /* map through lookup table */ - if (data <= 0) + if (data <= 0) { pixel = bitstate->lut[0]; - else if (data >= bitstate->lutsize) + } else if (data >= bitstate->lutsize) { pixel = bitstate->lut[bitstate->lutsize-1]; - else + } else { pixel = bitstate->lut[data]; + } } else { /* convert */ - if (data & bitstate->signmask) + if (data & bitstate->signmask) { /* image memory contains signed data */ pixel = (FLOAT32) (INT32) (data | ~bitstate->mask); - else + } else { pixel = (FLOAT32) data; + } } *(FLOAT32*)(&im->image32[state->y][state->x]) = pixel; @@ -128,8 +135,9 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt } state->x = 0; /* reset bit buffer */ - if (bitstate->pad > 0) + if (bitstate->pad > 0) { bitstate->bitcount = 0; + } } } } diff --git a/src/libImaging/Blend.c b/src/libImaging/Blend.c index caaf2ba8b..0bac4cda9 100644 --- a/src/libImaging/Blend.c +++ b/src/libImaging/Blend.c @@ -28,24 +28,28 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) /* Check arguments */ if (!imIn1 || !imIn2 || imIn1->type != IMAGING_TYPE_UINT8 || imIn1->palette || strcmp(imIn1->mode, "1") == 0 - || imIn2->palette || strcmp(imIn2->mode, "1") == 0) + || imIn2->palette || strcmp(imIn2->mode, "1") == 0) { return ImagingError_ModeError(); + } if (imIn1->type != imIn2->type || imIn1->bands != imIn2->bands || imIn1->xsize != imIn2->xsize || - imIn1->ysize != imIn2->ysize) + imIn1->ysize != imIn2->ysize) { return ImagingError_Mismatch(); + } /* Shortcuts */ - if (alpha == 0.0) + if (alpha == 0.0) { return ImagingCopy(imIn1); - else if (alpha == 1.0) + } else if (alpha == 1.0) { return ImagingCopy(imIn2); + } imOut = ImagingNewDirty(imIn1->mode, imIn1->xsize, imIn1->ysize); - if (!imOut) + if (!imOut) { return NULL; + } if (alpha >= 0 && alpha <= 1.0) { /* Interpolate between bands */ @@ -53,9 +57,10 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) UINT8* in1 = (UINT8*) imIn1->image[y]; UINT8* in2 = (UINT8*) imIn2->image[y]; UINT8* out = (UINT8*) imOut->image[y]; - for (x = 0; x < imIn1->linesize; x++) + for (x = 0; x < imIn1->linesize; x++) { out[x] = (UINT8) ((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x])); + } } } else { /* Extrapolation; must make sure to clip resulting values */ @@ -66,12 +71,13 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) for (x = 0; x < imIn1->linesize; x++) { float temp = (float) ((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x])); - if (temp <= 0.0) + if (temp <= 0.0) { out[x] = 0; - else if (temp >= 255.0) + } else if (temp >= 255.0) { out[x] = 255; - else + } else { out[x] = (UINT8) temp; + } } } } diff --git a/src/libImaging/BoxBlur.c b/src/libImaging/BoxBlur.c index 9537c4f98..dcdc52cbc 100644 --- a/src/libImaging/BoxBlur.c +++ b/src/libImaging/BoxBlur.c @@ -184,8 +184,9 @@ ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius) int edgeB = MAX(imIn->xsize - radius - 1, 0); UINT32 *lineOut = calloc(imIn->xsize, sizeof(UINT32)); - if (lineOut == NULL) + if (lineOut == NULL) { return ImagingError_MemoryError(); + } // printf(">>> %d %d %d\n", radius, ww, fw); @@ -248,11 +249,13 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float radius, int n) imIn->type != imOut->type || imIn->bands != imOut->bands || imIn->xsize != imOut->xsize || - imIn->ysize != imOut->ysize) + imIn->ysize != imOut->ysize) { return ImagingError_Mismatch(); + } - if (imIn->type != IMAGING_TYPE_UINT8) + if (imIn->type != IMAGING_TYPE_UINT8) { return ImagingError_ModeError(); + } if (!(strcmp(imIn->mode, "RGB") == 0 || strcmp(imIn->mode, "RGBA") == 0 || @@ -261,12 +264,14 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float radius, int n) strcmp(imIn->mode, "CMYK") == 0 || strcmp(imIn->mode, "L") == 0 || strcmp(imIn->mode, "LA") == 0 || - strcmp(imIn->mode, "La") == 0)) + strcmp(imIn->mode, "La") == 0)) { return ImagingError_ModeError(); + } imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize); - if (!imTransposed) + if (!imTransposed) { return NULL; + } /* Apply blur in one dimension. Use imOut as a destination at first pass, diff --git a/src/libImaging/Chops.c b/src/libImaging/Chops.c index a1673dff6..9dd708f75 100644 --- a/src/libImaging/Chops.c +++ b/src/libImaging/Chops.c @@ -23,20 +23,22 @@ int x, y;\ Imaging imOut;\ imOut = create(imIn1, imIn2, mode);\ - if (!imOut)\ + if (!imOut) {\ return NULL;\ + }\ for (y = 0; y < imOut->ysize; y++) {\ UINT8* out = (UINT8*) imOut->image[y];\ UINT8* in1 = (UINT8*) imIn1->image[y];\ UINT8* in2 = (UINT8*) imIn2->image[y];\ for (x = 0; x < imOut->linesize; x++) {\ int temp = operation;\ - if (temp <= 0)\ + if (temp <= 0) {\ out[x] = 0;\ - else if (temp >= 255)\ + } else if (temp >= 255) {\ out[x] = 255;\ - else\ + } else {\ out[x] = temp;\ + }\ }\ }\ return imOut; @@ -45,8 +47,9 @@ int x, y;\ Imaging imOut;\ imOut = create(imIn1, imIn2, mode);\ - if (!imOut)\ + if (!imOut) {\ return NULL;\ + }\ for (y = 0; y < imOut->ysize; y++) {\ UINT8* out = (UINT8*) imOut->image[y];\ UINT8* in1 = (UINT8*) imIn1->image[y];\ @@ -63,11 +66,13 @@ create(Imaging im1, Imaging im2, char* mode) int xsize, ysize; if (!im1 || !im2 || im1->type != IMAGING_TYPE_UINT8 || - (mode != NULL && (strcmp(im1->mode, "1") || strcmp(im2->mode, "1")))) + (mode != NULL && (strcmp(im1->mode, "1") || strcmp(im2->mode, "1")))) { return (Imaging) ImagingError_ModeError(); + } if (im1->type != im2->type || - im1->bands != im2->bands) + im1->bands != im2->bands) { return (Imaging) ImagingError_Mismatch(); + } xsize = (im1->xsize < im2->xsize) ? im1->xsize : im2->xsize; ysize = (im1->ysize < im2->ysize) ? im1->ysize : im2->ysize; diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 9f5722543..5e69b067d 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -123,8 +123,9 @@ static void l2bit(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++) + for (x = 0; x < xsize; x++) { *out++ = (*in++ >= 128) ? 255 : 0; + } } static void @@ -206,8 +207,9 @@ static void la2l(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { *out++ = in[0]; + } } static void @@ -240,18 +242,20 @@ static void rgb2bit(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */ *out++ = (L(in) >= 128000) ? 255 : 0; + } } static void rgb2l(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */ *out++ = L24(in) >> 16; + } } static void @@ -653,12 +657,13 @@ i2l(UINT8* out, const UINT8* in_, int xsize) for (x = 0; x < xsize; x++, out++, in_ += 4) { INT32 v; memcpy(&v, in_, sizeof(v)); - if (v <= 0) + if (v <= 0) { *out = 0; - else if (v >= 255) + } else if (v >= 255) { *out = 255; - else + } else { *out = (UINT8) v; + } } } @@ -681,12 +686,13 @@ i2rgb(UINT8* out, const UINT8* in_, int xsize) int x; INT32* in = (INT32*) in_; for (x = 0; x < xsize; x++, in++, out+=4) { - if (*in <= 0) + if (*in <= 0) { out[0] = out[1] = out[2] = 0; - else if (*in >= 255) + } else if (*in >= 255) { out[0] = out[1] = out[2] = 255; - else + } else { out[0] = out[1] = out[2] = (UINT8) *in; + } out[3] = 255; } } @@ -741,12 +747,13 @@ f2l(UINT8* out, const UINT8* in_, int xsize) for (x = 0; x < xsize; x++, out++, in_ += 4) { FLOAT32 v; memcpy(&v, in_, sizeof(v)); - if (v <= 0.0) + if (v <= 0.0) { *out = 0; - else if (v >= 255.0) + } else if (v >= 255.0) { *out = 255; - else + } else { *out = (UINT8) v; + } } } @@ -797,8 +804,9 @@ static void ycbcr2l(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { *out++ = in[0]; + } } static void @@ -908,22 +916,26 @@ static void I16L_L(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++, in += 2) - if (in[1] != 0) + for (x = 0; x < xsize; x++, in += 2) { + if (in[1] != 0) { *out++ = 255; - else + } else { *out++ = in[0]; + } + } } static void I16B_L(UINT8* out, const UINT8* in, int xsize) { int x; - for (x = 0; x < xsize; x++, in += 2) - if (in[0] != 0) + for (x = 0; x < xsize; x++, in += 2) { + if (in[0] != 0) { *out++ = 255; - else + } else { *out++ = in[1]; + } + } } static struct { @@ -1056,8 +1068,9 @@ p2bit(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) { int x; /* FIXME: precalculate greyscale palette? */ - for (x = 0; x < xsize; x++) + for (x = 0; x < xsize; x++) { *out++ = (L(&palette[in[x]*4]) >= 128000) ? 255 : 0; + } } static void @@ -1065,8 +1078,9 @@ pa2bit(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) { int x; /* FIXME: precalculate greyscale palette? */ - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { *out++ = (L(&palette[in[0]*4]) >= 128000) ? 255 : 0; + } } static void @@ -1074,8 +1088,9 @@ p2l(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) { int x; /* FIXME: precalculate greyscale palette? */ - for (x = 0; x < xsize; x++) + for (x = 0; x < xsize; x++) { *out++ = L(&palette[in[x]*4]) / 1000; + } } static void @@ -1083,8 +1098,9 @@ pa2l(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) { int x; /* FIXME: precalculate greyscale palette? */ - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { *out++ = L(&palette[in[0]*4]) / 1000; + } } static void @@ -1138,8 +1154,9 @@ pa2i(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette) { int x; INT32* out = (INT32*) out_; - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { *out++ = L(&palette[in[0]*4]) / 1000; + } } static void @@ -1157,8 +1174,9 @@ pa2f(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette) { int x; FLOAT32* out = (FLOAT32*) out_; - for (x = 0; x < xsize; x++, in += 4) + for (x = 0; x < xsize; x++, in += 4) { *out++ = (float) L(&palette[in[0]*4]) / 1000.0F; + } } static void @@ -1273,46 +1291,50 @@ frompalette(Imaging imOut, Imaging imIn, const char *mode) /* Map palette image to L, RGB, RGBA, or CMYK */ - if (!imIn->palette) + if (!imIn->palette) { return (Imaging) ImagingError_ValueError("no palette"); + } alpha = !strcmp(imIn->mode, "PA"); - if (strcmp(mode, "1") == 0) + if (strcmp(mode, "1") == 0) { convert = alpha ? pa2bit : p2bit; - else if (strcmp(mode, "L") == 0) + } else if (strcmp(mode, "L") == 0) { convert = alpha ? pa2l : p2l; - else if (strcmp(mode, "LA") == 0) + } else if (strcmp(mode, "LA") == 0) { convert = alpha ? pa2la : p2la; - else if (strcmp(mode, "PA") == 0) + } else if (strcmp(mode, "PA") == 0) { convert = p2pa; - else if (strcmp(mode, "I") == 0) + } else if (strcmp(mode, "I") == 0) { convert = alpha ? pa2i : p2i; - else if (strcmp(mode, "F") == 0) + } else if (strcmp(mode, "F") == 0) { convert = alpha ? pa2f : p2f; - else if (strcmp(mode, "RGB") == 0) + } else if (strcmp(mode, "RGB") == 0) { convert = alpha ? pa2rgb : p2rgb; - else if (strcmp(mode, "RGBA") == 0) + } else if (strcmp(mode, "RGBA") == 0) { convert = alpha ? pa2rgba : p2rgba; - else if (strcmp(mode, "RGBX") == 0) + } else if (strcmp(mode, "RGBX") == 0) { convert = alpha ? pa2rgba : p2rgba; - else if (strcmp(mode, "CMYK") == 0) + } else if (strcmp(mode, "CMYK") == 0) { convert = alpha ? pa2cmyk : p2cmyk; - else if (strcmp(mode, "YCbCr") == 0) + } else if (strcmp(mode, "YCbCr") == 0) { convert = alpha ? pa2ycbcr : p2ycbcr; - else if (strcmp(mode, "HSV") == 0) + } else if (strcmp(mode, "HSV") == 0) { convert = alpha ? pa2hsv : p2hsv; - else + } else { return (Imaging) ImagingError_ValueError("conversion not supported"); + } imOut = ImagingNew2Dirty(mode, imOut, imIn); - if (!imOut) + if (!imOut) { return NULL; + } ImagingSectionEnter(&cookie); - for (y = 0; y < imIn->ysize; y++) + for (y = 0; y < imIn->ysize; y++) { (*convert)((UINT8*) imOut->image[y], (UINT8*) imIn->image[y], imIn->xsize, imIn->palette->palette); + } ImagingSectionLeave(&cookie); return imOut; @@ -1330,26 +1352,30 @@ topalette(Imaging imOut, Imaging imIn, const char *mode, ImagingPalette inpalett ImagingPalette palette = inpalette;; /* Map L or RGB/RGBX/RGBA to palette image */ - if (strcmp(imIn->mode, "L") != 0 && strncmp(imIn->mode, "RGB", 3) != 0) + if (strcmp(imIn->mode, "L") != 0 && strncmp(imIn->mode, "RGB", 3) != 0) { return (Imaging) ImagingError_ValueError("conversion not supported"); + } alpha = !strcmp(mode, "PA"); if (palette == NULL) { /* FIXME: make user configurable */ - if (imIn->bands == 1) + if (imIn->bands == 1) { palette = ImagingPaletteNew("RGB"); /* Initialised to grey ramp */ - else + } else { palette = ImagingPaletteNewBrowser(); /* Standard colour cube */ + } } - if (!palette) + if (!palette) { return (Imaging) ImagingError_ValueError("no palette"); + } imOut = ImagingNew2Dirty(mode, imOut, imIn); if (!imOut) { - if (palette != inpalette) + if (palette != inpalette) { ImagingPaletteDelete(palette); + } return NULL; } @@ -1376,8 +1402,9 @@ topalette(Imaging imOut, Imaging imIn, const char *mode, ImagingPalette inpalett /* Create mapping cache */ if (ImagingPaletteCachePrepare(palette) < 0) { ImagingDelete(imOut); - if (palette != inpalette) + if (palette != inpalette) { ImagingPaletteDelete(palette); + } return NULL; } @@ -1415,8 +1442,9 @@ topalette(Imaging imOut, Imaging imIn, const char *mode, ImagingPalette inpalett /* get closest colour */ cache = &ImagingPaletteCache(palette, r, g, b); - if (cache[0] == 0x100) + if (cache[0] == 0x100) { ImagingPaletteCacheUpdate(palette, r, g, b); + } if (alpha) { out[x*4] = out[x*4+1] = out[x*4+2] = (UINT8) cache[0]; out[x*4+3] = 255; @@ -1464,8 +1492,9 @@ topalette(Imaging imOut, Imaging imIn, const char *mode, ImagingPalette inpalett /* get closest colour */ cache = &ImagingPaletteCache(palette, r, g, b); - if (cache[0] == 0x100) + if (cache[0] == 0x100) { ImagingPaletteCacheUpdate(palette, r, g, b); + } if (alpha) { out[x*4] = out[x*4+1] = out[x*4+2] = (UINT8) cache[0]; out[x*4+3] = 255; @@ -1477,12 +1506,14 @@ topalette(Imaging imOut, Imaging imIn, const char *mode, ImagingPalette inpalett ImagingSectionLeave(&cookie); } - if (inpalette != palette) + if (inpalette != palette) { ImagingPaletteCacheDelete(palette); + } } - if (inpalette != palette) + if (inpalette != palette) { ImagingPaletteDelete(palette); + } return imOut; } @@ -1495,12 +1526,14 @@ tobilevel(Imaging imOut, Imaging imIn, int dither) int* errors; /* Map L or RGB to dithered 1 image */ - if (strcmp(imIn->mode, "L") != 0 && strcmp(imIn->mode, "RGB") != 0) + if (strcmp(imIn->mode, "L") != 0 && strcmp(imIn->mode, "RGB") != 0) { return (Imaging) ImagingError_ValueError("conversion not supported"); + } imOut = ImagingNew2Dirty("1", imOut, imIn); - if (!imOut) + if (!imOut) { return NULL; + } errors = calloc(imIn->xsize + 1, sizeof(int)); if (!errors) { @@ -1582,42 +1615,50 @@ convert(Imaging imOut, Imaging imIn, const char *mode, ImagingShuffler convert; int y; - if (!imIn) + if (!imIn) { return (Imaging) ImagingError_ModeError(); + } if (!mode) { /* Map palette image to full depth */ - if (!imIn->palette) + if (!imIn->palette) { return (Imaging) ImagingError_ModeError(); + } mode = imIn->palette->mode; - } else + } else { /* Same mode? */ - if (!strcmp(imIn->mode, mode)) + if (!strcmp(imIn->mode, mode)) { return ImagingCopy2(imOut, imIn); + } + } /* test for special conversions */ - if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "PA") == 0) + if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "PA") == 0) { return frompalette(imOut, imIn, mode); + } - if (strcmp(mode, "P") == 0 || strcmp(mode, "PA") == 0) + if (strcmp(mode, "P") == 0 || strcmp(mode, "PA") == 0) { return topalette(imOut, imIn, mode, palette, dither); + } - if (dither && strcmp(mode, "1") == 0) + if (dither && strcmp(mode, "1") == 0) { return tobilevel(imOut, imIn, dither); + } /* standard conversion machinery */ convert = NULL; - for (y = 0; converters[y].from; y++) + for (y = 0; converters[y].from; y++) { if (!strcmp(imIn->mode, converters[y].from) && !strcmp(mode, converters[y].to)) { convert = converters[y].convert; break; } + } if (!convert) #ifdef notdef @@ -1632,13 +1673,15 @@ convert(Imaging imOut, Imaging imIn, const char *mode, #endif imOut = ImagingNew2Dirty(mode, imOut, imIn); - if (!imOut) + if (!imOut) { return NULL; + } ImagingSectionEnter(&cookie); - for (y = 0; y < imIn->ysize; y++) + for (y = 0; y < imIn->ysize; y++) { (*convert)((UINT8*) imOut->image[y], (UINT8*) imIn->image[y], imIn->xsize); + } ImagingSectionLeave(&cookie); return imOut; @@ -1727,17 +1770,19 @@ ImagingConvertInPlace(Imaging imIn, const char* mode) int y; /* limited support for inplace conversion */ - if (strcmp(imIn->mode, "L") == 0 && strcmp(mode, "1") == 0) + if (strcmp(imIn->mode, "L") == 0 && strcmp(mode, "1") == 0) { convert = l2bit; - else if (strcmp(imIn->mode, "1") == 0 && strcmp(mode, "L") == 0) + } else if (strcmp(imIn->mode, "1") == 0 && strcmp(mode, "L") == 0) { convert = bit2l; - else + } else { return ImagingError_ModeError(); + } ImagingSectionEnter(&cookie); - for (y = 0; y < imIn->ysize; y++) + for (y = 0; y < imIn->ysize; y++) { (*convert)((UINT8*) imIn->image[y], (UINT8*) imIn->image[y], imIn->xsize); + } ImagingSectionLeave(&cookie); return imIn; diff --git a/src/libImaging/Copy.c b/src/libImaging/Copy.c index e0bdaf63e..5b4899f39 100644 --- a/src/libImaging/Copy.c +++ b/src/libImaging/Copy.c @@ -25,21 +25,25 @@ _copy(Imaging imOut, Imaging imIn) ImagingSectionCookie cookie; int y; - if (!imIn) + if (!imIn) { return (Imaging) ImagingError_ValueError(NULL); + } imOut = ImagingNew2Dirty(imIn->mode, imOut, imIn); - if (!imOut) + if (!imOut) { return NULL; + } ImagingCopyPalette(imOut, imIn); ImagingSectionEnter(&cookie); - if (imIn->block != NULL && imOut->block != NULL) + if (imIn->block != NULL && imOut->block != NULL) { memcpy(imOut->block, imIn->block, imIn->ysize * imIn->linesize); - else - for (y = 0; y < imIn->ysize; y++) + } else { + for (y = 0; y < imIn->ysize; y++) { memcpy(imOut->image[y], imIn->image[y], imIn->linesize); + } + } ImagingSectionLeave(&cookie); return imOut; diff --git a/src/libImaging/Crop.c b/src/libImaging/Crop.c index a2b679681..29b4cf9d9 100644 --- a/src/libImaging/Crop.c +++ b/src/libImaging/Crop.c @@ -27,8 +27,9 @@ ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1) int dx0, dy0, dx1, dy1; INT32 zero = 0; - if (!imIn) + if (!imIn) { return (Imaging) ImagingError_ModeError(); + } xsize = sx1 - sx0; if (xsize < 0) @@ -38,13 +39,15 @@ ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1) ysize = 0; imOut = ImagingNewDirty(imIn->mode, xsize, ysize); - if (!imOut) + if (!imOut) { return NULL; + } ImagingCopyPalette(imOut, imIn); - if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize) + if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize) { (void) ImagingFill(imOut, &zero); + } dx0 = -sx0; dy0 = -sy0; diff --git a/src/libImaging/Dib.c b/src/libImaging/Dib.c index 504290231..202b0c9fa 100644 --- a/src/libImaging/Dib.c +++ b/src/libImaging/Dib.c @@ -40,8 +40,9 @@ ImagingGetModeDIB(int size_out[2]) mode = "P"; if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)) { mode = "RGB"; - if (GetDeviceCaps(dc, BITSPIXEL) == 1) + if (GetDeviceCaps(dc, BITSPIXEL) == 1) { mode = "1"; + } } if (size_out) { @@ -66,14 +67,16 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) /* Check mode */ if (strcmp(mode, "1") != 0 && strcmp(mode, "L") != 0 && - strcmp(mode, "RGB") != 0) + strcmp(mode, "RGB") != 0) { return (ImagingDIB) ImagingError_ModeError(); + } /* Create DIB context and info header */ /* malloc check ok, small constant allocation */ dib = (ImagingDIB) malloc(sizeof(*dib)); - if (!dib) + if (!dib) { return (ImagingDIB) ImagingError_MemoryError(); + } /* malloc check ok, small constant allocation */ dib->info = (BITMAPINFO*) malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)); @@ -113,9 +116,9 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) dib->pixelsize = strlen(mode); dib->linesize = (xsize * dib->pixelsize + 3) & -4; - if (dib->pixelsize == 1) + if (dib->pixelsize == 1) { dib->pack = dib->unpack = (ImagingShuffler) memcpy; - else { + } else { dib->pack = ImagingPackBGR; dib->unpack = ImagingPackBGR; } @@ -174,14 +177,16 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) * images. */ i = 10; - for (r = 0; r < 256; r += 51) - for (g = 0; g < 256; g += 51) + for (r = 0; r < 256; r += 51) { + for (g = 0; g < 256; g += 51) { for (b = 0; b < 256; b += 51) { pal->palPalEntry[i].peRed = r; pal->palPalEntry[i].peGreen = g; pal->palPalEntry[i].peBlue = b; i++; } + } + } for (r = 1; r < 22-1; r++) { /* Black and white are already provided by the cube. */ pal->palPalEntry[i].peRed = @@ -195,14 +200,16 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) /* Colour DIB. Alternate palette. */ i = 10; - for (r = 0; r < 256; r += 37) - for (g = 0; g < 256; g += 32) + for (r = 0; r < 256; r += 37) { + for (g = 0; g < 256; g += 32) { for (b = 0; b < 256; b += 64) { pal->palPalEntry[i].peRed = r; pal->palPalEntry[i].peGreen = g; pal->palPalEntry[i].peBlue = b; i++; } + } + } #endif @@ -223,9 +230,10 @@ ImagingPasteDIB(ImagingDIB dib, Imaging im, int xy[4]) /* FIXME: check size! */ int y; - for (y = 0; y < im->ysize; y++) + for (y = 0; y < im->ysize; y++) { dib->pack(dib->bits + dib->linesize*(dib->ysize-(xy[1]+y)-1) + xy[0]*dib->pixelsize, im->image[y], im->xsize); + } } @@ -234,8 +242,9 @@ ImagingExposeDIB(ImagingDIB dib, void *dc) { /* Copy bitmap to display */ - if (dib->palette != 0) + if (dib->palette != 0) { SelectPalette((HDC) dc, dib->palette, FALSE); + } BitBlt((HDC) dc, 0, 0, dib->xsize, dib->ysize, dib->dc, 0, 0, SRCCOPY); } @@ -251,8 +260,9 @@ ImagingDrawDIB(ImagingDIB dib, void *dc, int dst[4], int src[4]) dib->info, DIB_RGB_COLORS, SRCCOPY); } else { /* stretchblt (displays) */ - if (dib->palette != 0) + if (dib->palette != 0) { SelectPalette((HDC) dc, dib->palette, FALSE); + } StretchBlt((HDC) dc, dst[0], dst[1], dst[2]-dst[0], dst[3]-dst[1], dib->dc, src[0], src[1], src[2]-src[0], src[3]-src[1], SRCCOPY); @@ -275,8 +285,9 @@ ImagingQueryPaletteDIB(ImagingDIB dib, void *dc) /* Restore palette */ SelectPalette((HDC) dc, now, FALSE); - } else + } else { n = 0; + } return n; /* number of colours that was changed */ } @@ -286,14 +297,16 @@ ImagingDeleteDIB(ImagingDIB dib) { /* Clean up */ - if (dib->palette) + if (dib->palette) { DeleteObject(dib->palette); + } if (dib->bitmap) { SelectObject(dib->dc, dib->old_bitmap); DeleteObject(dib->bitmap); } - if (dib->dc) + if (dib->dc) { DeleteDC(dib->dc); + } free(dib->info); } diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index 58adc1b63..35e6e4893 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -79,8 +79,9 @@ point8(Imaging im, int x, int y, int ink) static inline void point32(Imaging im, int x, int y, int ink) { - if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) + if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) { im->image32[y][x] = ink; + } } static inline void @@ -103,16 +104,19 @@ hline8(Imaging im, int x0, int y0, int x1, int ink) int tmp, pixelwidth; if (y0 >= 0 && y0 < im->ysize) { - if (x0 > x1) + if (x0 > x1) { tmp = x0, x0 = x1, x1 = tmp; - if (x0 < 0) + } + if (x0 < 0) { x0 = 0; - else if (x0 >= im->xsize) + } else if (x0 >= im->xsize) { return; - if (x1 < 0) + } + if (x1 < 0) { return; - else if (x1 >= im->xsize) + } else if (x1 >= im->xsize) { x1 = im->xsize-1; + } if (x0 <= x1) { pixelwidth = strncmp(im->mode, "I;16", 4) == 0 ? 2 : 1; memset(im->image8[y0] + x0 * pixelwidth, (UINT8) ink, @@ -128,19 +132,23 @@ hline32(Imaging im, int x0, int y0, int x1, int ink) INT32* p; if (y0 >= 0 && y0 < im->ysize) { - if (x0 > x1) + if (x0 > x1) { tmp = x0, x0 = x1, x1 = tmp; - if (x0 < 0) + } + if (x0 < 0) { x0 = 0; - else if (x0 >= im->xsize) + } else if (x0 >= im->xsize) { return; - if (x1 < 0) + } + if (x1 < 0) { return; - else if (x1 >= im->xsize) + } else if (x1 >= im->xsize) { x1 = im->xsize-1; + } p = im->image32[y0]; - while (x0 <= x1) + while (x0 <= x1) { p[x0++] = ink; + } } } @@ -151,16 +159,19 @@ hline32rgba(Imaging im, int x0, int y0, int x1, int ink) unsigned int tmp1; if (y0 >= 0 && y0 < im->ysize) { - if (x0 > x1) + if (x0 > x1) { tmp = x0, x0 = x1, x1 = tmp; - if (x0 < 0) + } + if (x0 < 0) { x0 = 0; - else if (x0 >= im->xsize) + } else if (x0 >= im->xsize) { return; - if (x1 < 0) + } + if (x1 < 0) { return; - else if (x1 >= im->xsize) + } else if (x1 >= im->xsize) { x1 = im->xsize-1; + } if (x0 <= x1) { UINT8* out = (UINT8*) im->image[y0]+x0*4; UINT8* in = (UINT8*) &ink; @@ -183,19 +194,21 @@ line8(Imaging im, int x0, int y0, int x1, int y1, int ink) /* normalize coordinates */ dx = x1-x0; - if (dx < 0) + if (dx < 0) { dx = -dx, xs = -1; - else + } else { xs = 1; + } dy = y1-y0; - if (dy < 0) + if (dy < 0) { dy = -dy, ys = -1; - else + } else { ys = 1; + } n = (dx > dy) ? dx : dy; - if (dx == 0) + if (dx == 0) { /* vertical */ for (i = 0; i < dy; i++) { @@ -203,7 +216,7 @@ line8(Imaging im, int x0, int y0, int x1, int y1, int ink) y0 += ys; } - else if (dy == 0) + } else if (dy == 0) { /* horizontal */ for (i = 0; i < dx; i++) { @@ -211,7 +224,7 @@ line8(Imaging im, int x0, int y0, int x1, int y1, int ink) x0 += xs; } - else if (dx > dy) { + } else if (dx > dy) { /* bresenham, horizontal slope */ n = dx; @@ -259,19 +272,21 @@ line32(Imaging im, int x0, int y0, int x1, int y1, int ink) /* normalize coordinates */ dx = x1-x0; - if (dx < 0) + if (dx < 0) { dx = -dx, xs = -1; - else + } else { xs = 1; + } dy = y1-y0; - if (dy < 0) + if (dy < 0) { dy = -dy, ys = -1; - else + } else { ys = 1; + } n = (dx > dy) ? dx : dy; - if (dx == 0) + if (dx == 0) { /* vertical */ for (i = 0; i < dy; i++) { @@ -279,7 +294,7 @@ line32(Imaging im, int x0, int y0, int x1, int y1, int ink) y0 += ys; } - else if (dy == 0) + } else if (dy == 0) { /* horizontal */ for (i = 0; i < dx; i++) { @@ -287,7 +302,7 @@ line32(Imaging im, int x0, int y0, int x1, int y1, int ink) x0 += xs; } - else if (dx > dy) { + } else if (dx > dy) { /* bresenham, horizontal slope */ n = dx; @@ -335,19 +350,21 @@ line32rgba(Imaging im, int x0, int y0, int x1, int y1, int ink) /* normalize coordinates */ dx = x1-x0; - if (dx < 0) + if (dx < 0) { dx = -dx, xs = -1; - else + } else { xs = 1; + } dy = y1-y0; - if (dy < 0) + if (dy < 0) { dy = -dy, ys = -1; - else + } else { ys = 1; + } n = (dx > dy) ? dx : dy; - if (dx == 0) + if (dx == 0) { /* vertical */ for (i = 0; i < dy; i++) { @@ -355,7 +372,7 @@ line32rgba(Imaging im, int x0, int y0, int x1, int y1, int ink) y0 += ys; } - else if (dy == 0) + } else if (dy == 0) { /* horizontal */ for (i = 0; i < dx; i++) { @@ -363,7 +380,7 @@ line32rgba(Imaging im, int x0, int y0, int x1, int y1, int ink) x0 += xs; } - else if (dx > dy) { + } else if (dx > dy) { /* bresenham, horizontal slope */ n = dx; @@ -406,12 +423,13 @@ static int x_cmp(const void *x0, const void *x1) { float diff = *((float*)x0) - *((float*)x1); - if (diff < 0) + if (diff < 0) { return -1; - else if (diff > 0) + } else if (diff > 0) { return 1; - else + } else { return 0; + } } @@ -566,25 +584,28 @@ add_edge(Edge *e, int x0, int y0, int x1, int y1) { /* printf("edge %d %d %d %d\n", x0, y0, x1, y1); */ - if (x0 <= x1) + if (x0 <= x1) { e->xmin = x0, e->xmax = x1; - else + } else { e->xmin = x1, e->xmax = x0; + } - if (y0 <= y1) + if (y0 <= y1) { e->ymin = y0, e->ymax = y1; - else + } else { e->ymin = y1, e->ymax = y0; + } if (y0 == y1) { e->d = 0; e->dx = 0.0; } else { e->dx = ((float)(x1-x0)) / (y1-y0); - if (y0 == e->ymin) + if (y0 == e->ymin) { e->d = 1; - else + } else { e->d = -1; + } } e->x0 = x0; @@ -701,23 +722,27 @@ ImagingDrawRectangle(Imaging im, int x0, int y0, int x1, int y1, DRAWINIT(); - if (y0 > y1) + if (y0 > y1) { tmp = y0, y0 = y1, y1 = tmp; + } if (fill) { - if (y0 < 0) + if (y0 < 0) { y0 = 0; - else if (y0 >= im->ysize) + } else if (y0 >= im->ysize) { return 0; + } - if (y1 < 0) + if (y1 < 0) { return 0; - else if (y1 > im->ysize) + } else if (y1 > im->ysize) { y1 = im->ysize; + } - for (y = y0; y <= y1; y++) + for (y = y0; y <= y1; y++) { draw->hline(im, x0, y, x1, ink); + } } else { /* outline */ @@ -743,8 +768,9 @@ ImagingDrawPolygon(Imaging im, int count, int* xy, const void* ink_, DRAW* draw; INT32 ink; - if (count <= 0) + if (count <= 0) { return 0; + } DRAWINIT(); @@ -757,18 +783,21 @@ ImagingDrawPolygon(Imaging im, int count, int* xy, const void* ink_, (void) ImagingError_MemoryError(); return -1; } - for (i = n = 0; i < count-1; i++) + for (i = n = 0; i < count-1; i++) { add_edge(&e[n++], xy[i+i], xy[i+i+1], xy[i+i+2], xy[i+i+3]); - if (xy[i+i] != xy[0] || xy[i+i+1] != xy[1]) + } + if (xy[i+i] != xy[0] || xy[i+i+1] != xy[1]) { add_edge(&e[n++], xy[i+i], xy[i+i+1], xy[0], xy[1]); + } draw->polygon(im, n, e, ink, 0); free(e); } else { /* Outline */ - for (i = 0; i < count-1; i++) + for (i = 0; i < count-1; i++) { draw->line(im, xy[i+i], xy[i+i+1], xy[i+i+2], xy[i+i+3], ink); + } draw->line(im, xy[i+i], xy[i+i+1], xy[0], xy[1], ink); } @@ -838,8 +867,9 @@ ellipse(Imaging im, int x0, int y0, int x1, int y1, DRAWINIT(); - while (end < start) + while (end < start) { end += 360; + } if (end - start > 360) { // no need to go in loops @@ -848,8 +878,9 @@ ellipse(Imaging im, int x0, int y0, int x1, int y1, w = x1 - x0; h = y1 - y0; - if (w <= 0 || h <= 0) + if (w <= 0 || h <= 0) { return 0; + } cx = (x0 + x1) / 2; cy = (y0 + y1) / 2; @@ -860,10 +891,11 @@ ellipse(Imaging im, int x0, int y0, int x1, int y1, i = end; } ellipsePoint(cx, cy, w, h, i, &x, &y); - if (i != start) + if (i != start) { draw->line(im, lx, ly, x, y, ink); - else + } else { sx = x, sy = y; + } lx = x, ly = y; } @@ -874,8 +906,9 @@ ellipse(Imaging im, int x0, int y0, int x1, int y1, draw->line(im, cx, cy, sx, sy, ink); } } else if (mode == CHORD) { - if (x != sx || y != sy) + if (x != sx || y != sy) { draw->line(im, x, y, sx, sy, ink); + } } } } else { @@ -930,10 +963,11 @@ ellipse(Imaging im, int x0, int y0, int x1, int y1, i = end; } ellipsePoint(cx, cy, w, h, i, &x, &y); - if (i == start) + if (i == start) { sx_inner = x, sy_inner = y; - else + } else { add_edge(&e[n++], lx_inner, ly_inner, x, y); + } lx_inner = x, ly_inner = y; } } @@ -1026,8 +1060,9 @@ ImagingOutlineNew(void) ImagingOutline outline; outline = calloc(1, sizeof(struct ImagingOutlineInstance)); - if (!outline) + if (!outline) { return (ImagingOutline) ImagingError_MemoryError(); + } outline->edges = NULL; outline->count = outline->size = 0; @@ -1040,11 +1075,13 @@ ImagingOutlineNew(void) void ImagingOutlineDelete(ImagingOutline outline) { - if (!outline) + if (!outline) { return; + } - if (outline->edges) + if (outline->edges) { free(outline->edges); + } free(outline); } @@ -1068,8 +1105,9 @@ allocate(ImagingOutline outline, int extra) /* malloc check ok, overflow checked above */ e = realloc(outline->edges, outline->size * sizeof(Edge)); } - if (!e) + if (!e) { return NULL; + } outline->edges = e; } @@ -1095,8 +1133,9 @@ ImagingOutlineLine(ImagingOutline outline, float x1, float y1) Edge* e; e = allocate(outline, 1); - if (!e) + if (!e) { return -1; /* out of memory */ + } add_edge(e, (int) outline->x, (int) outline->y, (int) x1, (int) y1); @@ -1117,8 +1156,9 @@ ImagingOutlineCurve(ImagingOutline outline, float x1, float y1, #define STEPS 32 e = allocate(outline, STEPS); - if (!e) + if (!e) { return -1; /* out of memory */ + } xo = outline->x; yo = outline->y; @@ -1153,8 +1193,9 @@ ImagingOutlineCurve(ImagingOutline outline, float x1, float y1, int ImagingOutlineClose(ImagingOutline outline) { - if (outline->x == outline->x0 && outline->y == outline->y0) + if (outline->x == outline->x0 && outline->y == outline->y0) { return 0; + } return ImagingOutlineLine(outline, outline->x0, outline->y0); } @@ -1191,14 +1232,16 @@ ImagingOutlineTransform(ImagingOutline outline, double a[6]) y0 = eIn->y0; /* FIXME: ouch! */ - if (eIn->x0 == eIn->xmin) + if (eIn->x0 == eIn->xmin) { x1 = eIn->xmax; - else + } else { x1 = eIn->xmin; - if (eIn->y0 == eIn->ymin) + } + if (eIn->y0 == eIn->ymin) { y1 = eIn->ymax; - else + } else { y1 = eIn->ymin; + } /* full moon tonight! if this doesn't work, you may need to upgrade your compiler (make sure you have the right service diff --git a/src/libImaging/Effects.c b/src/libImaging/Effects.c index 7b4ff0b43..b9ff889d2 100644 --- a/src/libImaging/Effects.c +++ b/src/libImaging/Effects.c @@ -34,12 +34,14 @@ ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality) /* Check arguments */ width = extent[2] - extent[0]; height = extent[3] - extent[1]; - if (width < 0.0 || height < 0.0 || quality < 2) + if (width < 0.0 || height < 0.0 || quality < 2) { return (Imaging) ImagingError_ValueError(NULL); + } im = ImagingNewDirty("L", xsize, ysize); - if (!im) + if (!im) { return NULL; + } dr = width/(xsize-1); di = height/(ysize-1); @@ -82,8 +84,9 @@ ImagingEffectNoise(int xsize, int ysize, float sigma) double this, next; imOut = ImagingNewDirty("L", xsize, ysize); - if (!imOut) + if (!imOut) { return NULL; + } next = 0.0; nextok = 0; @@ -123,20 +126,23 @@ ImagingEffectSpread(Imaging imIn, int distance) imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } #define SPREAD(type, image)\ - for (y = 0; y < imOut->ysize; y++)\ + for (y = 0; y < imOut->ysize; y++) {\ for (x = 0; x < imOut->xsize; x++) {\ int xx = x + (rand() % distance) - distance/2;\ int yy = y + (rand() % distance) - distance/2;\ if (xx >= 0 && xx < imIn->xsize && yy >= 0 && yy < imIn->ysize) {\ imOut->image[yy][xx] = imIn->image[y][x];\ imOut->image[y][x] = imIn->image[yy][xx];\ - } else\ + } else {\ imOut->image[y][x] = imIn->image[y][x];\ - } + }\ + }\ + } if (imIn->image8) { SPREAD(UINT8, image8); diff --git a/src/libImaging/EpsEncode.c b/src/libImaging/EpsEncode.c index 2a6aad4a3..ac8a4059c 100644 --- a/src/libImaging/EpsEncode.c +++ b/src/libImaging/EpsEncode.c @@ -40,15 +40,17 @@ ImagingEpsEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) for (;;) { if (state->state == NEWLINE) { - if (bytes < 1) + if (bytes < 1) { break; + } *ptr++ = '\n'; bytes--; state->state = HEXBYTE; } - if (bytes < 2) + if (bytes < 2) { break; + } i = in[state->x++]; *ptr++ = hex[(i>>4)&15]; @@ -56,8 +58,9 @@ ImagingEpsEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) bytes -= 2; /* Skip junk bytes */ - if (im->bands == 3 && (state->x & 3) == 3) + if (im->bands == 3 && (state->x & 3) == 3) { state->x++; + } if (++state->count >= 79/2) { state->state = NEWLINE; diff --git a/src/libImaging/Except.c b/src/libImaging/Except.c index 4850b4f87..15ae21cc8 100644 --- a/src/libImaging/Except.c +++ b/src/libImaging/Except.c @@ -56,8 +56,9 @@ ImagingError_Mismatch(void) void * ImagingError_ValueError(const char *message) { - if (!message) + if (!message) { message = "exception: bad argument to function"; + } fprintf(stderr, "*** %s\n", message); return NULL; } diff --git a/src/libImaging/File.c b/src/libImaging/File.c index 117af9e35..14688d661 100644 --- a/src/libImaging/File.c +++ b/src/libImaging/File.c @@ -31,15 +31,18 @@ ImagingSaveRaw(Imaging im, FILE* fp) /* @PIL227: FIXME: for mode "1", map != 0 to 255 */ /* PGM "L" */ - for (y = 0; y < im->ysize; y++) + for (y = 0; y < im->ysize; y++) { fwrite(im->image[y], 1, im->xsize, fp); + } } else { /* PPM "RGB" or other internal format */ - for (y = 0; y < im->ysize; y++) - for (x = i = 0; x < im->xsize; x++, i += im->pixelsize) + for (y = 0; y < im->ysize; y++) { + for (x = i = 0; x < im->xsize; x++, i += im->pixelsize) { fwrite(im->image[y]+i, 1, im->bands, fp); + } + } } diff --git a/src/libImaging/Fill.c b/src/libImaging/Fill.c index 27022bc48..da143b4f9 100644 --- a/src/libImaging/Fill.c +++ b/src/libImaging/Fill.c @@ -30,27 +30,33 @@ ImagingFill(Imaging im, const void* colour) /* use generic API */ ImagingAccess access = ImagingAccessNew(im); if (access) { - for (y = 0; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++) + for (y = 0; y < im->ysize; y++) { + for (x = 0; x < im->xsize; x++) { access->put_pixel(im, x, y, colour); + } + } ImagingAccessDelete(im, access); } else { /* wipe the image */ - for (y = 0; y < im->ysize; y++) + for (y = 0; y < im->ysize; y++) { memset(im->image[y], 0, im->linesize); + } } } else { INT32 c = 0L; ImagingSectionEnter(&cookie); memcpy(&c, colour, im->pixelsize); if (im->image32 && c != 0L) { - for (y = 0; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++) + for (y = 0; y < im->ysize; y++) { + for (x = 0; x < im->xsize; x++) { im->image32[y][x] = c; + } + } } else { unsigned char cc = (unsigned char) *(UINT8*) colour; - for (y = 0; y < im->ysize; y++) + for (y = 0; y < im->ysize; y++) { memset(im->image[y], cc, im->linesize); + } } ImagingSectionLeave(&cookie); } diff --git a/src/libImaging/Filter.c b/src/libImaging/Filter.c index b033abf66..0897ddbfe 100644 --- a/src/libImaging/Filter.c +++ b/src/libImaging/Filter.c @@ -29,10 +29,12 @@ static inline UINT8 clip8(float in) { - if (in <= 0.0) + if (in <= 0.0) { return 0; - if (in >= 255.0) + } + if (in >= 255.0) { return 255; + } return (UINT8) in; } @@ -43,32 +45,40 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) int x, y; ImagingSectionCookie cookie; - if (xmargin < 0 && ymargin < 0) + if (xmargin < 0 && ymargin < 0) { return (Imaging) ImagingError_ValueError("bad kernel size"); + } imOut = ImagingNewDirty( imIn->mode, imIn->xsize+2*xmargin, imIn->ysize+2*ymargin); - if (!imOut) + if (!imOut) { return NULL; + } #define EXPAND_LINE(type, image, yin, yout) {\ - for (x = 0; x < xmargin; x++)\ + for (x = 0; x < xmargin; x++) {\ imOut->image[yout][x] = imIn->image[yin][0];\ - for (x = 0; x < imIn->xsize; x++)\ + }\ + for (x = 0; x < imIn->xsize; x++) {\ imOut->image[yout][x+xmargin] = imIn->image[yin][x];\ - for (x = 0; x < xmargin; x++)\ + }\ + for (x = 0; x < xmargin; x++) {\ imOut->image[yout][xmargin+imIn->xsize+x] =\ imIn->image[yin][imIn->xsize-1];\ - } + }\ +} #define EXPAND(type, image) {\ - for (y = 0; y < ymargin; y++)\ + for (y = 0; y < ymargin; y++) {\ EXPAND_LINE(type, image, 0, y);\ - for (y = 0; y < imIn->ysize; y++)\ + }\ + for (y = 0; y < imIn->ysize; y++) {\ EXPAND_LINE(type, image, y, y+ymargin);\ - for (y = 0; y < ymargin; y++)\ + }\ + for (y = 0; y < ymargin; y++) {\ EXPAND_LINE(type, image, imIn->ysize-1, ymargin+imIn->ysize+y);\ - } + }\ +} ImagingSectionEnter(&cookie); if (imIn->image8) { @@ -330,18 +340,22 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, Imaging imOut; ImagingSectionCookie cookie; - if ( ! im || im->type != IMAGING_TYPE_UINT8) + if ( ! im || im->type != IMAGING_TYPE_UINT8) { return (Imaging) ImagingError_ModeError(); + } - if (im->xsize < xsize || im->ysize < ysize) + if (im->xsize < xsize || im->ysize < ysize) { return ImagingCopy(im); + } - if ((xsize != 3 && xsize != 5) || xsize != ysize) + if ((xsize != 3 && xsize != 5) || xsize != ysize) { return (Imaging) ImagingError_ValueError("bad kernel size"); + } imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); - if (!imOut) + if (!imOut) { return NULL; + } ImagingSectionEnter(&cookie); if (xsize == 3) { diff --git a/src/libImaging/FliDecode.c b/src/libImaging/FliDecode.c index 5351d5664..84508013d 100644 --- a/src/libImaging/FliDecode.c +++ b/src/libImaging/FliDecode.c @@ -41,8 +41,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt /* If not even the chunk size is present, we'd better leave */ - if (bytes < 4) + if (bytes < 4) { return 0; + } /* We don't decode anything unless we have a full chunk in the input buffer */ @@ -50,8 +51,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt ptr = buf; framesize = I32(ptr); - if (framesize < I32(ptr)) + if (framesize < I32(ptr)) { return 0; + } /* Make sure this is a frame chunk. The Python driver takes case of other chunk types. */ @@ -112,8 +114,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt if (data[1] >= 128) { ERR_IF_DATA_OOB(4) i = 256-data[1]; /* run */ - if (x + i + i > state->xsize) + if (x + i + i > state->xsize) { break; + } for (j = 0; j < i; j++) { local_buf[x++] = data[2]; local_buf[x++] = data[3]; @@ -121,16 +124,18 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt data += 2 + 2; } else { i = 2 * (int) data[1]; /* chunk */ - if (x + i > state->xsize) + if (x + i > state->xsize) { break; + } ERR_IF_DATA_OOB(2+i) memcpy(local_buf + x, data + 2, i); data += 2 + i; x += i; } } - if (p < packets) + if (p < packets) { break; /* didn't process all packets */ + } } if (l < lines) { /* didn't process all lines */ @@ -151,22 +156,25 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt x += data[0]; /* skip pixels */ if (data[1] & 0x80) { i = 256-data[1]; /* run */ - if (x + i > state->xsize) + if (x + i > state->xsize) { break; + } ERR_IF_DATA_OOB(3) memset(out + x, data[2], i); data += 3; } else { i = data[1]; /* chunk */ - if (x + i > state->xsize) + if (x + i > state->xsize) { break; + } ERR_IF_DATA_OOB(2+i) memcpy(out + x, data + 2, i); data += i + 2; } } - if (p < packets) + if (p < packets) { break; /* didn't process all packets */ + } } if (y < ymax) { /* didn't process all lines */ @@ -176,8 +184,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt break; case 13: /* FLI BLACK chunk */ - for (y = 0; y < state->ysize; y++) + for (y = 0; y < state->ysize; y++) { memset(im->image[y], 0, state->xsize); + } break; case 15: /* FLI BRUN chunk */ @@ -197,8 +206,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt data += i + 1; } else { i = data[0]; - if (x + i > state->xsize) + if (x + i > state->xsize) { break; /* safety first */ + } memset(out + x, data[1], i); data += 2; } diff --git a/src/libImaging/Geometry.c b/src/libImaging/Geometry.c index fd5e25958..06d0cf24d 100644 --- a/src/libImaging/Geometry.c +++ b/src/libImaging/Geometry.c @@ -20,10 +20,12 @@ ImagingFlipLeftRight(Imaging imOut, Imaging imIn) ImagingSectionCookie cookie; int x, y, xr; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) + } + if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); @@ -32,8 +34,9 @@ ImagingFlipLeftRight(Imaging imOut, Imaging imIn) INT* in = (INT *)imIn->image[y]; \ INT* out = (INT *)imOut->image[y]; \ xr = imIn->xsize-1; \ - for (x = 0; x < imIn->xsize; x++, xr--) \ + for (x = 0; x < imIn->xsize; x++, xr--) { \ out[xr] = in[x]; \ + } \ } ImagingSectionEnter(&cookie); @@ -62,18 +65,21 @@ ImagingFlipTopBottom(Imaging imOut, Imaging imIn) ImagingSectionCookie cookie; int y, yr; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) + } + if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); ImagingSectionEnter(&cookie); yr = imIn->ysize - 1; - for (y = 0; y < imIn->ysize; y++, yr--) + for (y = 0; y < imIn->ysize; y++, yr--) { memcpy(imOut->image[yr], imIn->image[y], imIn->linesize); + } ImagingSectionLeave(&cookie); @@ -88,10 +94,12 @@ ImagingRotate90(Imaging imOut, Imaging imIn) int x, y, xx, yy, xr, xxsize, yysize; int xxx, yyy, xxxsize, yyysize; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) + } + if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); @@ -144,10 +152,12 @@ ImagingTranspose(Imaging imOut, Imaging imIn) int x, y, xx, yy, xxsize, yysize; int xxx, yyy, xxxsize, yyysize; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) + } + if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); @@ -199,10 +209,12 @@ ImagingTransverse(Imaging imOut, Imaging imIn) int x, y, xr, yr, xx, yy, xxsize, yysize; int xxx, yyy, xxxsize, yyysize; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) + } + if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); @@ -255,10 +267,12 @@ ImagingRotate180(Imaging imOut, Imaging imIn) ImagingSectionCookie cookie; int x, y, xr, yr; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) + } + if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); @@ -267,8 +281,9 @@ ImagingRotate180(Imaging imOut, Imaging imIn) INT* in = (INT *)imIn->image[y]; \ INT* out = (INT *)imOut->image[yr]; \ xr = imIn->xsize-1; \ - for (x = 0; x < imIn->xsize; x++, xr--) \ + for (x = 0; x < imIn->xsize; x++, xr--) { \ out[xr] = in[x]; \ + } \ } ImagingSectionEnter(&cookie); @@ -299,10 +314,12 @@ ImagingRotate270(Imaging imOut, Imaging imIn) int x, y, xx, yy, yr, xxsize, yysize; int xxx, yyy, xxxsize, yyysize; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); - if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) + } + if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize) { return (Imaging) ImagingError_Mismatch(); + } ImagingCopyPalette(imOut, imIn); @@ -415,8 +432,9 @@ nearest_filter8(void* out, Imaging im, double xin, double yin) { int x = COORD(xin); int y = COORD(yin); - if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) + if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { return 0; + } ((UINT8*)out)[0] = im->image8[y][x]; return 1; } @@ -426,8 +444,9 @@ nearest_filter16(void* out, Imaging im, double xin, double yin) { int x = COORD(xin); int y = COORD(yin); - if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) + if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { return 0; + } memcpy(out, im->image8[y] + x * sizeof(INT16), sizeof(INT16)); return 1; } @@ -437,8 +456,9 @@ nearest_filter32(void* out, Imaging im, double xin, double yin) { int x = COORD(xin); int y = COORD(yin); - if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) + if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { return 0; + } memcpy(out, &im->image32[y][x], sizeof(INT32)); return 1; } @@ -455,8 +475,9 @@ nearest_filter32(void* out, Imaging im, double xin, double yin) double v1, v2;\ double dx, dy;\ type* in;\ - if (xin < 0.0 || xin >= im->xsize || yin < 0.0 || yin >= im->ysize)\ + if (xin < 0.0 || xin >= im->xsize || yin < 0.0 || yin >= im->ysize) {\ return 0;\ + }\ xin -= 0.5;\ yin -= 0.5;\ x = FLOOR(xin);\ @@ -472,8 +493,9 @@ nearest_filter32(void* out, Imaging im, double xin, double yin) if (y+1 >= 0 && y+1 < im->ysize) {\ in = (type*) ((image)[y+1] + offset);\ BILINEAR(v2, in[x0], in[x1], dx);\ - } else\ + } else {\ v2 = v1;\ + }\ BILINEAR(v1, v1, v2, dy);\ } @@ -552,8 +574,9 @@ bilinear_filter32RGB(void* out, Imaging im, double xin, double yin) double v1, v2, v3, v4;\ double dx, dy;\ type* in;\ - if (xin < 0.0 || xin >= im->xsize || yin < 0.0 || yin >= im->ysize)\ + if (xin < 0.0 || xin >= im->xsize || yin < 0.0 || yin >= im->ysize) {\ return 0;\ + }\ xin -= 0.5;\ yin -= 0.5;\ x = FLOOR(xin);\ @@ -572,18 +595,21 @@ bilinear_filter32RGB(void* out, Imaging im, double xin, double yin) if (y+1 >= 0 && y+1 < im->ysize) {\ in = (type*) ((image)[y+1] + offset);\ BICUBIC(v2, in[x0], in[x1], in[x2], in[x3], dx);\ - } else\ + } else {\ v2 = v1;\ + }\ if (y+2 >= 0 && y+2 < im->ysize) {\ in = (type*) ((image)[y+2] + offset);\ BICUBIC(v3, in[x0], in[x1], in[x2], in[x3], dx);\ - } else\ + } else {\ v3 = v2;\ + }\ if (y+3 >= 0 && y+3 < im->ysize) {\ in = (type*) ((image)[y+3] + offset);\ BICUBIC(v4, in[x0], in[x1], in[x2], in[x3], dx);\ - } else\ + } else {\ v4 = v3;\ + }\ BICUBIC(v1, v1, v2, v3, v4, dy);\ } @@ -593,12 +619,13 @@ bicubic_filter8(void* out, Imaging im, double xin, double yin) { BICUBIC_HEAD(UINT8); BICUBIC_BODY(UINT8, im->image8, 1, 0); - if (v1 <= 0.0) + if (v1 <= 0.0) { ((UINT8*)out)[0] = 0; - else if (v1 >= 255.0) + } else if (v1 >= 255.0) { ((UINT8*)out)[0] = 255; - else + } else { ((UINT8*)out)[0] = (UINT8) v1; + } return 1; } @@ -643,12 +670,13 @@ bicubic_filter32LA(void* out, Imaging im, double xin, double yin) ((UINT8*)out)[2] = (UINT8) v1; } BICUBIC_BODY(UINT8, im->image, 4, 3); - if (v1 <= 0.0) + if (v1 <= 0.0) { ((UINT8*)out)[3] = 0; - else if (v1 >= 255.0) + } else if (v1 >= 255.0) { ((UINT8*)out)[3] = 255; - else + } else { ((UINT8*)out)[3] = (UINT8) v1; + } return 1; } @@ -659,12 +687,13 @@ bicubic_filter32RGB(void* out, Imaging im, double xin, double yin) BICUBIC_HEAD(UINT8); for (b = 0; b < im->bands; b++) { BICUBIC_BODY(UINT8, im->image, 4, b); - if (v1 <= 0.0) + if (v1 <= 0.0) { ((UINT8*)out)[b] = 0; - else if (v1 >= 255.0) + } else if (v1 >= 255.0) { ((UINT8*)out)[b] = 255; - else + } else { ((UINT8*)out)[b] = (UINT8) v1; + } } return 1; } @@ -678,7 +707,7 @@ getfilter(Imaging im, int filterid) { switch (filterid) { case IMAGING_TRANSFORM_NEAREST: - if (im->image8) + if (im->image8) { switch (im->type) { case IMAGING_TYPE_UINT8: return nearest_filter8; @@ -692,19 +721,21 @@ getfilter(Imaging im, int filterid) return nearest_filter32; } } - else + } else { return nearest_filter32; + } break; case IMAGING_TRANSFORM_BILINEAR: - if (im->image8) + if (im->image8) { return bilinear_filter8; - else if (im->image32) { + } else if (im->image32) { switch (im->type) { case IMAGING_TYPE_UINT8: - if (im->bands == 2) + if (im->bands == 2) { return bilinear_filter32LA; - else + } else { return bilinear_filter32RGB; + } case IMAGING_TYPE_INT32: return bilinear_filter32I; case IMAGING_TYPE_FLOAT32: @@ -713,15 +744,16 @@ getfilter(Imaging im, int filterid) } break; case IMAGING_TRANSFORM_BICUBIC: - if (im->image8) + if (im->image8) { return bicubic_filter8; - else if (im->image32) { + } else if (im->image32) { switch (im->type) { case IMAGING_TYPE_UINT8: - if (im->bands == 2) + if (im->bands == 2) { return bicubic_filter32LA; - else + } else { return bicubic_filter32RGB; + } case IMAGING_TYPE_INT32: return bicubic_filter32I; case IMAGING_TYPE_FLOAT32: @@ -751,32 +783,39 @@ ImagingGenericTransform( double xx, yy; ImagingTransformFilter filter = getfilter(imIn, filterid); - if (!filter) + if (!filter) { return (Imaging) ImagingError_ValueError("bad filter number"); + } - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); + } ImagingCopyPalette(imOut, imIn); ImagingSectionEnter(&cookie); - if (x0 < 0) + if (x0 < 0) { x0 = 0; - if (y0 < 0) + } + if (y0 < 0) { y0 = 0; - if (x1 > imOut->xsize) + } + if (x1 > imOut->xsize) { x1 = imOut->xsize; - if (y1 > imOut->ysize) + } + if (y1 > imOut->ysize) { y1 = imOut->ysize; + } for (y = y0; y < y1; y++) { out = imOut->image[y] + x0*imOut->pixelsize; for (x = x0; x < x1; x++) { if ( ! transform(&xx, &yy, x-x0, y-y0, transform_data) || ! filter(out, imIn, xx, yy)) { - if (fill) + if (fill) { memset(out, 0, imOut->pixelsize); + } } out += imOut->pixelsize; } @@ -801,19 +840,24 @@ ImagingScaleAffine(Imaging imOut, Imaging imIn, int xmin, xmax; int *xintab; - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); + } ImagingCopyPalette(imOut, imIn); - if (x0 < 0) + if (x0 < 0) { x0 = 0; - if (y0 < 0) + } + if (y0 < 0) { y0 = 0; - if (x1 > imOut->xsize) + } + if (x1 > imOut->xsize) { x1 = imOut->xsize; - if (y1 > imOut->ysize) + } + if (y1 > imOut->ysize) { y1 = imOut->ysize; + } /* malloc check ok, uses calloc for overflow */ xintab = (int*) calloc(imOut->xsize, sizeof(int)); @@ -833,8 +877,9 @@ ImagingScaleAffine(Imaging imOut, Imaging imIn, xin = COORD(xo); if (xin >= 0 && xin < (int) imIn->xsize) { xmax = x+1; - if (x < xmin) + if (x < xmin) { xmin = x; + } xintab[x] = xin; } xo += a[0]; @@ -845,12 +890,14 @@ ImagingScaleAffine(Imaging imOut, Imaging imIn, int yi = COORD(yo);\ pixel *in, *out;\ out = imOut->image[y];\ - if (fill && x1 > x0)\ + if (fill && x1 > x0) {\ memset(out+x0, 0, (x1-x0)*sizeof(pixel));\ + }\ if (yi >= 0 && yi < imIn->ysize) {\ in = imIn->image[yi];\ - for (x = xmin; x < xmax; x++)\ + for (x = xmin; x < xmax; x++) {\ out[x] = in[xintab[x]];\ + }\ }\ yo += a[4];\ } @@ -915,14 +962,16 @@ affine_fixed(Imaging imOut, Imaging imIn, xx = a2;\ yy = a5;\ out = imOut->image[y];\ - if (fill && x1 > x0)\ + if (fill && x1 > x0) {\ memset(out+x0, 0, (x1-x0)*sizeof(pixel));\ + }\ for (x = x0; x < x1; x++, out++) {\ xin = xx >> 16;\ if (xin >= 0 && xin < xsize) {\ yin = yy >> 16;\ - if (yin >= 0 && yin < ysize)\ + if (yin >= 0 && yin < ysize) {\ *out = imIn->image[yin][xin];\ + }\ }\ xx += a0;\ yy += a3;\ @@ -933,10 +982,11 @@ affine_fixed(Imaging imOut, Imaging imIn, ImagingSectionEnter(&cookie); - if (imIn->image8) + if (imIn->image8) { AFFINE_TRANSFORM_FIXED(UINT8, image8) - else + } else { AFFINE_TRANSFORM_FIXED(INT32, image32) + } ImagingSectionLeave(&cookie); @@ -973,24 +1023,30 @@ ImagingTransformAffine(Imaging imOut, Imaging imIn, return ImagingScaleAffine(imOut, imIn, x0, y0, x1, y1, a, fill); } - if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) + if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0) { return (Imaging) ImagingError_ModeError(); + } - if (x0 < 0) + if (x0 < 0) { x0 = 0; - if (y0 < 0) + } + if (y0 < 0) { y0 = 0; - if (x1 > imOut->xsize) + } + if (x1 > imOut->xsize) { x1 = imOut->xsize; - if (y1 > imOut->ysize) + } + if (y1 > imOut->ysize) { y1 = imOut->ysize; + } /* translate all four corners to check if they are within the range that can be represented by the fixed point arithmetics */ if (check_fixed(a, 0, 0) && check_fixed(a, x1-x0, y1-y0) && - check_fixed(a, 0, y1-y0) && check_fixed(a, x1-x0, 0)) + check_fixed(a, 0, y1-y0) && check_fixed(a, x1-x0, 0)) { return affine_fixed(imOut, imIn, x0, y0, x1, y1, a, filterid, fill); + } /* FIXME: cannot really think of any reasonable case when the following code is used. maybe we should fall back on the slow @@ -1010,14 +1066,16 @@ ImagingTransformAffine(Imaging imOut, Imaging imIn, xx = xo;\ yy = yo;\ out = imOut->image[y];\ - if (fill && x1 > x0)\ + if (fill && x1 > x0) {\ memset(out+x0, 0, (x1-x0)*sizeof(pixel));\ + }\ for (x = x0; x < x1; x++, out++) {\ xin = COORD(xx);\ if (xin >= 0 && xin < xsize) {\ yin = COORD(yy);\ - if (yin >= 0 && yin < ysize)\ + if (yin >= 0 && yin < ysize) {\ *out = imIn->image[yin][xin];\ + }\ }\ xx += a[0];\ yy += a[3];\ @@ -1028,10 +1086,11 @@ ImagingTransformAffine(Imaging imOut, Imaging imIn, ImagingSectionEnter(&cookie); - if (imIn->image8) + if (imIn->image8) { AFFINE_TRANSFORM(UINT8, image8) - else + } else { AFFINE_TRANSFORM(INT32, image32) + } ImagingSectionLeave(&cookie); diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index acb50fd95..dd875c557 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -36,7 +36,7 @@ ImagingGetBBox(Imaging im, int bbox[4]) #define GETBBOX(image, mask)\ for (y = 0; y < im->ysize; y++) {\ has_data = 0;\ - for (x = 0; x < im->xsize; x++)\ + for (x = 0; x < im->xsize; x++) {\ if (im->image[y][x] & mask) {\ has_data = 1;\ if (x < bbox[0])\ @@ -44,6 +44,7 @@ ImagingGetBBox(Imaging im, int bbox[4]) if (x >= bbox[2])\ bbox[2] = x+1;\ }\ + }\ if (has_data) {\ if (bbox[1] < 0)\ bbox[1] = y;\ @@ -72,8 +73,9 @@ ImagingGetBBox(Imaging im, int bbox[4]) } /* Check that we got a box */ - if (bbox[1] < 0) + if (bbox[1] < 0) { return 0; /* no data */ + } return 1; /* ok */ } @@ -94,21 +96,24 @@ ImagingGetProjection(Imaging im, UINT8* xproj, UINT8* yproj) #define GETPROJ(image, mask)\ for (y = 0; y < im->ysize; y++) {\ has_data = 0;\ - for (x = 0; x < im->xsize; x++)\ + for (x = 0; x < im->xsize; x++) {\ if (im->image[y][x] & mask) {\ has_data = 1;\ xproj[x] = 1;\ }\ - if (has_data)\ - yproj[y] = 1;\ - } + }\ + if (has_data) {\ + yproj[y] = 1;\ + }\ + } if (im->image8) { GETPROJ(image8, 0xff); } else { INT32 mask = 0xffffffff; - if (im->bands == 3) + if (im->bands == 3) { ((UINT8*) &mask)[3] = 0; + } GETPROJ(image32, mask); } @@ -128,8 +133,9 @@ ImagingGetExtrema(Imaging im, void *extrema) return -1; /* mismatch */ } - if (!im->xsize || !im->ysize) + if (!im->xsize || !im->ysize) { return 0; /* zero size */ + } switch (im->type) { case IMAGING_TYPE_UINT8: @@ -137,10 +143,11 @@ ImagingGetExtrema(Imaging im, void *extrema) for (y = 0; y < im->ysize; y++) { UINT8* in = im->image8[y]; for (x = 0; x < im->xsize; x++) { - if (imin > in[x]) + if (imin > in[x]) { imin = in[x]; - else if (imax < in[x]) + } else if (imax < in[x]) { imax = in[x]; + } } } ((UINT8*) extrema)[0] = (UINT8) imin; @@ -151,10 +158,11 @@ ImagingGetExtrema(Imaging im, void *extrema) for (y = 0; y < im->ysize; y++) { INT32* in = im->image32[y]; for (x = 0; x < im->xsize; x++) { - if (imin > in[x]) + if (imin > in[x]) { imin = in[x]; - else if (imax < in[x]) + } else if (imax < in[x]) { imax = in[x]; + } } } memcpy(extrema, &imin, sizeof(imin)); @@ -165,10 +173,11 @@ ImagingGetExtrema(Imaging im, void *extrema) for (y = 0; y < im->ysize; y++) { FLOAT32* in = (FLOAT32*) im->image32[y]; for (x = 0; x < im->xsize; x++) { - if (fmin > in[x]) + if (fmin > in[x]) { fmin = in[x]; - else if (fmax < in[x]) + } else if (fmax < in[x]) { fmax = in[x]; + } } } memcpy(extrema, &fmin, sizeof(fmin)); @@ -192,10 +201,11 @@ ImagingGetExtrema(Imaging im, void *extrema) #else memcpy(&v, pixel, sizeof(v)); #endif - if (imin > v) + if (imin > v) { imin = v; - else if (imax < v) + } else if (imax < v) { imax = v; + } } } v = (UINT16) imin; @@ -264,19 +274,23 @@ getcolors32(Imaging im, int maxcolors, int* size) /* printf("code_size=%d\n", code_size); */ /* printf("code_poly=%d\n", code_poly); */ - if (!code_size) + if (!code_size) { return ImagingError_MemoryError(); /* just give up */ + } - if (!im->image32) + if (!im->image32) { return ImagingError_ModeError(); + } table = calloc(code_size + 1, sizeof(ImagingColorItem)); - if (!table) + if (!table) { return ImagingError_MemoryError(); + } pixel_mask = 0xffffffff; - if (im->bands == 3) + if (im->bands == 3) { ((UINT8*) &pixel_mask)[3] = 0; + } colors = 0; @@ -289,8 +303,9 @@ getcolors32(Imaging im, int maxcolors, int* size) v = &table[i]; if (!v->count) { /* add to table */ - if (colors++ == maxcolors) + if (colors++ == maxcolors) { goto overflow; + } v->x = x; v->y = y; v->pixel = pixel; v->count = 1; @@ -300,15 +315,17 @@ getcolors32(Imaging im, int maxcolors, int* size) continue; } incr = (h ^ (h >> 3)) & code_mask; - if (!incr) + if (!incr) { incr = code_mask; + } for (;;) { i = (i + incr) & code_mask; v = &table[i]; if (!v->count) { /* add to table */ - if (colors++ == maxcolors) + if (colors++ == maxcolors) { goto overflow; + } v->x = x; v->y = y; v->pixel = pixel; v->count = 1; @@ -318,8 +335,9 @@ getcolors32(Imaging im, int maxcolors, int* size) break; } incr = incr << 1; - if (incr > code_mask) + if (incr > code_mask) { incr = incr ^ code_poly; + } } } } @@ -329,8 +347,9 @@ overflow: /* pack the table */ for (x = y = 0; x < (int) code_size; x++) if (table[x].count) { - if (x != y) + if (x != y) { table[y] = table[x]; + } y++; } table[y].count = 0; /* mark end of table */ diff --git a/src/libImaging/GifDecode.c b/src/libImaging/GifDecode.c index 8598cb6a9..5728ae1ce 100644 --- a/src/libImaging/GifDecode.c +++ b/src/libImaging/GifDecode.c @@ -142,11 +142,13 @@ ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t /* New GIF block */ /* We don't start decoding unless we have a full block */ - if (bytes < 1) + if (bytes < 1) { return ptr - buffer; + } c = *ptr; - if (bytes < c+1) + if (bytes < c+1) { return ptr - buffer; + } context->blocksize = c; @@ -167,13 +169,15 @@ ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t expanded. */ if (c == context->clear) { - if (state->state != 2) + if (state->state != 2) { state->state = 1; + } continue; } - if (c == context->end) + if (c == context->end) { break; + } i = 1; p = &context->lastdata; diff --git a/src/libImaging/GifEncode.c b/src/libImaging/GifEncode.c index 90441d184..e9c6c3149 100644 --- a/src/libImaging/GifEncode.c +++ b/src/libImaging/GifEncode.c @@ -48,12 +48,14 @@ emit(GIFENCODERSTATE *context, int byte) /* add current block to end of flush queue */ if (context->block) { block = context->flush; - while (block && block->next) + while (block && block->next) { block = block->next; - if (block) + } + if (block) { block->next = context->block; - else + } else { context->flush = context->block; + } } /* get a new block */ @@ -63,8 +65,9 @@ emit(GIFENCODERSTATE *context, int byte) } else { /* malloc check ok, small constant allocation */ block = malloc(sizeof(GIFENCODERBLOCK)); - if (!block) + if (!block) { return 0; + } } block->size = 0; @@ -154,14 +157,16 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (context->interlace) { context->interlace = 1; context->step = 8; - } else + } else { context->step = 1; + } context->last = -1; /* sanity check */ - if (state->xsize <= 0 || state->ysize <= 0) + if (state->xsize <= 0 || state->ysize <= 0) { state->state = ENCODE_EOF; + } } @@ -231,9 +236,9 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) this = state->buffer[state->x++]; - if (this == context->last) + if (this == context->last) { context->count++; - else { + } else { EMIT_RUN(label1); context->last = this; context->count = 1; @@ -263,12 +268,14 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (context->block) { GIFENCODERBLOCK* block; block = context->flush; - while (block && block->next) + while (block && block->next) { block = block->next; - if (block) + } + if (block) { block->next = context->block; - else + } else { context->flush = context->block; + } context->block = NULL; } @@ -287,8 +294,9 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (block->size > 0) { /* make sure it fits into the output buffer */ - if (bytes < block->size+1) + if (bytes < block->size+1) { return ptr - buf; + } ptr[0] = block->size; memcpy(ptr+1, block->data, block->size); @@ -300,16 +308,18 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) context->flush = block->next; - if (context->free) + if (context->free) { free(context->free); + } context->free = block; } if (state->state == EXIT) { /* this was the last block! */ - if (context->free) + if (context->free) { free(context->free); + } state->errcode = IMAGING_CODEC_END; return ptr - buf; } diff --git a/src/libImaging/HexDecode.c b/src/libImaging/HexDecode.c index 9b20bc2ac..1def8766f 100644 --- a/src/libImaging/HexDecode.c +++ b/src/libImaging/HexDecode.c @@ -30,8 +30,9 @@ ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt for (;;) { - if (bytes < 2) + if (bytes < 2) { return ptr - buf; + } a = HEX(ptr[0]); b = HEX(ptr[1]); diff --git a/src/libImaging/Histo.c b/src/libImaging/Histo.c index 0d08e9f08..b6f552ab3 100644 --- a/src/libImaging/Histo.c +++ b/src/libImaging/Histo.c @@ -29,8 +29,9 @@ void ImagingHistogramDelete(ImagingHistogram h) { - if (h->histogram) + if (h->histogram) { free(h->histogram); + } free(h); } @@ -59,15 +60,18 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax) INT32 imin, imax; FLOAT32 fmin, fmax, scale; - if (!im) + if (!im) { return ImagingError_ModeError(); + } if (imMask) { /* Validate mask */ - if (im->xsize != imMask->xsize || im->ysize != imMask->ysize) + if (im->xsize != imMask->xsize || im->ysize != imMask->ysize) { return ImagingError_Mismatch(); - if (strcmp(imMask->mode, "1") != 0 && strcmp(imMask->mode, "L") != 0) + } + if (strcmp(imMask->mode, "1") != 0 && strcmp(imMask->mode, "L") != 0) { return ImagingError_ValueError("bad transparency mask"); + } } h = ImagingHistogramNew(im); @@ -75,12 +79,15 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax) if (imMask) { /* mask */ if (im->image8) { - ImagingSectionEnter(&cookie); - for (y = 0; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++) - if (imMask->image8[y][x] != 0) - h->histogram[im->image8[y][x]]++; - ImagingSectionLeave(&cookie); + ImagingSectionEnter(&cookie); + for (y = 0; y < im->ysize; y++) { + for (x = 0; x < im->xsize; x++) { + if (imMask->image8[y][x] != 0) { + h->histogram[im->image8[y][x]]++; + } + } + } + ImagingSectionLeave(&cookie); } else { /* yes, we need the braces. C isn't Python! */ if (im->type != IMAGING_TYPE_UINT8) { ImagingHistogramDelete(h); @@ -89,25 +96,29 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax) ImagingSectionEnter(&cookie); for (y = 0; y < im->ysize; y++) { UINT8* in = (UINT8*) im->image32[y]; - for (x = 0; x < im->xsize; x++) + for (x = 0; x < im->xsize; x++) { if (imMask->image8[y][x] != 0) { h->histogram[(*in++)]++; h->histogram[(*in++)+256]++; h->histogram[(*in++)+512]++; h->histogram[(*in++)+768]++; - } else + } else { in += 4; + } + } } ImagingSectionLeave(&cookie); } } else { /* mask not given; process pixels in image */ if (im->image8) { - ImagingSectionEnter(&cookie); - for (y = 0; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++) + ImagingSectionEnter(&cookie); + for (y = 0; y < im->ysize; y++) { + for (x = 0; x < im->xsize; x++) { h->histogram[im->image8[y][x]]++; - ImagingSectionLeave(&cookie); + } + } + ImagingSectionLeave(&cookie); } else { switch (im->type) { case IMAGING_TYPE_UINT8: @@ -128,20 +139,23 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax) ImagingHistogramDelete(h); return ImagingError_ValueError("min/max not given"); } - if (!im->xsize || !im->ysize) + if (!im->xsize || !im->ysize) { break; + } memcpy(&imin, minmax, sizeof(imin)); memcpy(&imax, ((char*)minmax) + sizeof(imin), sizeof(imax)); - if (imin >= imax) + if (imin >= imax) { break; + } ImagingSectionEnter(&cookie); scale = 255.0F / (imax - imin); for (y = 0; y < im->ysize; y++) { INT32* in = im->image32[y]; for (x = 0; x < im->xsize; x++) { i = (int) (((*in++)-imin)*scale); - if (i >= 0 && i < 256) + if (i >= 0 && i < 256) { h->histogram[i]++; + } } } ImagingSectionLeave(&cookie); @@ -151,12 +165,14 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax) ImagingHistogramDelete(h); return ImagingError_ValueError("min/max not given"); } - if (!im->xsize || !im->ysize) + if (!im->xsize || !im->ysize) { break; + } memcpy(&fmin, minmax, sizeof(fmin)); memcpy(&fmax, ((char*)minmax) + sizeof(fmin), sizeof(fmax)); - if (fmin >= fmax) + if (fmin >= fmax) { break; + } ImagingSectionEnter(&cookie); scale = 255.0F / (fmax - fmin); for (y = 0; y < im->ysize; y++) { diff --git a/src/libImaging/Jpeg2KDecode.c b/src/libImaging/Jpeg2KDecode.c index d304511d1..d7a0a5b9f 100644 --- a/src/libImaging/Jpeg2KDecode.c +++ b/src/libImaging/Jpeg2KDecode.c @@ -84,10 +84,11 @@ struct j2k_decode_unpacker { static inline unsigned j2ku_shift(unsigned x, int n) { - if (n < 0) + if (n < 0) { return x >> -n; - else + } else { return x << n; + } } static void @@ -104,11 +105,13 @@ j2ku_gray_l(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, unsigned x, y; - if (csiz == 3) + if (csiz == 3) { csiz = 4; + } - if (shift < 0) + if (shift < 0) { offset += 1 << (-shift - 1); + } /* csiz*h*w + offset = tileinfo.datasize */ switch (csiz) { @@ -116,24 +119,27 @@ j2ku_gray_l(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, for (y = 0; y < h; ++y) { const UINT8 *data = &tiledata[y * w]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0; - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *row++ = j2ku_shift(offset + *data++, shift); + } } break; case 2: for (y = 0; y < h; ++y) { const UINT16 *data = (const UINT16 *)&tiledata[2 * y * w]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0; - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *row++ = j2ku_shift(offset + *data++, shift); + } } break; case 4: for (y = 0; y < h; ++y) { const UINT32 *data = (const UINT32 *)&tiledata[4 * y * w]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0; - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *row++ = j2ku_shift(offset + *data++, shift); + } } break; } @@ -154,35 +160,40 @@ j2ku_gray_i(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, unsigned x, y; - if (csiz == 3) + if (csiz == 3) { csiz = 4; + } - if (shift < 0) + if (shift < 0) { offset += 1 << (-shift - 1); + } switch (csiz) { case 1: for (y = 0; y < h; ++y) { const UINT8 *data = &tiledata[y * w]; UINT16 *row = (UINT16 *)im->image[y0 + y] + x0; - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *row++ = j2ku_shift(offset + *data++, shift); + } } break; case 2: for (y = 0; y < h; ++y) { const UINT16 *data = (const UINT16 *)&tiledata[2 * y * w]; UINT16 *row = (UINT16 *)im->image[y0 + y] + x0; - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *row++ = j2ku_shift(offset + *data++, shift); + } } break; case 4: for (y = 0; y < h; ++y) { const UINT32 *data = (const UINT32 *)&tiledata[4 * y * w]; UINT16 *row = (UINT16 *)im->image[y0 + y] + x0; - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *row++ = j2ku_shift(offset + *data++, shift); + } } break; } @@ -203,11 +214,13 @@ j2ku_gray_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, unsigned x, y; - if (shift < 0) + if (shift < 0) { offset += 1 << (-shift - 1); + } - if (csiz == 3) + if (csiz == 3) { csiz = 4; + } switch (csiz) { case 1: @@ -267,15 +280,19 @@ j2ku_graya_la(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, unsigned x, y; - if (csiz == 3) + if (csiz == 3) { csiz = 4; - if (acsiz == 3) + } + if (acsiz == 3) { acsiz = 4; + } - if (shift < 0) + if (shift < 0) { offset += 1 << (-shift - 1); - if (ashift < 0) + } + if (ashift < 0) { aoffset += 1 << (-ashift - 1); + } atiledata = tiledata + csiz * w * h; @@ -325,11 +342,13 @@ j2ku_srgb_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0; csiz[n] = (in->comps[n].prec + 7) >> 3; - if (csiz[n] == 3) + if (csiz[n] == 3) { csiz[n] = 4; + } - if (shifts[n] < 0) + if (shifts[n] < 0) { offsets[n] += 1 << (-shifts[n] - 1); + } cptr += csiz[n] * w * h; } @@ -337,8 +356,9 @@ j2ku_srgb_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, for (y = 0; y < h; ++y) { const UINT8 *data[3]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; - for (n = 0; n < 3; ++n) + for (n = 0; n < 3; ++n) { data[n] = &cdata[n][csiz[n] * y * w]; + } for (x = 0; x < w; ++x) { for (n = 0; n < 3; ++n) { @@ -377,11 +397,13 @@ j2ku_sycc_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0; csiz[n] = (in->comps[n].prec + 7) >> 3; - if (csiz[n] == 3) + if (csiz[n] == 3) { csiz[n] = 4; + } - if (shifts[n] < 0) + if (shifts[n] < 0) { offsets[n] += 1 << (-shifts[n] - 1); + } cptr += csiz[n] * w * h; } @@ -390,8 +412,9 @@ j2ku_sycc_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, const UINT8 *data[3]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; UINT8 *row_start = row; - for (n = 0; n < 3; ++n) + for (n = 0; n < 3; ++n) { data[n] = &cdata[n][csiz[n] * y * w]; + } for (x = 0; x < w; ++x) { for (n = 0; n < 3; ++n) { @@ -432,11 +455,13 @@ j2ku_srgba_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0; csiz[n] = (in->comps[n].prec + 7) >> 3; - if (csiz[n] == 3) + if (csiz[n] == 3) { csiz[n] = 4; + } - if (shifts[n] < 0) + if (shifts[n] < 0) { offsets[n] += 1 << (-shifts[n] - 1); + } cptr += csiz[n] * w * h; } @@ -444,8 +469,9 @@ j2ku_srgba_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, for (y = 0; y < h; ++y) { const UINT8 *data[4]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; - for (n = 0; n < 4; ++n) + for (n = 0; n < 4; ++n) { data[n] = &cdata[n][csiz[n] * y * w]; + } for (x = 0; x < w; ++x) { for (n = 0; n < 4; ++n) { @@ -483,11 +509,13 @@ j2ku_sycca_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0; csiz[n] = (in->comps[n].prec + 7) >> 3; - if (csiz[n] == 3) + if (csiz[n] == 3) { csiz[n] = 4; + } - if (shifts[n] < 0) + if (shifts[n] < 0) { offsets[n] += 1 << (-shifts[n] - 1); + } cptr += csiz[n] * w * h; } @@ -496,8 +524,9 @@ j2ku_sycca_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, const UINT8 *data[4]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; UINT8 *row_start = row; - for (n = 0; n < 4; ++n) + for (n = 0; n < 4; ++n) { data[n] = &cdata[n][csiz[n] * y * w]; + } for (x = 0; x < w; ++x) { for (n = 0; n < 4; ++n) { @@ -584,10 +613,11 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) possibly support is 4GB. We can't go larger than this, because OpenJPEG truncates this value for the final box in the file, and the box lengths in OpenJPEG are currently 32 bit. */ - if (context->length < 0) + if (context->length < 0) { opj_stream_set_user_data_length(stream, 0xffffffff); - else + } else { opj_stream_set_user_data_length(stream, context->length); + } #endif /* Setup decompression context */ @@ -696,8 +726,9 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) goto quick_exit; } - if (!should_continue) + if (!should_continue) { break; + } /* Adjust the tile co-ordinates based on the reduction (OpenJPEG doesn't do this for us) */ @@ -784,12 +815,15 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) } quick_exit: - if (codec) + if (codec) { opj_destroy_codec(codec); - if (image) + } + if (image) { opj_image_destroy(image); - if (stream) + } + if (stream) { opj_stream_destroy(stream); + } return -1; } @@ -804,8 +838,9 @@ ImagingJpeg2KDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t return -1; } - if (state->state == J2K_STATE_DONE || state->state == J2K_STATE_FAILED) + if (state->state == J2K_STATE_DONE || state->state == J2K_STATE_FAILED) { return -1; + } if (state->state == J2K_STATE_START) { state->state = J2K_STATE_DECODING; diff --git a/src/libImaging/Jpeg2KEncode.c b/src/libImaging/Jpeg2KEncode.c index aef1a4b94..49ef5e254 100644 --- a/src/libImaging/Jpeg2KEncode.c +++ b/src/libImaging/Jpeg2KEncode.c @@ -106,8 +106,9 @@ j2k_pack_l(Imaging im, UINT8 *buf, unsigned x,y; for (y = 0; y < h; ++y) { UINT8 *data = (UINT8 *)(im->image[y + y0] + x0); - for (x = 0; x < w; ++x) + for (x = 0; x < w; ++x) { *ptr++ = *data++; + } } } @@ -240,8 +241,9 @@ j2k_set_cinema_params(Imaging im, int components, opj_cparameters_t *params) } else { rate = ((float)(components * im->xsize * im->ysize * 8) / (params->tcp_rates[n] * 8)); - if (rate > CINEMA_24_CS_LENGTH) + if (rate > CINEMA_24_CS_LENGTH) { params->tcp_rates[n] = max_rate; + } } } @@ -257,8 +259,9 @@ j2k_set_cinema_params(Imaging im, int components, opj_cparameters_t *params) } else { rate = ((float)(components * im->xsize * im->ysize * 8) / (params->tcp_rates[n] * 8)); - if (rate > CINEMA_48_CS_LENGTH) + if (rate > CINEMA_48_CS_LENGTH) { params->tcp_rates[n] = max_rate; + } } } @@ -397,8 +400,9 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) float *pq; if (len) { - if (len > sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0])) + if (len > sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0])) { len = sizeof(params.tcp_rates)/sizeof(params.tcp_rates[0]); + } params.tcp_numlayers = (int)len; @@ -423,8 +427,9 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) params.cp_disto_alloc = 1; } - if (context->num_resolutions) + if (context->num_resolutions) { params.numresolution = context->num_resolutions; + } if (context->cblk_width >= 4 && context->cblk_width <= 1024 && context->cblk_height >= 4 && context->cblk_height <= 1024 @@ -455,18 +460,21 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) case OPJ_CINEMA2K_24: case OPJ_CINEMA2K_48: params.cp_rsiz = OPJ_CINEMA2K; - if (params.numresolution > 6) + if (params.numresolution > 6) { params.numresolution = 6; + } break; case OPJ_CINEMA4K_24: params.cp_rsiz = OPJ_CINEMA4K; - if (params.numresolution > 7) + if (params.numresolution > 7) { params.numresolution = 7; + } break; } - if (context->cinema_mode != OPJ_OFF) + if (context->cinema_mode != OPJ_OFF) { j2k_set_cinema_params(im, components, ¶ms); + } /* Set up the reference grid in the image */ image->x0 = params.image_offset_x0; @@ -526,10 +534,12 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) unsigned ty1 = ty0 + tile_height; unsigned pixy, pixh; - if (ty0 < params.image_offset_y0) + if (ty0 < params.image_offset_y0) { ty0 = params.image_offset_y0; - if (ty1 > ysiz) + } + if (ty1 > ysiz) { ty1 = ysiz; + } pixy = ty0 - params.image_offset_y0; pixh = ty1 - ty0; @@ -540,10 +550,12 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) unsigned pixx, pixw; unsigned data_size; - if (tx0 < params.image_offset_x0) + if (tx0 < params.image_offset_x0) { tx0 = params.image_offset_x0; - if (tx1 > xsiz) + } + if (tx1 > xsiz) { tx1 = xsiz; + } pixx = tx0 - params.image_offset_x0; pixw = tx1 - tx0; @@ -572,12 +584,15 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) ret = -1; quick_exit: - if (codec) + if (codec) { opj_destroy_codec(codec); - if (image) + } + if (image) { opj_image_destroy(image); - if (stream) + } + if (stream) { opj_stream_destroy(stream); + } return ret; } @@ -585,8 +600,9 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) int ImagingJpeg2KEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) { - if (state->state == J2K_STATE_FAILED) + if (state->state == J2K_STATE_FAILED) { return -1; + } if (state->state == J2K_STATE_START) { @@ -611,8 +627,9 @@ ImagingJpeg2KEncodeCleanup(ImagingCodecState state) { context->quality_layers = NULL; } - if (context->error_msg) + if (context->error_msg) { free ((void *)context->error_msg); + } context->error_msg = NULL; diff --git a/src/libImaging/JpegDecode.c b/src/libImaging/JpegDecode.c index ce0c2ca7c..fb6112044 100644 --- a/src/libImaging/JpegDecode.c +++ b/src/libImaging/JpegDecode.c @@ -176,8 +176,9 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t by if (context->source.skip > 0) { skip_input_data(&context->cinfo, context->source.skip); - if (context->source.skip > 0) + if (context->source.skip > 0) { return context->source.pub.next_input_byte - buf; + } } switch (state->state) { @@ -193,43 +194,46 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t by } while (ok == JPEG_HEADER_TABLES_ONLY); - if (ok == JPEG_SUSPENDED) + if (ok == JPEG_SUSPENDED) { break; + } /* Decoder settings */ /* jpegmode indicates whats in the file; if not set, we'll trust the decoder */ - if (strcmp(context->jpegmode, "L") == 0) + if (strcmp(context->jpegmode, "L") == 0) { context->cinfo.jpeg_color_space = JCS_GRAYSCALE; - else if (strcmp(context->jpegmode, "RGB") == 0) + } else if (strcmp(context->jpegmode, "RGB") == 0) { context->cinfo.jpeg_color_space = JCS_RGB; - else if (strcmp(context->jpegmode, "CMYK") == 0) + } else if (strcmp(context->jpegmode, "CMYK") == 0) { context->cinfo.jpeg_color_space = JCS_CMYK; - else if (strcmp(context->jpegmode, "YCbCr") == 0) + } else if (strcmp(context->jpegmode, "YCbCr") == 0) { context->cinfo.jpeg_color_space = JCS_YCbCr; - else if (strcmp(context->jpegmode, "YCbCrK") == 0) { + } else if (strcmp(context->jpegmode, "YCbCrK") == 0) { context->cinfo.jpeg_color_space = JCS_YCCK; } /* rawmode indicates what we want from the decoder. if not set, conversions are disabled */ - if (strcmp(context->rawmode, "L") == 0) + if (strcmp(context->rawmode, "L") == 0) { context->cinfo.out_color_space = JCS_GRAYSCALE; - else if (strcmp(context->rawmode, "RGB") == 0) + } else if (strcmp(context->rawmode, "RGB") == 0) { context->cinfo.out_color_space = JCS_RGB; + } #ifdef JCS_EXTENSIONS - else if (strcmp(context->rawmode, "RGBX") == 0) + else if (strcmp(context->rawmode, "RGBX") == 0) { context->cinfo.out_color_space = JCS_EXT_RGBX; + } #endif else if (strcmp(context->rawmode, "CMYK") == 0 || - strcmp(context->rawmode, "CMYK;I") == 0) + strcmp(context->rawmode, "CMYK;I") == 0) { context->cinfo.out_color_space = JCS_CMYK; - else if (strcmp(context->rawmode, "YCbCr") == 0) + } else if (strcmp(context->rawmode, "YCbCr") == 0) { context->cinfo.out_color_space = JCS_YCbCr; - else if (strcmp(context->rawmode, "YCbCrK") == 0) + } else if (strcmp(context->rawmode, "YCbCrK") == 0) { context->cinfo.out_color_space = JCS_YCCK; - else { + } else { /* Disable decoder conversions */ context->cinfo.jpeg_color_space = JCS_UNKNOWN; context->cinfo.out_color_space = JCS_UNKNOWN; @@ -251,8 +255,9 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t by /* Set things up for decompression (this processes the entire file if necessary to return data line by line) */ - if (!jpeg_start_decompress(&context->cinfo)) + if (!jpeg_start_decompress(&context->cinfo)) { break; + } state->state++; /* fall through */ @@ -263,15 +268,17 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t by ok = 1; while (state->y < state->ysize) { ok = jpeg_read_scanlines(&context->cinfo, &state->buffer, 1); - if (ok != 1) + if (ok != 1) { break; + } state->shuffle((UINT8*) im->image[state->y + state->yoff] + state->xoff * im->pixelsize, state->buffer, state->xsize); state->y++; } - if (ok != 1) + if (ok != 1) { break; + } state->state++; /* fall through */ @@ -280,8 +287,9 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t by /* Finish decompression */ if (!jpeg_finish_decompress(&context->cinfo)) { /* FIXME: add strictness mode test */ - if (state->y < state->ysize) + if (state->y < state->ysize) { break; + } } /* Clean up */ diff --git a/src/libImaging/JpegEncode.c b/src/libImaging/JpegEncode.c index 2eb35d6bf..5f3b29c66 100644 --- a/src/libImaging/JpegEncode.c +++ b/src/libImaging/JpegEncode.c @@ -127,17 +127,19 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) break; case 24: context->cinfo.input_components = 3; - if (strcmp(im->mode, "YCbCr") == 0) + if (strcmp(im->mode, "YCbCr") == 0) { context->cinfo.in_color_space = JCS_YCbCr; - else + } else { context->cinfo.in_color_space = JCS_RGB; + } break; case 32: context->cinfo.input_components = 4; context->cinfo.in_color_space = JCS_CMYK; #ifdef JCS_EXTENSIONS - if (strcmp(context->rawmode, "RGBX") == 0) + if (strcmp(context->rawmode, "RGBX") == 0) { context->cinfo.in_color_space = JCS_EXT_RGBX; + } #endif break; default: @@ -214,8 +216,9 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) break; } } - if (context->progressive) - jpeg_simple_progression(&context->cinfo); + if (context->progressive) { + jpeg_simple_progression(&context->cinfo); + } context->cinfo.smoothing_factor = context->smooth; context->cinfo.optimize_coding = (boolean) context->optimize; if (context->xdpi > 0 && context->ydpi > 0) { @@ -261,17 +264,19 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (context->extra) { /* copy extra buffer to output buffer */ unsigned int n = context->extra_size - context->extra_offset; - if (n > context->destination.pub.free_in_buffer) + if (n > context->destination.pub.free_in_buffer) { n = context->destination.pub.free_in_buffer; + } memcpy(context->destination.pub.next_output_byte, context->extra + context->extra_offset, n); context->destination.pub.next_output_byte += n; context->destination.pub.free_in_buffer -= n; context->extra_offset += n; - if (context->extra_offset >= context->extra_size) + if (context->extra_offset >= context->extra_size) { state->state++; - else + } else { break; + } } else state->state++; @@ -286,21 +291,24 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) (UINT8*) im->image[state->y + state->yoff] + state->xoff * im->pixelsize, state->xsize); ok = jpeg_write_scanlines(&context->cinfo, &state->buffer, 1); - if (ok != 1) + if (ok != 1) { break; + } state->y++; } - if (ok != 1) + if (ok != 1) { break; + } state->state++; /* fall through */ case 5: /* Finish compression */ - if (context->destination.pub.free_in_buffer < 100) + if (context->destination.pub.free_in_buffer < 100) { break; + } jpeg_finish_compress(&context->cinfo); /* Clean up */ diff --git a/src/libImaging/Matrix.c b/src/libImaging/Matrix.c index 22d5d642c..e02b7b312 100644 --- a/src/libImaging/Matrix.c +++ b/src/libImaging/Matrix.c @@ -27,14 +27,16 @@ ImagingConvertMatrix(Imaging im, const char *mode, float m[]) int x, y; /* Assume there's enough data in the buffer */ - if (!im) + if (!im) { return (Imaging) ImagingError_ModeError(); + } if (strcmp(mode, "L") == 0 && im->bands == 3) { imOut = ImagingNewDirty("L", im->xsize, im->ysize); - if (!imOut) + if (!imOut) { return NULL; + } for (y = 0; y < im->ysize; y++) { UINT8* in = (UINT8*) im->image[y]; @@ -50,8 +52,9 @@ ImagingConvertMatrix(Imaging im, const char *mode, float m[]) } else if (strlen(mode) == 3 && im->bands == 3) { imOut = ImagingNewDirty(mode, im->xsize, im->ysize); - if (!imOut) + if (!imOut) { return NULL; + } for (y = 0; y < im->ysize; y++) { UINT8* in = (UINT8*) im->image[y]; @@ -67,8 +70,9 @@ ImagingConvertMatrix(Imaging im, const char *mode, float m[]) in += 4; out += 4; } } - } else + } else { return (Imaging) ImagingError_ModeError(); + } return imOut; } diff --git a/src/libImaging/ModeFilter.c b/src/libImaging/ModeFilter.c index 5237d0732..b1cf7442c 100644 --- a/src/libImaging/ModeFilter.c +++ b/src/libImaging/ModeFilter.c @@ -25,12 +25,14 @@ ImagingModeFilter(Imaging im, int size) UINT8 maxpixel; int histogram[256]; - if (!im || im->bands != 1 || im->type != IMAGING_TYPE_UINT8) + if (!im || im->bands != 1 || im->type != IMAGING_TYPE_UINT8) { return (Imaging) ImagingError_ModeError(); + } imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); - if (!imOut) + if (!imOut) { return NULL; + } size = size / 2; @@ -46,27 +48,32 @@ ImagingModeFilter(Imaging im, int size) the added complexity... */ memset(histogram, 0, sizeof(histogram)); - for (yy = y - size; yy <= y + size; yy++) + for (yy = y - size; yy <= y + size; yy++) { if (yy >= 0 && yy < imOut->ysize) { UINT8* in = &IMAGING_PIXEL_L(im, 0, yy); - for (xx = x - size; xx <= x + size; xx++) - if (xx >= 0 && xx < imOut->xsize) + for (xx = x - size; xx <= x + size; xx++) { + if (xx >= 0 && xx < imOut->xsize) { histogram[in[xx]]++; + } + } } + } /* find most frequent pixel value in this region */ maxpixel = 0; maxcount = histogram[maxpixel]; - for (i = 1; i < 256; i++) + for (i = 1; i < 256; i++) { if (histogram[i] > maxcount) { maxcount = histogram[i]; maxpixel = (UINT8) i; } + } - if (maxcount > 2) + if (maxcount > 2) { out[x] = maxpixel; - else + } else { out[x] = IMAGING_PIXEL_L(im, x, y); + } } diff --git a/src/libImaging/Negative.c b/src/libImaging/Negative.c index fe08e4753..09c946bbe 100644 --- a/src/libImaging/Negative.c +++ b/src/libImaging/Negative.c @@ -26,16 +26,20 @@ ImagingNegative(Imaging im) Imaging imOut; int x, y; - if (!im) + if (!im) { return (Imaging) ImagingError_ModeError(); + } imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); - if (!imOut) + if (!imOut) { return NULL; + } - for (y = 0; y < im->ysize; y++) - for (x = 0; x < im->linesize; x++) + for (y = 0; y < im->ysize; y++) { + for (x = 0; x < im->linesize; x++) { imOut->image[y][x] = ~im->image[y][x]; + } + } return imOut; } diff --git a/src/libImaging/Offset.c b/src/libImaging/Offset.c index 0f9793c3c..29f038153 100644 --- a/src/libImaging/Offset.c +++ b/src/libImaging/Offset.c @@ -24,38 +24,44 @@ ImagingOffset(Imaging im, int xoffset, int yoffset) int x, y; Imaging imOut; - if (!im) + if (!im) { return (Imaging) ImagingError_ModeError(); + } imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); - if (!imOut) + if (!imOut) { return NULL; + } ImagingCopyPalette(imOut, im); /* make offsets positive to avoid negative coordinates */ xoffset %= im->xsize; xoffset = im->xsize - xoffset; - if (xoffset < 0) + if (xoffset < 0) { xoffset += im->xsize; + } yoffset %= im->ysize; yoffset = im->ysize - yoffset; - if (yoffset < 0) + if (yoffset < 0) { yoffset += im->ysize; + } #define OFFSET(image)\ - for (y = 0; y < im->ysize; y++)\ + for (y = 0; y < im->ysize; y++) {\ for (x = 0; x < im->xsize; x++) {\ int yi = (y + yoffset) % im->ysize;\ int xi = (x + xoffset) % im->xsize;\ imOut->image[y][x] = im->image[yi][xi];\ + }\ } - if (im->image8) + if (im->image8) { OFFSET(image8) - else + } else { OFFSET(image32) + } return imOut; } diff --git a/src/libImaging/Pack.c b/src/libImaging/Pack.c index a239464d4..0be7ad8c9 100644 --- a/src/libImaging/Pack.c +++ b/src/libImaging/Pack.c @@ -80,16 +80,18 @@ pack1(UINT8* out, const UINT8* in, int pixels) /* bilevel (black is 0) */ b = 0; m = 128; for (i = 0; i < pixels; i++) { - if (in[i] != 0) + if (in[i] != 0) { b |= m; + } m >>= 1; if (m == 0) { *out++ = b; b = 0; m = 128; } } - if (m != 128) + if (m != 128) { *out++ = b; + } } static void @@ -99,16 +101,18 @@ pack1I(UINT8* out, const UINT8* in, int pixels) /* bilevel (black is 1) */ b = 0; m = 128; for (i = 0; i < pixels; i++) { - if (in[i] == 0) + if (in[i] == 0) { b |= m; + } m >>= 1; if (m == 0) { *out++ = b; b = 0; m = 128; } } - if (m != 128) + if (m != 128) { *out++ = b; + } } static void @@ -118,16 +122,18 @@ pack1R(UINT8* out, const UINT8* in, int pixels) /* bilevel, lsb first (black is 0) */ b = 0; m = 1; for (i = 0; i < pixels; i++) { - if (in[i] != 0) + if (in[i] != 0) { b |= m; + } m <<= 1; if (m == 256){ *out++ = b; b = 0; m = 1; } } - if (m != 1) + if (m != 1) { *out++ = b; + } } static void @@ -137,16 +143,18 @@ pack1IR(UINT8* out, const UINT8* in, int pixels) /* bilevel, lsb first (black is 1) */ b = 0; m = 1; for (i = 0; i < pixels; i++) { - if (in[i] == 0) + if (in[i] == 0) { b |= m; + } m <<= 1; if (m == 256){ *out++ = b; b = 0; m = 1; } } - if (m != 1) + if (m != 1) { *out++ = b; + } } static void @@ -154,8 +162,9 @@ pack1L(UINT8* out, const UINT8* in, int pixels) { int i; /* bilevel, stored as bytes */ - for (i = 0; i < pixels; i++) + for (i = 0; i < pixels; i++) { out[i] = (in[i] != 0) ? 255 : 0; + } } static void @@ -167,8 +176,9 @@ packP4(UINT8* out, const UINT8* in, int pixels) in += 2; pixels -= 2; } - if (pixels) + if (pixels) { out[0] = (in[0] << 4); + } } static void @@ -407,12 +417,13 @@ packI16B(UINT8* out, const UINT8* in_, int pixels) for (i = 0; i < pixels; i++) { INT32 in; memcpy(&in, in_, sizeof(in)); - if (in <= 0) + if (in <= 0) { tmp_ = 0; - else if (in > 65535) + } else if (in > 65535) { tmp_ = 65535; - else + } else { tmp_ = in; + } C16B; out += 2; in_ += sizeof(in); } @@ -496,40 +507,45 @@ copy4I(UINT8* out, const UINT8* in, int pixels) { /* RGBA, CMYK quadruples, inverted */ int i; - for (i = 0; i < pixels*4; i++) + for (i = 0; i < pixels*4; i++) { out[i] = ~in[i]; + } } static void band0(UINT8* out, const UINT8* in, int pixels) { int i; - for (i = 0; i < pixels; i++, in += 4) + for (i = 0; i < pixels; i++, in += 4) { out[i] = in[0]; + } } static void band1(UINT8* out, const UINT8* in, int pixels) { int i; - for (i = 0; i < pixels; i++, in += 4) + for (i = 0; i < pixels; i++, in += 4) { out[i] = in[1]; + } } static void band2(UINT8* out, const UINT8* in, int pixels) { int i; - for (i = 0; i < pixels; i++, in += 4) + for (i = 0; i < pixels; i++, in += 4) { out[i] = in[2]; + } } static void band3(UINT8* out, const UINT8* in, int pixels) { int i; - for (i = 0; i < pixels; i++, in += 4) + for (i = 0; i < pixels; i++, in += 4) { out[i] = in[3]; + } } static struct { @@ -673,12 +689,14 @@ ImagingFindPacker(const char* mode, const char* rawmode, int* bits_out) int i; /* find a suitable pixel packer */ - for (i = 0; packers[i].rawmode; i++) + for (i = 0; packers[i].rawmode; i++) { if (strcmp(packers[i].mode, mode) == 0 && strcmp(packers[i].rawmode, rawmode) == 0) { - if (bits_out) + if (bits_out) { *bits_out = packers[i].bits; + } return packers[i].pack; } + } return NULL; } diff --git a/src/libImaging/PackDecode.c b/src/libImaging/PackDecode.c index b9c846f4b..34671828a 100644 --- a/src/libImaging/PackDecode.c +++ b/src/libImaging/PackDecode.c @@ -28,8 +28,9 @@ ImagingPackbitsDecode(Imaging im, ImagingCodecState state, for (;;) { - if (bytes < 1) + if (bytes < 1) { return ptr - buf; + } if (ptr[0] & 0x80) { @@ -40,8 +41,9 @@ ImagingPackbitsDecode(Imaging im, ImagingCodecState state, } /* Run */ - if (bytes < 2) + if (bytes < 2) { return ptr - buf; + } for (n = 257 - ptr[0]; n > 0; n--) { if (state->x >= state->bytes) { @@ -58,8 +60,9 @@ ImagingPackbitsDecode(Imaging im, ImagingCodecState state, /* Literal */ n = ptr[0]+2; - if (bytes < n) + if (bytes < n) { return ptr - buf; + } for (i = 1; i < n; i++) { if (state->x >= state->bytes) { diff --git a/src/libImaging/Palette.c b/src/libImaging/Palette.c index 7aee6e8ee..ac548f50c 100644 --- a/src/libImaging/Palette.c +++ b/src/libImaging/Palette.c @@ -30,12 +30,14 @@ ImagingPaletteNew(const char* mode) int i; ImagingPalette palette; - if (strcmp(mode, "RGB") && strcmp(mode, "RGBA")) + if (strcmp(mode, "RGB") && strcmp(mode, "RGBA")) { return (ImagingPalette) ImagingError_ModeError(); + } palette = calloc(1, sizeof(struct ImagingPaletteInstance)); - if (!palette) + if (!palette) { return (ImagingPalette) ImagingError_MemoryError(); + } strncpy(palette->mode, mode, IMAGING_MODE_LENGTH-1); palette->mode[IMAGING_MODE_LENGTH-1] = 0; @@ -60,8 +62,9 @@ ImagingPaletteNewBrowser(void) ImagingPalette palette; palette = ImagingPaletteNew("RGB"); - if (!palette) + if (!palette) { return NULL; + } /* Blank out unused entries */ /* FIXME: Add 10-level windows palette here? */ @@ -74,14 +77,16 @@ ImagingPaletteNewBrowser(void) /* Simple 6x6x6 colour cube */ - for (b = 0; b < 256; b += 51) - for (g = 0; g < 256; g += 51) + for (b = 0; b < 256; b += 51) { + for (g = 0; g < 256; g += 51) { for (r = 0; r < 256; r += 51) { palette->palette[i*4+0] = r; palette->palette[i*4+1] = g; palette->palette[i*4+2] = b; i++; } + } + } /* Blank out unused entries */ /* FIXME: add 30-level greyscale wedge here? */ @@ -102,12 +107,14 @@ ImagingPaletteDuplicate(ImagingPalette palette) ImagingPalette new_palette; - if (!palette) + if (!palette) { return NULL; + } /* malloc check ok, small constant allocation */ new_palette = malloc(sizeof(struct ImagingPaletteInstance)); - if (!new_palette) + if (!new_palette) { return (ImagingPalette) ImagingError_MemoryError(); + } memcpy(new_palette, palette, sizeof(struct ImagingPaletteInstance)); @@ -123,8 +130,9 @@ ImagingPaletteDelete(ImagingPalette palette) /* Destroy palette object */ if (palette) { - if (palette->cache) + if (palette->cache) { free(palette->cache); + } free(palette); } } @@ -209,8 +217,9 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) tmax += (b <= bc) ? BDIST(b, b1) : BDIST(b, b0); dmin[i] = tmin; - if (tmax < dmax) + if (tmax < dmax) { dmax = tmax; /* keep the smallest max distance only */ + } } @@ -220,10 +229,11 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) * all slots in that box. We only check boxes for which the min * distance is less than or equal the smallest max distance */ - for (i = 0; i < BOXVOLUME; i++) + for (i = 0; i < BOXVOLUME; i++) { d[i] = (unsigned int) ~0; + } - for (i = 0; i < 256; i++) + for (i = 0; i < 256; i++) { if (dmin[i] <= dmax) { @@ -262,6 +272,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) rx += 2 * RSTEP * RSTEP; } } + } /* Step 3 -- Update cache */ @@ -269,10 +280,13 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) * cache slot in the box. Update the cache. */ j = 0; - for (r = r0; r < r1; r+=4) - for (g = g0; g < g1; g+=4) - for (b = b0; b < b1; b+=4) + for (r = r0; r < r1; r+=4) { + for (g = g0; g < g1; g+=4) { + for (b = b0; b < b1; b+=4) { ImagingPaletteCache(palette, r, g, b) = c[j++]; + } + } + } } @@ -297,8 +311,9 @@ ImagingPaletteCachePrepare(ImagingPalette palette) } /* Mark all entries as empty */ - for (i = 0; i < entries; i++) + for (i = 0; i < entries; i++) { palette->cache[i] = 0x100; + } } diff --git a/src/libImaging/Paste.c b/src/libImaging/Paste.c index 0bda25739..6dfff4afb 100644 --- a/src/libImaging/Paste.c +++ b/src/libImaging/Paste.c @@ -37,8 +37,9 @@ paste(Imaging imOut, Imaging imIn, int dx, int dy, int sx, int sy, xsize *= pixelsize; - for (y = 0; y < ysize; y++) + for (y = 0; y < ysize; y++) { memcpy(imOut->image[y+dy]+dx, imIn->image[y+sy]+sx, xsize); + } } static inline void @@ -57,8 +58,9 @@ paste_mask_1(Imaging imOut, Imaging imIn, Imaging imMask, UINT8* in = imIn->image8[y+sy]+sx; UINT8* mask = imMask->image8[y+sy]+sx; for (x = 0; x < xsize; x++) { - if (*mask++) + if (*mask++) { *out = *in; + } out++, in++; } } @@ -70,8 +72,9 @@ paste_mask_1(Imaging imOut, Imaging imIn, Imaging imMask, INT32* in = imIn->image32[y+sy]+sx; UINT8* mask = imMask->image8[y+sy]+sx; for (x = 0; x < xsize; x++) { - if (*mask++) + if (*mask++) { *out = *in; + } out++, in++; } } @@ -231,17 +234,22 @@ ImagingPaste(Imaging imOut, Imaging imIn, Imaging imMask, /* Determine which region to copy */ sx0 = sy0 = 0; - if (dx0 < 0) + if (dx0 < 0) { xsize += dx0, sx0 = -dx0, dx0 = 0; - if (dx0 + xsize > imOut->xsize) + } + if (dx0 + xsize > imOut->xsize) { xsize = imOut->xsize - dx0; - if (dy0 < 0) + } + if (dy0 < 0) { ysize += dy0, sy0 = -dy0, dy0 = 0; - if (dy0 + ysize > imOut->ysize) + } + if (dy0 + ysize > imOut->ysize) { ysize = imOut->ysize - dy0; + } - if (xsize <= 0 || ysize <= 0) + if (xsize <= 0 || ysize <= 0) { return 0; + } if (!imMask) { ImagingSectionEnter(&cookie); @@ -297,15 +305,17 @@ fill(Imaging imOut, const void* ink_, int dx, int dy, dx *= pixelsize; xsize *= pixelsize; - for (y = 0; y < ysize; y++) + for (y = 0; y < ysize; y++) { memset(imOut->image[y+dy]+dx, ink8, xsize); + } } else { for (y = 0; y < ysize; y++) { INT32* out = imOut->image32[y+dy]+dx; - for (x = 0; x < xsize; x++) + for (x = 0; x < xsize; x++) { out[x] = ink32; + } } } @@ -331,8 +341,9 @@ fill_mask_1(Imaging imOut, const void* ink_, Imaging imMask, UINT8* out = imOut->image8[y+dy]+dx; UINT8* mask = imMask->image8[y+sy]+sx; for (x = 0; x < xsize; x++) { - if (*mask++) + if (*mask++) { *out = ink8; + } out++; } } @@ -343,8 +354,9 @@ fill_mask_1(Imaging imOut, const void* ink_, Imaging imMask, INT32* out = imOut->image32[y+dy]+dx; UINT8* mask = imMask->image8[y+sy]+sx; for (x = 0; x < xsize; x++) { - if (*mask++) + if (*mask++) { *out = ink32; + } out++; } } @@ -494,17 +506,22 @@ ImagingFill2(Imaging imOut, const void* ink, Imaging imMask, /* Determine which region to fill */ sx0 = sy0 = 0; - if (dx0 < 0) + if (dx0 < 0) { xsize += dx0, sx0 = -dx0, dx0 = 0; - if (dx0 + xsize > imOut->xsize) + } + if (dx0 + xsize > imOut->xsize) { xsize = imOut->xsize - dx0; - if (dy0 < 0) + } + if (dy0 < 0) { ysize += dy0, sy0 = -dy0, dy0 = 0; - if (dy0 + ysize > imOut->ysize) + } + if (dy0 + ysize > imOut->ysize) { ysize = imOut->ysize - dy0; + } - if (xsize <= 0 || ysize <= 0) + if (xsize <= 0 || ysize <= 0) { return 0; + } if (!imMask) { ImagingSectionEnter(&cookie); diff --git a/src/libImaging/PcdDecode.c b/src/libImaging/PcdDecode.c index 6f86dc96b..ff192a174 100644 --- a/src/libImaging/PcdDecode.c +++ b/src/libImaging/PcdDecode.c @@ -38,8 +38,9 @@ ImagingPcdDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt for (;;) { /* We need data for two full lines before we can do anything */ - if (bytes < chunk) + if (bytes < chunk) { return ptr - buf; + } /* Unpack first line */ out = state->buffer; @@ -53,8 +54,9 @@ ImagingPcdDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt state->shuffle((UINT8*) im->image[state->y], state->buffer, state->xsize); - if (++state->y >= state->ysize) + if (++state->y >= state->ysize) { return -1; /* This can hardly happen */ + } /* Unpack second line */ out = state->buffer; @@ -68,8 +70,9 @@ ImagingPcdDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt state->shuffle((UINT8*) im->image[state->y], state->buffer, state->xsize); - if (++state->y >= state->ysize) + if (++state->y >= state->ysize) { return -1; + } ptr += chunk; bytes -= chunk; diff --git a/src/libImaging/PcxDecode.c b/src/libImaging/PcxDecode.c index a4e40864b..eb0fecc83 100644 --- a/src/libImaging/PcxDecode.c +++ b/src/libImaging/PcxDecode.c @@ -31,14 +31,16 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt for (;;) { - if (bytes < 1) + if (bytes < 1) { return ptr - buf; + } if ((*ptr & 0xC0) == 0xC0) { /* Run */ - if (bytes < 2) + if (bytes < 2) { return ptr - buf; + } n = ptr[0] & 0x3F; diff --git a/src/libImaging/Point.c b/src/libImaging/Point.c index a80b9e7d2..76c0e591d 100644 --- a/src/libImaging/Point.c +++ b/src/libImaging/Point.c @@ -35,8 +35,9 @@ im_point_8_8(Imaging imOut, Imaging imIn, im_point_context* context) for (y = 0; y < imIn->ysize; y++) { UINT8* in = imIn->image8[y]; UINT8* out = imOut->image8[y]; - for (x = 0; x < imIn->xsize; x++) + for (x = 0; x < imIn->xsize; x++) { out[x] = table[in[x]]; + } } } @@ -103,8 +104,9 @@ im_point_8_32(Imaging imOut, Imaging imIn, im_point_context* context) for (y = 0; y < imIn->ysize; y++) { UINT8* in = imIn->image8[y]; INT32* out = imOut->image32[y]; - for (x = 0; x < imIn->xsize; x++) + for (x = 0; x < imIn->xsize; x++) { memcpy(out + x, table + in[x] * sizeof(INT32), sizeof(INT32)); + } } } @@ -119,10 +121,11 @@ im_point_32_8(Imaging imOut, Imaging imIn, im_point_context* context) UINT8* out = imOut->image8[y]; for (x = 0; x < imIn->xsize; x++) { int v = in[x]; - if (v < 0) + if (v < 0) { v = 0; - else if (v > 65535) + } else if (v > 65535) { v = 65535; + } out[x] = table[v]; } } @@ -138,21 +141,26 @@ ImagingPoint(Imaging imIn, const char* mode, const void* table) im_point_context context; void (*point)(Imaging imIn, Imaging imOut, im_point_context* context); - if (!imIn) + if (!imIn) { return (Imaging) ImagingError_ModeError(); + } - if (!mode) + if (!mode) { mode = imIn->mode; + } if (imIn->type != IMAGING_TYPE_UINT8) { - if (imIn->type != IMAGING_TYPE_INT32 || strcmp(mode, "L") != 0) + if (imIn->type != IMAGING_TYPE_INT32 || strcmp(mode, "L") != 0) { goto mode_mismatch; - } else if (!imIn->image8 && strcmp(imIn->mode, mode) != 0) + } + } else if (!imIn->image8 && strcmp(imIn->mode, mode) != 0) { goto mode_mismatch; + } imOut = ImagingNew(mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } /* find appropriate handler */ if (imIn->type == IMAGING_TYPE_UINT8) { @@ -175,10 +183,12 @@ ImagingPoint(Imaging imIn, const char* mode, const void* table) point = im_point_8_8; break; } - } else + } else { point = im_point_8_32; - } else + } + } else { point = im_point_32_8; + } ImagingCopyPalette(imOut, imIn); @@ -213,8 +223,9 @@ ImagingPointTransform(Imaging imIn, double scale, double offset) return (Imaging) ImagingError_ModeError(); imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } switch (imIn->type) { case IMAGING_TYPE_INT32: @@ -223,8 +234,9 @@ ImagingPointTransform(Imaging imIn, double scale, double offset) INT32* in = imIn->image32[y]; INT32* out = imOut->image32[y]; /* FIXME: add clipping? */ - for (x = 0; x < imIn->xsize; x++) + for (x = 0; x < imIn->xsize; x++) { out[x] = in[x] * scale + offset; + } } ImagingSectionLeave(&cookie); break; @@ -233,8 +245,9 @@ ImagingPointTransform(Imaging imIn, double scale, double offset) for (y = 0; y < imIn->ysize; y++) { FLOAT32* in = (FLOAT32*) imIn->image32[y]; FLOAT32* out = (FLOAT32*) imOut->image32[y]; - for (x = 0; x < imIn->xsize; x++) + for (x = 0; x < imIn->xsize; x++) { out[x] = in[x] * scale + offset; + } } ImagingSectionLeave(&cookie); break; diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index b94dc6e1d..fdf1ea3b6 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -157,7 +157,9 @@ create_pixel_hash(Pixel *pixelData,uint32_t nPixels) /* malloc check ok, small constant allocation */ d=malloc(sizeof(PixelHashData)); - if (!d) return NULL; + if (!d) { + return NULL; + } hash=hashtable_new(pixel_hash,pixel_cmp); hashtable_set_user_data(hash,d); d->scale=0; @@ -197,7 +199,9 @@ static void destroy_pixel_hash(HashTable *hash) { PixelHashData *d=(PixelHashData *)hashtable_get_user_data(hash); - if (d) free(d); + if (d) { + free(d); + } hashtable_free(hash); } @@ -214,7 +218,9 @@ static int compute_box_volume(BoxNode *b) { unsigned char rl,rh,gl,gh,bl,bh; - if (b->volume>=0) return b->volume; + if (b->volume>=0) { + return b->volume; + } if (!b->head[0]) { b->volume=0; } else { @@ -242,7 +248,9 @@ hash_to_list(const HashTable *h, const Pixel pixel, const uint32_t count, void * /* malloc check ok, small constant allocation */ p=malloc(sizeof(PixelList)); - if (!p) return; + if (!p) { + return; + } p->flag=0; p->p=q; @@ -250,7 +258,9 @@ hash_to_list(const HashTable *h, const Pixel pixel, const uint32_t count, void * for (i=0;i<3;i++) { p->next[i]=pl[i]; p->prev[i]=NULL; - if (pl[i]) pl[i]->prev[i]=p; + if (pl[i]) { + pl[i]->prev[i]=p; + } pl[i]=p; } } @@ -268,7 +278,9 @@ mergesort_pixels(PixelList *head, int i) } for (c=t=head;c&&t;c=c->next[i],t=(t->next[i])?t->next[i]->next[i]:NULL); if (c) { - if (c->prev[i]) c->prev[i]->next[i]=NULL; + if (c->prev[i]) { + c->prev[i]->next[i]=NULL; + } c->prev[i]=NULL; } a=mergesort_pixels(head,i); @@ -285,9 +297,13 @@ mergesort_pixels(PixelList *head, int i) } c->prev[i]=p; c->next[i]=NULL; - if (p) p->next[i]=c; + if (p) { + p->next[i]=c; + } p=c; - if (!head) head=c; + if (!head) { + head=c; + } } if (a) { c->next[i]=a; @@ -442,17 +458,29 @@ splitlists(PixelList *h[3], for (c=h[i];c;c=n) { n=c->next[i]; if (c->flag) { /* move pixel to right list*/ - if (r) r->next[i]=c; else nh[1][i]=c; + if (r) { + r->next[i]=c; + } else { + nh[1][i]=c; + } c->prev[i]=r; r=c; } else { /* move pixel to left list */ - if (l) l->next[i]=c; else nh[0][i]=c; + if (l) { + l->next[i]=c; + } else { + nh[0][i]=c; + } c->prev[i]=l; l=c; } } - if (l) l->next[i]=NULL; - if (r) r->next[i]=NULL; + if (l) { + l->next[i]=NULL; + } + if (r) { + r->next[i]=NULL; + } nt[0][i]=l; nt[1][i]=r; } @@ -720,7 +748,9 @@ annotate_hash_table(BoxNode *n,HashTable *h,uint32_t *box) return 0; } } - if (n->head[0]) (*box)++; + if (n->head[0]) { + (*box)++; + } return 1; } @@ -756,7 +786,9 @@ resort_distance_tables(uint32_t *avgDist, for (k=j;k&&(*(skRow[k-1])>*(skRow[k]));k--) { skRow[k]=skRow[k-1]; } - if (k!=j) skRow[k]=skElt; + if (k!=j) { + skRow[k]=skElt; + } } } return 1; @@ -987,7 +1019,9 @@ compute_palette_from_median_cut( if (!(i%100)) { printf ("%05d\r",i); fflush(stdout); } if (checkContained(root,pixelData+i)>1) { printf ("pixel in two boxes\n"); - for(i=0;i<3;i++) free (avg[i]); + for(i=0;i<3;i++) { + free (avg[i]); + } free(count); return 0; } @@ -996,7 +1030,9 @@ compute_palette_from_median_cut( #ifndef NO_OUTPUT printf ("pixel lookup failed\n"); #endif - for(i=0;i<3;i++) free (avg[i]); + for(i=0;i<3;i++) { + free (avg[i]); + } free(count); return 0; } @@ -1004,7 +1040,9 @@ compute_palette_from_median_cut( #ifndef NO_OUTPUT printf ("panic - paletteEntry>=nPaletteEntries (%d>=%d)\n",(int)paletteEntry,(int)nPaletteEntries); #endif - for(i=0;i<3;i++) free (avg[i]); + for(i=0;i<3;i++) { + free (avg[i]); + } free(count); return 0; } @@ -1016,7 +1054,9 @@ compute_palette_from_median_cut( /* malloc check ok, using calloc */ p=calloc(nPaletteEntries, sizeof(Pixel)); if (!p) { - for(i=0;i<3;i++) free (avg[i]); + for(i=0;i<3;i++) { + free (avg[i]); + } free(count); return 0; } @@ -1026,7 +1066,9 @@ compute_palette_from_median_cut( p[i].c.b=(int)(.5+(double)avg[2][i]/(double)count[i]); } *palette=p; - for(i=0;i<3;i++) free (avg[i]); + for(i=0;i<3;i++) { + free (avg[i]); + } free(count); return 1; } @@ -1156,24 +1198,46 @@ k_means(Pixel *pixelData, #ifndef NO_OUTPUT printf (".(%d)",changes);fflush(stdout); #endif - if (changes<=threshold) break; + if (changes<=threshold) { + break; + } } #ifndef NO_OUTPUT printf("]\n"); #endif - if (avgDistSortKey) free(avgDistSortKey); - if (avgDist) free(avgDist); - for(i=0;i<3;i++) if (avg[i]) free (avg[i]); - if (count) free(count); + if (avgDistSortKey) { + free(avgDistSortKey); + } + if (avgDist) { + free(avgDist); + } + for(i=0;i<3;i++) { + if (avg[i]) { + free (avg[i]); + } + } + if (count) { + free(count); + } return 1; error_3: - if (avgDistSortKey) free(avgDistSortKey); + if (avgDistSortKey) { + free(avgDistSortKey); + } error_2: - if (avgDist) free(avgDist); + if (avgDist) { + free(avgDist); + } error_1: - for(i=0;i<3;i++) if (avg[i]) free (avg[i]); - if (count) free(count); + for(i=0;i<3;i++) { + if (avg[i]) { + free (avg[i]); + } + } + if (count) { + free(count); + } return 0; } @@ -1345,7 +1409,9 @@ quantize(Pixel *pixelData, #ifndef NO_OUTPUT printf ("k means...\n"); fflush(stdout); timer=clock(); #endif - if (kmeans) k_means(pixelData,nPixels,p,nPaletteEntries,qp,kmeans-1); + if (kmeans) { + k_means(pixelData,nPixels,p,nPaletteEntries,qp,kmeans-1); + } #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); #endif @@ -1357,8 +1423,12 @@ quantize(Pixel *pixelData, #ifndef NO_OUTPUT printf ("cleanup..."); fflush(stdout); timer=clock(); #endif - if (avgDist) free(avgDist); - if (avgDistSortKey) free(avgDistSortKey); + if (avgDist) { + free(avgDist); + } + if (avgDistSortKey) { + free(avgDistSortKey); + } destroy_pixel_hash(h); #ifndef NO_OUTPUT printf ("done (%f)\n",(clock()-timer)/(double)CLOCKS_PER_SEC); @@ -1367,15 +1437,25 @@ quantize(Pixel *pixelData, return 1; error_7: - if (avgDistSortKey) free(avgDistSortKey); + if (avgDistSortKey) { + free(avgDistSortKey); + } error_6: - if (avgDist) free(avgDist); + if (avgDist) { + free(avgDist); + } error_5: - if (qp) free(qp); + if (qp) { + free(qp); + } error_4: - if (p) free(p); + if (p) { + free(p); + } error_3: - if (root) free_box_tree(root); + if (root) { + free_box_tree(root); + } error_1: destroy_pixel_hash(h); error_0: @@ -1430,7 +1510,9 @@ quantize2(Pixel *pixelData, /* malloc check ok, using calloc */ p=calloc(nQuantPixels, sizeof(Pixel)); - if (!p) return 0; + if (!p) { + return 0; + } mean[0]=mean[1]=mean[2]=0; h=hashtable_new(unshifted_pixel_hash,unshifted_pixel_cmp); for (i=0;i 256) + } + if (colors < 1 || colors > 256) { /* FIXME: for colors > 256, consider returning an RGB image instead (see @PIL205) */ return (Imaging) ImagingError_ValueError("bad number of colors"); + } if (strcmp(im->mode, "L") != 0 && strcmp(im->mode, "P") != 0 && - strcmp(im->mode, "RGB") != 0 && strcmp(im->mode, "RGBA") !=0) + strcmp(im->mode, "RGB") != 0 && strcmp(im->mode, "RGBA") !=0) { return ImagingError_ModeError(); + } /* only octree and imagequant supports RGBA */ - if (!strcmp(im->mode, "RGBA") && mode != 2 && mode != 3) + if (!strcmp(im->mode, "RGBA") && mode != 2 && mode != 3) { return ImagingError_ModeError(); + } if (im->xsize > INT_MAX / im->ysize) { return ImagingError_MemoryError(); } /* malloc check ok, using calloc for final overflow, x*y above */ p = calloc(im->xsize * im->ysize, sizeof(Pixel)); - if (!p) + if (!p) { return ImagingError_MemoryError(); + } /* collect statistics */ @@ -1543,18 +1632,19 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans) /* FIXME: converting a "L" image to "P" with 256 colors should be done by a simple copy... */ - for (i = y = 0; y < im->ysize; y++) + for (i = y = 0; y < im->ysize; y++) { for (x = 0; x < im->xsize; x++, i++) { p[i].c.r = p[i].c.g = p[i].c.b = im->image8[y][x]; p[i].c.a = 255; } + } } else if (!strcmp(im->mode, "P")) { /* palette */ pp = im->palette->palette; - for (i = y = 0; y < im->ysize; y++) + for (i = y = 0; y < im->ysize; y++) { for (x = 0; x < im->xsize; x++, i++) { v = im->image8[y][x]; p[i].c.r = pp[v*4+0]; @@ -1562,13 +1652,16 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans) p[i].c.b = pp[v*4+2]; p[i].c.a = pp[v*4+3]; } + } } else if (!strcmp(im->mode, "RGB") || !strcmp(im->mode, "RGBA")) { /* true colour */ - for (i = y = 0; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++, i++) + for (i = y = 0; y < im->ysize; y++) { + for (x = 0; x < im->xsize; x++, i++) { p[i].v = im->image32[y][x]; + } + } } else { free(p); @@ -1647,9 +1740,11 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans) imOut = ImagingNewDirty("P", im->xsize, im->ysize); ImagingSectionEnter(&cookie); - for (i = y = 0; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++) + for (i = y = 0; y < im->ysize; y++) { + for (x = 0; x < im->xsize; x++) { imOut->image8[y][x] = (unsigned char) newData[i++]; + } + } free(newData); diff --git a/src/libImaging/QuantHash.c b/src/libImaging/QuantHash.c index 3fcbf3c02..6ff95d885 100644 --- a/src/libImaging/QuantHash.c +++ b/src/libImaging/QuantHash.c @@ -67,7 +67,9 @@ static uint32_t _findPrime(uint32_t start,int dir) { continue; } for (t=2;t=sqrt((double)start)) { break; @@ -144,7 +146,9 @@ static int _hashtable_insert_node(HashTable *h,HashNode *node,int resize,int upd node->next=*n; *n=node; h->count++; - if (resize) _hashtable_resize(h); + if (resize) { + _hashtable_resize(h); + } return 1; } else { return 0; @@ -169,13 +173,17 @@ static int _hashtable_insert(HashTable *h,HashKey_t key,HashVal_t val,int resize } if (!update) { t=malloc(sizeof(HashNode)); - if (!t) return 0; + if (!t) { + return 0; + } t->next=*n; *n=t; t->key=key; t->value=val; h->count++; - if (resize) _hashtable_resize(h); + if (resize) { + _hashtable_resize(h); + } return 1; } else { return 0; @@ -206,7 +214,9 @@ int hashtable_insert_or_update_computed(HashTable *h, } } t=malloc(sizeof(HashNode)); - if (!t) return 0; + if (!t) { + return 0; + } t->key=key; t->next=*n; *n=t; diff --git a/src/libImaging/QuantHeap.c b/src/libImaging/QuantHeap.c index 498d44b1d..6877e34a3 100644 --- a/src/libImaging/QuantHeap.c +++ b/src/libImaging/QuantHeap.c @@ -46,15 +46,21 @@ void ImagingQuantHeapFree(Heap *h) { static int _heap_grow(Heap *h,unsigned int newsize) { void *newheap; - if (!newsize) newsize=h->heapsize<<1; - if (newsizeheapsize) return 0; + if (!newsize) { + newsize=h->heapsize<<1; + } + if (newsizeheapsize) { + return 0; + } if (newsize > INT_MAX / sizeof(void *)){ return 0; } /* malloc check ok, using calloc for overflow, also checking above due to memcpy below*/ newheap=calloc(newsize, sizeof(void *)); - if (!newheap) return 0; + if (!newheap) { + return 0; + } memcpy(newheap,h->heap,sizeof(void *)*h->heapsize); free(h->heap); h->heap=newheap; @@ -140,7 +146,9 @@ Heap *ImagingQuantHeapNew(HeapCmpFunc cf) { /* malloc check ok, small constant allocation */ h=malloc(sizeof(Heap)); - if (!h) return NULL; + if (!h) { + return NULL; + } h->heapsize=INITIAL_SIZE; /* malloc check ok, using calloc for overflow */ h->heap=calloc(h->heapsize, sizeof(void *)); diff --git a/src/libImaging/QuantOctree.c b/src/libImaging/QuantOctree.c index 83d987544..fa45ae707 100644 --- a/src/libImaging/QuantOctree.c +++ b/src/libImaging/QuantOctree.c @@ -56,7 +56,9 @@ new_color_cube(int r, int g, int b, int a) { /* malloc check ok, small constant allocation */ cube = malloc(sizeof(struct _ColorCube)); - if (!cube) return NULL; + if (!cube) { + return NULL; + } cube->rBits = MAX(r, 0); cube->gBits = MAX(g, 0); @@ -175,7 +177,9 @@ create_sorted_color_palette(const ColorCube cube) { } /* malloc check ok, calloc + overflow check above for memcpy */ buckets = calloc(cube->size, sizeof(struct _ColorBucket)); - if (!buckets) return NULL; + if (!buckets) { + return NULL; + } memcpy(buckets, cube->buckets, sizeof(struct _ColorBucket)*cube->size); qsort(buckets, cube->size, sizeof(struct _ColorBucket), @@ -203,7 +207,9 @@ static ColorCube copy_color_cube(const ColorCube cube, ColorCube result; result = new_color_cube(rBits, gBits, bBits, aBits); - if (!result) return NULL; + if (!result) { + return NULL; + } if (cube->rBits > rBits) { dst_reduce[0] = cube->rBits - result->rBits; @@ -268,7 +274,9 @@ subtract_color_buckets(ColorCube cube, ColorBucket buckets, long nBuckets) { subtrahend = &buckets[i]; // If the subtrahend contains no buckets, there is nothing to subtract. - if (subtrahend->count == 0) continue; + if (subtrahend->count == 0) { + continue; + } avg_color_from_color_bucket(subtrahend, &p); minuend = color_bucket_from_cube(cube, &p); @@ -325,7 +333,9 @@ create_palette_array(const ColorBucket palette, unsigned int paletteLength) { /* malloc check ok, calloc for overflow */ paletteArray = calloc(paletteLength, sizeof(Pixel)); - if (!paletteArray) return NULL; + if (!paletteArray) { + return NULL; + } for (i=0; i nQuantPixels) + if (nCoarseColors > nQuantPixels) { nCoarseColors = nQuantPixels; + } /* how many space do we have in our palette for fine colors? */ nFineColors = nQuantPixels - nCoarseColors; /* create fine color palette */ paletteBucketsFine = create_sorted_color_palette(fineCube); - if (!paletteBucketsFine) goto error; + if (!paletteBucketsFine) { + goto error; + } /* remove the used fine colors from the coarse cube */ subtract_color_buckets(coarseCube, paletteBucketsFine, nFineColors); @@ -430,7 +447,9 @@ int quantize_octree(Pixel *pixelData, /* create our palette buckets with fine and coarse combined */ paletteBucketsCoarse = create_sorted_color_palette(coarseCube); - if (!paletteBucketsCoarse) goto error; + if (!paletteBucketsCoarse) { + goto error; + } paletteBuckets = combined_palette(paletteBucketsCoarse, nCoarseColors, paletteBucketsFine, nFineColors); @@ -438,19 +457,25 @@ int quantize_octree(Pixel *pixelData, paletteBucketsFine = NULL; free(paletteBucketsCoarse); paletteBucketsCoarse = NULL; - if (!paletteBuckets) goto error; + if (!paletteBuckets) { + goto error; + } /* add all coarse colors to our coarse lookup cube. */ coarseLookupCube = new_color_cube(cubeBits[4], cubeBits[5], cubeBits[6], cubeBits[7]); - if (!coarseLookupCube) goto error; + if (!coarseLookupCube) { + goto error; + } add_lookup_buckets(coarseLookupCube, paletteBuckets, nCoarseColors, 0); /* expand coarse cube (64) to larger fine cube (4k). the value of each coarse bucket is then present in the according 64 fine buckets. */ lookupCube = copy_color_cube(coarseLookupCube, cubeBits[0], cubeBits[1], cubeBits[2], cubeBits[3]); - if (!lookupCube) goto error; + if (!lookupCube) { + goto error; + } /* add fine colors to the lookup cube */ add_lookup_buckets(lookupCube, paletteBuckets, nFineColors, nCoarseColors); @@ -458,12 +483,16 @@ int quantize_octree(Pixel *pixelData, /* create result pixels and map palette indices */ /* malloc check ok, calloc for overflow */ qp = calloc(nPixels, sizeof(Pixel)); - if (!qp) goto error; + if (!qp) { + goto error; + } map_image_pixels(pixelData, nPixels, lookupCube, qp); /* convert palette buckets to RGB pixel palette */ *palette = create_palette_array(paletteBuckets, nQuantPixels); - if (!(*palette)) goto error; + if (!(*palette)) { + goto error; + } *quantizedPixels = qp; *paletteLength = nQuantPixels; diff --git a/src/libImaging/QuantPngQuant.c b/src/libImaging/QuantPngQuant.c index a9a547540..ef40b282b 100644 --- a/src/libImaging/QuantPngQuant.c +++ b/src/libImaging/QuantPngQuant.c @@ -95,9 +95,15 @@ quantize_pngquant( result = 1; err: - if (attr) liq_attr_destroy(attr); - if (image) liq_image_destroy(image); - if (remap) liq_result_destroy(remap); + if (attr) { + liq_attr_destroy(attr); + } + if (image) { + liq_image_destroy(image); + } + if (remap) { + liq_result_destroy(remap); + } free(charMatrix); free(charMatrixRows); if (!result) { diff --git a/src/libImaging/RankFilter.c b/src/libImaging/RankFilter.c index 0164861bb..e4f2679b2 100644 --- a/src/libImaging/RankFilter.c +++ b/src/libImaging/RankFilter.c @@ -30,15 +30,23 @@ static type Rank##type(type a[], int n, int k)\ i = l;\ j = m;\ do {\ - while (a[i] < x) i++;\ - while (x < a[j]) j--;\ + while (a[i] < x) {\ + i++;\ + }\ + while (x < a[j]) {\ + j--;\ + }\ if (i <= j) {\ SWAP(type, a[i], a[j]);\ i++; j--;\ }\ } while (i <= j);\ - if (j < k) l = i;\ - if (k < i) m = j;\ + if (j < k) {\ + l = i;\ + }\ + if (k < i) {\ + m = j;\ + }\ }\ return a[k];\ } @@ -54,11 +62,13 @@ ImagingRankFilter(Imaging im, int size, int rank) int x, y; int i, margin, size2; - if (!im || im->bands != 1 || im->type == IMAGING_TYPE_SPECIAL) + if (!im || im->bands != 1 || im->type == IMAGING_TYPE_SPECIAL) { return (Imaging) ImagingError_ModeError(); + } - if (!(size & 1)) + if (!(size & 1)) { return (Imaging) ImagingError_ValueError("bad filter size"); + } /* malloc check ok, for overflow in the define below */ if (size > INT_MAX / size || @@ -69,35 +79,40 @@ ImagingRankFilter(Imaging im, int size, int rank) size2 = size * size; margin = (size-1) / 2; - if (rank < 0 || rank >= size2) + if (rank < 0 || rank >= size2) { return (Imaging) ImagingError_ValueError("bad rank value"); + } imOut = ImagingNew(im->mode, im->xsize - 2*margin, im->ysize - 2*margin); - if (!imOut) + if (!imOut) { return NULL; + } /* malloc check ok, checked above */ #define RANK_BODY(type) do {\ type* buf = malloc(size2 * sizeof(type));\ - if (!buf)\ + if (!buf) {\ goto nomemory;\ - for (y = 0; y < imOut->ysize; y++)\ + }\ + for (y = 0; y < imOut->ysize; y++) {\ for (x = 0; x < imOut->xsize; x++) {\ - for (i = 0; i < size; i++)\ + for (i = 0; i < size; i++) {\ memcpy(buf + i*size, &IMAGING_PIXEL_##type(im, x, y+i),\ size * sizeof(type));\ + }\ IMAGING_PIXEL_##type(imOut, x, y) = Rank##type(buf, size2, rank);\ }\ + }\ free(buf); \ } while (0) - if (im->image8) + if (im->image8) { RANK_BODY(UINT8); - else if (im->type == IMAGING_TYPE_INT32) + } else if (im->type == IMAGING_TYPE_INT32) { RANK_BODY(INT32); - else if (im->type == IMAGING_TYPE_FLOAT32) + } else if (im->type == IMAGING_TYPE_FLOAT32) { RANK_BODY(FLOAT32); - else { + } else { /* safety net (we shouldn't end up here) */ ImagingDelete(imOut); return (Imaging) ImagingError_ModeError(); diff --git a/src/libImaging/RawDecode.c b/src/libImaging/RawDecode.c index 5e95905cc..ca3d37149 100644 --- a/src/libImaging/RawDecode.c +++ b/src/libImaging/RawDecode.c @@ -47,8 +47,9 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt if (state->ystep < 0) { state->y = state->ysize-1; state->ystep = -1; - } else + } else { state->ystep = 1; + } state->state = LINE; @@ -62,8 +63,9 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt /* Skip padding between lines */ - if (bytes < rawstate->skip) + if (bytes < rawstate->skip) { return ptr - buf; + } ptr += rawstate->skip; bytes -= rawstate->skip; @@ -72,8 +74,9 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt } - if (bytes < state->bytes) + if (bytes < state->bytes) { return ptr - buf; + } /* Unpack data */ state->shuffle((UINT8*) im->image[state->y + state->yoff] + diff --git a/src/libImaging/RawEncode.c b/src/libImaging/RawEncode.c index da614a24b..3c593480b 100644 --- a/src/libImaging/RawEncode.c +++ b/src/libImaging/RawEncode.c @@ -49,8 +49,9 @@ ImagingRawEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (state->ystep < 0) { state->y = state->ysize-1; state->ystep = -1; - } else + } else { state->ystep = 1; + } state->state = 1; @@ -68,9 +69,10 @@ ImagingRawEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) state->shuffle(ptr, (UINT8*) im->image[state->y + state->yoff] + state->xoff * im->pixelsize, state->xsize); - if (state->bytes > state->count) + if (state->bytes > state->count) { /* zero-pad the buffer, if necessary */ memset(ptr + state->count, 0, state->bytes - state->count); + } ptr += state->bytes; bytes -= state->bytes; diff --git a/src/libImaging/Reduce.c b/src/libImaging/Reduce.c index d6ef92f5b..f6488deb2 100644 --- a/src/libImaging/Reduce.c +++ b/src/libImaging/Reduce.c @@ -1374,11 +1374,13 @@ ImagingReduce(Imaging imIn, int xscale, int yscale, int box[4]) ImagingSectionCookie cookie; Imaging imOut = NULL; - if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0) + if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0) { return (Imaging) ImagingError_ModeError(); + } - if (imIn->type == IMAGING_TYPE_SPECIAL) + if (imIn->type == IMAGING_TYPE_SPECIAL) { return (Imaging) ImagingError_ModeError(); + } imOut = ImagingNewDirty(imIn->mode, (box[2] + xscale - 1) / xscale, diff --git a/src/libImaging/Resample.c b/src/libImaging/Resample.c index 0dc08611d..ec35303d8 100644 --- a/src/libImaging/Resample.c +++ b/src/libImaging/Resample.c @@ -13,28 +13,34 @@ struct filter { static inline double box_filter(double x) { - if (x > -0.5 && x <= 0.5) + if (x > -0.5 && x <= 0.5) { return 1.0; + } return 0.0; } static inline double bilinear_filter(double x) { - if (x < 0.0) + if (x < 0.0) { x = -x; - if (x < 1.0) + } + if (x < 1.0) { return 1.0-x; + } return 0.0; } static inline double hamming_filter(double x) { - if (x < 0.0) + if (x < 0.0) { x = -x; - if (x == 0.0) + } + if (x == 0.0) { return 1.0; - if (x >= 1.0) + } + if (x >= 1.0) { return 0.0; + } x = x * M_PI; return sin(x) / x * (0.54f + 0.46f * cos(x)); } @@ -43,20 +49,24 @@ static inline double bicubic_filter(double x) { /* https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm */ #define a -0.5 - if (x < 0.0) + if (x < 0.0) { x = -x; - if (x < 1.0) + } + if (x < 1.0) { return ((a + 2.0) * x - (a + 3.0)) * x*x + 1; - if (x < 2.0) + } + if (x < 2.0) { return (((x - 5) * x + 8) * x - 4) * a; + } return 0.0; #undef a } static inline double sinc_filter(double x) { - if (x == 0.0) + if (x == 0.0) { return 1.0; + } x = x * M_PI; return sin(x) / x; } @@ -64,8 +74,9 @@ static inline double sinc_filter(double x) static inline double lanczos_filter(double x) { /* truncated sinc */ - if (-3.0 <= x && x < 3.0) + if (-3.0 <= x && x < 3.0) { return sinc_filter(x) * sinc_filter(x/3); + } return 0.0; } @@ -224,12 +235,14 @@ precompute_coeffs(int inSize, float in0, float in1, int outSize, ss = 1.0 / filterscale; // Round the value xmin = (int) (center - support + 0.5); - if (xmin < 0) + if (xmin < 0) { xmin = 0; + } // Round the value xmax = (int) (center + support + 0.5); - if (xmax > inSize) + if (xmax > inSize) { xmax = inSize; + } xmax -= xmin; k = &kk[xx * ksize]; for (x = 0; x < xmax; x++) { @@ -238,8 +251,9 @@ precompute_coeffs(int inSize, float in0, float in1, int outSize, ww += w; } for (x = 0; x < xmax; x++) { - if (ww != 0.0) + if (ww != 0.0) { k[x] /= ww; + } } // Remaining values should stay empty if they are used despite of xmax. for (; x < ksize; x++) { @@ -295,8 +309,9 @@ ImagingResampleHorizontal_8bpc(Imaging imOut, Imaging imIn, int offset, xmax = bounds[xx * 2 + 1]; k = &kk[xx * ksize]; ss0 = 1 << (PRECISION_BITS -1); - for (x = 0; x < xmax; x++) + for (x = 0; x < xmax; x++) { ss0 += ((UINT8) imIn->image8[yy + offset][x + xmin]) * k[x]; + } imOut->image8[yy][xx] = clip8(ss0); } } @@ -379,8 +394,9 @@ ImagingResampleVertical_8bpc(Imaging imOut, Imaging imIn, int offset, ymax = bounds[yy * 2 + 1]; for (xx = 0; xx < imOut->xsize; xx++) { ss0 = 1 << (PRECISION_BITS -1); - for (y = 0; y < ymax; y++) + for (y = 0; y < ymax; y++) { ss0 += ((UINT8) imIn->image8[y + ymin][xx]) * k[y]; + } imOut->image8[yy][xx] = clip8(ss0); } } @@ -460,8 +476,9 @@ ImagingResampleHorizontal_32bpc(Imaging imOut, Imaging imIn, int offset, xmax = bounds[xx * 2 + 1]; k = &kk[xx * ksize]; ss = 0.0; - for (x = 0; x < xmax; x++) + for (x = 0; x < xmax; x++) { ss += IMAGING_PIXEL_I(imIn, x + xmin, yy + offset) * k[x]; + } IMAGING_PIXEL_I(imOut, xx, yy) = ROUND_UP(ss); } } @@ -474,8 +491,9 @@ ImagingResampleHorizontal_32bpc(Imaging imOut, Imaging imIn, int offset, xmax = bounds[xx * 2 + 1]; k = &kk[xx * ksize]; ss = 0.0; - for (x = 0; x < xmax; x++) + for (x = 0; x < xmax; x++) { ss += IMAGING_PIXEL_F(imIn, x + xmin, yy + offset) * k[x]; + } IMAGING_PIXEL_F(imOut, xx, yy) = ss; } } @@ -503,8 +521,9 @@ ImagingResampleVertical_32bpc(Imaging imOut, Imaging imIn, int offset, k = &kk[yy * ksize]; for (xx = 0; xx < imOut->xsize; xx++) { ss = 0.0; - for (y = 0; y < ymax; y++) + for (y = 0; y < ymax; y++) { ss += IMAGING_PIXEL_I(imIn, xx, y + ymin) * k[y]; + } IMAGING_PIXEL_I(imOut, xx, yy) = ROUND_UP(ss); } } @@ -517,8 +536,9 @@ ImagingResampleVertical_32bpc(Imaging imOut, Imaging imIn, int offset, k = &kk[yy * ksize]; for (xx = 0; xx < imOut->xsize; xx++) { ss = 0.0; - for (y = 0; y < ymax; y++) + for (y = 0; y < ymax; y++) { ss += IMAGING_PIXEL_F(imIn, xx, y + ymin) * k[y]; + } IMAGING_PIXEL_F(imOut, xx, yy) = ss; } } @@ -546,8 +566,9 @@ ImagingResample(Imaging imIn, int xsize, int ysize, int filter, float box[4]) ResampleFunction ResampleHorizontal; ResampleFunction ResampleVertical; - if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0) + if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0) { return (Imaging) ImagingError_ModeError(); + } if (imIn->type == IMAGING_TYPE_SPECIAL) { return (Imaging) ImagingError_ModeError(); diff --git a/src/libImaging/SgiRleDecode.c b/src/libImaging/SgiRleDecode.c index 2d903cc04..a03ecd456 100644 --- a/src/libImaging/SgiRleDecode.c +++ b/src/libImaging/SgiRleDecode.c @@ -33,11 +33,13 @@ static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize) for (;n > 0; n--) { pixel = *src++; - if (n == 1 && pixel != 0) + if (n == 1 && pixel != 0) { return n; + } count = pixel & RLE_MAX_RUN; - if (!count) + if (!count) { return count; + } if (x + count > xsize) { return -1; } @@ -71,11 +73,13 @@ static int expandrow2(UINT8* dest, const UINT8* src, int n, int z, int xsize) { pixel = src[1]; src+=2; - if (n == 1 && pixel != 0) + if (n == 1 && pixel != 0) { return n; + } count = pixel & RLE_MAX_RUN; - if (!count) + if (!count) { return count; + } if (x + count > xsize) { return -1; } @@ -151,11 +155,13 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state, goto sgi_finish_decode; } /* populate offsets table */ - for (c->tabindex = 0, c->bufindex = 0; c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4) + for (c->tabindex = 0, c->bufindex = 0; c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4) { read4B(&c->starttab[c->tabindex], &ptr[c->bufindex]); + } /* populate lengths table */ - for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(UINT32); c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4) + for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(UINT32); c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4) { read4B(&c->lengthtab[c->tabindex], &ptr[c->bufindex]); + } state->count += c->tablen * sizeof(UINT32) * 2; diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index ab476939a..c9a24e6aa 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -244,17 +244,21 @@ ImagingNewPrologue(const char *mode, int xsize, int ysize) void ImagingDelete(Imaging im) { - if (!im) + if (!im) { return; + } - if (im->palette) + if (im->palette) { ImagingPaletteDelete(im->palette); + } - if (im->destroy) + if (im->destroy) { im->destroy(im); + } - if (im->image) + if (im->image) { free(im->image); + } free(im); } @@ -399,8 +403,9 @@ ImagingAllocateArray(Imaging im, int dirty, int block_size) aligned_linesize = (im->linesize + arena->alignment - 1) & -arena->alignment; lines_per_block = (block_size - (arena->alignment - 1)) / aligned_linesize; - if (lines_per_block == 0) + if (lines_per_block == 0) { lines_per_block = 1; + } blocks_count = (im->ysize + lines_per_block - 1) / lines_per_block; // printf("NEW size: %dx%d, ls: %d, lpb: %d, blocks: %d\n", // im->xsize, im->ysize, aligned_linesize, lines_per_block, blocks_count); @@ -457,8 +462,9 @@ ImagingAllocateArray(Imaging im, int dirty, int block_size) static void ImagingDestroyBlock(Imaging im) { - if (im->block) + if (im->block) { free(im->block); + } } Imaging @@ -510,8 +516,9 @@ ImagingNewInternal(const char* mode, int xsize, int ysize, int dirty) } im = ImagingNewPrologue(mode, xsize, ysize); - if ( ! im) + if ( ! im) { return NULL; + } if (ImagingAllocateArray(im, dirty, ImagingDefaultArena.block_size)) { return im; @@ -550,8 +557,9 @@ ImagingNewBlock(const char* mode, int xsize, int ysize) } im = ImagingNewPrologue(mode, xsize, ysize); - if ( ! im) + if ( ! im) { return NULL; + } if (ImagingAllocateBlock(im)) { return im; @@ -576,8 +584,9 @@ ImagingNew2Dirty(const char* mode, Imaging imOut, Imaging imIn) } else { /* create new image */ imOut = ImagingNewDirty(mode, imIn->xsize, imIn->ysize); - if (!imOut) + if (!imOut) { return NULL; + } } return imOut; @@ -587,8 +596,9 @@ void ImagingCopyPalette(Imaging destination, Imaging source) { if (source->palette) { - if (destination->palette) + if (destination->palette) { ImagingPaletteDelete(destination->palette); + } destination->palette = ImagingPaletteDuplicate(source->palette); } } diff --git a/src/libImaging/SunRleDecode.c b/src/libImaging/SunRleDecode.c index e627c2c9a..acb39133a 100644 --- a/src/libImaging/SunRleDecode.c +++ b/src/libImaging/SunRleDecode.c @@ -31,13 +31,15 @@ ImagingSunRleDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t for (;;) { - if (bytes < 1) + if (bytes < 1) { return ptr - buf; + } if (ptr[0] == 0x80) { - if (bytes < 2) + if (bytes < 2) { break; + } n = ptr[1]; @@ -55,8 +57,9 @@ ImagingSunRleDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t } else { /* Run (3 bytes) */ - if (bytes < 3) + if (bytes < 3) { break; + } /* from (https://www.fileformat.info/format/sunraster/egff.htm) diff --git a/src/libImaging/TgaRleDecode.c b/src/libImaging/TgaRleDecode.c index bdbf27a14..b1364e004 100644 --- a/src/libImaging/TgaRleDecode.c +++ b/src/libImaging/TgaRleDecode.c @@ -33,8 +33,9 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state, if (state->ystep < 0) { state->y = state->ysize-1; state->ystep = -1; - } else + } else { state->ystep = 1; + } state->state = 1; @@ -44,15 +45,17 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state, for (;;) { - if (bytes < 1) + if (bytes < 1) { return ptr - buf; + } if (ptr[0] & 0x80) { /* Run (1 + pixelsize bytes) */ - if (bytes < 1 + depth) + if (bytes < 1 + depth) { break; + } n = depth * ((ptr[0] & 0x7f) + 1); @@ -61,12 +64,13 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state, return -1; } - if (depth == 1) + if (depth == 1) { memset(state->buffer + state->x, ptr[1], n); - else { + } else { int i; - for (i = 0; i < n; i += depth) + for (i = 0; i < n; i += depth) { memcpy(state->buffer + state->x + i, ptr+1, depth); + } } ptr += 1 + depth; @@ -77,8 +81,9 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state, /* Literal (1+n+1 bytes block) */ n = depth * (ptr[0] + 1); - if (bytes < 1 + n) + if (bytes < 1 + n) { break; + } if (state->x + n > state->bytes) { state->errcode = IMAGING_CODEC_OVERRUN; diff --git a/src/libImaging/TgaRleEncode.c b/src/libImaging/TgaRleEncode.c index 2fb831e6b..c65dcf5ec 100644 --- a/src/libImaging/TgaRleEncode.c +++ b/src/libImaging/TgaRleEncode.c @@ -22,8 +22,9 @@ ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (state->ystep < 0) { state->ystep = -1; state->y = state->ysize - 1; - } else + } else { state->ystep = 1; + } state->state = 1; } @@ -46,8 +47,9 @@ ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) assert(state->x <= state->xsize); /* Make sure we have space for the descriptor. */ - if (bytes < 1) + if (bytes < 1) { break; + } if (state->x == state->xsize) { state->x = 0; @@ -59,12 +61,13 @@ ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) } } - if (state->x == 0) + if (state->x == 0) { state->shuffle( state->buffer, (UINT8*)im->image[state->y + state->yoff] + state->xoff * im->pixelsize, state->xsize); + } row = state->buffer; @@ -87,28 +90,32 @@ ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) */ maxLookup = state->x + 126; /* A packet must not span multiple rows. */ - if (maxLookup > state->xsize - 1) + if (maxLookup > state->xsize - 1) { maxLookup = state->xsize - 1; + } if (isRaw) { - while (state->x < maxLookup) - if (!comparePixels(row, state->x, bytesPerPixel)) + while (state->x < maxLookup) { + if (!comparePixels(row, state->x, bytesPerPixel)) { ++state->x; - else { + } else { /* Two identical pixels will go to RLE packet. */ --state->x; break; } + } state->count += (state->x - startX) * bytesPerPixel; } else { descriptor |= 0x80; - while (state->x < maxLookup) - if (comparePixels(row, state->x, bytesPerPixel)) + while (state->x < maxLookup) { + if (comparePixels(row, state->x, bytesPerPixel)) { ++state->x; - else + } else { break; + } + } } } @@ -132,12 +139,14 @@ ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) assert(state->x > 0); assert(state->count <= state->x * bytesPerPixel); - if (bytes == 0) + if (bytes == 0) { break; + } flushCount = state->count; - if (flushCount > bytes) + if (flushCount > bytes) { flushCount = bytes; + } memcpy( dst, diff --git a/src/libImaging/Unpack.c b/src/libImaging/Unpack.c index 6235937d1..917da6ab3 100644 --- a/src/libImaging/Unpack.c +++ b/src/libImaging/Unpack.c @@ -350,8 +350,9 @@ unpackLI(UINT8* out, const UINT8* in, int pixels) { /* negative */ int i; - for (i = 0; i < pixels; i++) + for (i = 0; i < pixels; i++) { out[i] = ~in[i]; + } } static void @@ -1115,8 +1116,9 @@ static void NAME(UINT8* out_, const UINT8* in, int pixels)\ {\ int i;\ OUTTYPE* out = (OUTTYPE*) out_;\ - for (i = 0; i < pixels; i++, in += sizeof(INTYPE))\ + for (i = 0; i < pixels; i++, in += sizeof(INTYPE)) {\ out[i] = (OUTTYPE) ((INTYPE) GET);\ + }\ } #define UNPACK(NAME, COPY, INTYPE, OUTTYPE)\ @@ -1521,13 +1523,15 @@ ImagingFindUnpacker(const char* mode, const char* rawmode, int* bits_out) int i; /* find a suitable pixel unpacker */ - for (i = 0; unpackers[i].rawmode; i++) + for (i = 0; unpackers[i].rawmode; i++) { if (strcmp(unpackers[i].mode, mode) == 0 && strcmp(unpackers[i].rawmode, rawmode) == 0) { - if (bits_out) + if (bits_out) { *bits_out = unpackers[i].bits; + } return unpackers[i].unpack; } + } /* FIXME: configure a general unpacker based on the type codes... */ diff --git a/src/libImaging/UnsharpMask.c b/src/libImaging/UnsharpMask.c index a034bebf2..59e595e82 100644 --- a/src/libImaging/UnsharpMask.c +++ b/src/libImaging/UnsharpMask.c @@ -14,10 +14,12 @@ typedef UINT8 pixel[4]; static inline UINT8 clip8(int in) { - if (in >= 255) + if (in >= 255) { return 255; - if (in <= 0) + } + if (in <= 0) { return 0; + } return (UINT8) in; } @@ -39,8 +41,9 @@ ImagingUnsharpMask(Imaging imOut, Imaging imIn, float radius, int percent, /* First, do a gaussian blur on the image, putting results in imOut temporarily. All format checks are in gaussian blur. */ result = ImagingGaussianBlur(imOut, imIn, radius, 3); - if (!result) + if (!result) { return NULL; + } /* Now, go through each pixel, compare "normal" pixel to blurred pixel. If the difference is more than threshold values, apply diff --git a/src/libImaging/XbmDecode.c b/src/libImaging/XbmDecode.c index 088c71df3..607f1058a 100644 --- a/src/libImaging/XbmDecode.c +++ b/src/libImaging/XbmDecode.c @@ -27,8 +27,9 @@ ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt UINT8* ptr; - if (!state->state) + if (!state->state) { state->state = SKIP; + } ptr = buf; @@ -39,21 +40,24 @@ ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt /* Skip forward until next 'x' */ while (bytes > 0) { - if (*ptr == 'x') + if (*ptr == 'x') { break; + } ptr++; bytes--; } - if (bytes == 0) + if (bytes == 0) { return ptr - buf; + } state->state = BYTE; } - if (bytes < 3) + if (bytes < 3) { return ptr - buf; + } state->buffer[state->x] = (HEX(ptr[1])<<4) + HEX(ptr[2]); diff --git a/src/libImaging/ZipDecode.c b/src/libImaging/ZipDecode.c index 43601c38e..b0f8ad326 100644 --- a/src/libImaging/ZipDecode.c +++ b/src/libImaging/ZipDecode.c @@ -53,8 +53,9 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt if (!state->state) { /* Initialization */ - if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE) + if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE) { context->prefix = 1; /* PNG */ + } /* overflow check for malloc */ if (state->bytes > INT_MAX - 1) { @@ -121,12 +122,13 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt if (err < 0) { /* Something went wrong inside the compression library */ - if (err == Z_DATA_ERROR) + if (err == Z_DATA_ERROR) { state->errcode = IMAGING_CODEC_BROKEN; - else if (err == Z_MEM_ERROR) + } else if (err == Z_MEM_ERROR) { state->errcode = IMAGING_CODEC_MEMORY; - else + } else { state->errcode = IMAGING_CODEC_CONFIG; + } free(context->previous); context->previous = NULL; inflateEnd(&context->z_stream); @@ -149,28 +151,33 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt case 1: /* prior */ bpp = (state->bits + 7) / 8; - for (i = bpp+1; i <= row_len; i++) + for (i = bpp+1; i <= row_len; i++) { state->buffer[i] += state->buffer[i-bpp]; + } break; case 2: /* up */ - for (i = 1; i <= row_len; i++) + for (i = 1; i <= row_len; i++) { state->buffer[i] += context->previous[i]; + } break; case 3: /* average */ bpp = (state->bits + 7) / 8; - for (i = 1; i <= bpp; i++) + for (i = 1; i <= bpp; i++) { state->buffer[i] += context->previous[i]/2; - for (; i <= row_len; i++) + } + for (; i <= row_len; i++) { state->buffer[i] += (state->buffer[i-bpp] + context->previous[i])/2; + } break; case 4: /* paeth filtering */ bpp = (state->bits + 7) / 8; - for (i = 1; i <= bpp; i++) + for (i = 1; i <= bpp; i++) { state->buffer[i] += context->previous[i]; + } for (; i <= row_len; i++) { int a, b, c; int pa, pb, pc; @@ -201,8 +208,9 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt break; case ZIP_TIFF_PREDICTOR: bpp = (state->bits + 7) / 8; - for (i = bpp+1; i <= row_len; i++) + for (i = bpp+1; i <= row_len; i++) { state->buffer[i] += state->buffer[i-bpp]; + } break; } diff --git a/src/libImaging/ZipEncode.c b/src/libImaging/ZipEncode.c index fa1c4e728..6b44ed81a 100644 --- a/src/libImaging/ZipEncode.c +++ b/src/libImaging/ZipEncode.c @@ -128,12 +128,13 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (err < 0) { /* Something went wrong inside the compression library */ - if (err == Z_DATA_ERROR) + if (err == Z_DATA_ERROR) { state->errcode = IMAGING_CODEC_BROKEN; - else if (err == Z_MEM_ERROR) + } else if (err == Z_MEM_ERROR) { state->errcode = IMAGING_CODEC_MEMORY; - else + } else { state->errcode = IMAGING_CODEC_CONFIG; + } free(context->paeth); free(context->average); free(context->up); @@ -282,12 +283,13 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) if (err < 0) { /* Something went wrong inside the compression library */ - if (err == Z_DATA_ERROR) + if (err == Z_DATA_ERROR) { state->errcode = IMAGING_CODEC_BROKEN; - else if (err == Z_MEM_ERROR) + } else if (err == Z_MEM_ERROR) { state->errcode = IMAGING_CODEC_MEMORY; - else + } else { state->errcode = IMAGING_CODEC_CONFIG; + } free(context->paeth); free(context->average); free(context->up); @@ -331,8 +333,9 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) break; } - if (context->z_stream.avail_out == 0) + if (context->z_stream.avail_out == 0) { break; /* Buffer full */ + } } diff --git a/src/map.c b/src/map.c index 47cc9d6de..ef29062cc 100644 --- a/src/map.c +++ b/src/map.c @@ -47,12 +47,14 @@ PyImaging_MapperNew(const char* filename, int readonly) { ImagingMapperObject *mapper; - if (PyType_Ready(&ImagingMapperType) < 0) + if (PyType_Ready(&ImagingMapperType) < 0) { return NULL; + } mapper = PyObject_New(ImagingMapperObject, &ImagingMapperType); - if (mapper == NULL) + if (mapper == NULL) { return NULL; + } mapper->base = NULL; mapper->size = mapper->offset = 0; @@ -101,12 +103,15 @@ static void mapping_dealloc(ImagingMapperObject* mapper) { #ifdef _WIN32 - if (mapper->base != 0) + if (mapper->base != 0) { UnmapViewOfFile(mapper->base); - if (mapper->hMap != (HANDLE)-1) + } + if (mapper->hMap != (HANDLE)-1) { CloseHandle(mapper->hMap); - if (mapper->hFile != (HANDLE)-1) + } + if (mapper->hFile != (HANDLE)-1) { CloseHandle(mapper->hFile); + } mapper->base = 0; mapper->hMap = mapper->hFile = (HANDLE)-1; #endif @@ -122,18 +127,22 @@ mapping_read(ImagingMapperObject* mapper, PyObject* args) PyObject* buf; int size = -1; - if (!PyArg_ParseTuple(args, "|i", &size)) + if (!PyArg_ParseTuple(args, "|i", &size)) { return NULL; + } /* check size */ - if (size < 0 || mapper->offset + size > mapper->size) + if (size < 0 || mapper->offset + size > mapper->size) { size = mapper->size - mapper->offset; - if (size < 0) + } + if (size < 0) { size = 0; + } buf = PyBytes_FromStringAndSize(NULL, size); - if (!buf) + if (!buf) { return NULL; + } if (size > 0) { memcpy(PyBytes_AsString(buf), mapper->base + mapper->offset, size); @@ -148,8 +157,9 @@ mapping_seek(ImagingMapperObject* mapper, PyObject* args) { int offset; int whence = 0; - if (!PyArg_ParseTuple(args, "i|i", &offset, &whence)) + if (!PyArg_ParseTuple(args, "i|i", &offset, &whence)) { return NULL; + } switch (whence) { case 0: /* SEEK_SET */ @@ -193,17 +203,19 @@ mapping_readimage(ImagingMapperObject* mapper, PyObject* args) int stride; int orientation; if (!PyArg_ParseTuple(args, "s(ii)ii", &mode, &xsize, &ysize, - &stride, &orientation)) + &stride, &orientation)) { return NULL; + } if (stride <= 0) { /* FIXME: maybe we should call ImagingNewPrologue instead */ - if (!strcmp(mode, "L") || !strcmp(mode, "P")) + if (!strcmp(mode, "L") || !strcmp(mode, "P")) { stride = xsize; - else if (!strcmp(mode, "I;16") || !strcmp(mode, "I;16B")) + } else if (!strcmp(mode, "I;16") || !strcmp(mode, "I;16B")) { stride = xsize * 2; - else + } else { stride = xsize * 4; + } } size = ysize * stride; @@ -214,16 +226,20 @@ mapping_readimage(ImagingMapperObject* mapper, PyObject* args) } im = ImagingNewPrologue(mode, xsize, ysize); - if (!im) + if (!im) { return NULL; + } /* setup file pointers */ - if (orientation > 0) - for (y = 0; y < ysize; y++) + if (orientation > 0) { + for (y = 0; y < ysize; y++) { im->image[y] = mapper->base + mapper->offset + y * stride; - else - for (y = 0; y < ysize; y++) + } + } else { + for (y = 0; y < ysize; y++) { im->image[ysize-y-1] = mapper->base + mapper->offset + y * stride; + } + } im->destroy = ImagingDestroyMap; @@ -279,8 +295,9 @@ PyObject* PyImaging_Mapper(PyObject* self, PyObject* args) { char* filename; - if (!PyArg_ParseTuple(args, "s", &filename)) + if (!PyArg_ParseTuple(args, "s", &filename)) { return NULL; + } return (PyObject*) PyImaging_MapperNew(filename, 1); } @@ -319,8 +336,9 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args) int ystep; if (!PyArg_ParseTuple(args, "O(ii)sn(sii)", &target, &xsize, &ysize, - &codec, &offset, &mode, &stride, &ystep)) + &codec, &offset, &mode, &stride, &ystep)) { return NULL; + } if (!PyImaging_CheckBuffer(target)) { PyErr_SetString(PyExc_TypeError, "expected string or buffer"); @@ -328,12 +346,13 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args) } if (stride <= 0) { - if (!strcmp(mode, "L") || !strcmp(mode, "P")) + if (!strcmp(mode, "L") || !strcmp(mode, "P")) { stride = xsize; - else if (!strncmp(mode, "I;16", 4)) + } else if (!strncmp(mode, "I;16", 4)) { stride = xsize * 2; - else + } else { stride = xsize * 4; + } } if (stride > 0 && ysize > PY_SSIZE_T_MAX / stride) { @@ -349,8 +368,9 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args) } /* check buffer size */ - if (PyImaging_GetBuffer(target, &view) < 0) + if (PyImaging_GetBuffer(target, &view) < 0) { return NULL; + } if (view.len < 0) { PyErr_SetString(PyExc_ValueError, "buffer has negative size"); @@ -371,12 +391,15 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args) } /* setup file pointers */ - if (ystep > 0) - for (y = 0; y < ysize; y++) + if (ystep > 0) { + for (y = 0; y < ysize; y++) { im->image[y] = (char*)view.buf + offset + y * stride; - else - for (y = 0; y < ysize; y++) + } + } else { + for (y = 0; y < ysize; y++) { im->image[ysize-y-1] = (char*)view.buf + offset + y * stride; + } + } im->destroy = mapping_destroy_buffer; diff --git a/src/outline.c b/src/outline.c index 8a7cc32ad..d082febaa 100644 --- a/src/outline.c +++ b/src/outline.c @@ -39,12 +39,14 @@ _outline_new(void) { OutlineObject *self; - if (PyType_Ready(&OutlineType) < 0) + if (PyType_Ready(&OutlineType) < 0) { return NULL; + } self = PyObject_New(OutlineObject, &OutlineType); - if (self == NULL) + if (self == NULL) { return NULL; + } self->outline = ImagingOutlineNew(); @@ -61,8 +63,9 @@ _outline_dealloc(OutlineObject* self) ImagingOutline PyOutline_AsOutline(PyObject* outline) { - if (PyOutline_Check(outline)) + if (PyOutline_Check(outline)) { return ((OutlineObject*) outline)->outline; + } return NULL; } @@ -74,8 +77,9 @@ PyOutline_AsOutline(PyObject* outline) PyObject* PyOutline_Create(PyObject* self, PyObject* args) { - if (!PyArg_ParseTuple(args, ":outline")) + if (!PyArg_ParseTuple(args, ":outline")) { return NULL; + } return (PyObject*) _outline_new(); } @@ -88,8 +92,9 @@ static PyObject* _outline_move(OutlineObject* self, PyObject* args) { float x0, y0; - if (!PyArg_ParseTuple(args, "ff", &x0, &y0)) + if (!PyArg_ParseTuple(args, "ff", &x0, &y0)) { return NULL; + } ImagingOutlineMove(self->outline, x0, y0); @@ -101,8 +106,9 @@ static PyObject* _outline_line(OutlineObject* self, PyObject* args) { float x1, y1; - if (!PyArg_ParseTuple(args, "ff", &x1, &y1)) + if (!PyArg_ParseTuple(args, "ff", &x1, &y1)) { return NULL; + } ImagingOutlineLine(self->outline, x1, y1); @@ -114,8 +120,9 @@ static PyObject* _outline_curve(OutlineObject* self, PyObject* args) { float x1, y1, x2, y2, x3, y3; - if (!PyArg_ParseTuple(args, "ffffff", &x1, &y1, &x2, &y2, &x3, &y3)) + if (!PyArg_ParseTuple(args, "ffffff", &x1, &y1, &x2, &y2, &x3, &y3)) { return NULL; + } ImagingOutlineCurve(self->outline, x1, y1, x2, y2, x3, y3); @@ -126,8 +133,9 @@ _outline_curve(OutlineObject* self, PyObject* args) static PyObject* _outline_close(OutlineObject* self, PyObject* args) { - if (!PyArg_ParseTuple(args, ":close")) + if (!PyArg_ParseTuple(args, ":close")) { return NULL; + } ImagingOutlineClose(self->outline); @@ -139,8 +147,9 @@ static PyObject* _outline_transform(OutlineObject* self, PyObject* args) { double a[6]; - if (!PyArg_ParseTuple(args, "(dddddd)", a+0, a+1, a+2, a+3, a+4, a+5)) + if (!PyArg_ParseTuple(args, "(dddddd)", a+0, a+1, a+2, a+3, a+4, a+5)) { return NULL; + } ImagingOutlineTransform(self->outline, a); diff --git a/src/path.c b/src/path.c index f69755d16..3cbdd5d44 100644 --- a/src/path.c +++ b/src/path.c @@ -61,8 +61,9 @@ alloc_array(Py_ssize_t count) return NULL; } xy = malloc(2 * count * sizeof(double) + 1); - if (!xy) + if (!xy) { PyErr_NoMemory(); + } return xy; } @@ -74,8 +75,9 @@ path_new(Py_ssize_t count, double* xy, int duplicate) if (duplicate) { /* duplicate path */ double* p = alloc_array(count); - if (!p) + if (!p) { return NULL; + } memcpy(p, xy, count * 2 * sizeof(double)); xy = p; } @@ -120,8 +122,9 @@ PyPath_Flatten(PyObject* data, double **pxy) /* This was another path object. */ PyPathObject *path = (PyPathObject*) data; xy = alloc_array(path->count); - if (!xy) + if (!xy) { return -1; + } memcpy(xy, path->xy, 2 * path->count * sizeof(double)); *pxy = xy; return path->count; @@ -134,10 +137,12 @@ PyPath_Flatten(PyObject* data, double **pxy) float *ptr = (float*) buffer.buf; n = buffer.len / (2 * sizeof(float)); xy = alloc_array(n); - if (!xy) + if (!xy) { return -1; - for (i = 0; i < n+n; i++) + } + for (i = 0; i < n+n; i++) { xy[i] = ptr[i]; + } *pxy = xy; PyBuffer_Release(&buffer); return n; @@ -153,26 +158,28 @@ PyPath_Flatten(PyObject* data, double **pxy) j = 0; n = PyObject_Length(data); /* Just in case __len__ breaks (or doesn't exist) */ - if (PyErr_Occurred()) + if (PyErr_Occurred()) { return -1; + } /* Allocate for worst case */ xy = alloc_array(n); - if (!xy) + if (!xy) { return -1; + } /* Copy table to path array */ if (PyList_Check(data)) { for (i = 0; i < n; i++) { double x, y; PyObject *op = PyList_GET_ITEM(data, i); - if (PyFloat_Check(op)) + if (PyFloat_Check(op)) { xy[j++] = PyFloat_AS_DOUBLE(op); - else if (PyLong_Check(op)) + } else if (PyLong_Check(op)) { xy[j++] = (float) PyLong_AS_LONG(op); - else if (PyNumber_Check(op)) + } else if (PyNumber_Check(op)) { xy[j++] = PyFloat_AsDouble(op); - else if (PyArg_ParseTuple(op, "dd", &x, &y)) { + } else if (PyArg_ParseTuple(op, "dd", &x, &y)) { xy[j++] = x; xy[j++] = y; } else { @@ -184,13 +191,13 @@ PyPath_Flatten(PyObject* data, double **pxy) for (i = 0; i < n; i++) { double x, y; PyObject *op = PyTuple_GET_ITEM(data, i); - if (PyFloat_Check(op)) + if (PyFloat_Check(op)) { xy[j++] = PyFloat_AS_DOUBLE(op); - else if (PyLong_Check(op)) + } else if (PyLong_Check(op)) { xy[j++] = (float) PyLong_AS_LONG(op); - else if (PyNumber_Check(op)) + } else if (PyNumber_Check(op)) { xy[j++] = PyFloat_AsDouble(op); - else if (PyArg_ParseTuple(op, "dd", &x, &y)) { + } else if (PyArg_ParseTuple(op, "dd", &x, &y)) { xy[j++] = x; xy[j++] = y; } else { @@ -213,13 +220,13 @@ PyPath_Flatten(PyObject* data, double **pxy) return -1; } } - if (PyFloat_Check(op)) + if (PyFloat_Check(op)) { xy[j++] = PyFloat_AS_DOUBLE(op); - else if (PyLong_Check(op)) + } else if (PyLong_Check(op)) { xy[j++] = (float) PyLong_AS_LONG(op); - else if (PyNumber_Check(op)) + } else if (PyNumber_Check(op)) { xy[j++] = PyFloat_AsDouble(op); - else if (PyArg_ParseTuple(op, "dd", &x, &y)) { + } else if (PyArg_ParseTuple(op, "dd", &x, &y)) { xy[j++] = x; xy[j++] = y; } else { @@ -257,19 +264,22 @@ PyPath_Create(PyObject* self, PyObject* args) /* number of vertices */ xy = alloc_array(count); - if (!xy) + if (!xy) { return NULL; + } } else { /* sequence or other path */ PyErr_Clear(); - if (!PyArg_ParseTuple(args, "O", &data)) + if (!PyArg_ParseTuple(args, "O", &data)) { return NULL; + } count = PyPath_Flatten(data, &xy); - if (count < 0) + if (count < 0) { return NULL; + } } return (PyObject*) path_new(count, xy, 0); @@ -291,8 +301,9 @@ path_compact(PyPathObject* self, PyObject* args) double cityblock = 2.0; - if (!PyArg_ParseTuple(args, "|d:compact", &cityblock)) + if (!PyArg_ParseTuple(args, "|d:compact", &cityblock)) { return NULL; + } xy = self->xy; @@ -323,8 +334,9 @@ path_getbbox(PyPathObject* self, PyObject* args) double *xy; double x0, y0, x1, y1; - if (!PyArg_ParseTuple(args, ":getbbox")) + if (!PyArg_ParseTuple(args, ":getbbox")) { return NULL; + } xy = self->xy; @@ -332,14 +344,18 @@ path_getbbox(PyPathObject* self, PyObject* args) y0 = y1 = xy[1]; for (i = 1; i < self->count; i++) { - if (xy[i+i] < x0) + if (xy[i+i] < x0) { x0 = xy[i+i]; - if (xy[i+i] > x1) + } + if (xy[i+i] > x1) { x1 = xy[i+i]; - if (xy[i+i+1] < y0) + } + if (xy[i+i+1] < y0) { y0 = xy[i+i+1]; - if (xy[i+i+1] > y1) + } + if (xy[i+i+1] > y1) { y1 = xy[i+i+1]; + } } return Py_BuildValue("dddd", x0, y0, x1, y1); @@ -348,8 +364,9 @@ path_getbbox(PyPathObject* self, PyObject* args) static PyObject* path_getitem(PyPathObject* self, Py_ssize_t i) { - if (i < 0) + if (i < 0) { i = self->count + i; + } if (i < 0 || i >= self->count) { PyErr_SetString(PyExc_IndexError, "path index out of range"); return NULL; @@ -362,16 +379,19 @@ static PyObject* path_getslice(PyPathObject* self, Py_ssize_t ilow, Py_ssize_t ihigh) { /* adjust arguments */ - if (ilow < 0) + if (ilow < 0) { ilow = 0; - else if (ilow >= self->count) + } else if (ilow >= self->count) { ilow = self->count; - if (ihigh < 0) + } + if (ihigh < 0) { ihigh = 0; - if (ihigh < ilow) + } + if (ihigh < ilow) { ihigh = ilow; - else if (ihigh > self->count) + } else if (ihigh > self->count) { ihigh = self->count; + } return (PyObject*) path_new(ihigh - ilow, self->xy + ilow * 2, 1); } @@ -390,8 +410,9 @@ path_map(PyPathObject* self, PyObject* args) double *xy; PyObject* function; - if (!PyArg_ParseTuple(args, "O:map", &function)) + if (!PyArg_ParseTuple(args, "O:map", &function)) { return NULL; + } xy = self->xy; @@ -432,8 +453,9 @@ path_setitem(PyPathObject* self, Py_ssize_t i, PyObject* op) xy = &self->xy[i+i]; - if (!PyArg_ParseTuple(op, "dd", &xy[0], &xy[1])) + if (!PyArg_ParseTuple(op, "dd", &xy[0], &xy[1])) { return -1; + } return 0; } @@ -445,16 +467,18 @@ path_tolist(PyPathObject* self, PyObject* args) Py_ssize_t i; int flat = 0; - if (!PyArg_ParseTuple(args, "|i:tolist", &flat)) + if (!PyArg_ParseTuple(args, "|i:tolist", &flat)) { return NULL; + } if (flat) { list = PyList_New(self->count*2); for (i = 0; i < self->count*2; i++) { PyObject* item; item = PyFloat_FromDouble(self->xy[i]); - if (!item) + if (!item) { goto error; + } PyList_SetItem(list, i, item); } } else { @@ -462,8 +486,9 @@ path_tolist(PyPathObject* self, PyObject* args) for (i = 0; i < self->count; i++) { PyObject* item; item = Py_BuildValue("dd", self->xy[i+i], self->xy[i+i+1]); - if (!item) + if (!item) { goto error; + } PyList_SetItem(list, i, item); } } @@ -487,19 +512,20 @@ path_transform(PyPathObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "(dddddd)|d:transform", &a, &b, &c, &d, &e, &f, - &wrap)) + &wrap)) { return NULL; + } xy = self->xy; /* transform the coordinate set */ - if (b == 0.0 && d == 0.0) + if (b == 0.0 && d == 0.0) { /* scaling */ for (i = 0; i < self->count; i++) { xy[i+i] = a*xy[i+i]+c; xy[i+i+1] = e*xy[i+i+1]+f; } - else + } else { /* affine transform */ for (i = 0; i < self->count; i++) { double x = xy[i+i]; @@ -507,11 +533,14 @@ path_transform(PyPathObject* self, PyObject* args) xy[i+i] = a*x+b*y+c; xy[i+i+1] = d*x+e*y+f; } + } /* special treatment of geographical map data */ - if (wrap != 0.0) - for (i = 0; i < self->count; i++) + if (wrap != 0.0) { + for (i = 0; i < self->count; i++) { xy[i+i] = fmod(xy[i+i], wrap); + } + } Py_INCREF(Py_None); return Py_None; @@ -542,16 +571,18 @@ path_subscript(PyPathObject* self, PyObject* item) { if (PyIndex_Check(item)) { Py_ssize_t i; i = PyNumber_AsSsize_t(item, PyExc_IndexError); - if (i == -1 && PyErr_Occurred()) + if (i == -1 && PyErr_Occurred()) { return NULL; + } return path_getitem(self, i); } if (PySlice_Check(item)) { int len = 4; Py_ssize_t start, stop, step, slicelength; - if (PySlice_GetIndicesEx(item, len, &start, &stop, &step, &slicelength) < 0) + if (PySlice_GetIndicesEx(item, len, &start, &stop, &step, &slicelength) < 0) { return NULL; + } if (slicelength <= 0) { double *xy = alloc_array(0); From 9f2773b3f747e7d61faf66435e462fbe02cd5b76 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Mon, 11 May 2020 07:19:52 +1000 Subject: [PATCH 12/28] Added braces Co-authored-by: Hugo van Kemenade --- src/libImaging/JpegEncode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libImaging/JpegEncode.c b/src/libImaging/JpegEncode.c index 5f3b29c66..8882b61be 100644 --- a/src/libImaging/JpegEncode.c +++ b/src/libImaging/JpegEncode.c @@ -277,8 +277,9 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) } else { break; } - } else + } else { state->state++; + } case 4: if (1024 > context->destination.pub.free_in_buffer){ From 3a75e843f4af6220e70cb81b49dfdcc702a80797 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 11 May 2020 07:46:12 +1000 Subject: [PATCH 13/28] Added braces --- src/_imaging.c | 18 ++++++++++++------ src/_imagingft.c | 9 +++++---- src/encode.c | 7 +++++-- src/libImaging/Convert.c | 13 ++++++------- src/libImaging/Crop.c | 6 ++++-- src/libImaging/Draw.c | 3 ++- src/libImaging/GetBBox.c | 11 +++++++---- src/libImaging/GifDecode.c | 34 ++++++++++++++++++---------------- src/libImaging/Histo.c | 3 ++- src/libImaging/Point.c | 3 ++- src/libImaging/Quant.c | 12 +++++++++--- src/libImaging/RawEncode.c | 3 ++- src/libImaging/XbmEncode.c | 3 ++- src/libImaging/ZipEncode.c | 3 ++- 14 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index f0ba22040..40bfbf2fe 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -490,8 +490,9 @@ getpixel(Imaging im, ImagingAccess access, int x, int y) case IMAGING_TYPE_FLOAT32: return PyFloat_FromDouble(pixel.f); case IMAGING_TYPE_SPECIAL: - if (strncmp(im->mode, "I;16", 4) == 0) + if (strncmp(im->mode, "I;16", 4) == 0) { return PyLong_FromLong(pixel.h); + } break; } @@ -1456,8 +1457,9 @@ _point(ImagingObject* self, PyObject* args) lut[i*4] = CLIP8(data[i]); lut[i*4+1] = CLIP8(data[i+256]); lut[i*4+2] = CLIP8(data[i+512]); - if (n > 768) + if (n > 768) { lut[i*4+3] = CLIP8(data[i+768]); + } } im = ImagingPoint(self->image, mode, (void*) lut); } else { @@ -1523,16 +1525,18 @@ _putdata(ImagingObject* self, PyObject* args) /* Plain string data */ for (i = y = 0; i < n; i += image->xsize, y++) { x = n - i; - if (x > (int) image->xsize) + if (x > (int) image->xsize) { x = image->xsize; + } memcpy(image->image8[y], p+i, x); } } else { /* Scaled and clipped string data */ for (i = x = y = 0; i < n; i++) { image->image8[y][x] = CLIP8((int) (p[i] * scale + offset)); - if (++x >= (int) image->xsize) + if (++x >= (int) image->xsize) { x = 0, y++; + } } } } else { @@ -1932,12 +1936,14 @@ im_setmode(ImagingObject* self, PyObject* args) /* color to color */ strcpy(im->mode, mode); im->bands = modelen; - if (!strcmp(mode, "RGBA")) + if (!strcmp(mode, "RGBA")) { (void) ImagingFillBand(im, 3, 255); + } } else { /* trying doing an in-place conversion */ - if (!ImagingConvertInPlace(im, mode)) + if (!ImagingConvertInPlace(im, mode)) { return NULL; + } } if (self->access) { diff --git a/src/_imagingft.c b/src/_imagingft.c index 795ab4d20..a9b00431a 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -406,11 +406,11 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject * direction = RAQM_DIRECTION_DEFAULT; if (dir) { - if (strcmp(dir, "rtl") == 0) + if (strcmp(dir, "rtl") == 0) { direction = RAQM_DIRECTION_RTL; - else if (strcmp(dir, "ltr") == 0) + } else if (strcmp(dir, "ltr") == 0) { direction = RAQM_DIRECTION_LTR; - else if (strcmp(dir, "ttb") == 0) { + } else if (strcmp(dir, "ttb") == 0) { direction = RAQM_DIRECTION_TTB; if (p_raqm.version_atleast == NULL || !(*p_raqm.version_atleast)(0, 7, 0)) { PyErr_SetString(PyExc_ValueError, "libraqm 0.7 or greater required for 'ttb' direction"); @@ -694,8 +694,9 @@ font_getsize(FontObject* self, PyObject* args) offset = -glyph_info[i].y_advance - face->glyph->metrics.height - face->glyph->metrics.vertBearingY; - if (offset < 0) + if (offset < 0) { y_max -= offset; + } } if (bbox.xMax > x_max) { diff --git a/src/encode.c b/src/encode.c index b285292f3..1d463e9c4 100644 --- a/src/encode.c +++ b/src/encode.c @@ -1085,8 +1085,9 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) if (extra && extra_size > 0) { /* malloc check ok, length is from python parsearg */ char* p = malloc(extra_size); // Freed in JpegEncode, Case 5 - if (!p) + if (!p) { return PyErr_NoMemory(); + } memcpy(p, extra, extra_size); extra = p; } else { @@ -1097,7 +1098,9 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args) /* malloc check ok, length is from python parsearg */ char* pp = malloc(rawExifLen); // Freed in JpegEncode, Case 5 if (!pp) { - if (extra) free(extra); + if (extra) { + free(extra); + } return PyErr_NoMemory(); } memcpy(pp, rawExif, rawExifLen); diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 5e69b067d..b0b794d72 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1660,17 +1660,16 @@ convert(Imaging imOut, Imaging imIn, const char *mode, } } - if (!convert) + if (!convert) { #ifdef notdef return (Imaging) ImagingError_ValueError("conversion not supported"); #else - { - static char buf[256]; - /* FIXME: may overflow if mode is too large */ - sprintf(buf, "conversion from %s to %s not supported", imIn->mode, mode); - return (Imaging) ImagingError_ValueError(buf); - } + static char buf[256]; + /* FIXME: may overflow if mode is too large */ + sprintf(buf, "conversion from %s to %s not supported", imIn->mode, mode); + return (Imaging) ImagingError_ValueError(buf); #endif + } imOut = ImagingNew2Dirty(mode, imOut, imIn); if (!imOut) { diff --git a/src/libImaging/Crop.c b/src/libImaging/Crop.c index 29b4cf9d9..d136edbfc 100644 --- a/src/libImaging/Crop.c +++ b/src/libImaging/Crop.c @@ -32,11 +32,13 @@ ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1) } xsize = sx1 - sx0; - if (xsize < 0) + if (xsize < 0) { xsize = 0; + } ysize = sy1 - sy0; - if (ysize < 0) + if (ysize < 0) { ysize = 0; + } imOut = ImagingNewDirty(imIn->mode, xsize, ysize); if (!imOut) { diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index 35e6e4893..10aaae1f2 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -941,8 +941,9 @@ ellipse(Imaging im, int x0, int y0, int x1, int y1, } lx = x, ly = y; } - if (n == 0) + if (n == 0) { return 0; + } if (inner) { // Inner circle diff --git a/src/libImaging/GetBBox.c b/src/libImaging/GetBBox.c index dd875c557..9a8ae1f32 100644 --- a/src/libImaging/GetBBox.c +++ b/src/libImaging/GetBBox.c @@ -39,15 +39,18 @@ ImagingGetBBox(Imaging im, int bbox[4]) for (x = 0; x < im->xsize; x++) {\ if (im->image[y][x] & mask) {\ has_data = 1;\ - if (x < bbox[0])\ + if (x < bbox[0]) {\ bbox[0] = x;\ - if (x >= bbox[2])\ + }\ + if (x >= bbox[2]) {\ bbox[2] = x+1;\ + }\ }\ }\ if (has_data) {\ - if (bbox[1] < 0)\ - bbox[1] = y;\ + if (bbox[1] < 0) {\ + bbox[1] = y;\ + }\ bbox[3] = y+1;\ }\ } diff --git a/src/libImaging/GifDecode.c b/src/libImaging/GifDecode.c index 5728ae1ce..62170b15f 100644 --- a/src/libImaging/GifDecode.c +++ b/src/libImaging/GifDecode.c @@ -52,8 +52,9 @@ default:\ return -1;\ }\ - if (state->y < state->ysize)\ + if (state->y < state->ysize) {\ out = im->image8[state->y + state->yoff] + state->xoff;\ + }\ } @@ -70,24 +71,25 @@ ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t if (!state->state) { - /* Initialise state */ - if (context->bits < 0 || context->bits > 12) { - state->errcode = IMAGING_CODEC_CONFIG; - return -1; - } + /* Initialise state */ + if (context->bits < 0 || context->bits > 12) { + state->errcode = IMAGING_CODEC_CONFIG; + return -1; + } - /* Clear code */ - context->clear = 1 << context->bits; + /* Clear code */ + context->clear = 1 << context->bits; - /* End code */ - context->end = context->clear + 1; + /* End code */ + context->end = context->clear + 1; - /* Interlace */ - if (context->interlace) { - context->interlace = 1; - context->step = context->repeat = 8; - } else - context->step = 1; + /* Interlace */ + if (context->interlace) { + context->interlace = 1; + context->step = context->repeat = 8; + } else { + context->step = 1; + } state->state = 1; } diff --git a/src/libImaging/Histo.c b/src/libImaging/Histo.c index b6f552ab3..050c2840f 100644 --- a/src/libImaging/Histo.c +++ b/src/libImaging/Histo.c @@ -179,8 +179,9 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax) FLOAT32* in = (FLOAT32*) im->image32[y]; for (x = 0; x < im->xsize; x++) { i = (int) (((*in++)-fmin)*scale); - if (i >= 0 && i < 256) + if (i >= 0 && i < 256) { h->histogram[i]++; + } } } ImagingSectionLeave(&cookie); diff --git a/src/libImaging/Point.c b/src/libImaging/Point.c index 76c0e591d..b70840b07 100644 --- a/src/libImaging/Point.c +++ b/src/libImaging/Point.c @@ -219,8 +219,9 @@ ImagingPointTransform(Imaging imIn, double scale, double offset) if (!imIn || (strcmp(imIn->mode, "I") != 0 && strcmp(imIn->mode, "I;16") != 0 && - strcmp(imIn->mode, "F") != 0)) + strcmp(imIn->mode, "F") != 0)) { return (Imaging) ImagingError_ModeError(); + } imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize); if (!imOut) { diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index fdf1ea3b6..6c9f8d9b7 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -689,8 +689,12 @@ static void free_box_tree(BoxNode *n) { PixelList *p,*pp; - if (n->l) free_box_tree(n->l); - if (n->r) free_box_tree(n->r); + if (n->l) { + free_box_tree(n->l); + } + if (n->r) { + free_box_tree(n->r); + } for (p=n->head[0];p;p=pp) { pp=p->next[0]; free(p); @@ -1008,7 +1012,9 @@ compute_palette_from_median_cut( /* malloc check ok, using calloc */ if (!(avg[i]=calloc(nPaletteEntries, sizeof(uint32_t)))) { for(i=0;i<3;i++) { - if (avg[i]) free (avg[i]); + if (avg[i]) { + free (avg[i]); + } } free(count); return 0; diff --git a/src/libImaging/RawEncode.c b/src/libImaging/RawEncode.c index 3c593480b..fb4ab3346 100644 --- a/src/libImaging/RawEncode.c +++ b/src/libImaging/RawEncode.c @@ -41,8 +41,9 @@ ImagingRawEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) } state->count = state->bytes; state->bytes = bytes; - } else + } else { state->count = state->bytes; + } /* The "ystep" field specifies the orientation */ diff --git a/src/libImaging/XbmEncode.c b/src/libImaging/XbmEncode.c index cf72da619..d1bc086db 100644 --- a/src/libImaging/XbmEncode.c +++ b/src/libImaging/XbmEncode.c @@ -90,8 +90,9 @@ ImagingXbmEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) bytes--; state->count = 0; } - } else + } else { *ptr++ = '\n'; + } bytes -= 5; diff --git a/src/libImaging/ZipEncode.c b/src/libImaging/ZipEncode.c index 6b44ed81a..0b4435678 100644 --- a/src/libImaging/ZipEncode.c +++ b/src/libImaging/ZipEncode.c @@ -307,8 +307,9 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) } - if (context->z_stream.avail_out == 0) + if (context->z_stream.avail_out == 0) { break; /* Buffer full */ + } case 2: From 43072e3ca332c7fb204fc72e9dfa566f3b2cbd9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20N=C3=BC=C3=9Flein?= Date: Mon, 11 May 2020 10:26:49 +0200 Subject: [PATCH 14/28] Update docs/installation.rst Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- docs/installation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/installation.rst b/docs/installation.rst index 1da7fe772..122d19bc6 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -328,7 +328,7 @@ In Fedora, the command is:: .. Note:: ``redhat-rpm-config`` is required on Fedora 23, but not earlier versions. -Prerequisites for **Ubuntu 16.04 LTS or later** are installed with:: +Prerequisites for **Ubuntu 16.04 LTS - 20.04 LTS** are installed with:: sudo apt-get install libtiff5-dev libjpeg8-dev libopenjp2-7-dev zlib1g-dev \ libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk \ From 15ae39674e63c67a0421f217f340c1037e85d2bf Mon Sep 17 00:00:00 2001 From: Simon Andrieux Date: Mon, 11 May 2020 17:38:42 +0200 Subject: [PATCH 15/28] fix reading from empty buffer when loading .gbr --- Tests/test_file_gbr.py | 7 +++++++ src/PIL/GbrImagePlugin.py | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_gbr.py b/Tests/test_file_gbr.py index f183390bc..25e624886 100644 --- a/Tests/test_file_gbr.py +++ b/Tests/test_file_gbr.py @@ -15,3 +15,10 @@ def test_gbr_file(): with Image.open("Tests/images/gbr.gbr") as im: with Image.open("Tests/images/gbr.png") as target: assert_image_equal(target, im) + + +def test_multiples_operation(): + with Image.open("Tests/images/gbr.gbr") as im: + rect = (0, 0, 10, 10) + im.crop(rect) + im.crop(rect) diff --git a/src/PIL/GbrImagePlugin.py b/src/PIL/GbrImagePlugin.py index 292de435c..23a7a5c75 100644 --- a/src/PIL/GbrImagePlugin.py +++ b/src/PIL/GbrImagePlugin.py @@ -84,8 +84,9 @@ class GbrImageFile(ImageFile.ImageFile): self._data_size = width * height * color_depth def load(self): - self.im = Image.core.new(self.mode, self.size) - self.frombytes(self.fp.read(self._data_size)) + if not self.im: + self.im = Image.core.new(self.mode, self.size) + self.frombytes(self.fp.read(self._data_size)) # From f0871b70e7b06aecd4ed751d570faf2ae8bbd0bb Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 12 May 2020 00:11:42 -0700 Subject: [PATCH 16/28] Update src/PIL/ImageChops.py Apply wording suggestions about ImageChops.multiply Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/ImageChops.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageChops.py b/src/PIL/ImageChops.py index 904df484e..c1a2574e4 100644 --- a/src/PIL/ImageChops.py +++ b/src/PIL/ImageChops.py @@ -240,8 +240,10 @@ def subtract_modulo(image1, image2): def logical_and(image1, image2): """Logical AND between two images. - Both of the images must have mode "1". For an AND in RGB mode, use a - multiply() by a black-and-white mask. + Both of the images must have mode "1". If you would like to perform a + logical AND on an image with a mode other than "1", try + :py:meth:`~PIL.ImageChops.multiply` instead, using a black-and-white mask + as the second image. .. code-block:: python From 837bc0ad9c9dcb972ed72046c0bdabd456911fa9 Mon Sep 17 00:00:00 2001 From: Hugo Date: Wed, 13 May 2020 10:21:10 +0300 Subject: [PATCH 17/28] Fix broken string --- Tests/test_image_reduce.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_reduce.py b/Tests/test_image_reduce.py index 729645a0b..353d0def0 100644 --- a/Tests/test_image_reduce.py +++ b/Tests/test_image_reduce.py @@ -174,8 +174,10 @@ def assert_compare_images(a, b, max_average_diff, max_diff=255): average_diff = sum(i * num for i, num in enumerate(ch_hist)) / ( a.size[0] * a.size[1] ) - msg = "average pixel value difference {:.4f} > expected {:.4f} " - "for '{}' band".format(average_diff, max_average_diff, band) + msg = ( + "average pixel value difference {:.4f} > expected {:.4f} " + "for '{}' band".format(average_diff, max_average_diff, band) + ) assert max_average_diff >= average_diff, msg last_diff = [i for i, num in enumerate(ch_hist) if num > 0][-1] From fdc5993ace28ce57204f82380be6a3f433e14c76 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 16 May 2020 21:37:33 +1000 Subject: [PATCH 18/28] Improved grammar [ci skip] --- src/PIL/JpegImagePlugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 0e1a9cc0f..89e70f0e9 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -593,9 +593,9 @@ def convert_dict_qtables(qtables): def get_sampling(im): - # There's no subsampling when image have only 1 layer + # There's no subsampling when images have only 1 layer # (grayscale images) or when they are CMYK (4 layers), - # so set subsampling to default value. + # so set subsampling to the default value. # # NOTE: currently Pillow can't encode JPEG to YCCK format. # If YCCK support is added in the future, subsampling code will have From 67d26ed051a4a12c2203731323f9dd62db862108 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 May 2020 09:24:06 +1000 Subject: [PATCH 19/28] Use _accept function in example plugin [ci skip] --- docs/handbook/writing-your-own-file-decoder.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/handbook/writing-your-own-file-decoder.rst b/docs/handbook/writing-your-own-file-decoder.rst index 24e46ff00..4be34eefc 100644 --- a/docs/handbook/writing-your-own-file-decoder.rst +++ b/docs/handbook/writing-your-own-file-decoder.rst @@ -53,6 +53,11 @@ true color. from PIL import Image, ImageFile + + def _accept(prefix): + return prefix[:4] == b"SPAM" + + class SpamImageFile(ImageFile.ImageFile): format = "SPAM" @@ -60,12 +65,7 @@ true color. def _open(self): - # check header - header = self.fp.read(128) - if header[:4] != b"SPAM": - raise SyntaxError("not a SPAM file") - - header = header.split() + header = self.fp.read(128).split() # size in pixels (width, height) self._size = int(header[1]), int(header[2]) @@ -86,7 +86,7 @@ true color. ("raw", (0, 0) + self.size, 128, (self.mode, 0, 1)) ] - Image.register_open(SpamImageFile.format, SpamImageFile) + Image.register_open(SpamImageFile.format, SpamImageFile, _accept) Image.register_extension(SpamImageFile.format, ".spam") Image.register_extension(SpamImageFile.format, ".spa") # dos version From 45c4ba7a5f730821e8117944e959a7bd7b04c8cc Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 May 2020 11:04:02 +1000 Subject: [PATCH 20/28] Simplified test case --- Tests/test_file_gbr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Tests/test_file_gbr.py b/Tests/test_file_gbr.py index 25e624886..b95970889 100644 --- a/Tests/test_file_gbr.py +++ b/Tests/test_file_gbr.py @@ -17,8 +17,9 @@ def test_gbr_file(): assert_image_equal(target, im) -def test_multiples_operation(): +def test_multiple_load_operations(): with Image.open("Tests/images/gbr.gbr") as im: - rect = (0, 0, 10, 10) - im.crop(rect) - im.crop(rect) + im.load() + im.load() + with Image.open("Tests/images/gbr.png") as target: + assert_image_equal(target, im) From 75791835b3f38ee9db9dbc3a962bed3a78e7ffb1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 May 2020 11:08:05 +1000 Subject: [PATCH 21/28] Updated code to match other plugins --- src/PIL/GbrImagePlugin.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/PIL/GbrImagePlugin.py b/src/PIL/GbrImagePlugin.py index 23a7a5c75..f9d9cc772 100644 --- a/src/PIL/GbrImagePlugin.py +++ b/src/PIL/GbrImagePlugin.py @@ -84,9 +84,12 @@ class GbrImageFile(ImageFile.ImageFile): self._data_size = width * height * color_depth def load(self): - if not self.im: - self.im = Image.core.new(self.mode, self.size) - self.frombytes(self.fp.read(self._data_size)) + if self.im: + # Already loaded + return + + self.im = Image.core.new(self.mode, self.size) + self.frombytes(self.fp.read(self._data_size)) # From a995cc094f887d0c8045203427f52404a5521dd3 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 May 2020 11:35:24 +1000 Subject: [PATCH 22/28] Updated CHANGES.rst [ci skip] --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index f12a321c9..833d04357 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,9 @@ Changelog (Pillow) 7.2.0 (unreleased) ------------------ +- Fix repeatedly loading .gbr #4620 + [ElinksFr, radarhere] + - JPEG: Truncate icclist instead of setting to None #4613 [homm] From 3b621512156c46357c4f9b85fbcfbda0773bfc2b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 May 2020 16:05:54 +1000 Subject: [PATCH 23/28] Changed example function name to match use in code [ci skip] --- docs/handbook/writing-your-own-file-decoder.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/handbook/writing-your-own-file-decoder.rst b/docs/handbook/writing-your-own-file-decoder.rst index 4be34eefc..42a6c4822 100644 --- a/docs/handbook/writing-your-own-file-decoder.rst +++ b/docs/handbook/writing-your-own-file-decoder.rst @@ -17,8 +17,8 @@ itself. Such plug-ins usually have names like Pillow decodes files in 2 stages: 1. It loops over the available image plugins in the loaded order, and - calls the plugin's ``accept`` function with the first 16 bytes of - the file. If the ``accept`` function returns true, the plugin's + calls the plugin's ``_accept`` function with the first 16 bytes of + the file. If the ``_accept`` function returns true, the plugin's ``_open`` method is called to set up the image metadata and image tiles. The ``_open`` method is not for decoding the actual image data. From 6d5c2d0cdf161869180f4b32dee817df49cf512d Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 17 May 2020 14:35:58 +0300 Subject: [PATCH 24/28] Add testpaths to tell pytest where to search for tests --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 17e85bd21..30843b847 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,3 +10,4 @@ multi_line_output = 3 [tool:pytest] addopts = -rs +testpaths = Tests From 7daca6733d99c68fcbe0ca81a5fca559b3e112da Mon Sep 17 00:00:00 2001 From: Christoph Gohlke Date: Tue, 19 May 2020 15:35:32 -0700 Subject: [PATCH 25/28] Fix ImportError on Python 3.9.0b1 for Windows --- setup.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1236b5df5..97ff3f2b6 100755 --- a/setup.py +++ b/setup.py @@ -730,7 +730,11 @@ class pil_build_ext(build_ext): if struct.unpack("h", b"\0\1")[0] == 1: defs.append(("WORDS_BIGENDIAN", None)) - if sys.platform == "win32" and not (PLATFORM_PYPY or PLATFORM_MINGW): + if ( + sys.platform == "win32" and + sys.version_info < (3, 9) and + not (PLATFORM_PYPY or PLATFORM_MINGW) + ): defs.append(("PILLOW_VERSION", '"\\"%s\\""' % PILLOW_VERSION)) else: defs.append(("PILLOW_VERSION", '"%s"' % PILLOW_VERSION)) From ac7848c78157b0e70a4944d56a3b2c5ad6fd8f19 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 20 May 2020 20:31:14 +1000 Subject: [PATCH 26/28] Lint fix --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 97ff3f2b6..8c9f0213f 100755 --- a/setup.py +++ b/setup.py @@ -731,9 +731,9 @@ class pil_build_ext(build_ext): defs.append(("WORDS_BIGENDIAN", None)) if ( - sys.platform == "win32" and - sys.version_info < (3, 9) and - not (PLATFORM_PYPY or PLATFORM_MINGW) + sys.platform == "win32" + and sys.version_info < (3, 9) + and not (PLATFORM_PYPY or PLATFORM_MINGW) ): defs.append(("PILLOW_VERSION", '"\\"%s\\""' % PILLOW_VERSION)) else: From f271bc8b2fb1b8a86e30a303aca9a2bf55d54c7d Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 21 May 2020 00:17:56 +0300 Subject: [PATCH 27/28] Link pilfont to its new home in pillow-scripts --- docs/reference/ImageFont.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/reference/ImageFont.rst b/docs/reference/ImageFont.rst index bb7538096..1aa22aa51 100644 --- a/docs/reference/ImageFont.rst +++ b/docs/reference/ImageFont.rst @@ -9,13 +9,14 @@ this class store bitmap fonts, and are used with the :py:meth:`PIL.ImageDraw.Draw.text` method. PIL uses its own font file format to store bitmap fonts. You can use the -:command:`pilfont` utility to convert BDF and PCF font descriptors (X window -font formats) to this format. +:command:`pilfont` utility from +`pillow-scripts `_ +to convert BDF and PCF font descriptors (X window font formats) to this format. Starting with version 1.1.4, PIL can be configured to support TrueType and OpenType fonts (as well as other font formats supported by the FreeType library). For earlier versions, TrueType support is only available as part of -the imToolkit package +the imToolkit package. Example ------- From 995634c40154853c12c95c426807efe427b4d4e8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 23 May 2020 11:54:06 +1000 Subject: [PATCH 28/28] Updated macOS tested Pillow versions [ci skip] --- docs/installation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/installation.rst b/docs/installation.rst index 4b9ebdfd0..58784ee12 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -431,7 +431,7 @@ These platforms have been reported to work at the versions mentioned. +----------------------------------+------------------------------+--------------------------------+-----------------------+ |**Operating system** |**Tested Python versions** |**Latest tested Pillow version**|**Tested processors** | +----------------------------------+------------------------------+--------------------------------+-----------------------+ -| macOS 10.15 Catalina | 3.5, 3.6, 3.7, 3.8 | 7.0.0 |x86-64 | +| macOS 10.15 Catalina | 3.5, 3.6, 3.7, 3.8 | 7.1.2 |x86-64 | +----------------------------------+------------------------------+--------------------------------+-----------------------+ | macOS 10.14 Mojave | 2.7, 3.5, 3.6, 3.7 | 6.0.0 |x86-64 | | +------------------------------+--------------------------------+ +