Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tonyjpeg, Image
- def bgr2rgb(bmpstr):
- return "".join([bmpstr[i*3+2]+bmpstr[i*3+1]+bmpstr[i*3] for i in range(len(bmpstr)/3)])
- changing_cnt = 5
- def uslovie(cntr, block):
- if ((cntr % 3 != 2) or (cntr < 10)) :
- # or (len(filter(lambda x:x==0,block[:9]))>3 or len(filter(lambda x:x==0,block[-9:]))<7):
- # print 'skipping block {0}'.format(cntr)
- return 1
- else: return 0
- cend = 5
- def nomblo(block):
- a, b, c = block[3], block[4], block[cend]
- return a,b,c
- def do(ifile, ofile, otext, cb=None):
- fl=open(ifile,'rb');f=fl.read();fl.close()
- tj = tonyjpeg.TonyJpegDecoder()
- if cb is None:
- tj.callback = callback_write
- else:
- tj.callback = cb
- i = tj.DecompressImage(f)
- step = tj.Width*3+2
- pointer = 0
- t = []
- bmpstr = "".join([chr(x) for x in i])
- if len(bmpstr)>tj.Width*tj.Height*3:
- tail = bmpstr
- while True:
- if len(tail)==0:
- break
- head = tail[:step-2]
- tail = tail[step:]
- t.append(head)
- bmpstr2 = bgr2rgb(''.join(t))
- else:
- bmpstr2 = bgr2rgb(bmpstr)
- print len(bmpstr2)
- img = Image.frombuffer("RGB", (tj.Width, tj.Height), bmpstr2)
- img.save(ofile+'.jpg', quality=90)
- bits = None
- def do2(ifile, ofile, cb=None):
- fl=open(ifile,'rb');f=fl.read();fl.close()
- tj = tonyjpeg.TonyJpegDecoder()
- global bits
- bits = []
- if cb is None:
- tj.callback = callback_read
- else:
- tj.callback = cb
- tj.DecompressImage(f)
- fl=open(ofile,'wb');f=fl.write(''.join(bits));fl.close()
- odinol = 1
- def callback_write(block, cntr):
- global odinol
- if uslovie(cntr, block):
- return block
- bl = block
- c1,c2,c3 = nomblo(block)
- print 'c1,c2,c3',c1,' ',c2,' ',c3
- if (odinol == 0) and not (c3>c2 and c3>c1):
- print 'Block 1 {0}'.format(cntr)
- c3 = max(c1,c2)+changing_cnt
- bl[cend]=c3 if c3<=255 else 255
- if (odinol != 0) and not (c3<c2 and c3<c1):
- print 'Block 0 {0}'.format(cntr)
- c3 = min(c1,c2)-changing_cnt
- bl[cend]=c3 if c3 >=0 else 0
- print 'c1,c2,c3',c1,' ',c2,' ',c3
- odinol = 1 if odinol==0 else 0
- #is_1 = not is_1
- return bl
- def check(infile):
- do(infile, 'tmp1'+infile, '.test._txt', callback_fill1)
- do(infile, 'tmp0'+infile, '.test._txt', callback_fill0)
- do2('tmp1'+infile+'.jpg', '.check1')
- do2('tmp0'+infile+'.jpg', '.check0')
- fl=open('.check0','rb');f0=fl.read();fl.close()
- fl=open('.check1','rb');f1=fl.read();fl.close()
- l = len(f0)
- res = [f0[i]=='0' and f1[i]=='1' for i in range(l)]
- return res
- def callback_read(block, cntr):
- if uslovie(cntr, block):
- return block
- bl = block
- c1,c2,c3 = nomblo(block)
- if c3>c2 and c3>c1:
- bits.append('1')
- elif c3<c2 and c3<c1:
- bits.append('0')
- else:
- bits.append('2')
- return block
- def callback_fill1(block, cntr):
- if uslovie(cntr, block):
- return block
- bl = block
- c1,c2,c3 = nomblo(block)
- if not (c3>c2 and c3>c1):
- c3 = max(c1,c2)+changing_cnt
- bl[cend]=c3# if c3<=255 else 255
- return bl
- def callback_fill0(block, cntr):
- if uslovie(cntr, block):
- return block
- bl = block
- c1,c2,c3 = nomblo(block)
- if not (c3<c2 and c3<c1):
- c3 = min(c1,c2)-changing_cnt
- bl[cend]=c3# if c3>=0 else 0
- return bl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement