Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2003-2004, Yannick Verschueren
  3.  * Copyright (c) 2003-2004,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  *
  15.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  16.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25.  * POSSIBILITY OF SUCH DAMAGE.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include "j2k.h"
  33. #include "cio.h"
  34. #include "tcd.h"
  35. #include "int.h"
  36.  
  37. #define JPIP_JPIP 0x6a706970
  38.  
  39. #define JP2_JP   0x6a502020
  40. #define JP2_FTYP 0x66747970
  41. #define JP2_JP2H 0x6a703268
  42. #define JP2_IHDR 0x69686472
  43. #define JP2_COLR 0x636f6c72
  44. #define JP2_JP2C 0x6a703263
  45. #define JP2_URL  0x75726c20
  46. #define JP2_DBTL 0x6474626c
  47. #define JP2_BPCC 0x62706363
  48. #define JP2      0x6a703220
  49.  
  50.  
  51. void jp2_write_url(char *Idx_file)
  52. {
  53.   int len, lenp;
  54.   unsigned int i;
  55.   char str[256];
  56.  
  57.   sprintf(str, "%s", Idx_file);
  58.   lenp=cio_tell();
  59.   cio_skip(4);
  60.   cio_write(JP2_URL, 4);  // DBTL
  61.   cio_write(0,1);          // VERS
  62.   cio_write(0,3);          // FLAG
  63.  
  64.   for (i=0; i<strlen(str); i++) {
  65.         cio_write(str[i], 1);
  66.     }
  67.  
  68.   len=cio_tell()-lenp;
  69.   cio_seek(lenp);
  70.   cio_write(len,4);         // L
  71.   cio_seek(lenp+len);
  72. }
  73.  
  74. void jp2_write_dbtl(char *Idx_file)
  75. {
  76.   int len, lenp;
  77.  
  78.   lenp=cio_tell();
  79.   cio_skip(4);
  80.   cio_write(JP2_DBTL, 4);  // DBTL
  81.   cio_write(1,2);           // NDR : Only 1
  82.  
  83.   jp2_write_url(Idx_file); // URL Box
  84.  
  85.   len=cio_tell()-lenp;
  86.   cio_seek(lenp);
  87.   cio_write(len,4);         // L
  88.   cio_seek(lenp+len);
  89. }
  90.  
  91. int jp2_write_ihdr(j2k_image_t *j2k_img)
  92. {
  93.   int len, lenp,i;
  94.   int depth_0,depth, sign, BPC_ok=1;
  95.  
  96.   lenp=cio_tell();
  97.   cio_skip(4);
  98.   cio_write(JP2_IHDR, 4);  // IHDR
  99.  
  100.   cio_write(j2k_img->y1-j2k_img->x0,4);   // HEIGHT
  101.   cio_write(j2k_img->x1-j2k_img->x0,4);   // WIDTH
  102.   cio_write(j2k_img->numcomps,2);   // NC
  103.  
  104.   depth_0=j2k_img->comps[0].prec-1;
  105.   sign=j2k_img->comps[0].sgnd;
  106.  
  107.   for(i=1;i<j2k_img->numcomps;i++)
  108.     {
  109.       depth=j2k_img->comps[i].prec-1;
  110.       sign=j2k_img->comps[i].sgnd;
  111.       if(depth_0!=depth) BPC_ok=0;
  112.     }
  113.  
  114.   if (BPC_ok)
  115.     cio_write(depth_0+(sign<<7),1);
  116.   else
  117.     cio_write(255,1);
  118.  
  119.   cio_write(7,1);          // C : Always 7
  120.   cio_write(1,1);          // UnkC, colorspace unknow
  121.   cio_write(0,1);          // IPR, no intellectual property
  122.  
  123.   len=cio_tell()-lenp;
  124.   cio_seek(lenp);
  125.   cio_write(len,4);         // L
  126.   cio_seek(lenp+len);
  127.  
  128.   return BPC_ok;
  129. }
  130.  
  131. void jp2_write_bpcc(j2k_image_t *j2k_img)
  132. {
  133.   int len, lenp, i;
  134.  
  135.   lenp=cio_tell();
  136.   cio_skip(4);
  137.   cio_write(JP2_BPCC, 4);  // BPCC
  138.  
  139.   for(i=0;i<j2k_img->numcomps;i++)
  140.     cio_write(j2k_img->comps[i].prec-1+(j2k_img->comps[i].sgnd<<7),1);
  141.  
  142.   len=cio_tell()-lenp;
  143.   cio_seek(lenp);
  144.   cio_write(len,4);         // L
  145.   cio_seek(lenp+len);
  146. }
  147.  
  148. void jp2_write_colr(int BPC_ok, j2k_image_t *j2k_img)
  149. {
  150.   int len, lenp, meth;
  151.  
  152.   lenp=cio_tell();
  153.   cio_skip(4);
  154.   cio_write(JP2_COLR, 4);  // COLR
  155.  
  156.   if ((j2k_img->numcomps==1 || j2k_img->numcomps==3) && (BPC_ok && j2k_img->comps[0].prec==8))
  157.     meth=1;
  158.   else
  159.     meth=2;
  160.  
  161.   cio_write(meth,1);       // METH
  162.   cio_write(0,1);          // PREC
  163.   cio_write(0,1);          // APPROX
  164.  
  165.   if (meth==1)
  166.     cio_write(j2k_img->numcomps>1?16:17,4);          // EnumCS
  167.  
  168.   if (meth==2)
  169.     cio_write(0,1);        // PROFILE (??)
  170.  
  171.   len=cio_tell()-lenp;
  172.   cio_seek(lenp);
  173.   cio_write(len,4);         // L
  174.   cio_seek(lenp+len);
  175. }
  176.  
  177. /*
  178.  * Write the JP2H box
  179.  *
  180.  * JP2 Header box
  181.  *
  182.  */
  183. void jp2_write_jp2h(j2k_image_t *j2k_img)
  184. {
  185.   int len, lenp, BPC_ok;
  186.  
  187.   lenp=cio_tell();
  188.   cio_skip(4);
  189.   cio_write(JP2_JP2H, 4);           /* JP2H */
  190.  
  191.   BPC_ok=jp2_write_ihdr(j2k_img);
  192.  
  193.   if (!BPC_ok)
  194.     jp2_write_bpcc(j2k_img);
  195.   jp2_write_colr(BPC_ok, j2k_img);
  196.  
  197.   len=cio_tell()-lenp;
  198.   cio_seek(lenp);
  199.   cio_write(len,4);         /* L */
  200.   cio_seek(lenp+len);
  201. }
  202.  
  203. /*
  204.  * Write the FTYP box
  205.  *
  206.  * File type box
  207.  *
  208.  */
  209. void jp2_write_ftyp()
  210. {
  211.   int len, lenp;
  212.  
  213.   lenp=cio_tell();
  214.   cio_skip(4);
  215.   cio_write(JP2_FTYP, 4);   /* FTYP       */
  216.  
  217.   cio_write(JP2,4);         /* BR         */
  218.   cio_write(0,4);           /* MinV       */
  219.   cio_write(JP2,4);         /* CL0 : JP2  */
  220.   cio_write(JPIP_JPIP,4);   /* CL1 : JPIP */
  221.  
  222.   len=cio_tell()-lenp;
  223.   cio_seek(lenp);
  224.   cio_write(len,4);         /* L          */
  225.   cio_seek(lenp+len);
  226. }
  227.  
  228. /*
  229.  * Read the FTYP box
  230.  *
  231.  * File type box
  232.  *
  233.  */
  234. void jp2_read_ftyp(int length)
  235. {
  236.   int BR, MinV, type, i;
  237.  
  238.   BR = cio_read(4);         /* BR              */
  239.   MinV = cio_read(4);       /* MinV            */
  240.   length-=8;
  241.  
  242.   for (i=length/4;i>0;i--)
  243.     type = cio_read(4);     /* CLi : JP2, JPIP */
  244. }
  245.  
  246. int jp2_write_jp2c(char *J2K_file)
  247. {
  248.   int len, lenp, totlen, i;
  249.   FILE *src;
  250.   char *j2kfile;
  251.  
  252.   lenp=cio_tell();
  253.   cio_skip(4);
  254.   cio_write(JP2_JP2C, 4);  // JP2C
  255.  
  256.   src=fopen(J2K_file, "rb");
  257.   fseek(src, 0, SEEK_END);
  258.   totlen=ftell(src);
  259.   fseek(src, 0, SEEK_SET);
  260.  
  261.   j2kfile=(char*)malloc(totlen);
  262.   fread(j2kfile, 1, totlen, src);
  263.   fclose(src);
  264.  
  265.   for (i=0;i<totlen;i++)
  266.     cio_write(j2kfile[i],1);
  267.  
  268.   len=cio_tell()-lenp;
  269.   cio_seek(lenp);
  270.   cio_write(len,4);         // L
  271.   cio_seek(lenp+len);
  272.   return lenp;
  273. }
  274.  
  275. void jp2_write_jp()
  276. {
  277.   int len, lenp;
  278.  
  279.   lenp=cio_tell();
  280.   cio_skip(4);
  281.   cio_write(JP2_JP, 4);  // JP
  282.   cio_write(0x0d0a870a,4);
  283.   len=cio_tell()-lenp;
  284.   cio_seek(lenp);
  285.   cio_write(len,4);         // L
  286.   cio_seek(lenp+len);
  287. }
  288.  
  289. /*
  290.  * Read the JP box
  291.  *
  292.  * JPEG 2000 signature
  293.  *
  294.  * return 1 if error else 0
  295.  */
  296. int jp2_read_jp()
  297. {
  298.   if (0x0d0a870a!=cio_read(4))
  299.     return 1;
  300.   else
  301.     return 0;
  302. }
  303.