{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"! git clone https://github.com/dwgoon/jpegio\n# Once downloaded install the package\n!pip install jpegio/.\nimport jpegio as jio\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport jpegio as jpio\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#This code extract YCbCr channels from a jpeg object\ndef JPEGdecompressYCbCr(jpegStruct):\n    \n    nb_colors=len(jpegStruct.coef_arrays)\n        \n    [Col,Row] = np.meshgrid( range(8) , range(8) )\n    T = 0.5 * np.cos(np.pi * (2*Col + 1) * Row / (2 * 8))\n    T[0,:] = T[0,:] / np.sqrt(2)\n    \n    sz = np.array(jpegStruct.coef_arrays[0].shape)\n    \n    imDecompressYCbCr = np.zeros([sz[0], sz[1], nb_colors]);\n    szDct = (sz/8).astype('int')\n    \n    \n    \n    for ColorChannel in range(nb_colors):\n        tmpPixels = np.zeros(sz)\n    \n        DCTcoefs = jpegStruct.coef_arrays[ColorChannel];\n        if ColorChannel==0:\n            QM = jpegStruct.quant_tables[ColorChannel];\n        else:\n            QM = jpegStruct.quant_tables[1];\n        \n        for idxRow in range(szDct[0]):\n            for idxCol in range(szDct[1]):\n                D = DCTcoefs[idxRow*8:(idxRow+1)*8 , idxCol*8:(idxCol+1)*8]\n                tmpPixels[idxRow*8:(idxRow+1)*8 , idxCol*8:(idxCol+1)*8] = np.dot( np.transpose(T) , np.dot( QM * D , T ) )\n        imDecompressYCbCr[:,:,ColorChannel] = tmpPixels;\n    return imDecompressYCbCr\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nprint(os.listdir('../input/alaska2-image-steganalysis'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img=os.listdir('../input/alaska2-image-steganalysis/Cover')[:12]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"for i, img in enumerate(os.listdir('../input/alaska2-image-steganalysis/Cover')[:10]):\n    imgRGB = mpimg.imread('../input/alaska2-image-steganalysis/Cover/' + img)\n    jpegStruct = jpio.read('../input/alaska2-image-steganalysis/Cover/' + img)\n    imDecompressYCbCr = JPEGdecompressYCbCr(jpegStruct)\n\n    plt.subplot(2, 4, 1) ; plt.imshow(imgRGB)\n    plt.subplot(2, 4, 2) ; plt.imshow(imgRGB[:,:,0] , cmap='gray')\n    plt.subplot(2, 4, 3) ; plt.imshow(imgRGB[:,:,1] , cmap='gray')\n    plt.subplot(2, 4, 4) ; plt.imshow(imgRGB[:,:,2] , cmap='gray')\n\n    plt.subplot(2, 4, 5) ; plt.imshow(imDecompressYCbCr)\n    plt.subplot(2, 4, 6) ; plt.imshow(imDecompressYCbCr[:,:,0] , cmap='gray')\n    plt.subplot(2, 4, 7) ; plt.imshow(imDecompressYCbCr[:,:,1] , cmap='gray')\n    plt.subplot(2, 4, 8) ; plt.imshow(imDecompressYCbCr[:,:,2] , cmap='gray')\n    plt.show()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Now to get an idea of what the quality factor is ...\nfor i, img in enumerate(os.listdir('../input/alaska2-image-steganalysis/Cover')[:10]):\n    #Let's have a look at the jpeg struct :\n    jpegStruct = jpio.read('../input/alaska2-image-steganalysis/Cover/' + img)\n    print(jpegStruct.quant_tables[0])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#You can note that this matrix is used in the decompression (function JPEGdecompressYCbCr) as follows:\n    #tmpPixels[idxRow*8:(idxRow+1)*8 , idxCol*8:(idxCol+1)*8] = np.dot( np.transpose(T) , np.dot( QM * D , T ) )\n#In brief, the JPEG (mainly) acts in three steps\n# 1. Transform color from RGB to YCbCr\n# 2. Split pixels (YCbCr) into blocks of 8x8 pixels denoted Pix and apply over each block the DCT transform \n#DCT = T * Pix * T'\n# Where T is an orthonormal basis change matrix (computed at the begining of function JPEGdecompressYCbCr)\n# 3. The final part consist in dividing each DCT coefs by a specific factor and then round \n#DCTquantized = round( DCT / QM) = round( ( T * Pix * T' ) / QM )\n# In order to get Pix from DCTquantized you have to undo all step (except quantization) which yields:\n# T' * DCTquantized * QM * T  Pix\n#which is what I do here :\n    #tmpPixels[idxRow*8:(idxRow+1)*8 , idxCol*8:(idxCol+1)*8] = np.dot( np.transpose(T) , np.dot( QM * D , T ) )\n# The largest the terms in quatization matrix QM, the largest the division and the more rough is the quantization,\n# such matrix is usually determined from a standard matrix as  follows\nqualityFactor = 95\nquality = 200 - qualityFactor*2\n\ntable0 = np.array(\n    [ [ 16,  11,  10,  16,  24,  40,  51,  61 ],\n    [ 12,  12,  14,  19,  26,  58,  60,  55 ],\n    [ 14,  13,  16,  24,  40,  57,  69,  56 ],\n    [ 14,  17,  22,  29,  51,  87,  80,  62 ],\n    [ 18,  22,  37,  56,  68, 109, 103,  77 ],\n    [ 24,  35,  55,  64,  81, 104, 113,  92 ],\n    [ 49,  64,  78,  87, 103, 121, 120, 101 ],\n    [ 72,  92,  95,  98, 112, 100, 103,  99 ] ] )\n\ntable1 = np.array(\n    [ [ 17,  18,  24,  47,  99,  99,  99,  99 ],\n    [ 18,  21,  26,  66,  99,  99,  99,  99 ],\n    [ 24,  26,  56,  99,  99,  99,  99,  99 ],\n    [ 47,  66,  99,  99,  99,  99,  99,  99 ],\n    [ 99,  99,  99,  99,  99,  99,  99,  99 ],\n    [ 99,  99,  99,  99,  99,  99,  99,  99 ],\n    [ 99,  99,  99,  99,  99,  99,  99,  99 ],\n    [ 99,  99,  99,  99,  99,  99,  99,  99 ] ] )\n\nQMY = np.floor( (table0 * quality + 50) /100 )\nQMY[QMY<1] = 1 \n\nQMC = np.floor( (table0 * quality + 50) /100 )\nQMC[QMC<1] = 1 \n\nprint(QMY)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}