Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. import tonyjpeg, Image
  2. def bgr2rgb(bmpstr):
  3.     return "".join([bmpstr[i*3+2]+bmpstr[i*3+1]+bmpstr[i*3] for i in range(len(bmpstr)/3)])
  4.  
  5. changing_cnt = 5
  6.  
  7. def uslovie(cntr, block):
  8.     if ((cntr % 3 != 2) or (cntr < 10)) :
  9. #    or (len(filter(lambda x:x==0,block[:9]))>3 or len(filter(lambda x:x==0,block[-9:]))<7):
  10. #        print 'skipping block {0}'.format(cntr)
  11.         return 1
  12.     else: return 0
  13.    
  14. cend = 5
  15. def nomblo(block):
  16.     a, b, c = block[3], block[4], block[cend]
  17.     return a,b,c
  18.    
  19. def do(ifile, ofile, otext, cb=None):
  20.     fl=open(ifile,'rb');f=fl.read();fl.close()
  21.     tj = tonyjpeg.TonyJpegDecoder()
  22.     if cb is None:
  23.         tj.callback = callback_write
  24.     else:
  25.         tj.callback = cb
  26.     i = tj.DecompressImage(f)
  27.     step = tj.Width*3+2
  28.     pointer = 0
  29.     t = []
  30.     bmpstr = "".join([chr(x) for x in i])
  31.     if len(bmpstr)>tj.Width*tj.Height*3:
  32.        
  33.         tail = bmpstr
  34.         while True:
  35.             if len(tail)==0:
  36.                 break
  37.             head = tail[:step-2]
  38.             tail = tail[step:]
  39.             t.append(head)
  40.         bmpstr2 = bgr2rgb(''.join(t))
  41.     else:
  42.         bmpstr2 = bgr2rgb(bmpstr)
  43.     print len(bmpstr2)
  44.     img = Image.frombuffer("RGB", (tj.Width, tj.Height), bmpstr2)
  45.     img.save(ofile+'.jpg', quality=90)
  46.  
  47. bits = None
  48. def do2(ifile, ofile, cb=None):
  49.     fl=open(ifile,'rb');f=fl.read();fl.close()
  50.     tj = tonyjpeg.TonyJpegDecoder()
  51.     global bits
  52.     bits = []
  53.     if cb is None:
  54.         tj.callback = callback_read
  55.     else:
  56.         tj.callback = cb
  57.    
  58.     tj.DecompressImage(f)
  59.     fl=open(ofile,'wb');f=fl.write(''.join(bits));fl.close()
  60.    
  61. odinol = 1
  62. def callback_write(block, cntr):
  63.     global odinol
  64.     if uslovie(cntr, block):
  65.         return block
  66.     bl = block
  67.     c1,c2,c3 = nomblo(block)
  68.     print 'c1,c2,c3',c1,' ',c2,' ',c3
  69.     if (odinol == 0) and not (c3>c2 and c3>c1):
  70.         print 'Block 1 {0}'.format(cntr)
  71.         c3 = max(c1,c2)+changing_cnt
  72.         bl[cend]=c3 if c3<=255 else 255
  73.        
  74.    
  75.     if (odinol != 0) and not (c3<c2 and c3<c1):
  76.         print 'Block 0 {0}'.format(cntr)
  77.         c3 = min(c1,c2)-changing_cnt
  78.         bl[cend]=c3 if c3 >=0 else 0
  79.     print 'c1,c2,c3',c1,' ',c2,' ',c3
  80.     odinol = 1 if odinol==0 else 0
  81.     #is_1 = not is_1
  82.     return bl
  83.  
  84. def check(infile):
  85.     do(infile, 'tmp1'+infile, '.test._txt', callback_fill1)
  86.     do(infile, 'tmp0'+infile, '.test._txt', callback_fill0)
  87.     do2('tmp1'+infile+'.jpg', '.check1')
  88.     do2('tmp0'+infile+'.jpg', '.check0')
  89.     fl=open('.check0','rb');f0=fl.read();fl.close()
  90.     fl=open('.check1','rb');f1=fl.read();fl.close()
  91.     l = len(f0)
  92.     res = [f0[i]=='0' and f1[i]=='1' for i in range(l)]
  93.     return res
  94.        
  95.        
  96. def callback_read(block, cntr):
  97.     if uslovie(cntr, block):
  98.         return block
  99.     bl = block                  
  100.     c1,c2,c3 = nomblo(block)
  101.     if c3>c2 and c3>c1:
  102.         bits.append('1')
  103.     elif c3<c2 and c3<c1:
  104.         bits.append('0')
  105.     else:
  106.         bits.append('2')
  107.     return block
  108.  
  109. def callback_fill1(block, cntr):
  110.     if uslovie(cntr, block):
  111.         return block
  112.     bl = block
  113.     c1,c2,c3 = nomblo(block)
  114.     if not (c3>c2 and c3>c1):
  115.         c3 = max(c1,c2)+changing_cnt
  116.         bl[cend]=c3# if c3<=255 else 255
  117.     return bl
  118.  
  119. def callback_fill0(block, cntr):
  120.     if uslovie(cntr, block):
  121.         return block
  122.     bl = block
  123.     c1,c2,c3 = nomblo(block)
  124.     if not (c3<c2 and c3<c1):
  125.         c3 = min(c1,c2)-changing_cnt
  126.         bl[cend]=c3# if c3>=0 else 0
  127.     return bl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement