/*
 * Copyright 2017 Patrik Karlsson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.trixon.pacoma.collage;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Based on work by Adrien Vergé in https://github.com/adrienverge/PhotoCollage
 *
 * @author Patrik Karlsson
 */
public class UserCollage {

    private Page mPage;
    private final ArrayList<Photo> mPhotos;

    public UserCollage(ArrayList photos) {
        mPhotos = photos;
    }

    public void makePage(Collage collage) {
        double ratio = 1.0 * collage.getHeight() / collage.getWidth();

        double avg_ratio = mPhotos
                .stream()
                .mapToDouble(Photo::getRatio)
                .average()
                .getAsDouble();

        /*
        # Virtual number of images: since ~ 1 image over 3 is in a multi-cell
        # (i.e. takes two columns), it takes the space of 4 images.
        # So it's equivalent to 1/3 * 4 + 2/3 = 2 times the number of images.
         */
        int virtualNumOfImages = 2 * mPhotos.size();
        int numOfCols = (int) Math.round(Math.sqrt(avg_ratio / ratio * virtualNumOfImages));

        mPage = new Page(1, ratio, numOfCols);
        Collections.shuffle(mPhotos);

        mPhotos.forEach((photo) -> {
            mPage.addCell(photo);
        });
        mPage.adjust();
    }

    //TODO Clone deep copy
}

@@ -1885,8 +1885,1558 @@
  copy%0A%7D%0A
+/*%0Aclass UserCollage(object):%0A    %22%22%22Represents a user-defined collage%0A%0A    A UserCollage contains a list of photos (referenced by filenames) and a%0A    collage.Page object describing their layout in a final poster.%0A%0A    %22%22%22%0A    def __init__(self, photolist):%0A        self.photolist = photolist%0A%0A    def make_page(self, opts):%0A        # Define the output image height / width ratio%0A        ratio = 1.0 * opts.out_h / opts.out_w%0A%0A        # Compute a good number of columns. It depends on the ratio, the number%0A        # of images and the average ratio of these images. According to my%0A        # calculations, the number of column should be inversely proportional%0A        # to the square root of the output image ratio, and proportional to the%0A        # square root of the average input images ratio.%0A        avg_ratio = (sum(1.0 * photo.h / photo.w for photo in self.photolist) /%0A                     len(self.photolist))%0A        # Virtual number of images: since ~ 1 image over 3 is in a multi-cell%0A        # (i.e. takes two columns), it takes the space of 4 images.%0A        # So it's equivalent to 1/3 * 4 + 2/3 = 2 times the number of images.%0A        virtual_no_imgs = 2 * len(self.photolist)%0A        no_cols = int(round(math.sqrt(avg_ratio / ratio * virtual_no_imgs)))%0A%0A        self.page = collage.Page(1.0, ratio, no_cols)%0A        random.shuffle(self.photolist)%0A        for photo in self.photolist:%0A            self.page.add_cell(photo)%0A        self.page.adjust()%0A%0A    def duplicate(self):%0A        return UserCollage(copy.copy(self.photolist))%0A*/%0A

Add python code