Back to index

tetex-bin  3.0
jpeg.c
Go to the documentation of this file.
00001 /*  $Header$
00002 
00003     This is dvipdfm, a DVI to PDF translator.
00004     Copyright (C) 1998, 1999 by Mark A. Wicks
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019     
00020     The author may be contacted via the e-mail address
00021 
00022        mwicks@kettering.edu
00023 */
00024 
00025 #include <stdio.h>
00026 #include "system.h"
00027 #include "mem.h"
00028 #include "mfileio.h"
00029 #include "numbers.h"
00030 #include "dvi.h"
00031 #include "jpeg.h"
00032 #include "pdfobj.h"
00033 #include "pdfspecial.h"
00034 
00035 #define verbose 0
00036 
00037 #define SOF0  0xc0
00038 #define SOF1  0xc1
00039 #define SOF2  0xc2
00040 #define SOF3  0xc3
00041 #define SOF5  0xc5
00042 #define SOF6  0xc6
00043 #define SOF7  0xc7
00044 #define SOF9  0xc9
00045 #define SOF10 0xca
00046 #define SOF11 0xcb
00047 #define SOF13 0xcd
00048 #define SOF14 0xce
00049 #define SOF15 0xcf
00050 #define SOI   0xd8
00051 #define EOI   0xd9
00052 #define SOS   0xda
00053 #define COM   0xfe
00054 
00055 int check_for_jpeg(FILE *file)
00056 {
00057   rewind (file);
00058   if (get_unsigned_byte (file) != 0xff ||
00059       get_unsigned_byte (file) != SOI)
00060     return 0;
00061   return 1;
00062 }
00063 
00064 int jpeg_headers (struct jpeg *jpeg) 
00065 {
00066   UNSIGNED_BYTE byte;
00067   UNSIGNED_PAIR length;
00068   int i, done;
00069   done = 0;
00070   while (!done) {
00071     if ((byte = get_unsigned_byte (jpeg -> file)) != 0xff)
00072       return 0;
00073     while ((byte = get_unsigned_byte (jpeg -> file)) == 0xff);
00074     length = get_unsigned_pair (jpeg -> file);
00075     length -= 2;
00076     switch (byte) {
00077     case SOF0:
00078     case SOF1:
00079     case SOF2:
00080     case SOF3:
00081     case SOF5:
00082     case SOF6:
00083     case SOF7:
00084     case SOF9:
00085     case SOF10:
00086     case SOF11:
00087     case SOF13:
00088     case SOF14:
00089     case SOF15:
00090       jpeg -> bits_per_color = get_unsigned_byte (jpeg -> file);
00091       jpeg -> height = get_unsigned_pair (jpeg -> file);
00092       jpeg -> width = get_unsigned_pair (jpeg -> file);
00093       jpeg -> colors = get_unsigned_byte (jpeg -> file);
00094       if (verbose) {
00095        fprintf (stderr, "ht=%d,wd=%d,co=%d,bpc=%d\n",
00096                jpeg->height,jpeg->width,jpeg->colors,jpeg->bits_per_color);
00097       }
00098       
00099       done = 1;
00100       return 1;
00101     default:
00102       for (i=0; i<length; i++) {
00103        get_unsigned_byte(jpeg -> file);
00104       }
00105     }
00106   }
00107   return 0;                 /* Not reached */
00108 }
00109 
00110 struct jpeg *jpeg_open (FILE *file)
00111 {
00112   struct jpeg *jpeg;
00113   if (!check_for_jpeg(file)) {
00114     fprintf (stderr, "\nNot a JPEG file\n");
00115     return NULL;
00116   }
00117   jpeg = NEW (1, struct jpeg);
00118   jpeg -> file = file;
00119   if (!jpeg_headers(jpeg)) {
00120     fprintf (stderr, "\nCorrupt JPEG file?\n");
00121     RELEASE (jpeg);
00122     return NULL;
00123   }
00124   return jpeg;
00125 }
00126 
00127 void jpeg_close (struct jpeg *jpeg)
00128 {
00129   if (jpeg == NULL) {
00130     fprintf (stderr, "jpeg_closed: passed invalid pointer\n");
00131   }
00132   RELEASE (jpeg);
00133   return;
00134 }
00135 
00136