MFCを使ってPDFファイルにJPEG画像を組み込むには?


Junix  2003-07-17 13:00:42  No: 51743  IP: [192.*.*.*]

void CPdftestDlg::OnCreatepdf() 
{CPnPdf pdf;CString cmsg;int font;UpdateData(TRUE);if ( m_cpdfname == "" ) {AfxMessageBox("ファイル名を入力して下さい。");return;}if ( pdf.OpenFile(m_cpdfname) == 0 ) {pdf.SetPdfInfo("リンク先の情報");font = pdf.CreatePdfFont("#82l#82r#83S#83V#83b#83N",0,"EUC-H");
pdf.BeginPdfPage(2100,2970);cmsg = ConvJa("こんな感じ",CJ_SJIS_EUC);
pdf.ShowPdfTextBox(cmsg,400,400,0,0,"left","",font,64);pdf.EndPdfPage();pdf.CloseFile(-1);}}は文字をPDF化するのですがJPEGとGIFを入れたPDFを作ることが出来ません。どうしたらよいでしょうか?

編集 削除
pbmplus  2003-07-17 16:40:18  No: 51744  IP: [192.*.*.*]

CPnPdfって何ですか?
ライブラリを使用しているのでしたらライブラリ名を書かないと対処の使用がありません。 

<改行>
>void CPdftestDlg::OnCreatepdf() 
>{
>  CPnPdf pdf;
>  CString cmsg;
>  int font;
>
>  UpdateData(TRUE);
>
>  if ( m_cpdfname == "" ){
>     AfxMessageBox("ファイル名を入力して下さい。");
>     return;
>  }
>
>  if ( pdf.OpenFile(m_cpdfname) == 0 ) {
>    pdf.SetPdfInfo("リンク先の情報");
>    font = pdf.CreatePdfFont("#82l#82r#83S#83V#83b#83N",0,"EUC-H");
>    pdf.BeginPdfPage(2100,2970);
>    cmsg = ConvJa("こんな感じ",CJ_SJIS_EUC);
>    pdf.ShowPdfTextBox(cmsg,400,400,0,0,"left","",font,64);
>    pdf.EndPdfPage();
>    pdf.CloseFile(-1);
>  }
>}

編集 削除
Junix  2003-07-17 17:13:16  No: 51745  IP: [192.*.*.*]

// PnPdf.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "PnPdf.h"
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPnPdf
CPnTiffItem::CPnTiffItem()
{
  compress = 5;
  fillorder = 1;
  m_dat = NULL;
}

CPnTiffItem::~CPnTiffItem()
{
  if ( m_dat != NULL ) {
    delete m_dat;
    m_dat = NULL;
  }
}

CPnTiff::CPnTiff()
{
  m_cfname = _T("");
  fp = -1;
  m_ntcnt = 0;
  m_ncid = 0;
  m_njp = 0;
}

CPnTiff::~CPnTiff()
{
}

/*
int PdfSubInt(int *oi,char *iss,int spos,int inbf)
{
  int rc;

  if ( inbf == 0 ) {
    rc = PNetSubInt(oi,iss,spos);
  } else {
    rc = PNetSubIntn(oi,iss,spos);
  }
  return(rc);
}

int PdfSubShort(int *oi,char *iss,int spos,int inbf)
{
  int rc;

  if ( inbf == 0 ) {
    rc = PNetSubShort(oi,iss,spos);
  } else {
    rc = PNetSubShortn(oi,iss,spos);
  }
  return(rc);
}
*/

int CPnTiff::OpenFile(LPCSTR fname)
{
  CPnTiffItem *tItem;
  char fname1[250],buf[10];
  int i,stop,rc,pos,rpos,rpos1,tagn,id,inbf;

  inbf = 0;
  m_ntcnt = 0;
  rpos = 8;
  m_nts = 0;
  strcpy(fname1,fname);
  fp = _open(fname,_O_RDONLY|_O_BINARY);
  if ( fp < 0 ) {  
    char dv[10],fpath[200],ftitle[100],ext[10];

    _splitpath(fname,dv,fpath,ftitle,ext);
    if ( stricmp(ext,".img")==0 ) {
      sprintf(fname1,"%s%s%s.DOC",dv,fpath,ftitle);
      fp = _open(fname1,_O_RDONLY|_O_BINARY);
      if ( fp > -1 ) {
        _read(fp,fpath,12);
        PNetSubInt(&m_nts,fpath,0);
        m_nts += 12;
      }
    }
  }
  if ( fp > -1 ) {
    _lseek(fp,m_nts,SEEK_SET);
    _read(fp,buf,2);
    if ( strncmp(buf,"MM",2) == 0 ) {
      inbf = 1;
    }
    m_cfname = fname1;
    if ( m_njp == 0 ) {
      for ( stop=0;stop==0; ) {
        rc = _lseek(fp,rpos+m_nts,SEEK_SET);
        if ( (rpos+m_nts) != rc ) stop = 1;
        else {
          _lseek(fp,rpos+m_nts,SEEK_SET);
          _read(fp,buf,2);
          PdfSubShort(&tagn,buf,0,inbf);
          _lseek(fp,rpos+2+tagn*12+m_nts,SEEK_SET);
          rc = _read(fp,buf,4);
          if ( rc < 4 ) stop=1;
          else {
            PdfSubInt(&rpos1,buf,0,inbf);
            if ( rpos1 < 9 ) stop=1;
            tItem = new CPnTiffItem;
            for ( i=0;i<tagn;i++ ) {
              pos = rpos+2+i*12;
              _lseek(fp,pos+m_nts,SEEK_SET);
              _read(fp,buf,2);
              PdfSubShort(&id,buf,0,inbf);
              switch ( id ) {
              case 256:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->w,buf,0,inbf);
                break;
              case 257:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->h,buf,0,inbf);
                break;
              case 279:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->size,buf,0,inbf);
                break;
              case 273:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->pos,buf,0,inbf);
                break;
              case 259:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,2);
                PdfSubShort(&tItem->compress,buf,0,inbf);
                break;
              case 266:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,2);
                PdfSubShort(&tItem->fillorder,buf,0,inbf);
                break;
              }
            }
            tItem->no = m_ntcnt;
            m_titem.AddTail(tItem);
            rpos = rpos1;
            m_ntcnt++;
          }
        }
      }
    } else {
      for ( stop=0;stop==0; ) {
        rc = _lseek(fp,rpos+m_nts,SEEK_SET);
        if ( (rpos+m_nts) != rc ) stop = 1;
        else {
          _lseek(fp,rpos+m_nts,SEEK_SET);
          _read(fp,buf,2);
          PdfSubShort(&tagn,buf,0,inbf);
          _lseek(fp,rpos+2+tagn*12+m_nts,SEEK_SET);
          rc = _read(fp,buf,4);
          if ( rc < 4 ) stop=1;
          else {
            PdfSubInt(&rpos1,buf,0,inbf);
            if ( rpos1 < 9 ) stop=1;
            tItem = new CPnTiffItem;
            for ( i=0;i<tagn;i++ ) {
              pos = rpos+2+i*12;
              _lseek(fp,pos+m_nts,SEEK_SET);
              _read(fp,buf,2);
              PdfSubShort(&id,buf,0,inbf);
              switch ( id ) {
              case 256:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->w,buf,0,inbf);
                break;
              case 257:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->h,buf,0,inbf);
                break;
              case 279:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->size,buf,0,inbf);
                break;
              case 273:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,4);
                PdfSubInt(&tItem->pos,buf,0,inbf);
                break;
              case 259:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,2);
                PdfSubShort(&tItem->compress,buf,0,inbf);
                break;
              case 266:
                pos+=8;
                _lseek(fp,pos+m_nts,SEEK_SET);
                _read(fp,buf,2);
                PdfSubShort(&tItem->fillorder,buf,0,inbf);
                break;
              }
            }
            tItem->no = m_ntcnt;
            m_titem.AddTail(tItem);
            rpos += 260;
//            rpos = rpos1;
            m_ntcnt++;
          }
        }
      }
    }
    return(0);
  } else {
    return(-1);
  }
}

void CPnTiff::CloseFile()
{
  CPnTiffItem *tItem;
  POSITION pos;

  pos = m_titem.GetHeadPosition();
  while ( pos != NULL ) {
    tItem = m_titem.GetNext(pos);
    if ( tItem != NULL ) {
      delete tItem;
    }
  }
  m_titem.RemoveAll();
  if ( fp > -1 ) {
    _close(fp);
  }
}

CPnTiffItem *CPnTiff::GetTiffItem(int no)
{
  CPnTiffItem *tItem;
  POSITION pos;

  pos = m_titem.GetHeadPosition();
  while ( pos != NULL ) {
    tItem = m_titem.GetNext(pos);
    if ( tItem != NULL ) {
      if ( tItem->no == no ) {
        return(tItem);
      }
    }
  }
  return(NULL);
}

char *CPnTiff::GetTiffData(CPnTiffItem *tItem)
{
  char wmsg[10],*ret;
  int i;

  ret = new char[tItem->size];
  _lseek(fp,tItem->pos+m_nts,SEEK_SET);
  _read(fp,ret,tItem->size);
  if ( tItem->fillorder == 2 ) {
    for ( i=0;i<tItem->size;i++ ) {
      wmsg[0] = ret[i];
      wmsg[1] = 0;
      if ( bmc_qus(wmsg,0) == 1 ) bmc_set(wmsg,15);
      if ( bmc_qus(wmsg,1) == 1 ) bmc_set(wmsg,14);
      if ( bmc_qus(wmsg,2) == 1 ) bmc_set(wmsg,13);
      if ( bmc_qus(wmsg,3) == 1 ) bmc_set(wmsg,12);
      if ( bmc_qus(wmsg,4) == 1 ) bmc_set(wmsg,11);
      if ( bmc_qus(wmsg,5) == 1 ) bmc_set(wmsg,10);
      if ( bmc_qus(wmsg,6) == 1 ) bmc_set(wmsg,9);
      if ( bmc_qus(wmsg,7) == 1 ) bmc_set(wmsg,8);
      ret[i] = wmsg[1];
    } 
    tItem->fillorder = 1;
  }
  return(ret);
}

CPnObj::CPnObj()
{
  no = 0;
  type = 0;
  p1 = 0;
  p2 = 0;
  p3 = 0;
  p4 = 0;
  p5 = 0;
  p6 = 0;
  dat = _T("");
  s1 = _T("");
  s2 = _T("");
  s3 = _T("");
  s4 = _T("");
  s5 = _T("");
  s6 = _T("");
  int i;
  for ( i=0;i<10;i++ ) {
    xy[i] = 0;
  }
}

CPnObj::~CPnObj()
{
}

CPnPdf::CPnPdf()
{
  m_cfname = _T("");
  A4w = 595;
  A4h = 842;
  m_nobjcnt = 0;
  m_nfontcount = 0;
  m_nimgcount = 0;
}

CPnPdf::~CPnPdf()
{
}

int CPnPdf::OpenFile(LPCSTR fname)
{
  CPnObj *obj;

  m_cfname = fname;
  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 0;
  m_obj.AddTail(obj);
  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 1;
  m_obj.AddTail(obj);
  return(0);
}

void CPnPdf::CloseFile(int fp)
{
  CPnObj *obj;
  POSITION pos;
  CString txt,txt1;
  char *add;
  int len,alen;

  txt = MakePdf();
/*  add = NULL;
  alen = 0;
  len = txt.GetLength();
  if ( len < 10000 ) {
    alen = 10000 - len;
    add = (char *)malloc(alen+1);
    memset(add,' ',alen);
    *(add+alen) = 0;
    txt += add;
    free(add);
  }
*/
  if ( fp == -1 ) {
    fp = _open(m_cfname,_O_BINARY|_O_CREAT|_O_TRUNC|_O_RDWR,_S_IREAD|_S_IWRITE);
    if ( fp > -1 ) {
      _write(fp,(LPCSTR)txt,txt.GetLength());
      _close(fp);
    }
  } else {
    len = txt.GetLength();
    _write(fp,(char *)&len,sizeof(int));
    _write(fp,(LPCSTR)txt,txt.GetLength());
/*
    if ( len < 10000 ) {
      add = (char *)malloc(10000);
      memset(add,0x0a,10000);
      memcpy(add,(LPCSTR)txt,len);
      len = 10000;
      _write(fp,(char *)&len,sizeof(int));
      _write(fp,add,len);
      free(add);
    } else {
      _write(fp,(char *)&len,sizeof(int));
      _write(fp,(LPCSTR)txt,txt.GetLength());
    }
*/
  }
  pos = m_obj.GetHeadPosition();
  while ( pos != NULL ) {
    obj = m_obj.GetNext(pos);
    if ( obj != NULL ) {
      delete obj;
    }
  }
  m_obj.RemoveAll();
}

