{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import glob\n",
    "import os\n",
    "from PIL import Image\n",
    "import time\n",
    "\n",
    "\n",
    "def bitPadding(str1,str2,N):   \n",
    "    # padding n ending chars of str1 to the beginning of str2, keep the length of str2\n",
    "    tmpStr = str1+str2\n",
    "    return tmpStr[len(str1) - N : len(str1) - N + len(str2) ]\n",
    "\n",
    "def framePadding(fr1,fr2,N): # (string) frame length = 96 (12 * 8) \n",
    "    L = len(fr1) # = len(fr2)\n",
    "    Pix = ''\n",
    "    for i in range(L//12):\n",
    "        Pix += bitPadding(fr1[i*12:(i+1)*12],fr2[i*12:(i+1)*12],N)\n",
    "    \n",
    "    return Pix    \n",
    "    \n",
    "def rightCarryOver(tiffLine,N):  # Carry over 8 pixel for one line\n",
    "    #tiffLine: ndarray\n",
    "    orgStr = ''.join([np.binary_repr(i>>4, width=12) for i in tiffLine])   # original 12bit sequence\n",
    "    \n",
    "    #carrying bits over next 8 pixel = 96 bits \n",
    "    newStr = orgStr[:96]    #keep first pixel, transerve from second set of 8 pixels\n",
    "    for i in range(1, len(orgStr)//96):     # each 8 pixels \n",
    "        fr1 = orgStr[(i-1)*96 : i*96]\n",
    "        fr2 = orgStr[i*96 : (i+1)*96]\n",
    "        newStr += framePadding(fr1,fr2,N)\n",
    "        \n",
    "    return newStr\n",
    "\n",
    "def bitRightShift(tiffFile,N):\n",
    "    # tiffFile(16bit) image\n",
    "    # nOfBits: number of shifted bits to the right\n",
    "    \n",
    "    #load TiffImage\n",
    "    img = Image.open(tiffFile)\n",
    "    inArr = np.array(img)\n",
    "    [row,col] = inArr.shape\n",
    "    print(row,col)\n",
    "\n",
    "    print('mean = {}, min = {}'.format(inArr.mean(), inArr.min()))\n",
    "    \n",
    "    outArr = np.zeros(0, dtype=np.int16)     # initialize the output Image ndarray\n",
    "    for i in range(row):\n",
    "        #print (rightShiftInLine(inArr[i,:],nOfBits))\n",
    "        outArr = np.append(outArr,rightCarryOver(inArr[i,:],N))\n",
    "    \n",
    "    print(outArr[:])\n",
    "    print(outArr.shape)\n",
    "    \n",
    "    \n",
    "    return outArr    # 16 bit data nparray\n",
    "\n",
    "def main():\n",
    "    nOfBits = 0\n",
    "    tiffFile = '/home/usrp/workspace/imager/ms_imager/raw_files/sensor2_E100_G5_Modesto2_portion.tiff'\n",
    "    shifted_data = bitRightShift(tiffFile, nOfBits)\n",
    "    shifted_data.astype('int16').tofile(tiffFile[:-5] + '.16b_3')\n",
    "    \n",
    "    command = \"raw2tiff -w 927 -l 564 -c lzw:2 -d short -M \" #.format(col,row)\n",
    "    command += tiffFile[:-5] + '.16b_3' + \" \"\n",
    "    command += tiffFile[:-5] + \"_{}CarryOver.tiff\".format(nOfBits)\n",
    "    print(command)\n",
    "    rc = os.system(command)\n",
    "    print(rc)\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    t1 = time.time()\n",
    "    main()\n",
    "    print('Elapsed time: {} seconds'.format(time.time()-t1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
