Skip to content

Commit

Permalink
Improved encode() error handling on wrong array shape
Browse files Browse the repository at this point in the history
1. Improved encode() error handling on wrong array shape (silent crash on wrong array shape #57)
2. Added an extra turbojpeg default path for Mac OS
  • Loading branch information
lilohuang authored Jan 8, 2022
1 parent 96a8dfe commit ae3da71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages
setup(
name='PyTurboJPEG',
version='1.6.4',
version='1.6.5',
description='A Python wrapper of libjpeg-turbo for decoding and encoding JPEG image.',
author='Lilo Huang',
author_email='kuso.cc@gmail.com',
Expand Down
17 changes: 13 additions & 4 deletions turbojpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# SOFTWARE.

__author__ = 'Lilo Huang <kuso.cc@gmail.com>'
__version__ = '1.6.4'
__version__ = '1.6.5'

from ctypes import *
from ctypes.util import find_library
Expand All @@ -36,7 +36,10 @@

# default libTurboJPEG library path
DEFAULT_LIB_PATHS = {
'Darwin': ['/usr/local/opt/jpeg-turbo/lib/libturbojpeg.dylib'],
'Darwin': [
'/usr/local/opt/jpeg-turbo/lib/libturbojpeg.dylib',
'/opt/libjpeg-turbo/lib64/libturbojpeg.dylib'
],
'Linux': [
'/usr/lib/x86_64-linux-gnu/libturbojpeg.so.0',
'/usr/lib64/libturbojpeg.so.0',
Expand Down Expand Up @@ -111,6 +114,10 @@
TJXOPT_PROGRESSIVE = 32
TJXOPT_COPYNONE = 64

# pixel size
# see details in https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/turbojpeg.h
tjPixelSize = [3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4]

# MCU block width (in pixels) for a given level of chrominance subsampling.
# MCU block sizes:
# - 8x8 for no subsampling or grayscale
Expand Down Expand Up @@ -395,13 +402,12 @@ def decode(self, jpeg_buf, pixel_format=TJPF_BGR, scaling_factor=None, flags=0):
"""decodes JPEG memory buffer to numpy array."""
handle = self.__init_decompress()
try:
pixel_size = [3, 3, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4]
jpeg_array = np.frombuffer(jpeg_buf, dtype=np.uint8)
src_addr = self.__getaddr(jpeg_array)
scaled_width, scaled_height, _, _ = \
self.__get_header_and_dimensions(handle, jpeg_array.size, src_addr, scaling_factor)
img_array = np.empty(
[scaled_height, scaled_width, pixel_size[pixel_format]],
[scaled_height, scaled_width, tjPixelSize[pixel_format]],
dtype=np.uint8)
dest_addr = self.__getaddr(img_array)
status = self.__decompress(
Expand Down Expand Up @@ -477,6 +483,9 @@ def encode(self, img_array, quality=85, pixel_format=TJPF_BGR, jpeg_subsample=TJ
jpeg_buf = c_void_p()
jpeg_size = c_ulong()
height, width = img_array.shape[:2]
channel = tjPixelSize[pixel_format]
if channel > 1 and (len(img_array.shape) < 3 or img_array.shape[2] != channel):
raise ValueError('Invalid shape for image data')
src_addr = self.__getaddr(img_array)
status = self.__compress(
handle, src_addr, width, img_array.strides[0], height, pixel_format,
Expand Down

0 comments on commit ae3da71

Please sign in to comment.