Class: Sogoumap2pdf::ImageToPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/sogoumap2pdf/image_to_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(link_end, page_width, page_height) ⇒ ImageToPdf

Returns a new instance of ImageToPdf.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sogoumap2pdf/image_to_pdf.rb', line 10

def initialize(link_end,page_width,page_height)
  #@page_width=600
  @page_width=page_width.to_i
  #@page_width=800
  #@page_height=768
  @page_height=page_height.to_i
  #@page_height=480
  @page_overlap=50
  @text_height=15
  @pdf=PDF::Writer.new(:paper=>[0,0,@page_width,@page_height])
  @pdf.margins_pt(0,0,0,0)
  # initialize icons
  @icon_size=20
  @link_end=link_end
end

Instance Method Details

#big_img_crop(x, y, w, h) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sogoumap2pdf/image_to_pdf.rb', line 25

def big_img_crop(x,y,w,h)
  g=@g
  nx0=x/256
  ny0=y/256
  nxn=(x+w)/256+1
  nyn=(y+h)/256+1
  delta_x=x-nx0*256
  delta_y=y-ny0*256
  # p "x #{x}"
  # p "y #{y}"
  # p "delta_x #{delta_x}"
  # p "delta_y #{delta_y}"
  ci=ImageList.new # image list to hold results
  page=Rectangle.new( 0, 0, 0, 0)
  
  for i in nx0..nxn
    for j in ny0..nyn
      #p "big_img_crop #{i},#{j}"
      picture_file_name="~/.sogoumap/0/"+ [g.resource_type,g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+g.link_end
      picture_file_name=File.expand_path(picture_file_name)
      if File.exist?(picture_file_name) && File.size(picture_file_name)!= 0 then
        tumb=ImageList.new.read(picture_file_name)[0] # image list to hold results
        path_file_name="~/.sogoumap/0/"+ ["179",g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+".PNG"
        path_file_name=File.expand_path(path_file_name)
  
        if g.satellite_path && File.exist?(path_file_name) && File.size(path_file_name)!=0 then
  
          path_tumb=ImageList.new.read(path_file_name)[0] # image list to hold results
          tumb.composite!(path_tumb, 0, 0, OverCompositeOp)
        end
  
        #append tumb
        ci << tumb
        #update page
        page.x=(i-nx0)*256
        page.y=(nyn-j)*256
        ci.page=page
      else
      end
    end
  end
  if ci.length==0 then
    resimg=ImageList.new.read("NULL:white") { self.size = "#{g.npx*@tumb_x}x#{g.npy*@tumb_y}"}
  else
    resimg=ci.mosaic
  end
  return resimg.crop(SouthWestGravity,delta_x,delta_y,w,h,true)
end

#create_pages(g) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sogoumap2pdf/image_to_pdf.rb', line 84

def create_pages(g)
  # get the size of the image
  size_x=@big_img_width
  size_y=@big_img_height
  
  # calculate the number of requred rows and columns
  cols=(size_x-@page_overlap)/(@page_width-@page_overlap)
  rows=(size_y-@page_overlap)/(@page_height-@page_overlap)
  # create pdf
  for i in 0...rows
    for j in 0...cols
      p "creating page #{i} #{j}"
      p Time.now
      @pdf.new_page
      @pdf.add_destination("map part #{i},#{j}", "Fit")
      pgimg=big_img_crop(j*(@page_width-@page_overlap),
                           (rows-1-i)*(@page_height-@page_overlap),@page_width,@page_height)
  
      # pgimg=big_image.crop(j*(@page_width-@page_overlap),
      #                      i*(@page_height-@page_overlap),@page_width,@page_height,true)
      pgimg.write("itptmp#{@link_end}") { self.quality = 70 }
      @pdf.add_image_from_file("itptmp#{@link_end}",0,0)
      ss = PDF::Writer::StrokeStyle.new(4,:cap=>:round)
      @pdf.stroke_style ss
      if j>0 then
        @pdf.add_internal_link("map part #{i},#{j-1}",0,@page_height/2,
                               @icon_size,@page_height/2+@icon_size)
        @pdf.line(@icon_size,@page_height/2,0,@page_height/2+@icon_size/2).stroke
        @pdf.line(0,@page_height/2+@icon_size/2,@icon_size,@page_height/2+@icon_size).stroke
      end
      if j<cols-1 then
        @pdf.add_internal_link("map part #{i},#{j+1}",
                               @page_width-@icon_size,@page_height/2,
                               @page_width,@page_height/2+@icon_size)
        @pdf.line(@page_width-@icon_size,@page_height/2,
                  @page_width,@page_height/2+@icon_size/2).stroke
        @pdf.line(@page_width,@page_height/2+@icon_size/2,
                  @page_width-@icon_size,@page_height/2+@icon_size).stroke
      end
      if i>0 then
        @pdf.add_internal_link("map part #{i-1},#{j}",@page_width/2,@page_height,
                               @page_width/2+@icon_size,@page_height-@icon_size)
        @pdf.line(@page_width/2,@page_height-@icon_size,
                  @page_width/2+@icon_size/2,@page_height).stroke
        @pdf.line(@page_width/2+@icon_size/2,@page_height,
                  @page_width/2+@icon_size,@page_height-@icon_size).stroke
      end
      if i<rows-1 then
        @pdf.add_internal_link("map part #{i+1},#{j}",@page_width/2,0,
                                @page_width/2+@icon_size,@icon_size)
        @pdf.line(@page_width/2,@icon_size,
                  @page_width/2+@icon_size/2,0).stroke
        @pdf.line(@page_width/2+@icon_size/2,0,
                  @page_width/2+@icon_size,@icon_size).stroke
      end
  #        @pdf.restore_state          
    end
  end
end

#front_page(g) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sogoumap2pdf/image_to_pdf.rb', line 143

def front_page(g)
  size_x=@big_img_width
  size_y=@big_img_height
  # calculate the number of requred rows and columns
  cols=(size_x-@page_overlap)/(@page_width-@page_overlap)
  rows=(size_y-@page_overlap)/(@page_height-@page_overlap)
  
  ci=ImageList.new # image list to hold results
  page=Rectangle.new( 0, 0, 0, 0)
  @g=g
  for i in 0... g.npx
    p "resizing #{i} j"
    for j in 0...g.npy
      picture_file_name="~/.sogoumap/0/"+ [g.resource_type,g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+g.link_end
      picture_file_name=File.expand_path(picture_file_name)
      if File.exist?(picture_file_name) && File.size(picture_file_name)!= 0 then
        tumb=ImageList.new.read(picture_file_name)[0] # image list to hold results
  
        path_file_name="~/.sogoumap/0/"+ ["179",g.resource_level,((g.start_link_x+i)/200).to_s,((g.start_link_y+j)/200).to_s].join("/")+"/"+(g.start_link_x+i).to_s+"_"+(g.start_link_y+j).to_s+".PNG"
        path_file_name=File.expand_path(path_file_name)
        if g.satellite_path && File.exist?(path_file_name) && File.size(path_file_name)!=0 then
          path_tumb=ImageList.new.read(path_file_name)[0] # image list to hold results
          tumb.composite!(path_tumb, 0, 0, OverCompositeOp)
        end
        #resize each to tumb
        tumb.resize!(@tumb_x,@tumb_y)
        #append tumb
        ci << tumb
        #update page
        page.x=i*@tumb_x
        page.y=(g.npy-1-j)*@tumb_y
        ci.page=page
      else
      end
    end
  end
  #mosaic
  if ci.length==0 then
    p "@tumb_x,@tumb_y"
    p @tumb_x
    p @tumb_y
    p "g.npx,g.npy"
    p g.npx
    p g.npy
    p "#{g.npx*@tumb_x}x#{g.npy*@tumb_y}"
    ## can't work
    resimg=ImageList.new.read("null:white") { self.size = "#{g.npx.to_i*@tumb_x.to_i}x#{g.npy.to_i*@tumb_y.to_i}"}
  else
    resimg=ci.mosaic
  end
  #crop
  #map2pdf.rb:339:in `crop!': bignum too big to convert into `long' (RangeError)
  resimg.crop!(SouthWestGravity,@page_width,@page_height)
  resimg.write("itptmp#{@link_end}"){ self.quality = 70 }
  @pdf.add_image_from_file("itptmp#{@link_end}",0,0)
  ss = PDF::Writer::StrokeStyle.new(2)
  @pdf.stroke_style ss
  for i in 1...cols
    @pdf.line(i*@page_width/cols,0,
              i*@page_width/cols,@page_height).stroke
  end
  for i in 1...rows
    @pdf.line(0,i*@page_height/rows,
              @page_width,i*@page_height/rows).stroke
  end
  for i in 0...rows
    for j in 0...cols
     p "adding #{i} #{j} link in front page"
     p Time.now
     @pdf.add_internal_link("map part #{i},#{j}",
                            j*@page_width/cols, @page_height-i*@page_height/rows,
                            (j+1)*@page_width/cols, @page_height-(i+1)*@page_height/rows)
   end
  end
end

#perfect_fit(w, h, r, c) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/sogoumap2pdf/image_to_pdf.rb', line 74

def perfect_fit(w,h,r,c)
  @page_width=w
  @page_height=h
  @row=r
  @column=c
  @tumb_x=@page_width*256/(c*@page_width-(c-1)*@page_overlap)
  @tumb_y=@page_height*256/(r*@page_height-(r-1)*@page_overlap)
  @big_img_width,@big_img_height=c*@page_width-(c-1)*@page_overlap, r*@page_height-(r-1)*@page_overlap
  return c*@page_width-(c-1)*@page_overlap, r*@page_height-(r-1)*@page_overlap
end

#save(save_file) ⇒ Object



218
219
220
# File 'lib/sogoumap2pdf/image_to_pdf.rb', line 218

def save(save_file)
  @pdf.save_as(save_file)
end