| 1 | # This file is part of Xpra. |
| 2 | # Copyright (C) 2017 Antoine Martin <antoine@devloop.org.uk> |
| 3 | # Xpra is released under the terms of the GNU GPL v2, or, at your option, any |
| 4 | # later version. See the file COPYING for details. |
| 5 | |
| 6 | from xpra.log import Logger |
| 7 | log = Logger("encoder", "jpeg") |
| 8 | |
| 9 | from xpra.buffers.membuf cimport getbuf, MemBuf |
| 10 | from xpra.codecs.image_wrapper import ImageWrapper |
| 11 | from libc.stdint cimport uint8_t, uint32_t, uintptr_t |
| 12 | from xpra.buffers.membuf cimport getbuf, MemBuf |
| 13 | |
| 14 | |
| 15 | ctypedef int boolean |
| 16 | ctypedef unsigned int JDIMENSION |
| 17 | |
| 18 | cdef extern from "jpeglib.h": |
| 19 | int JPEG_LIB_VERSION |
| 20 | |
| 21 | ctypedef int J_COLOR_SPACE |
| 22 | J_COLOR_SPACE JCS_UNKNOWN # error/unspecified |
| 23 | J_COLOR_SPACE JCS_GRAYSCALE # monochrome |
| 24 | J_COLOR_SPACE JCS_RGB # red/green/blue as specified by the RGB_RED |
| 25 | # RGB_GREEN, RGB_BLUE, and RGB_PIXELSIZE macros |
| 26 | J_COLOR_SPACE JCS_YCbCr # Y/Cb/Cr (also known as YUV) |
| 27 | J_COLOR_SPACE JCS_CMYK # C/M/Y/K |
| 28 | J_COLOR_SPACE JCS_YCCK # Y/Cb/Cr/K |
| 29 | J_COLOR_SPACE JCS_EXT_RGB # red/green/blue |
| 30 | J_COLOR_SPACE JCS_EXT_RGBX # red/green/blue/x |
| 31 | J_COLOR_SPACE JCS_EXT_BGR # blue/green/red |
| 32 | J_COLOR_SPACE JCS_EXT_BGRX # blue/green/red/x |
| 33 | J_COLOR_SPACE JCS_EXT_XBGR # x/blue/green/red |
| 34 | J_COLOR_SPACE JCS_EXT_XRGB # x/red/green/blue |
| 35 | # |
| 36 | # When out_color_space it set to JCS_EXT_RGBX, JCS_EXT_BGRX, JCS_EXT_XBGR, |
| 37 | # or JCS_EXT_XRGB during decompression, the X byte is undefined, and in |
| 38 | # order to ensure the best performance, libjpeg-turbo can set that byte to |
| 39 | # whatever value it wishes. Use the following colorspace constants to |
| 40 | # ensure that the X byte is set to 0xFF, so that it can be interpreted as an |
| 41 | # opaque alpha channel. */ |
| 42 | J_COLOR_SPACE JCS_EXT_RGBA # red/green/blue/alpha |
| 43 | J_COLOR_SPACE JCS_EXT_BGRA # blue/green/red/alpha |
| 44 | J_COLOR_SPACE JCS_EXT_ABGR # alpha/blue/green/red |
| 45 | J_COLOR_SPACE JCS_EXT_ARGB # alpha/red/green/blue |
| 46 | J_COLOR_SPACE JCS_RGB565 # 5-bit red/6-bit green/5-bit blue |
| 47 | |
| 48 | cdef struct jpeg_common_struct: |
| 49 | pass |
| 50 | ctypedef jpeg_common_struct* j_common_ptr |
| 51 | |
| 52 | cdef struct jpeg_source_mgr: |
| 53 | pass |
| 54 | cdef struct jpeg_decompress_struct: |
| 55 | jpeg_error_mgr *err # Error handler module |
| 56 | jpeg_source_mgr *src |
| 57 | int input_components # # of color components in input image |
| 58 | J_COLOR_SPACE in_color_space # colorspace of input image |
| 59 | J_COLOR_SPACE out_color_space # colorspace for output |
| 60 | JDIMENSION output_width # scaled image width |
| 61 | JDIMENSION output_height # scaled image height |
| 62 | int out_color_components # # of color components in out_color_space |
| 63 | int output_components # # of color components returned |
| 64 | # output_components is 1 (a colormap index) when quantizing colors |
| 65 | # otherwise it equals out_color_components. |
| 66 | JDIMENSION output_scanline # 0 .. output_height-1 |
| 67 | |
| 68 | |
| 69 | cdef struct jpeg_error_mgr: |
| 70 | # Error exit handler: does not return to caller |
| 71 | void (*error_exit) (j_common_ptr cinfo) |
| 72 | # Conditionally emit a trace or warning message |
| 73 | void (*emit_message) (j_common_ptr cinfo, int msg_level) |
| 74 | # Routine that actually outputs a trace or error message |
| 75 | void (*output_message) (j_common_ptr cinfo) |
| 76 | # Format a message string for the most recent JPEG error or message |
| 77 | void (*format_message) (j_common_ptr cinfo, char *buffer) |
| 78 | # Reset error state variables at start of a new image |
| 79 | void (*reset_error_mgr) (j_common_ptr cinfo) |
| 80 | # The message ID code and any parameters are saved here. |
| 81 | # A message can have one string parameter or up to 8 int parameters. |
| 82 | int msg_code |
| 83 | #msg_parm union / TODO |
| 84 | int trace_level # max msg_level that will be displayed |
| 85 | long num_warnings # number of corrupt-data warnings |
| 86 | |
| 87 | jpeg_error_mgr * jpeg_std_error(jpeg_error_mgr *err) |
| 88 | |
| 89 | ctypedef jpeg_decompress_struct* j_decompress_ptr |
| 90 | |
| 91 | ctypedef unsigned char JSAMPLE |
| 92 | ctypedef JSAMPLE *JSAMPROW # ptr to one image row of pixel samples. |
| 93 | ctypedef JSAMPROW *JSAMPARRAY # ptr to some rows (a 2-D sample array) |
| 94 | ctypedef JSAMPARRAY *JSAMPIMAGE |
| 95 | |
| 96 | void jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize) |
| 97 | void jpeg_mem_src(j_decompress_ptr cinfo, const unsigned char *inbuffer, unsigned long insize) |
| 98 | boolean jpeg_start_decompress(j_decompress_ptr cinfo) |
| 99 | int jpeg_read_header(j_decompress_ptr cinfo, boolean require_image) |
| 100 | JDIMENSION jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines) |
| 101 | void jpeg_finish_decompress(j_decompress_ptr cinfo) |
| 102 | void jpeg_destroy_decompress(j_decompress_ptr cinfo) |
| 103 | |
| 104 | |
| 105 | cdef extern from "jerror.h": |
| 106 | pass |
| 107 | |
| 108 | cdef extern from "stdlib.h": |
| 109 | void free(void *ptr) |
| 110 | |
| 111 | |
| 112 | cdef extern from "../../buffers/memalign.h": |
| 113 | void *xmemalign(size_t size) |
| 114 | |
| 115 | cdef extern from "../../buffers/buffers.h": |
| 116 | object memory_as_pybuffer(void* ptr, Py_ssize_t buf_len, int readonly) |
| 117 | int object_as_buffer(object obj, const void ** buffer, Py_ssize_t * buffer_len) |
| 118 | |
| 119 | |
| 120 | def get_version(): |
| 121 | return JPEG_LIB_VERSION |
| 122 | |
| 123 | def get_encodings(): |
| 124 | return ["jpeg"] |
| 125 | |
| 126 | |
| 127 | cdef inline uint32_t roundup(uint32_t n, uint32_t m): |
| 128 | return (n + m - 1) & ~(m - 1) |
| 129 | |
| 130 | |
| 131 | def decompress(data, int width, int height, options={}): |
| 132 | cdef const uint8_t *buf |
| 133 | cdef Py_ssize_t buf_len |
| 134 | assert object_as_buffer(data, <const void**> &buf, &buf_len)==0, "unable to convert %s to a buffer" % type(data) |
| 135 | |
| 136 | cdef jpeg_decompress_struct cinfo |
| 137 | cdef jpeg_error_mgr jerr |
| 138 | cinfo.err = jpeg_std_error(&jerr) |
| 139 | jpeg_CreateDecompress(&cinfo, JPEG_LIB_VERSION, sizeof(jpeg_decompress_struct)) |
| 140 | |
| 141 | jpeg_mem_src(&cinfo, buf, buf_len) |
| 142 | rc = jpeg_read_header(&cinfo, True) |
| 143 | if rc!=1: |
| 144 | log.error("Error: data does not seem to be a normal JPEG") |
| 145 | return None |
| 146 | jpeg_start_decompress(&cinfo) |
| 147 | assert cinfo.output_width==width and cinfo.output_height==height, "invalid JPEG dimensions: expected %ix%i but got %ix%i" % (width, height, cinfo.output_width, cinfo.output_height) |
| 148 | log("jpeg.decoder decompress image: %ix%i", cinfo.output_width, cinfo.output_height) |
| 149 | cdef int pixel_size = cinfo.output_components |
| 150 | cdef unsigned int rowstride = pixel_size * cinfo.output_width |
| 151 | cdef size_t out_size = rowstride * cinfo.output_height |
| 152 | cdef MemBuf pixels = getbuf(out_size) |
| 153 | cdef uintptr_t rgb = <uintptr_t> pixels.get_mem() |
| 154 | |
| 155 | while cinfo.output_scanline < cinfo.output_height: |
| 156 | assert jpeg_read_scanlines(&cinfo, <JSAMPARRAY> &rgb, 1)==1 |
| 157 | rgb += rowstride |
| 158 | |
| 159 | jpeg_finish_decompress(&cinfo) |
| 160 | jpeg_destroy_decompress(&cinfo) |
| 161 | return ImageWrapper(0, 0, cinfo.output_width, cinfo.output_height, memoryview(pixels), "RGB", 24, rowstride, ImageWrapper.PACKED) |
| 162 | |
| 163 | def selftest(full=False): |
| 164 | try: |
| 165 | log("jpeg selftest") |
| 166 | import binascii |
| 167 | data = binascii.unhexlify("ffd8ffe000104a46494600010101004800480000fffe00134372656174656420776974682047494d50ffdb0043000302020302020303030304030304050805050404050a070706080c0a0c0c0b0a0b0b0d0e12100d0e110e0b0b1016101113141515150c0f171816141812141514ffdb00430103040405040509050509140d0b0d1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414ffc20011080010001003011100021101031101ffc4001500010100000000000000000000000000000008ffc40014010100000000000000000000000000000000ffda000c03010002100310000001aa4007ffc40014100100000000000000000000000000000020ffda00080101000105021fffc40014110100000000000000000000000000000020ffda0008010301013f011fffc40014110100000000000000000000000000000020ffda0008010201013f011fffc40014100100000000000000000000000000000020ffda0008010100063f021fffc40014100100000000000000000000000000000020ffda0008010100013f211fffda000c03010002000300000010924fffc40014110100000000000000000000000000000020ffda0008010301013f101fffc40014110100000000000000000000000000000020ffda0008010201013f101fffc40014100100000000000000000000000000000020ffda0008010100013f101fffd9") |
| 168 | img = decompress(data, 16, 16) |
| 169 | log("decompress(%i bytes)=%s", len(data), img) |
| 170 | finally: |
| 171 | pass |