Example #1
0
    def _test_compress(self, embed_data):
        image_path = os.path.join(tests_dir, 'origin.jpg')

        jaoutput = jpype.JClass('java.io.ByteArrayOutputStream')()
        jaimage = jpype.JClass(
            'java.awt.Toolkit').getDefaultToolkit().getImage(image_path)
        jaencoder = jpype.JClass('james.JpegEncoder')(jaimage, 80, jaoutput,
                                                      '')
        if embed_data:
            jaencoder.Compress(
                jpype.JClass('java.io.ByteArrayInputStream')(
                    jpype.java.lang.String(
                        embed_data.decode('utf-8')).getBytes()), 'abc123')
        else:
            jaencoder.Compress()
        jaarray = jaoutput.toByteArray()

        jpeg_encoder.F5Random = JavaF5Random
        pyoutput = StringIO.StringIO()
        pyimage = Image.open(image_path)
        pyencoder = JpegEncoder(pyimage, 80, pyoutput, '')
        pyencoder.compress(embed_data, 'abc123')
        pyarray = pyoutput.getvalue()
        pyoutput.close()

        self.assertEqual(len(jaarray), len(pyarray), 'length is not same')
        for i in range(len(jaarray)):
            aa = int(jaarray[i])
            bb = struct.unpack('b', pyarray[i])[0]
            self.assertEqual(aa, bb, '%d %d %d' % (i, aa, bb))
    def _test_compress(self, embed_data):
        image_path = os.path.join(tests_dir, 'origin.jpg')

        jaoutput = jpype.JClass('java.io.ByteArrayOutputStream')()
        jaimage = jpype.JClass('java.awt.Toolkit').getDefaultToolkit().getImage(image_path)
        jaencoder = jpype.JClass('james.JpegEncoder')(jaimage, 80, jaoutput, '')
        if embed_data:
            jaencoder.Compress(jpype.JClass('java.io.ByteArrayInputStream')(
                jpype.java.lang.String(embed_data.decode('utf-8')).getBytes()), 'abc123')
        else:
            jaencoder.Compress()
        jaarray = jaoutput.toByteArray()

        jpeg_encoder.F5Random = JavaF5Random
        pyoutput = StringIO.StringIO()
        pyimage = Image.open(image_path)
        pyencoder = JpegEncoder(pyimage, 80, pyoutput, '')
        pyencoder.compress(embed_data, 'abc123')
        pyarray = pyoutput.getvalue()
        pyoutput.close()

        self.assertEqual(len(jaarray), len(pyarray), 'length is not same')
        for i in range(len(jaarray)):
            aa = int(jaarray[i])
            bb = struct.unpack('b', pyarray[i])[0]
            self.assertEqual(aa, bb, '%d %d %d' % (i, aa, bb))
Example #3
0
    def decode(self):
        info_print("started decoding raw data")
        bit_reader = JpegBitReader(self.raw_data)
        pixels_mcu_horiz = self.jpeg_decode_metadata.horiz_pixels_in_mcu
        pixels_mcu_vert = self.jpeg_decode_metadata.vert_pixels_in_mcu

        n_mcu_horiz = math.ceil(self.jpeg_decode_metadata.width /
                                pixels_mcu_horiz)
        n_mcu_vert = math.ceil(self.jpeg_decode_metadata.height /
                               pixels_mcu_vert)

        self.huffman_decode_mcus(
            bit_reader, self.jpeg_decode_metadata.components_to_metadata,
            n_mcu_horiz, n_mcu_vert,
            self.jpeg_decode_metadata.restart_interval)
        info_print("time:", time.time())
        if ENCODING_TEST:
            encoder = JpegEncoder(self.jpeg_decode_metadata)
            res = encoder.encode(self._decoded_mcu_list)
            min_len = min(len(res), len(bit_reader._bytes))
            is_eq = res[:min_len] == bit_reader._bytes[:min_len]
            info_print(
                f"Equal? {'Yes ☺' if is_eq else 'No!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'}"
            )

            writer = JpegWriter()
            writer.write(self.jpeg_decode_metadata, res)
        self.de_quantize(self.jpeg_decode_metadata.components_to_metadata)
        info_print("time:", time.time())

        self.inverse_dct(
            self.jpeg_decode_metadata.components_to_metadata.keys())
        # TODO: The code up until now is true regardless of the number of components,
        #  from here and on it is color space dependent and needs to be fixed because the next function shouldn't be
        self.construct_pixel_map(n_mcu_horiz, pixels_mcu_horiz,
                                 pixels_mcu_vert)
        # DROR From here it should be color space dependent
        self._to_rgb()

        return bit_reader.get_byte_location(
        ), self._full_image_rgb, n_mcu_horiz * pixels_mcu_horiz, n_mcu_vert * pixels_mcu_vert
Example #4
0
            if not options.output:
                print 'you didn\'t specify the output jpeg file, if will be default output.jpg'
                options.output = 'output.jpg'
            elif os.path.exists(options.output) and os.path.isfile(
                    options.output):
                print 'the output file exists, do you really want to override it?'
                '''
                answer = raw_input('y/n: ')
                if answer != 'y':
                    print 'exit'
                    sys.exit(0)
                '''
            output = open(options.output, 'wb')

            if options.hasais == 'n':
                encoder = JpegEncoder(image, 80, output, options.comment,
                                      False)  #image、output是文件对象
            else:
                encoder = JpegEncoder(image, 80, output, options.comment,
                                      True)  #image、output是文件对象
            encoder.compress(data, options.password)
            output.close()
            print '\n----embed end--------'
        if options.type == 'x':
            print '----start extract------'
            if options.output:
                output = open(options.output, 'wb')
            else:
                output = StringIO.StringIO()
            image = open(options.image, 'rb')  #steg_image
            JpegExtract(output, options.password).extract(image.read())
                print 'there\'s no data to embed'
                sys.exit(0)

            if not options.output:
                print 'you didn\'t specify the output jpeg file, if will be default output.jpg'
                options.output = 'output.jpg'
            elif os.path.exists(options.output) and os.path.isfile(
                    options.output):
                print 'the output file exists, do you really want to override it?'
                answer = raw_input('y/n: ')
                if answer != 'y':
                    print 'exit'
                    sys.exit(0)
            output = open(options.output, 'wb')

            encoder = JpegEncoder(image, 80, output, options.comment)
            encoder.compress(data, options.password)
            output.close()
        if options.type == 'x':
            if options.output:
                output = open(options.output, 'wb')
            else:
                output = StringIO.StringIO()
            image = open(options.image, 'rb')
            JpegExtract(output, options.password).extract(image.read())

            if not options.output:
                print output.getvalue()

            image.close()
            output.close()
            if not data:
                print 'there\'s no data to embed'
                sys.exit(0)

            if not options.output:
                print 'you didn\'t specify the output jpeg file, if will be default output.jpg'
                options.output = 'output.jpg'
            elif os.path.exists(options.output) and os.path.isfile(options.output):
                print 'the output file exists, do you really want to override it?'
                answer = raw_input('y/n: ')
                if answer != 'y':
                    print 'exit'
                    sys.exit(0)
            output = open(options.output, 'wb')

            encoder = JpegEncoder(image, 80, output, options.comment)
            encoder.compress(data, options.password)
            output.close()
        if options.type == 'x':
            if options.output:
                output = open(options.output, 'wb')
            else:
                output = StringIO.StringIO()
            image = open(options.image, 'rb')
            JpegExtract(output, options.password).extract(image.read())

            if not options.output:
                print output.getvalue()

            image.close()
            output.close()