CPnObj *CPnPdf::GetObjByType(int type)
{
  CPnObj *obj;
  POSITION pos;

  pos = m_obj.GetHeadPosition();
  while ( pos != NULL ) {
    obj = m_obj.GetNext(pos);
    if ( obj != NULL ) {
      if ( obj->type == type ) {
        return(obj);
      }
    }
  }
  return(NULL);
}

void CPnPdf::SetPdfInfo(LPCSTR Keywords,LPCSTR Subject,LPCSTR Title,LPCSTR Creator,LPCSTR Author)
{
  CPnObj *obj;
    SYSTEMTIME stime;
  CString datetime;

  GetSystemTime(&stime);

  datetime.Format("%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d",
    stime.wYear,stime.wMonth,stime.wDay,
    stime.wHour,stime.wMinute,stime.wSecond);
  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 2;
  obj->s1 = Keywords;
  obj->s2 = Subject;
  obj->s3 = Title;
  obj->s4 = Creator;
  obj->s5 = Author;
  obj->s6 = datetime;
  m_obj.AddTail(obj);


void CPnPdf::BeginPdfPage(int w,int h)
{
  CPnObj *obj;

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 3;
  obj->p1 = w;
  obj->p2 = h;
  m_obj.AddTail(obj);
}

void CPnPdf::EndPdfPage()
{
  CPnObj *obj;

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 13;
  m_obj.AddTail(obj);
}

int CPnPdf::CreatePdfFont(LPCSTR font,int type,LPCSTR enc)
{
  CPnObj *obj;
  int nfcount;

  nfcount = m_nfontcount;
  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 4;
  obj->p1 = m_nfontcount++;
  obj->p2 = type;
  obj->s1 = font;
  obj->s2 = enc;
  m_obj.AddTail(obj);
  if ( nfcount == 0 ) {
    obj = new CPnObj;
    obj->no = ++m_nobjcnt;
    obj->type = 5;
    obj->s1.Format("%d",nfcount);
    m_obj.AddTail(obj);
  } else {
    CString cmsg;

    obj = GetObjByType(5);
    cmsg.Format(",%d",nfcount);
    obj->s1 += cmsg;
  }
  return(nfcount);
}

CString ConvSpcialChar(LPCSTR txt)
{
  CString ret;
  char wmsg[10];
  int i,len;

  ret = _T("");
  len = strlen(txt);
  for ( i=0;i<len;i++ ) {
    if ( (*(txt+i) == '(')||(*(txt+i) == ')') ) {
      ret += "\\";
    }
    wmsg[0] = *(txt+i);
    wmsg[1] = 0;
    ret += wmsg;
  }
  return(ret);
}

void CPnPdf::ShowPdfTextBox(LPCSTR txt,int x,int y,int n1,int n2,
      LPCSTR wpos,LPCSTR n3,int font,int size)
{
  CPnObj *obj;

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 10;
  obj->dat = ConvSpcialChar(txt);
  obj->p1 = x;
  obj->p2 = y;
  obj->p3 = n1;
  obj->p4 = n2;
  obj->p5 = font;
  obj->p6 = size;
  obj->s1 = wpos;
  obj->s2 = n3;
  m_obj.AddTail(obj);
}

void CPnPdf::ShowPdfDrawBox(int x,int y,int w,int h,int th)
{
  CPnObj *obj;

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 14;
  obj->xy[0] = x;
  obj->xy[1] = y;
  obj->xy[2] = x+w;
  obj->xy[3] = y;
  obj->xy[4] = x+w;
  obj->xy[5] = y+h;
  obj->xy[6] = x;
  obj->xy[7] = y+h;
  obj->xy[8] = x;
  obj->xy[9] = y;
  obj->p1 = th;
  obj->p2 = 5;
  m_obj.AddTail(obj);
}

void CPnPdf::ShowPdfDrawBmp(LPCSTR img,int imgs,int iw,int ih,int x,int y,int w,int h,int th)
{
  CPnObj *obj;

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->dat = CString(img,imgs);
  obj->type = 15;
  obj->xy[0] = iw;
  obj->xy[1] = ih;
  obj->xy[2] = x;
  obj->xy[3] = y;
  obj->xy[4] = w;
  obj->xy[5] = h;
  obj->p1 = th;
  obj->p2 = 5;
  m_obj.AddTail(obj);
}

int CPnPdf::CheckBaseFont(LPCSTR face)
{
  if ( strcmp(face,"Times-Roman")==0 ) {
    return(0);
  }
  return(1);
}

CString CPnPdf::GetBaseFont(int objno,LPCSTR face,LPCSTR enc,int type)
{
  CString ret;

  ret.Format("%d 0 obj\n"
        "<<\n" 
        "/Type /Font\n"
        "/Subtype /CIDFontType2\n"
        "/BaseFont /#82l#82r#83S#83V#83b#83N\n"
        "/WinCharSet 128\n"
        "/FontDescriptor %d 0 R\n"
        "/CIDSystemInfo\n"
        "<<\n"
        "/Registry(Adobe)\n"
        "/Ordering(Japan1)\n"
        "/Supplement 2\n"
        ">>\n"
        "/DW 1000\n"
        "/W [\n"
        "231 389 500\n"
        "631 631 500\n"
        "]\n"
        ">>\n" 
        "endobj\n",objno,objno+1);
  return(ret);
}

CString CPnPdf::GetFontName(int objno,LPCSTR face,LPCSTR enc,int type)
{
  CString ret;

  ret.Format("%d 0 obj\n"
        "<<\n" 
        "/Type /FontDescriptor\n"
        "/FontName /#82l#82r#83S#83V#83b#83N\n"
        "/Flags 7\n"
        "/FontBBox [ -100 -141 1100 1000 ]\n"
        "/MissingWidth 500\n"
        "/StemV 91\n"
        "/StemH 91\n"
        "/ItalicAngle 0\n"
        "/CapHeight 859\n"
        "/XHeight 430\n"
        "/Ascent 859\n"
        "/Descent -141\n"
        "/Leading 0\n"
        "/MaxWidth 1000\n"
        "/AvgWidth 500\n"
        "/Style\n"
        "<< /Panose <000000400000000000000000> >>\n"
        ">>\n"
        "endobj\n",objno);
  return(ret);
}

void CPnPdf::ShowPdfImageBox(LPCSTR img,int imgw,int imgh,int imgs,
        int swx,int swy,int sww,int swh,int flg)
{
  CPnObj *obj;
  int isno,skh,skw;

  if ( imgw * swh > imgh * sww ) {
    skh = imgh * sww / imgw;
    skw = sww;
  } else {
    skh = swh;
    skw = imgw * swh / imgh;
  }

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  isno = obj->no;
  obj->dat = CString(img,imgs);
  obj->type = 11;
  obj->p1 = imgw;
  obj->p2 = imgh;
  obj->p3 = imgs;
  obj->p4 = m_nimgcount;
  m_obj.AddTail(obj);

  obj = new CPnObj;
  obj->no = ++m_nobjcnt;
  obj->type = 12;
  obj->p1 = swx;
  obj->p2 = swy;
  obj->p3 = skw;
  obj->p4 = skh;
  obj->p5 = flg;
  obj->p6 = m_nimgcount;
  m_obj.AddTail(obj);
  m_nimgcount++;
}

CString CPnPdf::FindFontResouce(int fno)
{
  CPnObj *obj;
  POSITION pos;
  CString res;

  res = _T("");
  pos = m_obj.GetHeadPosition();
  while ( pos != NULL ) {
    obj = m_obj.GetNext(pos);
    if ( obj != NULL ) {
      if ( obj->type == 4 ) {
        if ( obj->p1 == fno ) {
          res.Format("/F%d %d 0 R\n",obj->p1,obj->no);
          pos = NULL;
        }
      }
    }
  }
  return(res);
}

unsigned char idbitc[] =  { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01 };

int bmc_iqus(char *ibit,int qus_no)
{
  unsigned char *bit;
  int byte;
  int bits;

  bit = (unsigned char *)ibit;
  byte = (int)(qus_no / 8);
  bits = (int)(qus_no % 8);
  if ((*(bit + byte) & idbitc[bits])==idbitc[bits]) {
    return(1);
  } else {
    return(0);
  }
}

CString CPnPdf::MakePdf()
{
  CPnObj *obj,*pobj;
  POSITION pos;
  CString ret,cmsg,objs,txt,gra,tres,ires;
  CString kids;
  int objno,objlen;
  int resouceno,xrpos;
  int hh;
  int objpos[30000],i,j;
  int contents[30000],ci;
  int page,ofs;

  ofs = 0;
  ret = _T("");
  obj = GetObjByType(3);
  if ( obj == NULL ) return(ret);
  hh = obj->p2;
// Init Parameter
  objno = 4;
  for ( i=0;i<30000;i++ ) {
    objpos[i] = 0;
    contents[i] = 0;
  }
  page = 0;
  ci = 0;
  kids = _T("");
// Header
  ret = _T("%PDF-1.3\n");
// Struct
  pos = m_obj.GetHeadPosition();
  while ( pos != NULL ) {
    obj = m_obj.GetNext(pos);
    if ( obj != NULL ) {
      if ( obj->type == 3 ) {
        pobj = obj;
        txt = _T("");
        gra = _T("");
        tres = _T("");
        ires = _T("");
        pobj->no = objno++;
      } else if ( obj->type == 4 ) {
// SetFontSource
        obj->no = objno;
        objpos[objno] = ret.GetLength();
        objs.Format("%d 0 obj\n",objno++);
        objs += _T("<</Type/Font\n");
        cmsg.Format("/Subtype/Type%d\n",obj->p2);
        objs += cmsg;
        cmsg.Format("/Encoding/%s\n",obj->s2);
        objs += cmsg;
        cmsg.Format("/BaseFont/%s\n",obj->s1);
        objs += cmsg;
        if ( CheckBaseFont(obj->s1) == 1 ) {
          cmsg.Format("/DescendantFonts [ %d 0 R ]\n",objno);
          objs += cmsg;
        }
        objs += _T(">>\nendobj\n");
        ret += objs;
        if ( CheckBaseFont(obj->s1) == 1 ) {
          objpos[objno] = ret.GetLength();
          objs = GetBaseFont(objno++,obj->s1,obj->s2,obj->p2);
          ret += objs;
          objpos[objno] = ret.GetLength();
          objs = GetFontName(objno++,obj->s1,obj->s2,obj->p2);
          ret += objs;
        }
        cmsg.Format("/F%d %d 0 R\n",obj->p1,obj->no);
        tres += cmsg;
      } else if ( obj->type == 11 ) {
// SetImageSource
        cmsg.Format("/I%d %d 0 R\n",obj->p4,objno);  //obj->no);
        ires += cmsg;
        objpos[objno] = ret.GetLength();
        obj->no = objno;
        cmsg.Format("%d 0 obj\n"
            "<</Type/XObject\n"
            "/Subtype/Image\n"
            "/Width %d\n"
            "/Height %d\n"
            "/BitsPerComponent 1\n"
            "/ColorSpace/DeviceGray\n"
            "/Filter[/CCITTFaxDecode]\n"
            "/DecodeParms[<</Columns %d/Rows %d/K -1>>]\n"
            "/Length %d\n>>\nstream\n",
            objno,obj->p1,obj->p2,obj->p1,obj->p2,obj->p3);
            ret += cmsg;
            ret += obj->dat;
        ret += "endstream\nendobj\n";
        objno++;
      } else if ( obj->type == 12 ) {
// ShowImageBox
        objs = _T("");
        objs += "q\n";
        cmsg.Format("%d 0 0 %d %d %d cm\n",obj->p3,obj->p4,obj->p1,hh - obj->p2 - obj->p4);
        objs += cmsg;
        cmsg.Format("/I%d Do\n",obj->p6);
        objs += cmsg;
        objs += "Q\n";
        objlen = objs.GetLength();
              
        objpos[objno] = ret.GetLength();
        contents[ci++] = objno;
        cmsg.Format("%d 0 obj\n<<\n/Length %d\n>>\n",objno,objlen);
        cmsg += "stream\n";
        ret += cmsg;
        ret += objs;
        ret += "endstream\n";
        ret += "endobj\n";
        objno++;
      } else if ( obj->type == 14 ) {
        if ( gra == "" ) {
          gra.Format("%d w\n",obj->p1);
        }
        for ( i=0;i<obj->p2;i++ ) {
          if ( i == 0 ) {
            cmsg.Format("%d %d m\n",obj->xy[i*2],hh-obj->xy[i*2+1]);
          } else {
            cmsg.Format("%d %d l\n",obj->xy[i*2],hh-obj->xy[i*2+1]);
          }
          gra += cmsg;
        }
        gra += _T("S\n");
      } else if ( obj->type == 15 ) {
        if ( gra == "" ) {
          gra.Format("%d w\n",obj->p1);
        }
        for ( i=0;i<obj->xy[1];i++ ) {
          for ( j=0;j<obj->xy[0];j++ ) {
            if ( bmc_iqus((char *)(LPCSTR)obj->dat,i*obj->xy[0]+j) == 1 ) {
              cmsg.Format("%d %d m\n",obj->xy[2]+j*obj->xy[4]/obj->xy[0],hh-obj->xy[3]-i*obj->xy[5]/obj->xy[1]-5);
              gra += cmsg;
              cmsg.Format("%d %d l\n",obj->xy[2]+j*obj->xy[4]/obj->xy[0],hh-obj->xy[3]-i*obj->xy[5]/obj->xy[1]-5);
              gra += cmsg;
            }
          }
        }
        gra += _T("S\n");
      } else if ( obj->type == 10 ) {
// ShowTextBox
        if ( txt == "" ) {
          txt.Format("/F%d %d Tf\n",obj->p5,obj->p6);
          ofs = obj->p6;
        } else {
          if ( ofs != obj->p6 ) {
            cmsg.Format("/F%d %d Tf\n",obj->p5,obj->p6);
            txt += cmsg;
            ofs = obj->p6;
          }
        }
        cmsg.Format("1 0 0 1 %d %d Tm\n",obj->p1,hh - obj->p2 - obj->p6);
        txt += cmsg;
        txt += "(";
        txt += obj->dat;
        txt += ")Tj\n";
        if ( tres == "" ) {
          tres = FindFontResouce(obj->p5);
        }
      } else if ( obj->type == 13 ) {
// Out Text Struct
        if ( txt != "" ) {
          objs = _T("");
          objs += "BT\n";
          objs += txt;
          objs += "ET\n";
          objlen = objs.GetLength();
              
          objpos[objno] = ret.GetLength();
          contents[ci++] = objno;
          cmsg.Format("%d 0 obj\n<<\n/Length %d\n>>\n",objno,objlen);
          cmsg += "stream\n";
          ret += cmsg;
          ret += objs;
          ret += "endstream\n";
          ret += "endobj\n";
          objno++;
        }
        if ( gra != "" ) {
          objlen = gra.GetLength();

          objpos[objno] = ret.GetLength();
          contents[ci++] = objno;
          cmsg.Format("%d 0 obj\n<<\n/Length %d\n>>\n",objno,objlen);
          cmsg += "stream\n";
          ret += cmsg;
          ret += gra;
          ret += "endstream\nendobj\n";
          objno++;
        }
// Make Page Resouce
        objpos[objno] = ret.GetLength();
        resouceno = objno;
        cmsg.Format("%d 0 obj\n",objno++);
        objs = cmsg;
        objs += _T("<</ProcSet[/PDF");
        if ( ires != "" ) {
          objs += _T("/ImageB");
        }
        if ( tres != "" ) {
          objs += _T("/Text");
        }
        objs += _T("]\n");
        if ( tres != "" ) {
          objs += _T("/Font<<\n");
          objs += tres;
          objs += _T(">>\n");
        }
        if ( ires != "" ) {
          objs += _T("/XObject<<\n");
          objs += ires;
          objs += _T(">>\n");
        }
        objs += _T(">>\nendobj\n");
        ret += objs;

        objs = _T("");
        objpos[pobj->no] = ret.GetLength();
        cmsg.Format("%d 0 R ",pobj->no);
        kids += cmsg;
        page++;
        cmsg.Format("%d 0 obj\n<<\n/Type/Page\n",pobj->no);
        objs += cmsg;
        objs += _T("/Parent 1 0 R\n");
        cmsg.Format("/Resources %d 0 R\n",resouceno);
        objs += cmsg;
        cmsg.Format("/MediaBox[0 0 %d %d]\n",pobj->p1,pobj->p2);
        objs += cmsg;
        cmsg.Format("/Contents[");
        objs += cmsg;
        for ( i=0;i<ci;i++ ) {
          cmsg.Format("%d 0 R ",contents[i]);
          objs += cmsg;
        }
        cmsg.Format("]\n>>\nendobj\n");
        objs += cmsg;
        ret += objs;
        ci = 0;
      }
    }
  }

// Page Data
  pos = m_obj.GetHeadPosition();
  while ( pos != NULL ) {
    obj = m_obj.GetNext(pos);
    if ( obj != NULL ) {
      if ( obj->type == 0 ) {
        objpos[1] = ret.GetLength();
        objs = _T("1 0 obj\n<<\n/Type/Pages\n");
        cmsg.Format("/Count %d\n",page);
        objs += cmsg;
        cmsg.Format("/Kids[%s]\n>>\nendobj\n",kids);
        objs += cmsg;
        ret += objs;
      } else if ( obj->type == 1 ) {
        objpos[2] = ret.GetLength();
        objs = _T("2 0 obj\n<<\n/Type/Catalog\n");
        objs += _T("/Pages 1 0 R\n");
        objs += _T(">>\nendobj\n");
        ret += objs;
      } else if ( obj->type == 2 ) {
        objpos[3] = ret.GetLength();
        objs = _T("3 0 obj\n<<\n");
        cmsg.Format("/Keywords (%s)\n",obj->s1);
        objs += cmsg;
        cmsg.Format("/Subject (%s)\n",obj->s2);
        objs += cmsg;
        cmsg.Format("/Title (%s)\n",obj->s3);
        objs += cmsg;
        cmsg.Format("/Creator (%s)\n",obj->s4);
        objs += cmsg;
        cmsg.Format("/Author (%s)\n",obj->s5);
        objs += cmsg;
        cmsg.Format("/CreationDate (D:%s)\n",obj->s6);
        objs += cmsg;
        objs += "/Producer ()\n";
        objs += ">>\nendobj\n";
        ret += objs;
      }
    }
  }
  
  xrpos = ret.GetLength();
  objs.Format("xref\n0 %d\n",objno);
  objs += _T("0000000000 65535 f \n"); 
  for ( i=1;i<objno;i++ ) {
    cmsg.Format("%10.10d 00000 n \n",objpos[i]);
    objs += cmsg; 
  }
  cmsg.Format("trailer\n<</Size %d\n/Info 3 0 R\n/Root 2 0 R\n>>\n",objno);
  objs += cmsg;
  cmsg.Format("startxref\n%d\n",xrpos);
  objs += cmsg;
  ret += objs;
  ret += _T("%%EOF\n");
  return(ret);
}

編集 削除
pbmplus  2003-07-17 20:42:44  No: 51746  IP: [192.*.*.*]

これは参った。降参。(> <)

えーと。ソースは全部見ていないのですが、
パッと見てたら「/Filter」が「/CCITTFaxDecode」しかないので
このライブラリだけではJPEG・GIFファイルを取り込むことは出来ないかと思います。

まあ、JPEGごときなら簡単ですので頑張って下さい。

編集 削除
Junix  2003-07-18 09:04:07  No: 51747  IP: [192.*.*.*]

おそらくDCTDecodeを組み込めば出来ると思いますが。
問題なのは if ( pdf.OpenFile(m_cpdfname) == 0 ) {
>    pdf.SetPdfInfo("リンク先の情報");
>    font = pdf.CreatePdfFont("#82l#82r#83S#83V#83b#83N",0,"EUC-H");
>    pdf.BeginPdfPage(2100,2970);
>    cmsg = ConvJa("こんな感じ",CJ_SJIS_EUC);
>    pdf.ShowPdfTextBox(cmsg,400,400,0,0,"left","",font,64);
>    pdf.EndPdfPage();
>    pdf.CloseFile(-1);
>  }
にJPEG画像を組み込む方法が分かりません。

編集 削除
pbmplus  2003-07-18 12:24:10  No: 51748  IP: [192.*.*.*]

あなたはこのライブラリの使い方が分からないのでしょうか。
それとも、自分で作成したライブラリに「JPEG画像を組み込む」
方法がわからないのでしょうか。質問の意味がイマイチ分かりません。

前者ならば私の手には負えません。ライブラリの付属文書などを読んでください。
後者ならばもう少し具体的に質問してください。

編集 削除
Junix  2003-07-18 13:16:24  No: 51749  IP: [192.*.*.*]

後者の方です。この方法ではcmsg = ConvJa("こんな感じ",CJ_SJIS_EUC);←この文字が含まれるPDFファイルの生成のみになってしまいます。JPEGやGIF・BMPなどの画像が含まれるPDFファイルの生成の仕方が分かりません。
obj->type = 11;を見てください。この様なPDFの画像書式をJPEGの場合どうなるのか?(DCTDecodeを使うところまではなんとなく分かります。)
また、その時pdf.SetPdfInfo("リンク先の情報");
>    font = pdf.CreatePdfFont("#82l#82r#83S#83V#83b#83N",0,"EUC-H");
>    pdf.BeginPdfPage(2100,2970);
>    cmsg = ConvJa("こんな感じ",CJ_SJIS_EUC);
>    pdf.ShowPdfTextBox(cmsg,400,400,0,0,"left","",font,64);
>    pdf.EndPdfPage();
>    pdf.CloseFile(-1);
>  }
はどう変形されるのかがいまいち分かりません。

