Commit 8c310e83682e9e7018b960b836b16be582b04e24
1 parent
fad8d727
Exists in
master
jpeg base resolved.
Showing
10 changed files
with
204 additions
and
46 deletions
Show diff stats
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +__author__ = 'chunk' | ... | ... |
| ... | ... | @@ -0,0 +1,77 @@ |
| 1 | +""" | |
| 2 | +Common utils. | |
| 3 | + | |
| 4 | +@author: chunk | |
| 5 | +chunkplus@gmail.com | |
| 6 | +2014 Dec | |
| 7 | +""" | |
| 8 | +__author__ = 'hadoop' | |
| 9 | + | |
| 10 | +import os, sys | |
| 11 | +import time | |
| 12 | + | |
| 13 | + | |
| 14 | +class Timer(): | |
| 15 | + def __init__(self): | |
| 16 | + self.__newtime = time.time() | |
| 17 | + self.__oldtime = self.__newtime | |
| 18 | + | |
| 19 | + def mark(self): | |
| 20 | + self.__oldtime = self.__newtime | |
| 21 | + self.__newtime = time.time() | |
| 22 | + return self.__newtime - self.__oldtime | |
| 23 | + | |
| 24 | + def report(self): | |
| 25 | + print "%-24s%fs" % ("time elapsed:", self.mark()) | |
| 26 | + | |
| 27 | + | |
| 28 | +def ttimer(): | |
| 29 | + newtime = time.time() | |
| 30 | + while True: | |
| 31 | + oldtime = newtime | |
| 32 | + newtime = time.time() | |
| 33 | + yield newtime - oldtime | |
| 34 | + | |
| 35 | + | |
| 36 | +def ctimer(): | |
| 37 | + newtime = time.clock() | |
| 38 | + while True: | |
| 39 | + oldtime = newtime | |
| 40 | + newtime = time.clock() | |
| 41 | + yield newtime - oldtime | |
| 42 | + | |
| 43 | + | |
| 44 | +def ski2cv(img): | |
| 45 | + if img.ndim >= 3 and img.shape[2] >= 3: | |
| 46 | + img[:, :, [0, 2]] = img[:, :, [2, 0]] | |
| 47 | + return img | |
| 48 | + | |
| 49 | + | |
| 50 | +if __name__ == '__main__': | |
| 51 | + timer = Timer() | |
| 52 | + | |
| 53 | + timer.mark() | |
| 54 | + timer.report() | |
| 55 | + | |
| 56 | + timer.mark() | |
| 57 | + time.sleep(1) | |
| 58 | + # for i in range(1000000): | |
| 59 | + # print i | |
| 60 | + timer.report() | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | ... | ... |
No preview for this file type
flip.py
| ... | ... | @@ -3,7 +3,7 @@ import sys |
| 3 | 3 | import struct |
| 4 | 4 | from PIL import _binary |
| 5 | 5 | import numpy as np |
| 6 | - | |
| 6 | +from array import array | |
| 7 | 7 | |
| 8 | 8 | i8 = _binary.i8 |
| 9 | 9 | o8 = _binary.o8 |
| ... | ... | @@ -13,21 +13,27 @@ i32 = _binary.i32be |
| 13 | 13 | a = mjpeg.Jpeg("res/test2.jpg") |
| 14 | 14 | # print a.component_count |
| 15 | 15 | def flipblock(block): |
| 16 | - tmp = str(block) | |
| 17 | - for i in range(0, len(block), 4): | |
| 18 | - short, = struct.unpack_from("h", tmp, i+2) | |
| 19 | - struct.pack_into("h", block, i+2, -short) | |
| 16 | + tmp = str(block) | |
| 17 | + for i in range(0, len(block), 4): | |
| 18 | + short, = struct.unpack_from("h", tmp, i + 2) | |
| 19 | + struct.pack_into("h", block, i + 2, -short) | |
| 20 | + | |
| 20 | 21 | |
| 21 | 22 | for comp in range(a.component_count): |
| 22 | - xmax, ymax = a.getcomponentdimensions(comp) | |
| 23 | - for y in range(ymax): | |
| 24 | - for x in range((xmax+1)/2): | |
| 25 | - block = a.getblock(x,y,comp) | |
| 26 | - block_to_show = np.frombuffer(block, dtype=np.int16, count=-1, offset=0).reshape(8,8) | |
| 27 | - print block_to_show | |
| 28 | - block2 = a.getblock(xmax-1 - x, y, comp) | |
| 29 | - flipblock(block) | |
| 30 | - flipblock(block2) | |
| 31 | - a.setblock(xmax-1-x, y, comp, block) | |
| 32 | - a.setblock(x,y,comp,block2) | |
| 23 | + xmax, ymax = a.getcomponentdimensions(comp) | |
| 24 | + for y in range(ymax): | |
| 25 | + for x in range((xmax + 1) / 2): | |
| 26 | + block = a.getblock(x, y, comp) | |
| 27 | + # block_to_show = np.frombuffer(block, dtype=np.int16, count=-1, offset=0).reshape(8,8) | |
| 28 | + # print block_to_show | |
| 29 | + block2 = a.getblock(xmax - 1 - x, y, comp) | |
| 30 | + | |
| 31 | + block_to_show = np.frombuffer(block, dtype=np.int16, count=-1, offset=0).reshape(8, 8) | |
| 32 | + block = bytearray(block_to_show) | |
| 33 | + | |
| 34 | + flipblock(block) | |
| 35 | + flipblock(block2) | |
| 36 | + a.setblock(xmax - 1 - x, y, comp, block) | |
| 37 | + a.setblock(x, y, comp, block2) | |
| 33 | 38 | |
| 39 | +a.write("test2ooout.jpg") | |
| 34 | 40 | \ No newline at end of file | ... | ... |
jpegObj/__init__.py
| ... | ... | @@ -4,7 +4,7 @@ print "[pysteg.jpeg] $Id: __init__.py 2204 2011-04-05 11:43:38Z georg $" |
| 4 | 4 | |
| 5 | 5 | from mjsteg import Jsteg |
| 6 | 6 | |
| 7 | -__all__ = ["Jpeg"] | |
| 7 | +__all__ = ['Jpeg'] | |
| 8 | 8 | |
| 9 | 9 | # We need standard components from :mod:`numpy`, and some auxiliary |
| 10 | 10 | # functions from submodules. |
| ... | ... | @@ -194,6 +194,12 @@ class Jpeg(Jsteg): |
| 194 | 194 | cID = self.getCompID(channel) |
| 195 | 195 | self.coef_arrays[cID] = matrix |
| 196 | 196 | |
| 197 | + blocks = self.getCoefBlocks(channel) | |
| 198 | + xmax, ymax = self.Jgetcompdim(cID) | |
| 199 | + for y in range(ymax): | |
| 200 | + for x in range(xmax): | |
| 201 | + block = blocks[y, x] | |
| 202 | + self.Jsetblock(x, y, cID, bytearray(block.astype(np.int16))) | |
| 197 | 203 | |
| 198 | 204 | def getCoefBlocks(self, channel="Y"): |
| 199 | 205 | """ |
| ... | ... | @@ -222,6 +228,8 @@ class Jpeg(Jsteg): |
| 222 | 228 | v, h = loc[0] * 8, loc[1] * 8 |
| 223 | 229 | self.coef_arrays[cID][v:v + 8, h:h + 8] = block |
| 224 | 230 | |
| 231 | + self.Jsetblock(loc[1], loc[0], cID, bytearray(block.astype(np.int16))) | |
| 232 | + | |
| 225 | 233 | # Decompression |
| 226 | 234 | # ------------- |
| 227 | 235 | |
| ... | ... | @@ -311,3 +319,5 @@ class Jpeg(Jsteg): |
| 311 | 319 | S = np.round(ibdct(D) + 128) |
| 312 | 320 | return S.astype(np.uint8) |
| 313 | 321 | |
| 322 | + | |
| 323 | + | ... | ... |
jpegObj/__init__.pyc
No preview for this file type
5.08 KB
tests.py
| ... | ... | @@ -0,0 +1,92 @@ |
| 1 | +__author__ = 'chunk' | |
| 2 | + | |
| 3 | +import numpy as np | |
| 4 | +import mjsteg | |
| 5 | +import jpegObj | |
| 6 | +from common import * | |
| 7 | + | |
| 8 | +timer = Timer() | |
| 9 | + | |
| 10 | +sample = [[7, 12, 14, -12, 1, 0, -1, 0], | |
| 11 | + [6, 5, -10, 0, 6, 0, 0, 0], | |
| 12 | + [0, 6, -5, 4, 0, -1, 0, 0], | |
| 13 | + [0, -3, 0, 1, -1, 0, 0, 0], | |
| 14 | + [-3, 5, 0, 0, 0, 0, 0, 0], | |
| 15 | + [2, -1, 0, 0, 0, 0, 0, 0], | |
| 16 | + [0, 0, 0, 0, 0, 0, 0, 0], | |
| 17 | + [0, 0, 0, 0, 0, 0, 0, 0]] | |
| 18 | + | |
| 19 | + | |
| 20 | +def diffblock(c1, c2): | |
| 21 | + diff = False | |
| 22 | + if np.array_equal(c1, c2): | |
| 23 | + print("blocks match") | |
| 24 | + else: | |
| 25 | + print("blocks not match") | |
| 26 | + diff = True | |
| 27 | + | |
| 28 | + return diff | |
| 29 | + | |
| 30 | + | |
| 31 | +def diffblocks(a, b): | |
| 32 | + diff = False | |
| 33 | + for comp in range(a.image_components): | |
| 34 | + xmax, ymax = a.Jgetcompdim(comp) | |
| 35 | + for y in range(ymax): | |
| 36 | + for x in range(xmax): | |
| 37 | + if a.Jgetblock(x, y, comp) != b.Jgetblock(x, y, comp): | |
| 38 | + print("blocks({},{}) in component {} not match".format(y, x, comp)) | |
| 39 | + diff = True | |
| 40 | + return diff | |
| 41 | + | |
| 42 | + | |
| 43 | +def test_setblocks(): | |
| 44 | + """ | |
| 45 | + wholewise | |
| 46 | + """ | |
| 47 | + imb = jpegObj.Jpeg("res/test4.jpg") | |
| 48 | + block = imb.getCoefBlock(channel='Y', loc=(-1, 2)) | |
| 49 | + print block | |
| 50 | + | |
| 51 | + imb.setCoefMatrix(np.array([[0] * 800 for i in range(600)]), channel='Y') | |
| 52 | + | |
| 53 | + block = imb.getCoefBlock(channel='Y', loc=(-1, 2)) | |
| 54 | + print block | |
| 55 | + | |
| 56 | + imb.Jwrite("res/test4.jpg") | |
| 57 | + | |
| 58 | + ima = jpegObj.Jpeg("res/test3.jpg") | |
| 59 | + imb = jpegObj.Jpeg("res/test4.jpg") | |
| 60 | + diffblocks(ima, imb) | |
| 61 | + | |
| 62 | + | |
| 63 | +def test_setblock(): | |
| 64 | + """ | |
| 65 | + blockwise | |
| 66 | + """ | |
| 67 | + imb = jpegObj.Jpeg("res/test4.jpg") | |
| 68 | + block = imb.getCoefBlock(channel='Y', loc=(0, 1)) | |
| 69 | + print block | |
| 70 | + | |
| 71 | + # print b.setCoefBlock(np.array([[27] + [0] * 7] + [[0] * 8 for i in range(7)]), loc=(0, 1)) | |
| 72 | + imb.setCoefBlock(np.array(sample), loc=(0, 1)) | |
| 73 | + blocks1 = imb.getCoefBlock(channel='Y', loc=(0, 1)) | |
| 74 | + print blocks1 | |
| 75 | + blocks2 = imb.Jgetblock(1, 0, 0) | |
| 76 | + block_to_show = np.frombuffer(blocks2, dtype=np.int16, count=-1, offset=0).reshape(8, 8) | |
| 77 | + print block_to_show | |
| 78 | + diffblock(blocks1, block_to_show) | |
| 79 | + | |
| 80 | + | |
| 81 | +if __name__ == '__main__': | |
| 82 | + timer.mark() | |
| 83 | + test_setblock() | |
| 84 | + timer.report() | |
| 85 | + | |
| 86 | + timer.mark() | |
| 87 | + test_setblocks() | |
| 88 | + timer.report() | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | ... | ... |
yaj.py
| ... | ... | @@ -1,28 +0,0 @@ |
| 1 | -__author__ = 'chunk' | |
| 2 | - | |
| 3 | -import numpy as np | |
| 4 | -import jpegObj | |
| 5 | - | |
| 6 | -b = jpegObj.Jpeg("res/test2.jpg") | |
| 7 | -# c = b.getCoefBlocks(channel='Y') | |
| 8 | -# c1,c2,c3 = b.getCoefBlocks(channel='All') | |
| 9 | -# print c2 | |
| 10 | - | |
| 11 | - | |
| 12 | -print b.setCoefBlock(np.array([[0] * 8 for i in range(8)])) | |
| 13 | - | |
| 14 | -c = b.getCoefBlock(channel='Y', loc=(0, 0)) | |
| 15 | -print c | |
| 16 | - | |
| 17 | -c = b.getCoefBlock(channel='Y', loc=(-1, 1)) | |
| 18 | -print c | |
| 19 | - | |
| 20 | -b.setCoefMatrix(np.array([[0] * 800 for i in range(600)]),channel='Y') | |
| 21 | - | |
| 22 | -c = b.getCoefBlock(channel='Y', loc=(-1, 1)) | |
| 23 | -print c | |
| 24 | - | |
| 25 | -c = b.getCoefBlock(channel='Y', loc=(0, 0)) | |
| 26 | -print c | |
| 27 | - | |
| 28 | - |