編集 削除
pbmplus  2003-07-18 19:04:03  No: 51750  IP: [192.*.*.*]

JPEGファイルの場合は以下のようなオブジェクトを
作成して丸ごとstream ... endstreamの間に突っ込めば完了です。

ただし、突っ込む前に画像サイズ・カラースペースを取得しておく必要があります。

5 0 obj
<<
/Type /XObject
/Subtype /Image
/Name /Im0              % 変数名 
/Width 117 /Height 17   % 画像のサイズ
/BitsPerComponent 8     % ビット深度(とりあえず8のままで問題ないはず)
/Filter /DCTDecode      % 圧縮形式(DCT=JPEG)  
/ColorSpace /DeviceRGB  % カラースペース(RGB/CMYKの2種類ある)
/Length 1579            % ストリームのサイズ
>>
stream
 ...                    % ここにJPEGファイルを丸ごと突っ込む
endstream
endobj


# 情報が少ないのであなたのソースがどうこうとは言えません。

編集 削除
pbmplus  2003-07-18 19:12:12  No: 51751  IP: [192.*.*.*]

<以前、ここで配布されていたソース>

// JPEG2PDF,JPEGtoPDF,JPG2PDF,JPGToPDF

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

#define FALSE               0
#define TRUE                1

WORD  SwapEndian(WORD S);
DWORD GetFileSize(FILE *fp);
BOOL  CopyStream(FILE *Src,FILE *Dest);
BOOL  GetJPEGSize(FILE *JPGStream,WORD *AWidth,WORD *AHeight,BOOL *CMYK);
void  Write_CrossReferenceTable(FILE *AStream,DWORD ObjectPosArray[],int Count);
void  Write_ContentsObject(FILE *AStream,DWORD ObjectPosArray[],int *ObjectIndex,int w,int h);
int   JPGtoPDF(const char *OpenName,const char *SaveName);

int main(int argc,char *argv[])
{
   if (argc >= 2)
     return (JPGtoPDF(argv[1],"c:\\jpg2pdf.pdf"));
   else
   {
     printf("Not Found!\n");
     return(0);
   }
}

WORD SwapEndian(WORD S)
{
 return ((S & 0x00FF) << 8) | ((S & 0xFF00) >> 8)  ;
}

DWORD GetFileSize(FILE *fp)
{
 int Pos;
 DWORD Size;
 
 Pos =ftell(fp);
 fseek(fp, 0, SEEK_END );    
 Size = ftell(fp);    
 fseek(fp, Pos, SEEK_SET );
 return Size;
}

BOOL CopyStream(FILE *Src,FILE *Dest)
{
 BYTE  *buffer;
 int   Pos;
 DWORD FileSize;

 Pos =ftell(Src);
 FileSize=GetFileSize(Src);

 buffer=(BYTE *)malloc(FileSize);
 if (buffer==NULL) 
   return FALSE;
 fseek(Src,0,SEEK_SET);
 fread(buffer,1,FileSize,Src);       
 fwrite(buffer,1,FileSize,Dest);
 free(buffer);

 fseek(Src,Pos,SEEK_SET);
 return TRUE;
}

BOOL GetJPEGSize(FILE *JPGStream,WORD *AWidth,WORD *AHeight,BOOL *CMYK)
{
 WORD wrk;
 BYTE Sampling;

 WORD SOF0 =0xFFC0; /* Normal */
 WORD SOF2 =0xFFC2; /* Progressive */

 /* JFIF */
 if (fread(&wrk,2,1,JPGStream)<1) 
   return FALSE;

 if (SwapEndian(wrk)!=0xFFD8)
    return FALSE;      

 while (1)
 {
     if (fread(&wrk,2,1,JPGStream)<1) 
       return FALSE;
     wrk=SwapEndian(wrk);
     
     /* JPEG Maker */
     if ((wrk==SOF0) | (wrk==SOF2))
     {  
        /* Skip Segment Length  */
        if (fseek(JPGStream,ftell(JPGStream)+2,SEEK_SET)) 
         return FALSE;

        /* Skip Sample */
        if (fseek(JPGStream,ftell(JPGStream)+1,SEEK_SET)) 
         return FALSE;

        /* Height */
        if (fread(&wrk,2,1,JPGStream)<1) 
          return FALSE;
        *AHeight=SwapEndian(wrk);

        /* Width */          
        if (fread(&wrk,2,1,JPGStream)<1) 
         return FALSE;
        *AWidth=SwapEndian(wrk);
        
        /* ColorMode */
        if (fread(&Sampling,1,1,JPGStream)<1) 
          return FALSE;
        
        switch (Sampling)
        {
          case 3  : *CMYK = FALSE; break; /* RGB  */
          case 4  : *CMYK = TRUE ; break; /* CMYK */
          default : return FALSE;         /* ???  */ 
        }

        return TRUE; 
     }
     else if ((wrk==0xFFFF) | (wrk==0xFFD9))
     {
         return FALSE;  
     }

     /* Skip Segment */  
     if (fread(&wrk,2,1,JPGStream)<1) 
       return FALSE;
     
     if (fseek(JPGStream,ftell(JPGStream)+SwapEndian(wrk)-2,SEEK_SET )) 
       return FALSE;
 }
}

void Write_CrossReferenceTable(FILE *AStream,DWORD ObjectPosArray[],int Count)
{
   int i;

   fprintf(AStream,"xref\n");
   fprintf(AStream,"0 %d\n",Count+1);
   fprintf(AStream,"0000000000 65535 f \n");

   for (i= 0; i<=Count-1;i++)
      fprintf(AStream,"%0.10d 00000 n \n",ObjectPosArray[i]);
}

void Write_ContentsObject(FILE *AStream,DWORD ObjectPosArray[],int *ObjectIndex,int w,int h)
{
   int Length;

   /* Contents */
   ObjectPosArray[*ObjectIndex]  =(DWORD)ftell(AStream);
     fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1);
     fprintf(AStream,"<< /Length %d 0 R >>\n",*ObjectIndex+2);
     fprintf(AStream,"stream\n");

        /* stream */
        Length=ftell(AStream);
          fprintf(AStream,"q\n");
          fprintf(AStream,"%d 0 0 %d 0 0 cm\n",w,h);
          fprintf(AStream,"/Im0 Do\n");
          fprintf(AStream,"Q\n");
        Length=ftell(AStream)-Length;

     fprintf(AStream,"endstream\n");
     fprintf(AStream,"endobj\n");
   *ObjectIndex=*ObjectIndex+1;

   /* stream Length */
   ObjectPosArray[*ObjectIndex] =(DWORD)ftell(AStream);
     fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1);
     fprintf(AStream,"%d\n",Length);
     fprintf(AStream,"endobj\n");
   *ObjectIndex=*ObjectIndex+1;
}

int JPGtoPDF(const char *OpenName,const char *SaveName)
{
 BOOL  cmyk; 
 WORD  w,h;
 int   ObjectIndex;
 DWORD ObjectPosArray[10];
 FILE  *JPGStream,*AStream; 

    ObjectIndex=0;

    /* Open Jpeg File */
    JPGStream=fopen(OpenName,"rb");
    if(JPGStream==NULL)
    {
       printf("Error : Can not Open File.\n");
       return(-1);  
    }

    /* Get JPEG size */
    if (GetJPEGSize(JPGStream,&w,&h,&cmyk)==FALSE)
    {
       printf("Error : Can not get JPEG size.\n");
       return(-1);  
    }

    /* Create PDF File */
    AStream=fopen(SaveName,"wb+");
    if(AStream==NULL)
    {
        printf("Error : Can not Create File.\n");
        fclose(JPGStream);
        return(-1); 
    }

    /* ------------------------------------------------------------- */
    /*  Writting PDF                                                 */
    /* ------------------------------------------------------------- */

    /* PDF version */
    fprintf(AStream,"%%PDF-1.2\n");

    /* Catalog */
    ObjectPosArray[ObjectIndex] =ftell(AStream);
      fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
      fprintf(AStream,"<<\n");
      fprintf(AStream,"/Type /Catalog\n");
      fprintf(AStream,"/Pages 2 0 R\n");
      /* View Option (100%) */
      /*fprintf(AStream,"/OpenAction [3 0 R /XYZ -32768 -32768 1 ]\n"); */
      fprintf(AStream,">>\n");
      fprintf(AStream,"endobj\n");
    ObjectIndex++;

     /* Parent Pages */
     ObjectPosArray[ObjectIndex] =ftell(AStream);
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /Pages\n");
       fprintf(AStream,"/Kids [ 3 0 R ]\n");
       fprintf(AStream,"/Count 1\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

     /* Kids Page */
     ObjectPosArray[ObjectIndex] =ftell(AStream);
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /Page\n");
       fprintf(AStream,"/Parent 2 0 R\n");
       fprintf(AStream,"/Resources\n");
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/XObject << /Im0 4 0 R >>\n");
       fprintf(AStream,"/ProcSet [ /PDF /ImageC ]\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"/MediaBox [ 0 0 %d %d ]\n",w,h);
       fprintf(AStream,"/Contents 5 0 R\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

     /* XObject Resource */
     ObjectPosArray[ObjectIndex] =ftell(AStream); 
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /XObject\n");
       fprintf(AStream,"/Subtype /Image\n");
       fprintf(AStream,"/Name /Im0\n");
       fprintf(AStream,"/Width %d\n",w);
       fprintf(AStream,"/Height %d\n",h);
       fprintf(AStream,"/BitsPerComponent 8\n");
       fprintf(AStream,"/Filter [/DCTDecode]\n");
       if (cmyk==FALSE)
         fprintf(AStream,"/ColorSpace /DeviceRGB\n");
       else
       {
         fprintf(AStream,"/ColorSpace /DeviceCMYK\n");
         fprintf(AStream,"/Decode[1 0 1 0 1 0 1 0]\n"); /* Photoshop CMYK (NOT BIT) */
       }
       fprintf(AStream,"/Length %d >>\n",GetFileSize(JPGStream));
       fprintf(AStream,"stream\n");       
       if (CopyStream(JPGStream,AStream)==FALSE)
       {
          printf("Error : No Memory \n");
          return(-1);
       }
       fprintf(AStream,"endstream\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

    /* Contents Stream & Object */
    Write_ContentsObject(AStream,ObjectPosArray,&ObjectIndex,w,h);

    /* CrossReferenceTable */
    ObjectPosArray[ObjectIndex] =ftell(AStream);
    Write_CrossReferenceTable(AStream,ObjectPosArray,(int)ObjectIndex);

    /* trailer */
    fprintf(AStream,"trailer\n");
    fprintf(AStream,"<<\n");
    fprintf(AStream,"/Size %d\n",ObjectIndex+1);
    fprintf(AStream,"/Root 1 0 R\n");
    fprintf(AStream,">>\n");
    fprintf(AStream,"startxref\n");
    fprintf(AStream,"%d\n",ObjectPosArray[ObjectIndex]);
    fprintf(AStream,"%%%%EOF\n");

    fclose(JPGStream); fclose(AStream);

    printf("\nSuccess!\n");

    return (0);
}

編集 削除
Junix  2003-07-22 09:13:15  No: 51752  IP: [192.*.*.*]

お手数をおかけしました。
GIF画像の組み込みもJPEGと同じように行えば良いのでしょうか??

編集 削除
Junix  2003-07-22 11:28:10  No: 51753  IP: [192.*.*.*]

たびたび申し訳ありません。
上記の配布されてますソースを実行するためには、どうすればよいのでしょうか?

編集 削除
pbmplus  2003-07-22 12:40:19  No: 51754  IP: [192.*.*.*]

> GIF画像の組み込みもJPEGと同じように行えば良いのでしょうか??

それはちょっとわからないです。
(実際、自分で試してみたのですが、うまくいかなかったので途中で
 あきらめました・・・。この辺りはAcrobatで作成されるPDFファイル
 を分析・解析しないとわからないです。)

> 上記の配布されてますソースを実行するためには、どうすればよいのでしょうか?

[コンパイル]

Visual C++ 5.0/6.0
  Win32 Console Application でコンパイルします。
Borland C++ Compiler
  普通にコンパイル (「CPad for Borland C++ Compiler」を使っています)

# 「.Net」や「C++ Builder」「GNU」などは使ったことがないので分かりません。

[実行]

>int main(int argc,char *argv[])
>{
>   if (argc >= 2)
>     return (JPGtoPDF(argv[1],"c:\\jpg2pdf.pdf"));
>   else
>   {
>     printf("Not Found!\n");
>     return(0);
>   }
>}

メインがコレですので、プログラムの引数に「PDFファイルに変換したいJPEGのファイル名」を
渡せば良いかと思います。もちろん、作成されたEXEファイルにJPEGファイルをドロップしてもOKかと思います。

編集 削除
Junix  2003-07-22 13:34:40  No: 51755  IP: [192.*.*.*]

ありがとうございました。
GIFの方は解析してみます。
最後に、上記のPnPDF.cppとMFCを使った以下のソース:
// pdftestDlg.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "pdftest.h"
#include "pdftestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// アプリケーションのバージョン情報で使われている CAboutDlg ダイアログ

class CAboutDlg : public CDialog
{
public:
  CAboutDlg();

// ダイアログ データ
  //{{AFX_DATA(CAboutDlg)
  enum { IDD = IDD_ABOUTBOX };
  //}}AFX_DATA

  // ClassWizard は仮想関数のオーバーライドを生成します
  //{{AFX_VIRTUAL(CAboutDlg)
  protected:
  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV のサポート
  //}}AFX_VIRTUAL

// インプリメンテーション
protected:
  //{{AFX_MSG(CAboutDlg)
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
  //{{AFX_DATA_INIT(CAboutDlg)
  //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CAboutDlg)
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  //{{AFX_MSG_MAP(CAboutDlg)
    // メッセージ ハンドラがありません。
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPdftestDlg ダイアログ

CPdftestDlg::CPdftestDlg(CWnd* pParent /*=NULL*/)
  : CDialog(CPdftestDlg::IDD, pParent)
{
  //{{AFX_DATA_INIT(CPdftestDlg)
  m_cpdfname = _T("");
  //}}AFX_DATA_INIT
  // メモ: LoadIcon は Win32 の DestroyIcon のサブシーケンスを要求しません。
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPdftestDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CPdftestDlg)
  DDX_Text(pDX, IDC_PDFNAME, m_cpdfname);
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPdftestDlg, CDialog)
  //{{AFX_MSG_MAP(CPdftestDlg)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  ON_BN_CLICKED(IDC_CREATEPDF, OnCreatepdf)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPdftestDlg メッセージ ハンドラ

BOOL CPdftestDlg::OnInitDialog()
{
  CDialog::OnInitDialog();

  // "バージョン情報..." メニュー項目をシステム メニューへ追加します。

  // IDM_ABOUTBOX はコマンド メニューの範囲でなければなりません。
  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  ASSERT(IDM_ABOUTBOX < 0xF000);

  CMenu* pSysMenu = GetSystemMenu(FALSE);
  if (pSysMenu != NULL)
  {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
      pSysMenu->AppendMenu(MF_SEPARATOR);
      pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
  }

  // このダイアログ用のアイコンを設定します。フレームワークはアプリケーションのメイン
  // ウィンドウがダイアログでない時は自動的に設定しません。
  SetIcon(m_hIcon, TRUE);      // 大きいアイコンを設定
  SetIcon(m_hIcon, FALSE);    // 小さいアイコンを設定
  
  // TODO: 特別な初期化を行う時はこの場所に追加してください。
  
  return TRUE;  // TRUE を返すとコントロールに設定したフォーカスは失われません。
}

void CPdftestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
  if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
  }
  else
  {
    CDialog::OnSysCommand(nID, lParam);
  }
}

// もしダイアログボックスに最小化ボタンを追加するならば、アイコンを描画する
// コードを以下に記述する必要があります。MFC アプリケーションは document/view
// モデルを使っているので、この処理はフレームワークにより自動的に処理されます。

void CPdftestDlg::OnPaint() 
{
  if (IsIconic())
  {
    CPaintDC dc(this); // 描画用のデバイス コンテキスト

    SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

    // クライアントの矩形領域内の中央
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2;

    // アイコンを描画します。
    dc.DrawIcon(x, y, m_hIcon);
  }
  else
  {
    CDialog::OnPaint();
  }
}

// システムは、ユーザーが最小化ウィンドウをドラッグしている間、
// カーソルを表示するためにここを呼び出します。
HCURSOR CPdftestDlg::OnQueryDragIcon()
{
  return (HCURSOR) m_hIcon;
}

void CPdftestDlg::OnCreatepdf() 
{
  CPnPdf pdf;
  CString cmsg;
  int font;

  UpdateData(TRUE);
  if ( m_cpdfname == "" ) {
    AfxMessageBox("ファイル名を入力して下さい。");
    return;
  }
  if ( pdf.OpenFile(m_cpdfname) == 0 ) {
    pdf.SetPdfInfo("Patent","Patent Document","Title","PatentNet","SSE");    
    font = pdf.CreatePdfFont("#82l#82r#83S#83V#83b#83N",0,"EUC-H");
    pdf.BeginPdfPage(2100,2970);
    cmsg = ConvJa("練習",CJ_SJIS_EUC);
    //cmsg = ConvJa("jpgs1.jpg",);
    pdf.ShowPdfTextBox(cmsg,400,400,0,0,"left","",font,64);
    pdf.EndPdfPage();
    pdf.CloseFile(-1);}}


にJpegファイルを参照してPDF化を行うにはどのようにすれば良いのでしょうか?

編集 削除
pbmplus  2003-07-22 23:18:14  No: 51756  IP: [192.*.*.*]

JPEGファイルをPDFファイルに変換する方法は、JPEG2PDFのソースをご覧下さい。
また、「テキスト+イメージ」付きPDFファイルを作成する場合にもある程度は
役に立つと思いますのでそちらを参考にして頑張って下さい。

編集 削除
Junix  2003-07-24 09:47:34  No: 51757  IP: [192.*.*.*]

GIFの解析を色々してみたのですが、
PDFの書式:
100 0 obj
<<
/Type /XObject
/Subtype /Image
/Name /im14
/Filter /FlateDecode 
/Width 100
/Height 100
/BitsPerComponent 8
/ColorSpace [ /Indexed /DeviceRGB 255 99 0 R ]
/Length 101 0 R
>>
stream
画像データ(BMP?)
endstream
endobj
101 0 obj
1549
endobj
99 0 obj
<<
/Length 104 0 R
/Filter /ASCII85Decode 
>>
stream
GIFデータ
endstream
endobj
をJPEG2PDFのソースに組み込ませようと思いましたが、うまく動作しませんでした。BOOL GetJPEGSizeも変形しなければいけませんよね??

編集 削除
pbmplus  2003-07-24 12:27:23  No: 51758  IP: [192.*.*.*]

そりゃそうですよ。
GetJPEGSize()は「JPEGファイルから画像のサイズを取得する」
ものなのでGIFやBMPファイルの画像サイズを取得することは出来ません。

ちなみにBMPファイルは stream ... endstream 間に
「生データ」を入れないとダメですので注意してください。

編集 削除
Junix  2003-07-24 13:15:57  No: 51759  IP: [192.*.*.*]

ですよねー。ってことは、GetJPEGSizeと同じような方法でGIFについてもできるのでしょうか?
BMPの生データというのは文字化けしているデータを直書き込みということですか??

編集 削除
pbmplus  2003-07-24 16:44:46  No: 51760  IP: [192.*.*.*]

> GetJPEGSizeと同じような方法でGIFについてもできるのでしょうか?

似たようなやり方で出来ると思います。詳しくは下記を参照してください。

GIF Info(GIFの仕様書)
http://www.geocities.co.jp/SiliconValley/3453/gif_info/index_jp.html

> BMPの生データというのは文字化けしているデータを
> 直書き込みということですか??

データ的に言うとBMPファイル固有のヘッダやカラーパレットなどを
抜かした部分です。ただし、場合によっては倍数の計算やRLE圧縮の展開なども
少し絡むので慣れてないと扱うのが大変です。

編集 削除
Junix  2003-07-25 09:39:38  No: 51761  IP: [192.*.*.*]

GIFの仕様書を参考に変形してみたのですが、エラーが回避できません。
SwapEndianとGetJPEGSizeの所でつまづいてます。
どうかこの未熟者を助けてください!!

編集 削除
pbmplus  2003-07-25 19:22:05  No: 51762  IP: [192.*.*.*]

GIF画像サイズを取得する方法は?
http://madia.world.coocan.jp/cgi-bin/Vcbbs/wwwlng.cgi?print+200307/03070045.txt

にて解決してください。

編集 削除
Junix  2003-07-28 17:40:48  No: 51763  IP: [192.*.*.*]

BMPのサイズは取得したのですが、PDFに組み込む際にFlateDecodeの部分で問題が発生してしまいました。先日、生データを入れるということですが具体的に以下のソース:
/* XObject Resource */
      ObjectPosArray[ObjectIndex] =ftell(AStream); 
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /XObject\n");
       fprintf(AStream,"/Subtype /Image\n");
       fprintf(AStream,"/Name /Im0\n");
       fprintf(AStream,"/Width %d\n",width);
       fprintf(AStream,"/Height %d\n",height);
       fprintf(AStream,"/BitsPerComponent 4\n");
       fprintf(AStream,"/Filter /[FlateDecode]\n");
     //fprintf(AStream,"\n");
       fprintf(AStream,"/Length %d >>\n",GetFileSize(BMPStream));
       fprintf(AStream,"stream\n");       
       if (CopyStream(BMPStream,AStream)==FALSE)
       {
          printf("Error : No Memory \n");
          return(-1);
       }
       fprintf(AStream,"endstream\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;
で生データの挿入方法が分かりませんの教えてください。

編集 削除
pbmplus  2003-07-29 13:51:31  No: 51764  IP: [192.*.*.*]

フィルターが「無圧縮の場合は生データ」
フィルターが「FlateDecodeの場合は生データを圧縮したもの」
をstream ... endstream 間に入れる必要があります。

< シンタックスエラー >
  
  [誤] /Filter /[FlateDecode]
  [正] /Filter [/FlateDecode]

# 最初は24bitでテストしたほうが良いですよ。

編集 削除
pbmplus  2003-07-29 13:55:28  No: 51765  IP: [192.*.*.*]

24bitビットマップをPDFファイルに変換するソース(Delphi)

// BMP2PDF,BMPtoPDF 

unit bmp2pdf;

interface
uses
    Windows,Classes, Graphics, SysUtils;

    procedure BMPtoPDF(BMP:TBitmap;SaveName:String);

implementation

type
 pRGBArray = ^TRGBArray;
 TRGBArray = array[0..32768-1] of TRGBTriple;

procedure Write_CrossReferenceTable(AStream: TStream;PosArray : array of Dword;Count:Integer);
Var
   i :Integer;
begin
  With TStringStream(AStream) do
  begin
    WriteString('xref'#10);
    WriteString(Format('0 %d'#10,[Count+1]));
    WriteString('0000000000 65535 f '#10);
    for i:= 0 to Count-1 do
    begin
     WriteString(Format('%0.10d',[PosArray[i]])+' 00000 n '#10);
    end;
  end;
end;

procedure Write_ContentsObject(AStream: TStream;Index : Dword; Width,Height : Integer);
Var
   MemoryStream : TMemoryStream;
begin
  MemoryStream:=TMemoryStream.Create;
  Try
     // Stream
     With TStringStream(MemoryStream) do
     begin
       WriteString('q'#10);
         WriteString(Format('%d 0 0 %d 0 0 cm'#10,[Width,Height]));
         WriteString('/Im0 Do'#10);
       WriteString('Q'#10);
     end;

     MemoryStream.Position:=0;

     // Object
     With TStringStream(AStream) do
     begin
       WriteString(Format('%d 0 obj'#10,[Index]));
       WriteString(Format('<< /Length %d >>'#10,[MemoryStream.Size]));
       WriteString('stream'#10);
       AStream.CopyFrom(MemoryStream,MemoryStream.Size) ;
       WriteString('endstream'#10);
       WriteString('endobj'#10);
     end;
  finally
    MemoryStream.Free;
  end;
end;

procedure GetBitmapData(AStream :TStream;BMP:TBitmap);
var
 tmp :TBitmap;
 Buffer  : Pointer;
 SrcRow,DestRow  : pRGBArray;
 Row,Col,DestCnt : Integer;
begin

 DestCnt:=0;

 tmp := TBitmap.Create;
 tmp.Assign(BMP);
 tmp.PixelFormat:=pf24bit;

 GetMem(Buffer,tmp.Width*tmp.height*3);
 DestRow :=Buffer;

 try   
   for Row:=0 to tmp.Height-1 do
   begin
     SrcRow :=tmp.ScanLine[Row];
     for Col:=0 to tmp.Width-1 do
     begin
       DestRow[DestCnt].rgbtBlue  := SrcRow[Col].rgbtRed;
       DestRow[DestCnt].rgbtGreen := SrcRow[Col].rgbtGreen;
       DestRow[DestCnt].rgbtRed   := SrcRow[Col].rgbtBlue;
       Inc(DestCnt);
     end;
   end;

   AStream.Write(DestRow^,tmp.Width*tmp.Height*3);

  finally
     tmp.Free;
     FreeMem(Buffer);
  end;
end;

procedure BMPtoPDF(BMP:TBitmap;SaveName:String);
Var
  AStream,BitsData  : TStream;
  ObjectIndex  : Integer;
  ObjectPosArray  : array [0..10] of Dword;
begin

  if BMP=nil then
   raise  Exception.Create('Bitmap is nil');

  if SaveName='' then
   raise  Exception.Create('SaveName is nil');

  ObjectIndex :=0;

  AStream  :=TFileStream.Create(SaveName,fmCreate)  ;
  BitsData :=TMemorySTream.Create;
  Try          
     GetBitmapData(BitsData,BMP);
     BitsData.Position:=0;

     // PDF version
     TStringStream(AStream).WriteString('%PDF-1.2'#10);

     // Catalog
     ObjectPosArray[ObjectIndex] :=AStream.Position;
     With TStringStream(AStream) do
     begin
          WriteString(Format('%d 0 obj'#10,[ObjectIndex+1]));
          WriteString('<<'#10);
          WriteString('/Type /Catalog'#10);
          WriteString('/Pages 2 0 R'#10);
          // View Option (100%) 
          WriteString('/OpenAction [3 0 R /XYZ -32768 -32768 1 ]'#10);       
          WriteString('>>'#10);
          WriteString('endobj'#10);
     end;
     Inc(ObjectIndex);
   
     // Parent Pages
     ObjectPosArray[ObjectIndex] :=AStream.Position;
     With TStringStream(AStream) do
     begin
          WriteString(Format('%d 0 obj'#10,[ObjectIndex+1]));
          WriteString('<<'#10);
          WriteString('/Type /Pages'#10);
          WriteString('/Kids [ 3 0 R ]'#10);
          WriteString('/Count 1'#10);
          WriteString('>>'#10);         
          WriteString('endobj'#10);
     end;
     Inc(ObjectIndex);

     // Kids Page
     ObjectPosArray[ObjectIndex] :=AStream.Position;
     With TStringStream(AStream) do
     begin
          WriteString(Format('%d 0 obj'#10,[ObjectIndex+1]));
          WriteString('<<'#10);
          WriteString('/Type /Page'#10);
          WriteString('/Parent 2 0 R'#10);
          WriteString('/Resources'#10);
          WriteString('<<'#10);
          WriteString('/XObject << /Im0 4 0 R >>'#10);
          WriteString('/ProcSet [ /PDF /ImageC ]'#10);
          WriteString('>>'#10);
          WriteString(Format('/MediaBox [ 0 0 %d %d ]'#10, [BMP.Width,BMP.Height]));
          WriteString('/Contents 5 0 R'#10);
          WriteString('>>'#10);
          WriteString('endobj'#10);
     end;
     Inc(ObjectIndex);

     // XObject Resource
     ObjectPosArray[ObjectIndex] :=AStream.Position;   
     With TStringStream(AStream) do
     begin
          WriteString(Format('%d 0 obj'#10,[ObjectIndex+1]));
          WriteString('<<'#10);
          WriteString('/Type /XObject'#10);
          WriteString('/Subtype /Image'#10);
          WriteString('/Name /Im0'#10);
          WriteString(Format('/Width %d'#10,[BMP.Width]));
          WriteString(Format('/Height %d'#10,[BMP.Height]));
          WriteString('/BitsPerComponent 8'#10);
          WriteString('/Filter []'#10);
          WriteString('/ColorSpace /DeviceRGB'#10);
          WriteString(Format('/Length %d >>'#10,[BitsData.Size]));
          WriteString('stream'#10);
          AStream.CopyFrom(BitsData,BitsData.Size);
          WriteString('endstream'#10);
          WriteString('endobj'#10);
     end;
     Inc(ObjectIndex);

     // Contents Stream & Object
     ObjectPosArray[ObjectIndex] :=AStream.Position;
     With TStringStream(AStream) do
     begin
        Write_ContentsObject(AStream,ObjectIndex+1,BMP.Width,BMP.Height);
     end;
     Inc(ObjectIndex);

     // CrossReferenceTable
     ObjectPosArray[ObjectIndex] :=AStream.Position;
     Write_CrossReferenceTable(AStream,ObjectPosArray,ObjectIndex);

     // trailer
     With TStringStream(AStream) do
     begin
         WriteString('trailer'#10);
         WriteString('<<'#10);
         WriteString(Format('/Size %d'#10,[ObjectIndex+1]));
         WriteString('/Root 1 0 R'#10);
         WriteString('>>'#10);
         WriteString('startxref'#10);
         WriteString(Format('%d'#10,[ObjectPosArray[ObjectIndex]]));
         WriteString('%%EOF');
     end;

  finally
    AStream.Free;
    BitsData.Free;
  end;
end;

end.

編集 削除
Junix  2003-07-29 19:00:32  No: 51766  IP: [192.*.*.*]

// BMPを組み込んだPDFファイルの作成

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

#define FALSE               0
#define TRUE                1

WORD  SwapEndian(WORD S);
DWORD GetFileSize(FILE *fp);
BOOL  CopyStream(FILE *Src,FILE *Dest);
BOOL  GetBMPSize(FILE *BMPStream,long *offset);
void  Write_CrossReferenceTable(FILE *AStream,DWORD ObjectPosArray[],int Count);
void  Write_ContentsObject(FILE *AStream,DWORD ObjectPosArray[],int *ObjectIndex,int w,int h);
int   BMPtoPDF(const char *OpenName,const char *SaveName);

int main(int argc,char *argv[])
{
   if (argc >= 2)
     return (BMPtoPDF(argv[1],"c:\\bmppdf.pdf"));
   else
   {
     printf("Not Found!\n");
     return(0);
   }
}


/*WORD SwapEndian(WORD S)
{
 return ((S & 0x00FF) << 8) | ((S & 0xFF00) >> 8)  ;
}*/

DWORD GetFileSize(FILE *fp)
{
 int Pos;
 DWORD Size;
 
 Pos =ftell(fp);
 fseek(fp, 0, SEEK_END );    
 Size = ftell(fp);    
 fseek(fp, Pos, SEEK_SET );
 return Size;
}

BOOL CopyStream(FILE *Src,FILE *Dest)
{
 BYTE  *buffer;
 int   Pos;
 DWORD FileSize;

 Pos =ftell(Src);
 FileSize=GetFileSize(Src);

 buffer=(BYTE *)malloc(FileSize);
 if (buffer==NULL) 
   return FALSE;
 fseek(Src,0,SEEK_SET);
 fread(buffer,1,FileSize,Src);       
 fwrite(buffer,1,FileSize,Dest);
 free(buffer);

 fseek(Src,Pos,SEEK_SET);
 return TRUE;
}


BOOL GetBMPSize(FILE *BMPStream,long *offset)
{
 //WORD wrk;
 //BYTE Sampling;

 //WORD SOF0 =0xFFC0; /* 通常 */
 //WORD SOF2 =0xFFC2; /* 進歩 */

/*----------------------------*/
/* ファイルヘッダ部 (14 Byte) */
/*----------------------------*/

  int count = 0;
  long var_long;
  short var_short;
  char s[10];

  // BITMAP 認識文字 "BM"
  if (fread(s, 2, 1, BMPStream) == 1) {
    if (memcmp(s, "BM", 2) == 0) {
      printf("[BM] BITMAP file\n");
    } 
    else {
      fprintf(stderr, "%s : Not a BITMAP file\n", s);
      exit(1);
    }
    count += 2;
  }
  printf("  [BITMAPFILEHEADER]\n");
  // ファイルサイズ
  if (fread(&var_long, 4, 1, BMPStream) == 1) {
    printf("  Size          : %ld [Byte]\n", var_long);
    count += 4;
  }
  // 予約領域 0
  if (fread(&var_short, 2, 1, BMPStream) == 1) {
    count += 2;
  }
  // 予約領域 0
  if (fread(&var_short, 2, 1, BMPStream) == 1) {
    count += 2;
  }
  // ファイルの先頭から画像データまでの位置
  if (fread(&var_long, 4, 1, BMPStream) == 1) {
    printf("  OffBits       : %ld [Byte]\n", var_long);
    *offset = var_long;
    count += 4;
  }
  return count;
}

/*------------------------------------------------------------------*/
/* 情報ヘッダ部 (12 Byte -> OS/2 Bitmap, 40 Byte -> Windows Bitmap) */
/*------------------------------------------------------------------*/
int infoheader(FILE *BMPStream,
               short *infosize,
               long *width, long *height,
               long *x_coodinate, long *y_coodinate,
               short *BitCount, 
               long *ClrUsed)
{
  int count = 0;
  long var_long, compress = 0;
  short var_short;

  // BITMAPINFOHEADER のサイズ
  if (fread(&var_long, 4, 1, BMPStream) == 1) {
    count += 4;
    *infosize = var_long;
  }

  printf("  [BITMAPINFOHEADER]\n");
  // OS/2 Bitmap
  if (*infosize == 12) {
    // 画像データの幅
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  Width         : %d [pixel]\n", var_short);
      *width = var_short;
      count += 2;
    }
    // 画像データの高さ 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  Height        : %d [pixel]\n", var_short);
      *height = var_short;
      count += 2;
    }
    // プレーン数 (1のみ) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      count += 2;
    }
    // 1画素あたりのビット数 (1, 4, 8, 24, 32) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  BitCount      : %d [bit]\n", var_short);
      *BitCount = var_short;
      count += 2;
    }
  }
  // Windows BMP 
  else if (*infosize == 40) {
    // 画像データの幅 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  Width         : %ld [pixel]\n", var_long);
      *width = var_long;
      count += 4;
    }
    // 画像データの高さ
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  Height        : %ld [pixel]\n", var_long);
      *height = var_long;
      count += 4;
    }
    // プレーン数 (1のみ) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      count += 2;
    }
    // 1画素あたりのビット数 (1, 4, 8, 24, 32) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  BitCount      : %d [bit]\n", var_short);
      *BitCount = var_short;
      count += 2;
    }
    // 圧縮方式  0 : 無圧縮 
    //           1 : BI_RLE8 8bit RunLength 圧縮 
    //           2 : BI_RLE4 4bit RunLength 圧縮 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  Compression   : %ld\n", var_long);
      compress = var_long;
      count += 4;
    }
    // 画像データのサイズ 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  SizeImage     : %ld [Byte]\n", var_long);
      count += 4;
    }
    // 横方向解像度 (Pixel/meter) 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  XPelsPerMeter : %ld [pixel/m]\n", var_long);
      *x_coodinate = var_long;
      count += 4;
    }
    // 縦方向解像度 (Pixel/meter) 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  YPelsPerMeter : %ld [pixel/m]\n", var_long);
      *y_coodinate = var_long;
      count += 4;
    }
    // 使用色数 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  ClrUsed       : %ld [color]\n", var_long);
      *ClrUsed = var_long;
      count += 4;
    }
    // 重要な色の数 0の場合すべての色 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      count += 4;
    }
  } 
  else {
    fprintf(stderr, "Bitmap Info Header error\n");
  }

  if (compress != 0) {
    fprintf(stderr, "圧縮ビットマップには対応していません\n");
    exit(1);
  }
  if (*BitCount == 4 || *BitCount == 8 || *BitCount == 24 || *BitCount == 32) {
    ;
  } 
  else {
    fprintf(stderr, "%d ビット色には対応していません\n", *BitCount);
    exit(1);
  }
  return count;
}


/*-------------*/
/* OS/2 bitmap */
/*-------------*/
int rgbtriple(FILE *BMPStream,
              long used,
              unsigned char *red,
              unsigned char *green, 
              unsigned char *blue)
{
  long i;
  int count = 0;

  // ビットの並びは B G R 
  for (i = 0; i < used; i++) {
    blue[i] = fgetc(BMPStream);
    green[i] = fgetc(BMPStream);
    red[i] = fgetc(BMPStream);
    count++;
  }

  return count;
}

/*----------------*/
/* Windows bitmap */
/*----------------*/
int rgbquad(FILE *BMPStream,
            long used,
            unsigned char *red, 
            unsigned char *green, 
            unsigned char *blue)
{
  long i;
  int receive, count = 0;

  // ビットの並びは B G R 予約 
  for (i = 0; i < used; i++) {
    blue[i] = fgetc(BMPStream);
    green[i] = fgetc(BMPStream);
    red[i] = fgetc(BMPStream);
    receive = fgetc(BMPStream);
    count++;
  }
  return count;
}


void Write_CrossReferenceTable(FILE *AStream,DWORD ObjectPosArray[],int Count)
{
   int i;

   fprintf(AStream,"xref\n");
   fprintf(AStream,"0 %d\n",Count+1);
   fprintf(AStream,"0000000000 65535 f \n");

   for (i= 0; i<=Count-1;i++)
      fprintf(AStream,"%0.10d 00000 n \n",ObjectPosArray[i]);
}

void Write_ContentsObject(FILE *AStream,DWORD ObjectPosArray[],int *ObjectIndex,int w,int h)
{
   int Length;

   /* PDFコンテンツ */
   ObjectPosArray[*ObjectIndex]  =(DWORD)ftell(AStream);
     fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1);
     fprintf(AStream,"<< /Length %d 0 R >>\n",*ObjectIndex+2);
     fprintf(AStream,"stream\n");

        /* stream */
        Length=ftell(AStream);
          fprintf(AStream,"q\n");
          fprintf(AStream,"%d 0 0 %d 0 0 cm\n",w,h);
          fprintf(AStream,"/Im0 Do\n");
          fprintf(AStream,"Q\n");
        Length=ftell(AStream)-Length;

     fprintf(AStream,"endstream\n");
     fprintf(AStream,"endobj\n");
   *ObjectIndex=*ObjectIndex+1;

   /* stream Length */
   ObjectPosArray[*ObjectIndex] =(DWORD)ftell(AStream);
     fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1);
     fprintf(AStream,"%d\n",Length);
     fprintf(AStream,"endobj\n");
   *ObjectIndex=*ObjectIndex+1;
}

int BMPtoPDF(const char *OpenName,const char *SaveName)
{
 int   ObjectIndex;
 short infosize, bits;
 long used = 0, color = 0, offset, width, height, xreso, yreso;
 unsigned char red[256],blue[256],green[256];
 DWORD ObjectPosArray[10];
 FILE  *BMPStream,*AStream; 

    ObjectIndex=0;

    /* BMPファイルを開く*/
    BMPStream=fopen(OpenName,"rb");
    if(BMPStream==NULL)
    {
       printf("Error : Can not Open File.\n");
       return(-1);  
    }

  // ヘッダ情報 BITMAPFILEHEADER 
   GetBMPSize(BMPStream, &offset);
   // ヘッダ情報 BITMAPINFOHEADER 
   infoheader(BMPStream, &infosize, &width, &height, &xreso, &yreso, &bits, &color);
   //color = usedcolor(bits, color);

 // OS/2 Bitmap 
  if (infosize == 12) {
    if (bits == 1 || bits == 4 || bits == 8) {
      used = rgbtriple(BMPStream, color, red, green, blue);
    }
    printf("[OS/2 bitmap] --- %d bit %ld color\n", bits, used);
  }
  // Windows Bitmap 
  else if (infosize == 40) {
    if (bits == 1 || bits == 4 || bits == 8) {
      used = rgbquad(BMPStream, color, red, green, blue);
    }
    printf("[Windows bitmap] --- %d bit %ld color\n", bits, used);
  } 
  else if (infosize == 108) {
    printf("[other bitmap]\n");
  }
  else {
    fprintf(stderr, "BITMAP INFOHEADER error\n");
  }

    /* PDFファイル作成 */
    AStream=fopen(SaveName,"wb+");
    if(AStream==NULL)
    {
        printf("Error : Can not Create File.\n");
        fclose(BMPStream);
        return(-1); 
    }



    /* ------------------------------------------------------------- */
    /*  Writting PDF (PDFの書式)                                     */
    /* ------------------------------------------------------------- */

    /* PDF のバージョン */
    fprintf(AStream,"%%PDF-1.2\n");

    /* Catalog */
    ObjectPosArray[ObjectIndex] =ftell(AStream);
      fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
      fprintf(AStream,"<<\n");
      fprintf(AStream,"/Type /Catalog\n");
      fprintf(AStream,"/Pages 2 0 R\n");
      /* View Option (100%) */
      /*fprintf(AStream,"/OpenAction [3 0 R /XYZ -32768 -32768 1 ]\n"); */
      fprintf(AStream,">>\n");
      fprintf(AStream,"endobj\n");
    ObjectIndex++;

     /* Parent Pages */
     ObjectPosArray[ObjectIndex] =ftell(AStream);
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /Pages\n");
       fprintf(AStream,"/Kids [ 3 0 R ]\n");
       fprintf(AStream,"/Count 1\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

     /* Kids Page */
     ObjectPosArray[ObjectIndex] =ftell(AStream);
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /Page\n");
       fprintf(AStream,"/Parent 2 0 R\n");
       fprintf(AStream,"/Resources\n");
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/XObject << /Im0 4 0 R >>\n");
       fprintf(AStream,"/ProcSet [ /PDF /ImageC ]\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"/MediaBox [ 0 0 %d %d ]\n",width,height);
       fprintf(AStream,"/Contents 5 0 R\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

     /* XObject Resource */
      ObjectPosArray[ObjectIndex] =ftell(AStream); 
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /XObject\n");
       fprintf(AStream,"/Subtype /Image\n");
       fprintf(AStream,"/Name /Im0\n");
       fprintf(AStream,"/Width %d\n",width);
       fprintf(AStream,"/Height %d\n",height);
     //fprintf(AStream,"/BitsPerComponent 1\n");
     //fprintf(AStream,"/ColorSpace/DeviceGray\n");
     //fprintf(AStream,"/Filter[/CCITTFaxDecode]\n");
     //fprintf(AStream,"/DecodeParms[<</Columns %d/Rows %d/K -1>>]\n");
       //fprintf(AStream,"/BitsPerComponent 4\n");
       //fprintf(AStream,"/Filter [/FlateDecode]\n");
     fprintf(AStream,"/BitsPerComponent 8\n");
       fprintf(AStream,"/Filter []\n");
     //fprintf(AStream,"\n");
       fprintf(AStream,"/Length %d >>\n",GetFileSize(BMPStream));
       fprintf(AStream,"stream\n");
      

       if (CopyStream(BMPStream,AStream)==FALSE)
       {
          printf("Error : No Memory \n");
          return(-1);
       }
       fprintf(AStream,"endstream\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

    /* Contents Stream & Object */
    Write_ContentsObject(AStream,ObjectPosArray,&ObjectIndex,width,height);

    /* CrossReferenceTable */
    ObjectPosArray[ObjectIndex] =ftell(AStream);
    Write_CrossReferenceTable(AStream,ObjectPosArray,(int)ObjectIndex);

    /* trailer */
    fprintf(AStream,"trailer\n");
    fprintf(AStream,"<<\n");
    fprintf(AStream,"/Size %d\n",ObjectIndex+1);
    fprintf(AStream,"/Root 1 0 R\n");
    fprintf(AStream,">>\n");
    fprintf(AStream,"startxref\n");
    fprintf(AStream,"%d\n",ObjectPosArray[ObjectIndex]);
    fprintf(AStream,"%%%%EOF\n");

    fclose(BMPStream); fclose(AStream);

    printf("\nSuccess!\n");

    return (0);
}
これを見ておかしい所を指摘してもらえませんか??

編集 削除
pbmplus  2003-07-30 22:33:57  No: 51767  IP: [192.*.*.*]

非常に雑ですが8bitと24bitのビットマップを
PDFファイルに変換できるようにいじってみました。試してみてください。

// BMP2PDF (8bit/24bitのみ対応)

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

#define FALSE               0
#define TRUE                1

BOOL  GetBMPSize(FILE *BMPStream,long *offset);
void  Write_CrossReferenceTable(FILE *AStream,DWORD ObjectPosArray[],int Count);
void  Write_ContentsObject(FILE *AStream,DWORD ObjectPosArray[],int *ObjectIndex,int w,int h);
int   BMPtoPDF(const char *OpenName,const char *SaveName);

int main(int argc,char *argv[])
{
   if (argc >= 2)
     return (BMPtoPDF(argv[1],"c:\\bmppdf.pdf"));
   else
   {
     printf("Not Found!\n");
     return(0);
   }   
}


/* 1ラインの幅を算出 */
int GetLineWidth(WORD biBitCount, int biWidth)
{
  return ((biBitCount * biWidth + 15) / 16) * 2 ;
}

/* イメージデータのサイズを算出 */
int GetBitsSize(int biWidth, int biHeight,WORD biBitCount)
{
    switch (biBitCount)
    {   
       case     1: return ( ((1 * biWidth + 15) / 16) * 2 * (biHeight) );break;
       case     4: return ( ((4 * biWidth + 15) / 16) * 2 * (biHeight) );break;
       case     8: return ( biWidth*biHeight )  ;break;
       case    24: return ( biWidth*biHeight*3 );break;
       default   : return ( 0 );break;
    }
}

/* イメージデータの取得 */
BOOL GetBitsData(FILE *AStream,int bfOffBits,BYTE *BitsData, int biWidth, int biHeight,WORD biBitCount)
{
 int i;
 BYTE Dummy[4],DummyCnt;

 fseek(AStream,bfOffBits,SEEK_SET);

 switch (biBitCount)
 {   
   case     1:;
   case     4:;
   case     8: 
                DummyCnt= biWidth % 4;
                switch (DummyCnt)
                {
                   case 0:
                       for (i=0;i< biHeight;i++)
                           fread(BitsData+(biWidth*i),1,biWidth,AStream); 
                       break;
                   case 1 :;
                   case 2 :;
                   case 3 :;
                       for (i=0;i< biHeight;i++)
                       {
                           fread(BitsData+(biWidth*i),1,biWidth,AStream); 
                           fread(Dummy,1,4-DummyCnt,AStream); 
                       }
                       break;
                   default: return (FALSE);                
                }
                break;
       
       ;break;
   case    24: 
                DummyCnt= biWidth % 4;
                switch (DummyCnt)
                {
                   case 0:
                       for (i=0;i< biHeight;i++)
                           fread(BitsData+(biWidth*3*i),1,biWidth*3,AStream); 
                       break;
                   case 1 :;
                   case 2 :;
                   case 3 :
                       for (i=0;i< biHeight;i++)
                       {
                           fread(BitsData+(biWidth*3*i),1,biWidth*3,AStream); 
                           fread(Dummy,1,DummyCnt,AStream); 
                       }
                       break;
                   default: return (FALSE);                
                }
                break;
 }
 return (TRUE);
}

/* イメージデータを反転させる */
BOOL TurnBits(BYTE *BitsData,int BitsSize,int biWidth,WORD biBitCount)
{
 int i,j,LineWidth=0;
 BYTE *buffer,*Linebuffer,tmp;

 // 1ラインの幅を算出
 switch (biBitCount)
 {   
   case     1: ;
   case     4: LineWidth=GetLineWidth(biBitCount,biWidth);break;
   case     8: LineWidth=biWidth;  break;
   case    24: LineWidth=biWidth*3;break;
 } 

 Linebuffer=(BYTE *)malloc(LineWidth);
 if (Linebuffer==NULL) 
   return FALSE;

 buffer=(BYTE *)malloc(BitsSize);
 if (buffer==NULL)
 {
   free(Linebuffer);
   return FALSE;
 }

 if (biBitCount==24)
 {
     for (i=0;i< (BitsSize / LineWidth);i++)
     {
         /* 1ラインを取得 */
        memcpy(Linebuffer,BitsData+(LineWidth*i),LineWidth); 
   
        /* BGR<->RGB変換 */  
        for (j=0;j<LineWidth ;j+=3)
        {
          tmp             = Linebuffer[j];
          Linebuffer[j]   = Linebuffer[j+2];
          Linebuffer[j+2] = tmp;
        }
        /* 1ラインをメモリに書き込む */
        memcpy(buffer+ (BitsSize- LineWidth * (i+1) ),Linebuffer,LineWidth);
     }
 }
 else
 {  
     for (i=0;i< (BitsSize / LineWidth);i++)
     {
        /* 1ラインを取得 */
        memcpy(Linebuffer,BitsData+(LineWidth*i),LineWidth); 
        /* 1ラインをメモリに書き込む */
        memcpy(buffer+ (BitsSize- LineWidth * (i+1) ),Linebuffer,LineWidth);
     }     
 }

 // 元のデータと置き換える
 memcpy(BitsData,buffer,BitsSize);

 free(buffer); free(Linebuffer);

 return(TRUE);
}

BOOL GetBMPSize(FILE *BMPStream,long *offset)
{
 //WORD wrk;
 //BYTE Sampling;

 //WORD SOF0 =0xFFC0; /* 通常 */
 //WORD SOF2 =0xFFC2; /* 進歩 */

/*----------------------------*/
/* ファイルヘッダ部 (14 Byte) */
/*----------------------------*/

  int count = 0;
  long var_long;
  short var_short;
  char s[10];

  // BITMAP 認識文字 "BM"
  if (fread(s, 2, 1, BMPStream) == 1) {
    if (memcmp(s, "BM", 2) == 0) {
      printf("[BM] BITMAP file\n");
    } 
    else {
      fprintf(stderr, "%s : Not a BITMAP file\n", s);
      exit(1);
    }
    count += 2;
  }
  printf("  [BITMAPFILEHEADER]\n");
  // ファイルサイズ
  if (fread(&var_long, 4, 1, BMPStream) == 1) {
    printf("  Size          : %ld [Byte]\n", var_long);
    count += 4;
  }
  // 予約領域 0
  if (fread(&var_short, 2, 1, BMPStream) == 1) {
    count += 2;
  }
  // 予約領域 0
  if (fread(&var_short, 2, 1, BMPStream) == 1) {
    count += 2;
  }
  // ファイルの先頭から画像データまでの位置
  if (fread(&var_long, 4, 1, BMPStream) == 1) {
    printf("  OffBits       : %ld [Byte]\n", var_long);
    *offset = var_long;
    count += 4;
  }
  return count;
}

/*------------------------------------------------------------------*/
/* 情報ヘッダ部 (12 Byte -> OS/2 Bitmap, 40 Byte -> Windows Bitmap) */
/*------------------------------------------------------------------*/
int infoheader(FILE *BMPStream,
               short *infosize,
               long *width, long *height,
               long *x_coodinate, long *y_coodinate,
               short *BitCount, 
               long *ClrUsed)
{
  int count = 0;
  long var_long, compress = 0;
  short var_short;

  // BITMAPINFOHEADER のサイズ
  if (fread(&var_long, 4, 1, BMPStream) == 1) {
    count += 4;
    *infosize = var_long;
  }

  printf("  [BITMAPINFOHEADER]\n");
  // OS/2 Bitmap
  if (*infosize == 12) {
    // 画像データの幅
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  Width         : %d [pixel]\n", var_short);
      *width = var_short;
      count += 2;
    }
    // 画像データの高さ 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  Height        : %d [pixel]\n", var_short);
      *height = var_short;
      count += 2;
    }
    // プレーン数 (1のみ) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      count += 2;
    }
    // 1画素あたりのビット数 (1, 4, 8, 24, 32) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  BitCount      : %d [bit]\n", var_short);
      *BitCount = var_short;
      count += 2;
    }
  }
  // Windows BMP 
  else if (*infosize == 40) {
    // 画像データの幅 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  Width         : %ld [pixel]\n", var_long);
      *width = var_long;
      count += 4;
    }
    // 画像データの高さ
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  Height        : %ld [pixel]\n", var_long);
      *height = var_long;
      count += 4;
    }
    // プレーン数 (1のみ) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      count += 2;
    }
    // 1画素あたりのビット数 (1, 4, 8, 24, 32) 
    if (fread(&var_short, 2, 1, BMPStream) == 1) {
      printf("  BitCount      : %d [bit]\n", var_short);
      *BitCount = var_short;
      count += 2;
    }
    // 圧縮方式  0 : 無圧縮 
    //           1 : BI_RLE8 8bit RunLength 圧縮 
    //           2 : BI_RLE4 4bit RunLength 圧縮 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  Compression   : %ld\n", var_long);
      compress = var_long;
      count += 4;
    }
    // 画像データのサイズ 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  SizeImage     : %ld [Byte]\n", var_long);
      count += 4;
    }
    // 横方向解像度 (Pixel/meter) 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  XPelsPerMeter : %ld [pixel/m]\n", var_long);
      *x_coodinate = var_long;
      count += 4;
    }
    // 縦方向解像度 (Pixel/meter) 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  YPelsPerMeter : %ld [pixel/m]\n", var_long);
      *y_coodinate = var_long;
      count += 4;
    }
    // 使用色数 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      printf("  ClrUsed       : %ld [color]\n", var_long);
      *ClrUsed = var_long;
      count += 4;
    }
    // 重要な色の数 0の場合すべての色 
    if (fread(&var_long, 4, 1, BMPStream) == 1) {
      count += 4;
    }
  } 
  else {
    fprintf(stderr, "Bitmap Info Header error\n");
  }

  if (compress != 0) {
    fprintf(stderr, "圧縮ビットマップには対応していません\n");
    exit(1);
  }
  if (*BitCount == 4 || *BitCount == 8 || *BitCount == 24 || *BitCount == 32) {
    ;
  } 
  else {
    fprintf(stderr, "%d ビット色には対応していません\n", *BitCount);
    exit(1);
  }
  return count;
}


/*-------------*/
/* OS/2 bitmap */
/*-------------*/
int rgbtriple(FILE *BMPStream,
              long used,
              unsigned char *red,
              unsigned char *green, 
              unsigned char *blue)
{
  long i;
  int count = 0;

  // ビットの並びは B G R 
  for (i = 0; i < used; i++) {
    blue[i] = fgetc(BMPStream);
    green[i] = fgetc(BMPStream);
    red[i] = fgetc(BMPStream);
    count++;
  }

  return count;
}

/*----------------*/
/* Windows bitmap */
/*----------------*/
int rgbquad(FILE *BMPStream,
            long used,
            unsigned char *red, 
            unsigned char *green, 
            unsigned char *blue)
{
  long i;
  int receive, count = 0;

  // ビットの並びは B G R 予約 
  for (i = 0; i < used; i++) {
    blue[i] = fgetc(BMPStream);
    green[i] = fgetc(BMPStream);
    red[i] = fgetc(BMPStream);
    receive = fgetc(BMPStream);
    count++;
  }
  return count;
}


void Write_CrossReferenceTable(FILE *AStream,DWORD ObjectPosArray[],int Count)
{
   int i;

   fprintf(AStream,"xref\n");
   fprintf(AStream,"0 %d\n",Count+1);
   fprintf(AStream,"0000000000 65535 f \n");

   for (i= 0; i<=Count-1;i++)
      fprintf(AStream,"%0.10d 00000 n \n",ObjectPosArray[i]);
}

void Write_ContentsObject(FILE *AStream,DWORD ObjectPosArray[],int *ObjectIndex,int w,int h)
{
   int Length;

   /* PDFコンテンツ */
   ObjectPosArray[*ObjectIndex]  =(DWORD)ftell(AStream);
     fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1);
     fprintf(AStream,"<< /Length %d 0 R >>\n",*ObjectIndex+2);
     fprintf(AStream,"stream\n");

        /* stream */
        Length=ftell(AStream);
          fprintf(AStream,"q\n");
          fprintf(AStream,"%d 0 0 %d 0 0 cm\n",w,h);
          fprintf(AStream,"/Im0 Do\n");
          fprintf(AStream,"Q\n");
        Length=ftell(AStream)-Length;

     fprintf(AStream,"endstream\n");
     fprintf(AStream,"endobj\n");
   *ObjectIndex=*ObjectIndex+1;

   /* stream Length */
   ObjectPosArray[*ObjectIndex] =(DWORD)ftell(AStream);
     fprintf(AStream,"%d 0 obj\n",*ObjectIndex+1);
     fprintf(AStream,"%d\n",Length);
     fprintf(AStream,"endobj\n");
   *ObjectIndex=*ObjectIndex+1;
}

int BMPtoPDF(const char *OpenName,const char *SaveName)
{
 int   ObjectIndex;
 short infosize, bits;
 long used = 0, color = 0, offset, width, height, xreso, yreso;
 unsigned char red[256],blue[256],green[256];
 DWORD ObjectPosArray[10];
 FILE  *BMPStream,*AStream;

 int i,j=0;
 DWORD BitsSize;          // イメージデータのサイズ
 BYTE *BitsData;          // イメージデータ
 BYTE  BitsPalette[768];  // カラーパレット 


    ObjectIndex=0;

    /* BMPファイルを開く*/
    BMPStream=fopen(OpenName,"rb");
    if(BMPStream==NULL)
    {
       printf("Error : Can not Open File.\n");
       return(-1);  
    }

   // ヘッダ情報 BITMAPFILEHEADER 
   GetBMPSize(BMPStream, &offset);
   // ヘッダ情報 BITMAPINFOHEADER 
   infoheader(BMPStream, &infosize, &width, &height, &xreso, &yreso, &bits, &color);
   //color = usedcolor(bits, color);

 // OS/2 Bitmap 
  if (infosize == 12) {
    if (bits == 1 || bits == 4 || bits == 8) {
      used = rgbtriple(BMPStream, color, red, green, blue);
    }
    printf("[OS/2 bitmap] --- %d bit %ld color\n", bits, used);
  }
  // Windows Bitmap 
  else if (infosize == 40) {
    if (bits == 1 || bits == 4 || bits == 8) {
      used = rgbquad(BMPStream, color, red, green, blue);
    }
    printf("[Windows bitmap] --- %d bit %ld color\n", bits, used);
  } 
  else if (infosize == 108) {
    printf("[other bitmap]\n");
  }
  else {
    fprintf(stderr, "BITMAP INFOHEADER error\n");
  }


    /* とりあえず、8bitと24bitのみ*/
    if (!(bits == 8 || bits == 24))
    {
       printf("Error : 対応していない形式です\n");
       fclose(BMPStream);
       return(-1);  
    }  
  
    /* PDFファイル作成 */
    AStream=fopen(SaveName,"wb+");
    if(AStream==NULL)
    {
        printf("Error : Can not Create File.\n");
        fclose(BMPStream);
        return(-1); 
    }

    /* ------------------------------------------------------------- */
    /*  Writting PDF (PDFの書式)                                     */
    /* ------------------------------------------------------------- */

    /* PDF のバージョン */
    fprintf(AStream,"%%PDF-1.2\n");

    /* Catalog */
    ObjectPosArray[ObjectIndex] =ftell(AStream);
      fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
      fprintf(AStream,"<<\n");
      fprintf(AStream,"/Type /Catalog\n");
      fprintf(AStream,"/Pages 2 0 R\n");
      /* View Option (100%) */
      fprintf(AStream,"/OpenAction [3 0 R /XYZ -32768 -32768 1 ]\n");
      fprintf(AStream,">>\n");
      fprintf(AStream,"endobj\n");
    ObjectIndex++;

     /* Parent Pages */
     ObjectPosArray[ObjectIndex] =ftell(AStream);
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /Pages\n");
       fprintf(AStream,"/Kids [ 3 0 R ]\n");
       fprintf(AStream,"/Count 1\n");
       fprintf(AStream,">>\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

     /* Kids Page */
     ObjectPosArray[ObjectIndex] =ftell(AStream);
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /Page\n");
       fprintf(AStream,"/Parent 2 0 R\n");
       fprintf(AStream,"/Resources\n");
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/XObject << /Im0 4 0 R >>\n");

       // ProcSet
       switch (bits)
       {   
         case 4  :;
         case 8  : fprintf(AStream,"/ProcSet [ /PDF /ImageI ]\n");break;
         default : fprintf(AStream,"/ProcSet [ /PDF /ImageC ]\n");break;
       }       

       fprintf(AStream,">>\n");
       fprintf(AStream,"/MediaBox [ 0 0 %d %d ]\n",width,abs(height));

       // コンテンツのオブジェクト番号
       switch (bits)
       {   
         case 4  : ;
         case 8  : fprintf(AStream,"/Contents 6 0 R\n");break;
         default : fprintf(AStream,"/Contents 5 0 R\n");break;
       }           

       fprintf(AStream,">>\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

      /* XObject Resource */
      ObjectPosArray[ObjectIndex] =ftell(AStream); 
       fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
       fprintf(AStream,"<<\n");
       fprintf(AStream,"/Type /XObject\n");
       fprintf(AStream,"/Subtype /Image\n");
       fprintf(AStream,"/Name /Im0\n");
       fprintf(AStream,"/Width %d\n",width);
       fprintf(AStream,"/Height %d\n",abs(height));

       fprintf(AStream,"/Filter []\n");

       /* BitsPerComponent */
       switch (bits)
       {   
         case 1  : fprintf(AStream,"/BitsPerComponent 1\n");break; 
         case 4  : fprintf(AStream,"/BitsPerComponent 4\n");break;
         default : fprintf(AStream,"/BitsPerComponent 8\n");break;
       }                   

       /* ColorSpace */
       switch (bits)
       {   
          case     1   : fprintf(AStream,"/ColorSpace /DeviceGray\n");break; 
          case     4   : ;
          case     8   : fprintf(AStream,"/ColorSpace [/Indexed /DeviceRGB 255 %d 0 R]\n",ObjectIndex+2); break;
          case     24  : fprintf(AStream,"/ColorSpace /DeviceRGB\n");break;
       }
       
       /* イメージデータのサイズを算出 */
       BitsSize =GetBitsSize(width, abs(height),bits);
       fprintf(AStream,"/Length %d >>\n",BitsSize);

       /*  イメージデータの書き込み */       
       fprintf(AStream,"stream\n");
          
         /* 生データを突っ込む */     
         BitsData=(BYTE *)malloc(BitsSize);
          GetBitsData(BMPStream,offset,BitsData,width,height,bits);  
          TurnBits(BitsData,BitsSize,width,bits);
          fwrite(BitsData,1,BitsSize,AStream);
         free(BitsData);

       fprintf(AStream,"\nendstream\n");
       fprintf(AStream,"endobj\n");
     ObjectIndex++;

     // カラーパレットの書き込み
     if (bits==4 || bits==8)
     {
         for (i=0;i<768;i+=3)
         {
            BitsPalette[i]  = red[j];
            BitsPalette[i+1]= green[j];
            BitsPalette[i+2]= blue[j];
            j++;  
         }

        ObjectPosArray[ObjectIndex] =ftell(AStream); 
          fprintf(AStream,"%d 0 obj\n",ObjectIndex+1);
          fprintf(AStream,"<<\n");
          fprintf(AStream,"/Length 768\n");
          fprintf(AStream,"/Filter []\n");
          fprintf(AStream,">>\n");
          fprintf(AStream,"stream\n");
          fwrite(BitsPalette,1,768,AStream);
          fprintf(AStream,"\nendstream\n");
          fprintf(AStream,"endobj\n");
        ObjectIndex++;     
     }

    /* Contents Stream & Object */
    Write_ContentsObject(AStream,ObjectPosArray,&ObjectIndex,width,height);

    /* CrossReferenceTable */
    ObjectPosArray[ObjectIndex] =ftell(AStream);
    Write_CrossReferenceTable(AStream,ObjectPosArray,(int)ObjectIndex);

    /* trailer */
    fprintf(AStream,"trailer\n");
    fprintf(AStream,"<<\n");
    fprintf(AStream,"/Size %d\n",ObjectIndex+1);
    fprintf(AStream,"/Root 1 0 R\n");
    fprintf(AStream,">>\n");
    fprintf(AStream,"startxref\n");
    fprintf(AStream,"%d\n",ObjectPosArray[ObjectIndex]);
    fprintf(AStream,"%%%%EOF\n");

    fclose(BMPStream); fclose(AStream);

    printf("\nSuccess!\n");

    return (0);  
}

編集 削除
Junixいまssづ  2003-08-04 12:50:25  No: 51768  IP: [192.*.*.*]

ありがとうございました!!これで、JpegとBMPの理解がかなり深まりました。解決しました。

GIFについては勉強中ですが、Jpeg・BMP同様Filterの部分(Decode)でつまずいています。

編集 削除
Junix  2003-08-14 17:44:29  No: 51769  IP: [192.*.*.*]

上記のプログラムを低水準ファイル入出力で書き換えたいのですがうまくいきません。どうすれば良いでしょうか??

編集 削除